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 | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 92 | /* Generic helper macro to convert characters of different types. |
| 93 | from_type and to_type have to be valid type names, begin and end |
| 94 | are pointers to the source characters which should be of type |
| 95 | "from_type *". to is a pointer of type "to_type *" and points to the |
| 96 | buffer where the result characters are written to. */ |
| 97 | #define _PyUnicode_CONVERT_BYTES(from_type, to_type, begin, end, to) \ |
| 98 | do { \ |
| 99 | const from_type *iter_; to_type *to_; \ |
| 100 | for (iter_ = (begin), to_ = (to_type *)(to); \ |
| 101 | iter_ < (end); \ |
| 102 | ++iter_, ++to_) { \ |
| 103 | *to_ = (to_type)*iter_; \ |
| 104 | } \ |
| 105 | } while (0) |
| 106 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 107 | #define _PyUnicode_UTF8(op) \ |
| 108 | (((PyCompactUnicodeObject*)(op))->utf8) |
| 109 | #define PyUnicode_UTF8(op) \ |
| 110 | (assert(PyUnicode_Check(op)), \ |
| 111 | assert(PyUnicode_IS_READY(op)), \ |
| 112 | PyUnicode_IS_COMPACT_ASCII(op) ? \ |
| 113 | ((char*)((PyASCIIObject*)(op) + 1)) : \ |
| 114 | _PyUnicode_UTF8(op)) |
Victor Stinner | bc8b81b | 2011-09-29 19:31:34 +0200 | [diff] [blame] | 115 | #define _PyUnicode_UTF8_LENGTH(op) \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 116 | (((PyCompactUnicodeObject*)(op))->utf8_length) |
| 117 | #define PyUnicode_UTF8_LENGTH(op) \ |
| 118 | (assert(PyUnicode_Check(op)), \ |
| 119 | assert(PyUnicode_IS_READY(op)), \ |
| 120 | PyUnicode_IS_COMPACT_ASCII(op) ? \ |
| 121 | ((PyASCIIObject*)(op))->length : \ |
| 122 | _PyUnicode_UTF8_LENGTH(op)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 123 | #define _PyUnicode_WSTR(op) (((PyASCIIObject*)(op))->wstr) |
| 124 | #define _PyUnicode_WSTR_LENGTH(op) (((PyCompactUnicodeObject*)(op))->wstr_length) |
| 125 | #define _PyUnicode_LENGTH(op) (((PyASCIIObject *)(op))->length) |
| 126 | #define _PyUnicode_STATE(op) (((PyASCIIObject *)(op))->state) |
| 127 | #define _PyUnicode_HASH(op) (((PyASCIIObject *)(op))->hash) |
| 128 | #define _PyUnicode_KIND(op) \ |
| 129 | (assert(PyUnicode_Check(op)), \ |
| 130 | ((PyASCIIObject *)(op))->state.kind) |
| 131 | #define _PyUnicode_GET_LENGTH(op) \ |
| 132 | (assert(PyUnicode_Check(op)), \ |
| 133 | ((PyASCIIObject *)(op))->length) |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 134 | #define _PyUnicode_DATA_ANY(op) (((PyUnicodeObject*)(op))->data.any) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 135 | |
Victor Stinner | b15d4d8 | 2011-09-28 23:59:20 +0200 | [diff] [blame] | 136 | /* The Unicode string has been modified: reset the hash */ |
| 137 | #define _PyUnicode_DIRTY(op) do { _PyUnicode_HASH(op) = -1; } while (0) |
| 138 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 139 | /* This dictionary holds all interned unicode strings. Note that references |
| 140 | to strings in this dictionary are *not* counted in the string's ob_refcnt. |
| 141 | When the interned string reaches a refcnt of 0 the string deallocation |
| 142 | function will delete the reference from this dictionary. |
| 143 | |
| 144 | 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] | 145 | 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] | 146 | */ |
| 147 | static PyObject *interned; |
| 148 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 149 | /* The empty Unicode object is shared to improve performance. */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 150 | static PyObject *unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 151 | |
| 152 | /* Single character Unicode strings in the Latin-1 range are being |
| 153 | shared as well. */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 154 | static PyObject *unicode_latin1[256]; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 155 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 156 | /* Fast detection of the most frequent whitespace characters */ |
| 157 | const unsigned char _Py_ascii_whitespace[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 158 | 0, 0, 0, 0, 0, 0, 0, 0, |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 159 | /* case 0x0009: * CHARACTER TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 160 | /* case 0x000A: * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 161 | /* case 0x000B: * LINE TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 162 | /* case 0x000C: * FORM FEED */ |
| 163 | /* case 0x000D: * CARRIAGE RETURN */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 164 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 165 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 166 | /* case 0x001C: * FILE SEPARATOR */ |
| 167 | /* case 0x001D: * GROUP SEPARATOR */ |
| 168 | /* case 0x001E: * RECORD SEPARATOR */ |
| 169 | /* case 0x001F: * UNIT SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 170 | 0, 0, 0, 0, 1, 1, 1, 1, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 171 | /* case 0x0020: * SPACE */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 172 | 1, 0, 0, 0, 0, 0, 0, 0, |
| 173 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 174 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 175 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 176 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 177 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 178 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 179 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 180 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 181 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 182 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 183 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 184 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 185 | }; |
| 186 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 187 | static PyObject * |
| 188 | unicode_encode_call_errorhandler(const char *errors, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 189 | PyObject **errorHandler,const char *encoding, const char *reason, |
| 190 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 191 | Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos); |
| 192 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 193 | static void |
| 194 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 195 | const char *encoding, |
| 196 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 197 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 198 | const char *reason); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 199 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 200 | /* Same for linebreaks */ |
| 201 | static unsigned char ascii_linebreak[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 202 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 203 | /* 0x000A, * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 204 | /* 0x000B, * LINE TABULATION */ |
| 205 | /* 0x000C, * FORM FEED */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 206 | /* 0x000D, * CARRIAGE RETURN */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 207 | 0, 0, 1, 1, 1, 1, 0, 0, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 208 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 209 | /* 0x001C, * FILE SEPARATOR */ |
| 210 | /* 0x001D, * GROUP SEPARATOR */ |
| 211 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 212 | 0, 0, 0, 0, 1, 1, 1, 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, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 217 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 218 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 219 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 220 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 221 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 222 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 223 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 224 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 225 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 226 | }; |
| 227 | |
Ezio Melotti | 48a2f8f | 2011-09-29 00:18:19 +0300 | [diff] [blame] | 228 | /* The max unicode value is always 0x10FFFF while using the PEP-393 API. |
| 229 | 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] | 230 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 231 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 232 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 233 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 234 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 235 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 236 | /* This is actually an illegal character, so it should |
| 237 | not be passed to unichr. */ |
| 238 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 239 | #endif |
| 240 | } |
| 241 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 242 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 243 | |
| 244 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 245 | to keep things simple, we use a single bitmask, using the least 5 |
| 246 | bits from each unicode characters as the bit index. */ |
| 247 | |
| 248 | /* the linebreak mask is set up by Unicode_Init below */ |
| 249 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 250 | #if LONG_BIT >= 128 |
| 251 | #define BLOOM_WIDTH 128 |
| 252 | #elif LONG_BIT >= 64 |
| 253 | #define BLOOM_WIDTH 64 |
| 254 | #elif LONG_BIT >= 32 |
| 255 | #define BLOOM_WIDTH 32 |
| 256 | #else |
| 257 | #error "LONG_BIT is smaller than 32" |
| 258 | #endif |
| 259 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 260 | #define BLOOM_MASK unsigned long |
| 261 | |
| 262 | static BLOOM_MASK bloom_linebreak; |
| 263 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 264 | #define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
| 265 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 266 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 267 | #define BLOOM_LINEBREAK(ch) \ |
| 268 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 269 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 270 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 271 | Py_LOCAL_INLINE(BLOOM_MASK) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 272 | make_bloom_mask(int kind, void* ptr, Py_ssize_t len) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 273 | { |
| 274 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 275 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 276 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 277 | Py_ssize_t i; |
| 278 | |
| 279 | mask = 0; |
| 280 | for (i = 0; i < len; i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 281 | BLOOM_ADD(mask, PyUnicode_READ(kind, ptr, i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 282 | |
| 283 | return mask; |
| 284 | } |
| 285 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 286 | #define BLOOM_MEMBER(mask, chr, str) \ |
| 287 | (BLOOM(mask, chr) \ |
| 288 | && (PyUnicode_FindChar(str, chr, 0, PyUnicode_GET_LENGTH(str), 1) >= 0)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 289 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 290 | /* --- Unicode Object ----------------------------------------------------- */ |
| 291 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 292 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 293 | fixup(PyUnicodeObject *self, Py_UCS4 (*fixfct)(PyUnicodeObject *s)); |
| 294 | |
| 295 | Py_LOCAL_INLINE(char *) findchar(void *s, int kind, |
| 296 | Py_ssize_t size, Py_UCS4 ch, |
| 297 | int direction) |
| 298 | { |
| 299 | /* like wcschr, but doesn't stop at NULL characters */ |
| 300 | Py_ssize_t i; |
| 301 | if (direction == 1) { |
| 302 | for(i = 0; i < size; i++) |
| 303 | if (PyUnicode_READ(kind, s, i) == ch) |
| 304 | return (char*)s + PyUnicode_KIND_SIZE(kind, i); |
| 305 | } |
| 306 | else { |
| 307 | for(i = size-1; i >= 0; i--) |
| 308 | if (PyUnicode_READ(kind, s, i) == ch) |
| 309 | return (char*)s + PyUnicode_KIND_SIZE(kind, i); |
| 310 | } |
| 311 | return NULL; |
| 312 | } |
| 313 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 314 | static int |
| 315 | unicode_resize(register PyUnicodeObject *unicode, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 316 | Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 317 | { |
| 318 | void *oldstr; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 319 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 320 | /* Resizing is only supported for old unicode objects. */ |
| 321 | assert(!PyUnicode_IS_COMPACT(unicode)); |
| 322 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 323 | |
| 324 | /* ... and only if they have not been readied yet, because |
| 325 | callees usually rely on the wstr representation when resizing. */ |
| 326 | assert(unicode->data.any == NULL); |
| 327 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 328 | /* Shortcut if there's nothing much to do. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 329 | if (_PyUnicode_WSTR_LENGTH(unicode) == length) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 330 | goto reset; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 331 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 332 | /* Resizing shared object (unicode_empty or single character |
| 333 | objects) in-place is not allowed. Use PyUnicode_Resize() |
| 334 | instead ! */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 335 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 336 | if (unicode == unicode_empty || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 337 | (_PyUnicode_WSTR_LENGTH(unicode) == 1 && |
| 338 | _PyUnicode_WSTR(unicode)[0] < 256U && |
| 339 | unicode_latin1[_PyUnicode_WSTR(unicode)[0]] == unicode)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 340 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 341 | "can't resize shared str objects"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 342 | return -1; |
| 343 | } |
| 344 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 345 | /* We allocate one more byte to make sure the string is Ux0000 terminated. |
| 346 | The overallocation is also used by fastsearch, which assumes that it's |
| 347 | safe to look at str[length] (without making any assumptions about what |
| 348 | it contains). */ |
| 349 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 350 | oldstr = _PyUnicode_WSTR(unicode); |
| 351 | _PyUnicode_WSTR(unicode) = PyObject_REALLOC(_PyUnicode_WSTR(unicode), |
| 352 | sizeof(Py_UNICODE) * (length + 1)); |
| 353 | if (!_PyUnicode_WSTR(unicode)) { |
| 354 | _PyUnicode_WSTR(unicode) = (Py_UNICODE *)oldstr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 355 | PyErr_NoMemory(); |
| 356 | return -1; |
| 357 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 358 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 359 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 360 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 361 | reset: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 362 | if (unicode->data.any != NULL) { |
| 363 | PyObject_FREE(unicode->data.any); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 364 | if (_PyUnicode_UTF8(unicode) && _PyUnicode_UTF8(unicode) != unicode->data.any) { |
| 365 | PyObject_FREE(_PyUnicode_UTF8(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 366 | } |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 367 | _PyUnicode_UTF8(unicode) = NULL; |
| 368 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 369 | unicode->data.any = NULL; |
| 370 | _PyUnicode_LENGTH(unicode) = 0; |
| 371 | _PyUnicode_STATE(unicode).interned = _PyUnicode_STATE(unicode).interned; |
| 372 | _PyUnicode_STATE(unicode).kind = PyUnicode_WCHAR_KIND; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 373 | } |
Victor Stinner | b15d4d8 | 2011-09-28 23:59:20 +0200 | [diff] [blame] | 374 | _PyUnicode_DIRTY(unicode); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 375 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | /* 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] | 380 | Ux0000 terminated; some code (e.g. new_identifier) |
| 381 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 382 | |
| 383 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 384 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 385 | |
| 386 | */ |
| 387 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 388 | #ifdef Py_DEBUG |
| 389 | int unicode_old_new_calls = 0; |
| 390 | #endif |
| 391 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 392 | static PyUnicodeObject * |
| 393 | _PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 394 | { |
| 395 | register PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 396 | size_t new_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 397 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 398 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 399 | if (length == 0 && unicode_empty != NULL) { |
| 400 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 401 | return (PyUnicodeObject*)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 404 | /* Ensure we won't overflow the size. */ |
| 405 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 406 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 407 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 408 | if (length < 0) { |
| 409 | PyErr_SetString(PyExc_SystemError, |
| 410 | "Negative size passed to _PyUnicode_New"); |
| 411 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 414 | #ifdef Py_DEBUG |
| 415 | ++unicode_old_new_calls; |
| 416 | #endif |
| 417 | |
| 418 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
| 419 | if (unicode == NULL) |
| 420 | return NULL; |
| 421 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 422 | _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); |
| 423 | if (!_PyUnicode_WSTR(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 424 | PyErr_NoMemory(); |
| 425 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 426 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 427 | |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 428 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 429 | * the caller fails before initializing str -- unicode_resize() |
| 430 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 431 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 432 | * We don't want unicode_resize to read uninitialized memory in |
| 433 | * that case. |
| 434 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 435 | _PyUnicode_WSTR(unicode)[0] = 0; |
| 436 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 437 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 438 | _PyUnicode_HASH(unicode) = -1; |
| 439 | _PyUnicode_STATE(unicode).interned = 0; |
| 440 | _PyUnicode_STATE(unicode).kind = 0; |
| 441 | _PyUnicode_STATE(unicode).compact = 0; |
| 442 | _PyUnicode_STATE(unicode).ready = 0; |
| 443 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 444 | _PyUnicode_DATA_ANY(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 445 | _PyUnicode_LENGTH(unicode) = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 446 | _PyUnicode_UTF8(unicode) = NULL; |
| 447 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 448 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 449 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 450 | onError: |
Amaury Forgeot d'Arc | 7888d08 | 2008-08-01 01:06:32 +0000 | [diff] [blame] | 451 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
| 452 | _Py_DEC_REFTOTAL; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 453 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 454 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 455 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 458 | #ifdef Py_DEBUG |
| 459 | int unicode_new_new_calls = 0; |
| 460 | |
| 461 | /* Functions wrapping macros for use in debugger */ |
| 462 | char *_PyUnicode_utf8(void *unicode){ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 463 | return PyUnicode_UTF8(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | void *_PyUnicode_compact_data(void *unicode) { |
| 467 | return _PyUnicode_COMPACT_DATA(unicode); |
| 468 | } |
| 469 | void *_PyUnicode_data(void *unicode){ |
| 470 | printf("obj %p\n", unicode); |
| 471 | printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); |
| 472 | printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); |
| 473 | printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); |
| 474 | printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); |
| 475 | printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); |
| 476 | return PyUnicode_DATA(unicode); |
| 477 | } |
| 478 | #endif |
| 479 | |
| 480 | PyObject * |
| 481 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) |
| 482 | { |
| 483 | PyObject *obj; |
| 484 | PyCompactUnicodeObject *unicode; |
| 485 | void *data; |
| 486 | int kind_state; |
| 487 | int is_sharing = 0, is_ascii = 0; |
| 488 | Py_ssize_t char_size; |
| 489 | Py_ssize_t struct_size; |
| 490 | |
| 491 | /* Optimization for empty strings */ |
| 492 | if (size == 0 && unicode_empty != NULL) { |
| 493 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 494 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | #ifdef Py_DEBUG |
| 498 | ++unicode_new_new_calls; |
| 499 | #endif |
| 500 | |
| 501 | struct_size = sizeof(PyCompactUnicodeObject); |
| 502 | if (maxchar < 128) { |
| 503 | kind_state = PyUnicode_1BYTE_KIND; |
| 504 | char_size = 1; |
| 505 | is_ascii = 1; |
| 506 | struct_size = sizeof(PyASCIIObject); |
| 507 | } |
| 508 | else if (maxchar < 256) { |
| 509 | kind_state = PyUnicode_1BYTE_KIND; |
| 510 | char_size = 1; |
| 511 | } |
| 512 | else if (maxchar < 65536) { |
| 513 | kind_state = PyUnicode_2BYTE_KIND; |
| 514 | char_size = 2; |
| 515 | if (sizeof(wchar_t) == 2) |
| 516 | is_sharing = 1; |
| 517 | } |
| 518 | else { |
| 519 | kind_state = PyUnicode_4BYTE_KIND; |
| 520 | char_size = 4; |
| 521 | if (sizeof(wchar_t) == 4) |
| 522 | is_sharing = 1; |
| 523 | } |
| 524 | |
| 525 | /* Ensure we won't overflow the size. */ |
| 526 | if (size < 0) { |
| 527 | PyErr_SetString(PyExc_SystemError, |
| 528 | "Negative size passed to PyUnicode_New"); |
| 529 | return NULL; |
| 530 | } |
| 531 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) |
| 532 | return PyErr_NoMemory(); |
| 533 | |
| 534 | /* Duplicated allocation code from _PyObject_New() instead of a call to |
| 535 | * PyObject_New() so we are able to allocate space for the object and |
| 536 | * it's data buffer. |
| 537 | */ |
| 538 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); |
| 539 | if (obj == NULL) |
| 540 | return PyErr_NoMemory(); |
| 541 | obj = PyObject_INIT(obj, &PyUnicode_Type); |
| 542 | if (obj == NULL) |
| 543 | return NULL; |
| 544 | |
| 545 | unicode = (PyCompactUnicodeObject *)obj; |
| 546 | if (is_ascii) |
| 547 | data = ((PyASCIIObject*)obj) + 1; |
| 548 | else |
| 549 | data = unicode + 1; |
| 550 | _PyUnicode_LENGTH(unicode) = size; |
| 551 | _PyUnicode_HASH(unicode) = -1; |
| 552 | _PyUnicode_STATE(unicode).interned = 0; |
| 553 | _PyUnicode_STATE(unicode).kind = kind_state; |
| 554 | _PyUnicode_STATE(unicode).compact = 1; |
| 555 | _PyUnicode_STATE(unicode).ready = 1; |
| 556 | _PyUnicode_STATE(unicode).ascii = is_ascii; |
| 557 | if (is_ascii) { |
| 558 | ((char*)data)[size] = 0; |
| 559 | _PyUnicode_WSTR(unicode) = NULL; |
| 560 | } |
| 561 | else if (kind_state == PyUnicode_1BYTE_KIND) { |
| 562 | ((char*)data)[size] = 0; |
| 563 | _PyUnicode_WSTR(unicode) = NULL; |
| 564 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 565 | unicode->utf8_length = 0; |
| 566 | unicode->utf8 = NULL; |
| 567 | } |
| 568 | else { |
| 569 | unicode->utf8 = NULL; |
| 570 | if (kind_state == PyUnicode_2BYTE_KIND) |
| 571 | ((Py_UCS2*)data)[size] = 0; |
| 572 | else /* kind_state == PyUnicode_4BYTE_KIND */ |
| 573 | ((Py_UCS4*)data)[size] = 0; |
| 574 | if (is_sharing) { |
| 575 | _PyUnicode_WSTR_LENGTH(unicode) = size; |
| 576 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; |
| 577 | } |
| 578 | else { |
| 579 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 580 | _PyUnicode_WSTR(unicode) = NULL; |
| 581 | } |
| 582 | } |
| 583 | return obj; |
| 584 | } |
| 585 | |
| 586 | #if SIZEOF_WCHAR_T == 2 |
| 587 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this |
| 588 | will decode surrogate pairs, the other conversions are implemented as macros |
| 589 | for efficency. |
| 590 | |
| 591 | This function assumes that unicode can hold one more code point than wstr |
| 592 | characters for a terminating null character. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame^] | 593 | static void |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 594 | unicode_convert_wchar_to_ucs4(const wchar_t *begin, const wchar_t *end, |
| 595 | PyUnicodeObject *unicode) |
| 596 | { |
| 597 | const wchar_t *iter; |
| 598 | Py_UCS4 *ucs4_out; |
| 599 | |
| 600 | assert(unicode && PyUnicode_Check(unicode)); |
| 601 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 602 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); |
| 603 | |
| 604 | for (iter = begin; iter < end; ) { |
| 605 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + |
| 606 | _PyUnicode_GET_LENGTH(unicode))); |
| 607 | if (*iter >= 0xD800 && *iter <= 0xDBFF |
| 608 | && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) |
| 609 | { |
| 610 | *ucs4_out++ = (((iter[0] & 0x3FF)<<10) | (iter[1] & 0x3FF)) + 0x10000; |
| 611 | iter += 2; |
| 612 | } |
| 613 | else { |
| 614 | *ucs4_out++ = *iter; |
| 615 | iter++; |
| 616 | } |
| 617 | } |
| 618 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 619 | _PyUnicode_GET_LENGTH(unicode))); |
| 620 | |
| 621 | return 0; |
| 622 | } |
| 623 | #endif |
| 624 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 625 | static int |
| 626 | _PyUnicode_Dirty(PyObject *unicode) |
| 627 | { |
| 628 | assert(PyUnicode_Check(unicode)); |
| 629 | if (Py_REFCNT(unicode) != 1) { |
| 630 | PyErr_SetString(PyExc_ValueError, |
| 631 | "Cannot modify a string having more than 1 reference"); |
| 632 | return -1; |
| 633 | } |
| 634 | _PyUnicode_DIRTY(unicode); |
| 635 | return 0; |
| 636 | } |
| 637 | |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 638 | Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 639 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 640 | PyObject *from, Py_ssize_t from_start, |
| 641 | Py_ssize_t how_many) |
| 642 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 643 | unsigned int from_kind, to_kind; |
| 644 | void *from_data, *to_data; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 645 | |
Victor Stinner | b153615 | 2011-09-30 02:26:10 +0200 | [diff] [blame] | 646 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 647 | PyErr_BadInternalCall(); |
| 648 | return -1; |
| 649 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 650 | |
| 651 | if (PyUnicode_READY(from)) |
| 652 | return -1; |
| 653 | if (PyUnicode_READY(to)) |
| 654 | return -1; |
| 655 | |
Victor Stinner | ff9e50f | 2011-09-28 22:17:19 +0200 | [diff] [blame] | 656 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 657 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
| 658 | PyErr_Format(PyExc_ValueError, |
| 659 | "Cannot write %zi characters at %zi " |
| 660 | "in a string of %zi characters", |
| 661 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 662 | return -1; |
| 663 | } |
Victor Stinner | f5ca1a2 | 2011-09-28 23:54:59 +0200 | [diff] [blame] | 664 | if (how_many == 0) |
| 665 | return 0; |
| 666 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 667 | if (_PyUnicode_Dirty(to)) |
Victor Stinner | f5ca1a2 | 2011-09-28 23:54:59 +0200 | [diff] [blame] | 668 | return -1; |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 669 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 670 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 671 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 672 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 673 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 674 | |
| 675 | if (from_kind == to_kind) { |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 676 | /* fast path */ |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 677 | Py_MEMCPY((char*)to_data |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 678 | + PyUnicode_KIND_SIZE(to_kind, to_start), |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 679 | (char*)from_data |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 680 | + PyUnicode_KIND_SIZE(from_kind, from_start), |
| 681 | PyUnicode_KIND_SIZE(to_kind, how_many)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 682 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 683 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 684 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 685 | { |
| 686 | _PyUnicode_CONVERT_BYTES( |
| 687 | Py_UCS1, Py_UCS2, |
| 688 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 689 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 690 | PyUnicode_2BYTE_DATA(to) + to_start |
| 691 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 692 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 693 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 694 | && to_kind == PyUnicode_4BYTE_KIND) |
| 695 | { |
| 696 | _PyUnicode_CONVERT_BYTES( |
| 697 | Py_UCS1, Py_UCS4, |
| 698 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 699 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 700 | PyUnicode_4BYTE_DATA(to) + to_start |
| 701 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 702 | } |
| 703 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 704 | && to_kind == PyUnicode_4BYTE_KIND) |
| 705 | { |
| 706 | _PyUnicode_CONVERT_BYTES( |
| 707 | Py_UCS2, Py_UCS4, |
| 708 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 709 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 710 | PyUnicode_4BYTE_DATA(to) + to_start |
| 711 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 712 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 713 | else { |
| 714 | int invalid_kinds; |
| 715 | if (from_kind > to_kind) { |
| 716 | /* slow path to check for character overflow */ |
| 717 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 718 | Py_UCS4 ch, maxchar; |
| 719 | Py_ssize_t i; |
| 720 | |
| 721 | maxchar = 0; |
| 722 | invalid_kinds = 0; |
| 723 | for (i=0; i < how_many; i++) { |
| 724 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 725 | if (ch > maxchar) { |
| 726 | maxchar = ch; |
| 727 | if (maxchar > to_maxchar) { |
| 728 | invalid_kinds = 1; |
| 729 | break; |
| 730 | } |
| 731 | } |
| 732 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 733 | } |
| 734 | } |
| 735 | else |
| 736 | invalid_kinds = 1; |
| 737 | if (invalid_kinds) { |
| 738 | PyErr_Format(PyExc_ValueError, |
| 739 | "Cannot copy UCS%u characters " |
| 740 | "into a string of UCS%u characters", |
| 741 | 1 << (from_kind - 1), |
| 742 | 1 << (to_kind -1)); |
| 743 | return -1; |
| 744 | } |
| 745 | } |
| 746 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 747 | } |
| 748 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 749 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 750 | correct string length can be computed before converting a string to UCS4. |
| 751 | This function counts single surrogates as a character and not as a pair. |
| 752 | |
| 753 | Return 0 on success, or -1 on error. */ |
| 754 | static int |
| 755 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 756 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 757 | { |
| 758 | const wchar_t *iter; |
| 759 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame^] | 760 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 761 | if (num_surrogates == NULL || maxchar == NULL) { |
| 762 | PyErr_SetString(PyExc_SystemError, |
| 763 | "unexpected NULL arguments to " |
| 764 | "PyUnicode_FindMaxCharAndNumSurrogatePairs"); |
| 765 | return -1; |
| 766 | } |
| 767 | |
| 768 | *num_surrogates = 0; |
| 769 | *maxchar = 0; |
| 770 | |
| 771 | for (iter = begin; iter < end; ) { |
| 772 | if (*iter > *maxchar) |
| 773 | *maxchar = *iter; |
| 774 | #if SIZEOF_WCHAR_T == 2 |
| 775 | if (*iter >= 0xD800 && *iter <= 0xDBFF |
| 776 | && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) |
| 777 | { |
| 778 | Py_UCS4 surrogate_val; |
| 779 | surrogate_val = (((iter[0] & 0x3FF)<<10) |
| 780 | | (iter[1] & 0x3FF)) + 0x10000; |
| 781 | ++(*num_surrogates); |
| 782 | if (surrogate_val > *maxchar) |
| 783 | *maxchar = surrogate_val; |
| 784 | iter += 2; |
| 785 | } |
| 786 | else |
| 787 | iter++; |
| 788 | #else |
| 789 | iter++; |
| 790 | #endif |
| 791 | } |
| 792 | return 0; |
| 793 | } |
| 794 | |
| 795 | #ifdef Py_DEBUG |
| 796 | int unicode_ready_calls = 0; |
| 797 | #endif |
| 798 | |
| 799 | int |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 800 | _PyUnicode_Ready(PyObject *obj) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 801 | { |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 802 | PyUnicodeObject *unicode = (PyUnicodeObject *)obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 803 | wchar_t *end; |
| 804 | Py_UCS4 maxchar = 0; |
| 805 | Py_ssize_t num_surrogates; |
| 806 | #if SIZEOF_WCHAR_T == 2 |
| 807 | Py_ssize_t length_wo_surrogates; |
| 808 | #endif |
| 809 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 810 | /* _PyUnicode_Ready() is only intented for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 811 | strings were created using _PyObject_New() and where no canonical |
| 812 | representation (the str field) has been set yet aka strings |
| 813 | which are not yet ready. */ |
| 814 | assert(PyUnicode_Check(obj)); |
| 815 | assert(!PyUnicode_IS_READY(obj)); |
| 816 | assert(!PyUnicode_IS_COMPACT(obj)); |
| 817 | assert(_PyUnicode_KIND(obj) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 818 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 819 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 820 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 821 | /* Actually, it should neither be interned nor be anything else: */ |
| 822 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 823 | |
| 824 | #ifdef Py_DEBUG |
| 825 | ++unicode_ready_calls; |
| 826 | #endif |
| 827 | |
| 828 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 829 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 830 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 831 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 832 | |
| 833 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 834 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 835 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 836 | PyErr_NoMemory(); |
| 837 | return -1; |
| 838 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 839 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 840 | _PyUnicode_WSTR(unicode), end, |
| 841 | PyUnicode_1BYTE_DATA(unicode)); |
| 842 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 843 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 844 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 845 | if (maxchar < 128) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 846 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 847 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 848 | } |
| 849 | else { |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 850 | _PyUnicode_UTF8(unicode) = NULL; |
| 851 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 852 | } |
| 853 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 854 | _PyUnicode_WSTR(unicode) = NULL; |
| 855 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 856 | } |
| 857 | /* In this case we might have to convert down from 4-byte native |
| 858 | wchar_t to 2-byte unicode. */ |
| 859 | else if (maxchar < 65536) { |
| 860 | assert(num_surrogates == 0 && |
| 861 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 862 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 863 | #if SIZEOF_WCHAR_T == 2 |
| 864 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 865 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 866 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 867 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 868 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 869 | _PyUnicode_UTF8(unicode) = NULL; |
| 870 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 871 | #else |
| 872 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 873 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 874 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 875 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 876 | PyErr_NoMemory(); |
| 877 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 878 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 879 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 880 | _PyUnicode_WSTR(unicode), end, |
| 881 | PyUnicode_2BYTE_DATA(unicode)); |
| 882 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 883 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 884 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 885 | _PyUnicode_UTF8(unicode) = NULL; |
| 886 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 887 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 888 | _PyUnicode_WSTR(unicode) = NULL; |
| 889 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 890 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 891 | } |
| 892 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 893 | else { |
| 894 | #if SIZEOF_WCHAR_T == 2 |
| 895 | /* in case the native representation is 2-bytes, we need to allocate a |
| 896 | new normalized 4-byte version. */ |
| 897 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 898 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 899 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 900 | PyErr_NoMemory(); |
| 901 | return -1; |
| 902 | } |
| 903 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 904 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 905 | _PyUnicode_UTF8(unicode) = NULL; |
| 906 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame^] | 907 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 908 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 909 | _PyUnicode_WSTR(unicode) = NULL; |
| 910 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 911 | #else |
| 912 | assert(num_surrogates == 0); |
| 913 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 914 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 915 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 916 | _PyUnicode_UTF8(unicode) = NULL; |
| 917 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 918 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 919 | #endif |
| 920 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 921 | } |
| 922 | _PyUnicode_STATE(unicode).ready = 1; |
| 923 | return 0; |
| 924 | } |
| 925 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 926 | static void |
| 927 | unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 928 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 929 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 930 | case SSTATE_NOT_INTERNED: |
| 931 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 932 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 933 | case SSTATE_INTERNED_MORTAL: |
| 934 | /* revive dead object temporarily for DelItem */ |
| 935 | Py_REFCNT(unicode) = 3; |
| 936 | if (PyDict_DelItem(interned, (PyObject *)unicode) != 0) |
| 937 | Py_FatalError( |
| 938 | "deletion of interned string failed"); |
| 939 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 940 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 941 | case SSTATE_INTERNED_IMMORTAL: |
| 942 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 943 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 944 | default: |
| 945 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 946 | } |
| 947 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 948 | if (_PyUnicode_WSTR(unicode) && |
| 949 | (!PyUnicode_IS_READY(unicode) || |
| 950 | _PyUnicode_WSTR(unicode) != PyUnicode_DATA(unicode))) |
| 951 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 952 | if (!PyUnicode_IS_COMPACT_ASCII(unicode) |
| 953 | && _PyUnicode_UTF8(unicode) |
| 954 | && _PyUnicode_UTF8(unicode) != PyUnicode_DATA(unicode)) |
| 955 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 956 | |
| 957 | if (PyUnicode_IS_COMPACT(unicode)) { |
| 958 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 959 | } |
| 960 | else { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 961 | if (_PyUnicode_DATA_ANY(unicode)) |
| 962 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 963 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 964 | } |
| 965 | } |
| 966 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 967 | static int |
| 968 | _PyUnicode_Resize(PyUnicodeObject **unicode, Py_ssize_t length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 969 | { |
| 970 | register PyUnicodeObject *v; |
| 971 | |
| 972 | /* Argument checks */ |
| 973 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 974 | PyErr_BadInternalCall(); |
| 975 | return -1; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 976 | } |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 977 | v = *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 978 | if (v == NULL || !PyUnicode_Check(v) || Py_REFCNT(v) != 1 || length < 0 || |
| 979 | PyUnicode_IS_COMPACT(v) || _PyUnicode_WSTR(v) == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 980 | PyErr_BadInternalCall(); |
| 981 | return -1; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | /* Resizing unicode_empty and single character objects is not |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 985 | possible since these are being shared. |
| 986 | The same goes for new-representation unicode objects or objects which |
| 987 | have already been readied. |
| 988 | For these, we simply return a fresh copy with the same Unicode content. |
| 989 | */ |
| 990 | if ((_PyUnicode_WSTR_LENGTH(v) != length && |
| 991 | (v == unicode_empty || _PyUnicode_WSTR_LENGTH(v) == 1)) || |
| 992 | PyUnicode_IS_COMPACT(v) || v->data.any) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 993 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 994 | if (w == NULL) |
| 995 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 996 | Py_UNICODE_COPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(v), |
| 997 | length < _PyUnicode_WSTR_LENGTH(v) ? length : _PyUnicode_WSTR_LENGTH(v)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 998 | Py_DECREF(*unicode); |
| 999 | *unicode = w; |
| 1000 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 1004 | objects, since we can modify them in-place. */ |
| 1005 | return unicode_resize(v, length); |
| 1006 | } |
| 1007 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1008 | int |
| 1009 | PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1010 | { |
| 1011 | return _PyUnicode_Resize((PyUnicodeObject **)unicode, length); |
| 1012 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1013 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1014 | static PyObject* |
| 1015 | get_latin1_char(unsigned char ch) |
| 1016 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1017 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1018 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1019 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1020 | if (!unicode) |
| 1021 | return NULL; |
| 1022 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
| 1023 | unicode_latin1[ch] = unicode; |
| 1024 | } |
| 1025 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1026 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1027 | } |
| 1028 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1029 | PyObject * |
| 1030 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1031 | { |
| 1032 | PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1033 | Py_UCS4 maxchar = 0; |
| 1034 | Py_ssize_t num_surrogates; |
| 1035 | |
| 1036 | if (u == NULL) |
| 1037 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1038 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1039 | /* If the Unicode data is known at construction time, we can apply |
| 1040 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1041 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1042 | /* Optimization for empty strings */ |
| 1043 | if (size == 0 && unicode_empty != NULL) { |
| 1044 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1045 | return unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1046 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1047 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1048 | /* Single character Unicode objects in the Latin-1 range are |
| 1049 | shared when using this constructor */ |
| 1050 | if (size == 1 && *u < 256) |
| 1051 | return get_latin1_char((unsigned char)*u); |
| 1052 | |
| 1053 | /* If not empty and not single character, copy the Unicode data |
| 1054 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1055 | if (find_maxchar_surrogates(u, u + size, |
| 1056 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1057 | return NULL; |
| 1058 | |
| 1059 | unicode = (PyUnicodeObject *) PyUnicode_New(size - num_surrogates, |
| 1060 | maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1061 | if (!unicode) |
| 1062 | return NULL; |
| 1063 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1064 | switch (PyUnicode_KIND(unicode)) { |
| 1065 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1066 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1067 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1068 | break; |
| 1069 | case PyUnicode_2BYTE_KIND: |
| 1070 | #if Py_UNICODE_SIZE == 2 |
| 1071 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1072 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1073 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1074 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1075 | #endif |
| 1076 | break; |
| 1077 | case PyUnicode_4BYTE_KIND: |
| 1078 | #if SIZEOF_WCHAR_T == 2 |
| 1079 | /* This is the only case which has to process surrogates, thus |
| 1080 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame^] | 1081 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1082 | #else |
| 1083 | assert(num_surrogates == 0); |
| 1084 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1085 | #endif |
| 1086 | break; |
| 1087 | default: |
| 1088 | assert(0 && "Impossible state"); |
| 1089 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1090 | |
| 1091 | return (PyObject *)unicode; |
| 1092 | } |
| 1093 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1094 | PyObject * |
| 1095 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1096 | { |
| 1097 | PyUnicodeObject *unicode; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1098 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1099 | if (size < 0) { |
| 1100 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1101 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1102 | return NULL; |
| 1103 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1104 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1105 | /* 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] | 1106 | some optimizations which share commonly used objects. |
| 1107 | Also, this means the input must be UTF-8, so fall back to the |
| 1108 | UTF-8 decoder at the end. */ |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1109 | if (u != NULL) { |
| 1110 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1111 | /* Optimization for empty strings */ |
| 1112 | if (size == 0 && unicode_empty != NULL) { |
| 1113 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1114 | return unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1115 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1116 | |
| 1117 | /* Single characters are shared when using this constructor. |
| 1118 | Restrict to ASCII, since the input must be UTF-8. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1119 | if (size == 1 && Py_CHARMASK(*u) < 128) |
| 1120 | return get_latin1_char(Py_CHARMASK(*u)); |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 1121 | |
| 1122 | return PyUnicode_DecodeUTF8(u, size, NULL); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
Walter Dörwald | 5550731 | 2007-05-18 13:12:10 +0000 | [diff] [blame] | 1125 | unicode = _PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1126 | if (!unicode) |
| 1127 | return NULL; |
| 1128 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1129 | return (PyObject *)unicode; |
| 1130 | } |
| 1131 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1132 | PyObject * |
| 1133 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1134 | { |
| 1135 | size_t size = strlen(u); |
| 1136 | if (size > PY_SSIZE_T_MAX) { |
| 1137 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1138 | return NULL; |
| 1139 | } |
| 1140 | |
| 1141 | return PyUnicode_FromStringAndSize(u, size); |
| 1142 | } |
| 1143 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1144 | static PyObject* |
| 1145 | _PyUnicode_FromUCS1(const unsigned char* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1146 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1147 | PyObject *res; |
| 1148 | unsigned char max = 127; |
| 1149 | Py_ssize_t i; |
| 1150 | for (i = 0; i < size; i++) { |
| 1151 | if (u[i] & 0x80) { |
| 1152 | max = 255; |
| 1153 | break; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1154 | } |
| 1155 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1156 | res = PyUnicode_New(size, max); |
| 1157 | if (!res) |
| 1158 | return NULL; |
| 1159 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
| 1160 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1163 | static PyObject* |
| 1164 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1165 | { |
| 1166 | PyObject *res; |
| 1167 | Py_UCS2 max = 0; |
| 1168 | Py_ssize_t i; |
| 1169 | for (i = 0; i < size; i++) |
| 1170 | if (u[i] > max) |
| 1171 | max = u[i]; |
| 1172 | res = PyUnicode_New(size, max); |
| 1173 | if (!res) |
| 1174 | return NULL; |
| 1175 | if (max >= 256) |
| 1176 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
| 1177 | else |
| 1178 | for (i = 0; i < size; i++) |
| 1179 | PyUnicode_1BYTE_DATA(res)[i] = (Py_UCS1)u[i]; |
| 1180 | return res; |
| 1181 | } |
| 1182 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1183 | static PyObject* |
| 1184 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1185 | { |
| 1186 | PyObject *res; |
| 1187 | Py_UCS4 max = 0; |
| 1188 | Py_ssize_t i; |
| 1189 | for (i = 0; i < size; i++) |
| 1190 | if (u[i] > max) |
| 1191 | max = u[i]; |
| 1192 | res = PyUnicode_New(size, max); |
| 1193 | if (!res) |
| 1194 | return NULL; |
| 1195 | if (max >= 0x10000) |
| 1196 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
| 1197 | else { |
| 1198 | int kind = PyUnicode_KIND(res); |
| 1199 | void *data = PyUnicode_DATA(res); |
| 1200 | for (i = 0; i < size; i++) |
| 1201 | PyUnicode_WRITE(kind, data, i, u[i]); |
| 1202 | } |
| 1203 | return res; |
| 1204 | } |
| 1205 | |
| 1206 | PyObject* |
| 1207 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 1208 | { |
| 1209 | switch(kind) { |
| 1210 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1211 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1212 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1213 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1214 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1215 | return _PyUnicode_FromUCS4(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1216 | } |
Victor Stinner | 202b62b | 2011-10-01 23:48:37 +0200 | [diff] [blame] | 1217 | PyErr_SetString(PyExc_ValueError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1218 | return NULL; |
| 1219 | } |
| 1220 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1221 | PyObject* |
| 1222 | PyUnicode_Copy(PyObject *unicode) |
| 1223 | { |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1224 | Py_ssize_t size; |
| 1225 | PyObject *copy; |
| 1226 | void *data; |
| 1227 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1228 | if (!PyUnicode_Check(unicode)) { |
| 1229 | PyErr_BadInternalCall(); |
| 1230 | return NULL; |
| 1231 | } |
| 1232 | if (PyUnicode_READY(unicode)) |
| 1233 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1234 | |
| 1235 | size = PyUnicode_GET_LENGTH(unicode); |
| 1236 | copy = PyUnicode_New(size, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 1237 | if (!copy) |
| 1238 | return NULL; |
| 1239 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 1240 | |
| 1241 | data = PyUnicode_DATA(unicode); |
| 1242 | switch (PyUnicode_KIND(unicode)) |
| 1243 | { |
| 1244 | case PyUnicode_1BYTE_KIND: |
| 1245 | memcpy(PyUnicode_1BYTE_DATA(copy), data, size); |
| 1246 | break; |
| 1247 | case PyUnicode_2BYTE_KIND: |
| 1248 | memcpy(PyUnicode_2BYTE_DATA(copy), data, sizeof(Py_UCS2) * size); |
| 1249 | break; |
| 1250 | case PyUnicode_4BYTE_KIND: |
| 1251 | memcpy(PyUnicode_4BYTE_DATA(copy), data, sizeof(Py_UCS4) * size); |
| 1252 | break; |
| 1253 | default: |
| 1254 | assert(0); |
| 1255 | break; |
| 1256 | } |
| 1257 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1258 | } |
| 1259 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1260 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1261 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 1262 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1263 | |
| 1264 | void* |
| 1265 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 1266 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1267 | Py_ssize_t len; |
| 1268 | void *result; |
| 1269 | unsigned int skind; |
| 1270 | |
| 1271 | if (PyUnicode_READY(s)) |
| 1272 | return NULL; |
| 1273 | |
| 1274 | len = PyUnicode_GET_LENGTH(s); |
| 1275 | skind = PyUnicode_KIND(s); |
| 1276 | if (skind >= kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1277 | PyErr_SetString(PyExc_RuntimeError, "invalid widening attempt"); |
| 1278 | return NULL; |
| 1279 | } |
| 1280 | switch(kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1281 | case PyUnicode_2BYTE_KIND: |
| 1282 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); |
| 1283 | if (!result) |
| 1284 | return PyErr_NoMemory(); |
| 1285 | assert(skind == PyUnicode_1BYTE_KIND); |
| 1286 | _PyUnicode_CONVERT_BYTES( |
| 1287 | Py_UCS1, Py_UCS2, |
| 1288 | PyUnicode_1BYTE_DATA(s), |
| 1289 | PyUnicode_1BYTE_DATA(s) + len, |
| 1290 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1291 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1292 | case PyUnicode_4BYTE_KIND: |
| 1293 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); |
| 1294 | if (!result) |
| 1295 | return PyErr_NoMemory(); |
| 1296 | if (skind == PyUnicode_2BYTE_KIND) { |
| 1297 | _PyUnicode_CONVERT_BYTES( |
| 1298 | Py_UCS2, Py_UCS4, |
| 1299 | PyUnicode_2BYTE_DATA(s), |
| 1300 | PyUnicode_2BYTE_DATA(s) + len, |
| 1301 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1302 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1303 | else { |
| 1304 | assert(skind == PyUnicode_1BYTE_KIND); |
| 1305 | _PyUnicode_CONVERT_BYTES( |
| 1306 | Py_UCS1, Py_UCS4, |
| 1307 | PyUnicode_1BYTE_DATA(s), |
| 1308 | PyUnicode_1BYTE_DATA(s) + len, |
| 1309 | result); |
| 1310 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1311 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1312 | default: |
| 1313 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1314 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1315 | PyErr_SetString(PyExc_ValueError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1316 | return NULL; |
| 1317 | } |
| 1318 | |
| 1319 | static Py_UCS4* |
| 1320 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 1321 | int copy_null) |
| 1322 | { |
| 1323 | int kind; |
| 1324 | void *data; |
| 1325 | Py_ssize_t len, targetlen; |
| 1326 | if (PyUnicode_READY(string) == -1) |
| 1327 | return NULL; |
| 1328 | kind = PyUnicode_KIND(string); |
| 1329 | data = PyUnicode_DATA(string); |
| 1330 | len = PyUnicode_GET_LENGTH(string); |
| 1331 | targetlen = len; |
| 1332 | if (copy_null) |
| 1333 | targetlen++; |
| 1334 | if (!target) { |
| 1335 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 1336 | PyErr_NoMemory(); |
| 1337 | return NULL; |
| 1338 | } |
| 1339 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 1340 | if (!target) { |
| 1341 | PyErr_NoMemory(); |
| 1342 | return NULL; |
| 1343 | } |
| 1344 | } |
| 1345 | else { |
| 1346 | if (targetsize < targetlen) { |
| 1347 | PyErr_Format(PyExc_SystemError, |
| 1348 | "string is longer than the buffer"); |
| 1349 | if (copy_null && 0 < targetsize) |
| 1350 | target[0] = 0; |
| 1351 | return NULL; |
| 1352 | } |
| 1353 | } |
| 1354 | if (kind != PyUnicode_4BYTE_KIND) { |
| 1355 | Py_ssize_t i; |
| 1356 | for (i = 0; i < len; i++) |
| 1357 | target[i] = PyUnicode_READ(kind, data, i); |
| 1358 | } |
| 1359 | else |
| 1360 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
| 1361 | if (copy_null) |
| 1362 | target[len] = 0; |
| 1363 | return target; |
| 1364 | } |
| 1365 | |
| 1366 | Py_UCS4* |
| 1367 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 1368 | int copy_null) |
| 1369 | { |
| 1370 | if (target == NULL || targetsize < 1) { |
| 1371 | PyErr_BadInternalCall(); |
| 1372 | return NULL; |
| 1373 | } |
| 1374 | return as_ucs4(string, target, targetsize, copy_null); |
| 1375 | } |
| 1376 | |
| 1377 | Py_UCS4* |
| 1378 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 1379 | { |
| 1380 | return as_ucs4(string, NULL, 0, 1); |
| 1381 | } |
| 1382 | |
| 1383 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1384 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1385 | PyObject * |
| 1386 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1387 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1388 | if (w == NULL) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 1389 | if (size == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1390 | return PyUnicode_New(0, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1391 | PyErr_BadInternalCall(); |
| 1392 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 1395 | if (size == -1) { |
| 1396 | size = wcslen(w); |
| 1397 | } |
| 1398 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1399 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1402 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1403 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1404 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1405 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 1406 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1407 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1408 | *fmt++ = '%'; |
| 1409 | if (width) { |
| 1410 | if (zeropad) |
| 1411 | *fmt++ = '0'; |
| 1412 | fmt += sprintf(fmt, "%d", width); |
| 1413 | } |
| 1414 | if (precision) |
| 1415 | fmt += sprintf(fmt, ".%d", precision); |
| 1416 | if (longflag) |
| 1417 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1418 | else if (longlongflag) { |
| 1419 | /* longlongflag should only ever be nonzero on machines with |
| 1420 | HAVE_LONG_LONG defined */ |
| 1421 | #ifdef HAVE_LONG_LONG |
| 1422 | char *f = PY_FORMAT_LONG_LONG; |
| 1423 | while (*f) |
| 1424 | *fmt++ = *f++; |
| 1425 | #else |
| 1426 | /* we shouldn't ever get here */ |
| 1427 | assert(0); |
| 1428 | *fmt++ = 'l'; |
| 1429 | #endif |
| 1430 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1431 | else if (size_tflag) { |
| 1432 | char *f = PY_FORMAT_SIZE_T; |
| 1433 | while (*f) |
| 1434 | *fmt++ = *f++; |
| 1435 | } |
| 1436 | *fmt++ = c; |
| 1437 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1440 | /* helper for PyUnicode_FromFormatV() */ |
| 1441 | |
| 1442 | static const char* |
| 1443 | parse_format_flags(const char *f, |
| 1444 | int *p_width, int *p_precision, |
| 1445 | int *p_longflag, int *p_longlongflag, int *p_size_tflag) |
| 1446 | { |
| 1447 | int width, precision, longflag, longlongflag, size_tflag; |
| 1448 | |
| 1449 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ |
| 1450 | f++; |
| 1451 | width = 0; |
| 1452 | while (Py_ISDIGIT((unsigned)*f)) |
| 1453 | width = (width*10) + *f++ - '0'; |
| 1454 | precision = 0; |
| 1455 | if (*f == '.') { |
| 1456 | f++; |
| 1457 | while (Py_ISDIGIT((unsigned)*f)) |
| 1458 | precision = (precision*10) + *f++ - '0'; |
| 1459 | if (*f == '%') { |
| 1460 | /* "%.3%s" => f points to "3" */ |
| 1461 | f--; |
| 1462 | } |
| 1463 | } |
| 1464 | if (*f == '\0') { |
| 1465 | /* bogus format "%.1" => go backward, f points to "1" */ |
| 1466 | f--; |
| 1467 | } |
| 1468 | if (p_width != NULL) |
| 1469 | *p_width = width; |
| 1470 | if (p_precision != NULL) |
| 1471 | *p_precision = precision; |
| 1472 | |
| 1473 | /* Handle %ld, %lu, %lld and %llu. */ |
| 1474 | longflag = 0; |
| 1475 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 1476 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1477 | |
| 1478 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 1479 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1480 | longflag = 1; |
| 1481 | ++f; |
| 1482 | } |
| 1483 | #ifdef HAVE_LONG_LONG |
| 1484 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 1485 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1486 | longlongflag = 1; |
| 1487 | f += 2; |
| 1488 | } |
| 1489 | #endif |
| 1490 | } |
| 1491 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 1492 | 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] | 1493 | size_tflag = 1; |
| 1494 | ++f; |
| 1495 | } |
| 1496 | if (p_longflag != NULL) |
| 1497 | *p_longflag = longflag; |
| 1498 | if (p_longlongflag != NULL) |
| 1499 | *p_longlongflag = longlongflag; |
| 1500 | if (p_size_tflag != NULL) |
| 1501 | *p_size_tflag = size_tflag; |
| 1502 | return f; |
| 1503 | } |
| 1504 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1505 | /* maximum number of characters required for output of %ld. 21 characters |
| 1506 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 1507 | #define MAX_LONG_CHARS 21 |
| 1508 | /* maximum number of characters required for output of %lld. |
| 1509 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 1510 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 1511 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 1512 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1513 | PyObject * |
| 1514 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 1515 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1516 | va_list count; |
| 1517 | Py_ssize_t callcount = 0; |
| 1518 | PyObject **callresults = NULL; |
| 1519 | PyObject **callresult = NULL; |
| 1520 | Py_ssize_t n = 0; |
| 1521 | int width = 0; |
| 1522 | int precision = 0; |
| 1523 | int zeropad; |
| 1524 | const char* f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1525 | PyUnicodeObject *string; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1526 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1527 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1528 | Py_UCS4 maxchar = 127; /* result is ASCII by default */ |
| 1529 | Py_UCS4 argmaxchar; |
| 1530 | Py_ssize_t numbersize = 0; |
| 1531 | char *numberresults = NULL; |
| 1532 | char *numberresult = NULL; |
| 1533 | Py_ssize_t i; |
| 1534 | int kind; |
| 1535 | void *data; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1536 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 1537 | Py_VA_COPY(count, vargs); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1538 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 1539 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 1540 | * 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] | 1541 | * result in an array) |
| 1542 | * also esimate a upper bound for all the number formats in the string, |
| 1543 | * numbers will be formated in step 3 and be keept in a '\0'-separated |
| 1544 | * buffer before putting everything together. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1545 | for (f = format; *f; f++) { |
| 1546 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1547 | int longlongflag; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1548 | /* skip width or width.precision (eg. "1.2" of "%1.2f") */ |
| 1549 | f = parse_format_flags(f, &width, NULL, NULL, &longlongflag, NULL); |
| 1550 | if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V') |
| 1551 | ++callcount; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1552 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1553 | else if (*f == 'd' || *f=='u' || *f=='i' || *f=='x' || *f=='p') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1554 | #ifdef HAVE_LONG_LONG |
| 1555 | if (longlongflag) { |
| 1556 | if (width < MAX_LONG_LONG_CHARS) |
| 1557 | width = MAX_LONG_LONG_CHARS; |
| 1558 | } |
| 1559 | else |
| 1560 | #endif |
| 1561 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 1562 | including sign. Decimal takes the most space. This |
| 1563 | isn't enough for octal. If a width is specified we |
| 1564 | need more (which we allocate later). */ |
| 1565 | if (width < MAX_LONG_CHARS) |
| 1566 | width = MAX_LONG_CHARS; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1567 | |
| 1568 | /* account for the size + '\0' to separate numbers |
| 1569 | inside of the numberresults buffer */ |
| 1570 | numbersize += (width + 1); |
| 1571 | } |
| 1572 | } |
| 1573 | else if ((unsigned char)*f > 127) { |
| 1574 | PyErr_Format(PyExc_ValueError, |
| 1575 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 1576 | "string, got a non-ASCII byte: 0x%02x", |
| 1577 | (unsigned char)*f); |
| 1578 | return NULL; |
| 1579 | } |
| 1580 | } |
| 1581 | /* step 2: allocate memory for the results of |
| 1582 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
| 1583 | if (callcount) { |
| 1584 | callresults = PyObject_Malloc(sizeof(PyObject *) * callcount); |
| 1585 | if (!callresults) { |
| 1586 | PyErr_NoMemory(); |
| 1587 | return NULL; |
| 1588 | } |
| 1589 | callresult = callresults; |
| 1590 | } |
| 1591 | /* step 2.5: allocate memory for the results of formating numbers */ |
| 1592 | if (numbersize) { |
| 1593 | numberresults = PyObject_Malloc(numbersize); |
| 1594 | if (!numberresults) { |
| 1595 | PyErr_NoMemory(); |
| 1596 | goto fail; |
| 1597 | } |
| 1598 | numberresult = numberresults; |
| 1599 | } |
| 1600 | |
| 1601 | /* step 3: format numbers and figure out how large a buffer we need */ |
| 1602 | for (f = format; *f; f++) { |
| 1603 | if (*f == '%') { |
| 1604 | const char* p; |
| 1605 | int longflag; |
| 1606 | int longlongflag; |
| 1607 | int size_tflag; |
| 1608 | int numprinted; |
| 1609 | |
| 1610 | p = f; |
| 1611 | zeropad = (f[1] == '0'); |
| 1612 | f = parse_format_flags(f, &width, &precision, |
| 1613 | &longflag, &longlongflag, &size_tflag); |
| 1614 | switch (*f) { |
| 1615 | case 'c': |
| 1616 | { |
| 1617 | Py_UCS4 ordinal = va_arg(count, int); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 1618 | maxchar = Py_MAX(maxchar, ordinal); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1619 | n++; |
| 1620 | break; |
| 1621 | } |
| 1622 | case '%': |
| 1623 | n++; |
| 1624 | break; |
| 1625 | case 'i': |
| 1626 | case 'd': |
| 1627 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 1628 | width, precision, *f); |
| 1629 | if (longflag) |
| 1630 | numprinted = sprintf(numberresult, fmt, |
| 1631 | va_arg(count, long)); |
| 1632 | #ifdef HAVE_LONG_LONG |
| 1633 | else if (longlongflag) |
| 1634 | numprinted = sprintf(numberresult, fmt, |
| 1635 | va_arg(count, PY_LONG_LONG)); |
| 1636 | #endif |
| 1637 | else if (size_tflag) |
| 1638 | numprinted = sprintf(numberresult, fmt, |
| 1639 | va_arg(count, Py_ssize_t)); |
| 1640 | else |
| 1641 | numprinted = sprintf(numberresult, fmt, |
| 1642 | va_arg(count, int)); |
| 1643 | n += numprinted; |
| 1644 | /* advance by +1 to skip over the '\0' */ |
| 1645 | numberresult += (numprinted + 1); |
| 1646 | assert(*(numberresult - 1) == '\0'); |
| 1647 | assert(*(numberresult - 2) != '\0'); |
| 1648 | assert(numprinted >= 0); |
| 1649 | assert(numberresult <= numberresults + numbersize); |
| 1650 | break; |
| 1651 | case 'u': |
| 1652 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 1653 | width, precision, 'u'); |
| 1654 | if (longflag) |
| 1655 | numprinted = sprintf(numberresult, fmt, |
| 1656 | va_arg(count, unsigned long)); |
| 1657 | #ifdef HAVE_LONG_LONG |
| 1658 | else if (longlongflag) |
| 1659 | numprinted = sprintf(numberresult, fmt, |
| 1660 | va_arg(count, unsigned PY_LONG_LONG)); |
| 1661 | #endif |
| 1662 | else if (size_tflag) |
| 1663 | numprinted = sprintf(numberresult, fmt, |
| 1664 | va_arg(count, size_t)); |
| 1665 | else |
| 1666 | numprinted = sprintf(numberresult, fmt, |
| 1667 | va_arg(count, unsigned int)); |
| 1668 | n += numprinted; |
| 1669 | numberresult += (numprinted + 1); |
| 1670 | assert(*(numberresult - 1) == '\0'); |
| 1671 | assert(*(numberresult - 2) != '\0'); |
| 1672 | assert(numprinted >= 0); |
| 1673 | assert(numberresult <= numberresults + numbersize); |
| 1674 | break; |
| 1675 | case 'x': |
| 1676 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
| 1677 | numprinted = sprintf(numberresult, fmt, va_arg(count, int)); |
| 1678 | n += numprinted; |
| 1679 | numberresult += (numprinted + 1); |
| 1680 | assert(*(numberresult - 1) == '\0'); |
| 1681 | assert(*(numberresult - 2) != '\0'); |
| 1682 | assert(numprinted >= 0); |
| 1683 | assert(numberresult <= numberresults + numbersize); |
| 1684 | break; |
| 1685 | case 'p': |
| 1686 | numprinted = sprintf(numberresult, "%p", va_arg(count, void*)); |
| 1687 | /* %p is ill-defined: ensure leading 0x. */ |
| 1688 | if (numberresult[1] == 'X') |
| 1689 | numberresult[1] = 'x'; |
| 1690 | else if (numberresult[1] != 'x') { |
| 1691 | memmove(numberresult + 2, numberresult, |
| 1692 | strlen(numberresult) + 1); |
| 1693 | numberresult[0] = '0'; |
| 1694 | numberresult[1] = 'x'; |
| 1695 | numprinted += 2; |
| 1696 | } |
| 1697 | n += numprinted; |
| 1698 | numberresult += (numprinted + 1); |
| 1699 | assert(*(numberresult - 1) == '\0'); |
| 1700 | assert(*(numberresult - 2) != '\0'); |
| 1701 | assert(numprinted >= 0); |
| 1702 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1703 | break; |
| 1704 | case 's': |
| 1705 | { |
| 1706 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 1707 | const char *s = va_arg(count, const char*); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1708 | PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); |
| 1709 | if (!str) |
| 1710 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1711 | /* since PyUnicode_DecodeUTF8 returns already flexible |
| 1712 | unicode objects, there is no need to call ready on them */ |
| 1713 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 1714 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1715 | n += PyUnicode_GET_LENGTH(str); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1716 | /* Remember the str and switch to the next slot */ |
| 1717 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1718 | break; |
| 1719 | } |
| 1720 | case 'U': |
| 1721 | { |
| 1722 | PyObject *obj = va_arg(count, PyObject *); |
| 1723 | assert(obj && PyUnicode_Check(obj)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1724 | if (PyUnicode_READY(obj) == -1) |
| 1725 | goto fail; |
| 1726 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 1727 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1728 | n += PyUnicode_GET_LENGTH(obj); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1729 | break; |
| 1730 | } |
| 1731 | case 'V': |
| 1732 | { |
| 1733 | PyObject *obj = va_arg(count, PyObject *); |
| 1734 | const char *str = va_arg(count, const char *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1735 | PyObject *str_obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1736 | assert(obj || str); |
| 1737 | assert(!obj || PyUnicode_Check(obj)); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1738 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1739 | if (PyUnicode_READY(obj) == -1) |
| 1740 | goto fail; |
| 1741 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 1742 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1743 | n += PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1744 | *callresult++ = NULL; |
| 1745 | } |
| 1746 | else { |
| 1747 | str_obj = PyUnicode_DecodeUTF8(str, strlen(str), "replace"); |
| 1748 | if (!str_obj) |
| 1749 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1750 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 1751 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1752 | n += PyUnicode_GET_LENGTH(str_obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1753 | *callresult++ = str_obj; |
| 1754 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1755 | break; |
| 1756 | } |
| 1757 | case 'S': |
| 1758 | { |
| 1759 | PyObject *obj = va_arg(count, PyObject *); |
| 1760 | PyObject *str; |
| 1761 | assert(obj); |
| 1762 | str = PyObject_Str(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1763 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1764 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1765 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 1766 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1767 | n += PyUnicode_GET_LENGTH(str); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1768 | /* Remember the str and switch to the next slot */ |
| 1769 | *callresult++ = str; |
| 1770 | break; |
| 1771 | } |
| 1772 | case 'R': |
| 1773 | { |
| 1774 | PyObject *obj = va_arg(count, PyObject *); |
| 1775 | PyObject *repr; |
| 1776 | assert(obj); |
| 1777 | repr = PyObject_Repr(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1778 | if (!repr || PyUnicode_READY(repr) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1779 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1780 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(repr); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 1781 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1782 | n += PyUnicode_GET_LENGTH(repr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1783 | /* Remember the repr and switch to the next slot */ |
| 1784 | *callresult++ = repr; |
| 1785 | break; |
| 1786 | } |
| 1787 | case 'A': |
| 1788 | { |
| 1789 | PyObject *obj = va_arg(count, PyObject *); |
| 1790 | PyObject *ascii; |
| 1791 | assert(obj); |
| 1792 | ascii = PyObject_ASCII(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1793 | if (!ascii || PyUnicode_READY(ascii) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1794 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1795 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(ascii); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 1796 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1797 | n += PyUnicode_GET_LENGTH(ascii); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1798 | /* Remember the repr and switch to the next slot */ |
| 1799 | *callresult++ = ascii; |
| 1800 | break; |
| 1801 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1802 | default: |
| 1803 | /* if we stumble upon an unknown |
| 1804 | formatting code, copy the rest of |
| 1805 | the format string to the output |
| 1806 | string. (we cannot just skip the |
| 1807 | code, since there's no way to know |
| 1808 | what's in the argument list) */ |
| 1809 | n += strlen(p); |
| 1810 | goto expand; |
| 1811 | } |
| 1812 | } else |
| 1813 | n++; |
| 1814 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1815 | expand: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1816 | /* step 4: fill the buffer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1817 | /* Since we've analyzed how much space we need, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1818 | we don't have to resize the string. |
| 1819 | There can be no errors beyond this point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1820 | string = (PyUnicodeObject *)PyUnicode_New(n, maxchar); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1821 | if (!string) |
| 1822 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1823 | kind = PyUnicode_KIND(string); |
| 1824 | data = PyUnicode_DATA(string); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1825 | callresult = callresults; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1826 | numberresult = numberresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1827 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1828 | for (i = 0, f = format; *f; f++) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1829 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1830 | const char* p; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1831 | |
| 1832 | p = f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1833 | f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL); |
| 1834 | /* checking for == because the last argument could be a empty |
| 1835 | string, which causes i to point to end, the assert at the end of |
| 1836 | the loop */ |
| 1837 | assert(i <= PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1838 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1839 | switch (*f) { |
| 1840 | case 'c': |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 1841 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1842 | const int ordinal = va_arg(vargs, int); |
| 1843 | PyUnicode_WRITE(kind, data, i++, ordinal); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1844 | break; |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 1845 | } |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 1846 | case 'i': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1847 | case 'd': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1848 | case 'u': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1849 | case 'x': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1850 | case 'p': |
| 1851 | /* unused, since we already have the result */ |
| 1852 | if (*f == 'p') |
| 1853 | (void) va_arg(vargs, void *); |
| 1854 | else |
| 1855 | (void) va_arg(vargs, int); |
| 1856 | /* extract the result from numberresults and append. */ |
| 1857 | for (; *numberresult; ++i, ++numberresult) |
| 1858 | PyUnicode_WRITE(kind, data, i, *numberresult); |
| 1859 | /* skip over the separating '\0' */ |
| 1860 | assert(*numberresult == '\0'); |
| 1861 | numberresult++; |
| 1862 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1863 | break; |
| 1864 | case 's': |
| 1865 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1866 | /* unused, since we already have the result */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1867 | Py_ssize_t size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1868 | (void) va_arg(vargs, char *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1869 | size = PyUnicode_GET_LENGTH(*callresult); |
| 1870 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 1871 | if (PyUnicode_CopyCharacters((PyObject*)string, i, |
| 1872 | *callresult, 0, |
| 1873 | size) < 0) |
| 1874 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1875 | i += size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1876 | /* We're done with the unicode()/repr() => forget it */ |
| 1877 | Py_DECREF(*callresult); |
| 1878 | /* switch to next unicode()/repr() result */ |
| 1879 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1880 | break; |
| 1881 | } |
| 1882 | case 'U': |
| 1883 | { |
| 1884 | PyObject *obj = va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1885 | Py_ssize_t size; |
| 1886 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
| 1887 | size = PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 1888 | if (PyUnicode_CopyCharacters((PyObject*)string, i, |
| 1889 | obj, 0, |
| 1890 | size) < 0) |
| 1891 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1892 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1893 | break; |
| 1894 | } |
| 1895 | case 'V': |
| 1896 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1897 | Py_ssize_t size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1898 | PyObject *obj = va_arg(vargs, PyObject *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1899 | va_arg(vargs, const char *); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1900 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1901 | size = PyUnicode_GET_LENGTH(obj); |
| 1902 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 1903 | if (PyUnicode_CopyCharacters((PyObject*)string, i, |
| 1904 | obj, 0, |
| 1905 | size) < 0) |
| 1906 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1907 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1908 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1909 | size = PyUnicode_GET_LENGTH(*callresult); |
| 1910 | assert(PyUnicode_KIND(*callresult) <= |
| 1911 | PyUnicode_KIND(string)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 1912 | if (PyUnicode_CopyCharacters((PyObject*)string, i, |
| 1913 | *callresult, |
| 1914 | 0, size) < 0) |
| 1915 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1916 | i += size; |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1917 | Py_DECREF(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1918 | } |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1919 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1920 | break; |
| 1921 | } |
| 1922 | case 'S': |
| 1923 | case 'R': |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 1924 | case 'A': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1925 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1926 | /* unused, since we already have the result */ |
| 1927 | (void) va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1928 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 1929 | if (PyUnicode_CopyCharacters((PyObject*)string, i, |
| 1930 | *callresult, 0, |
| 1931 | PyUnicode_GET_LENGTH(*callresult)) < 0) |
| 1932 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1933 | i += PyUnicode_GET_LENGTH(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1934 | /* We're done with the unicode()/repr() => forget it */ |
| 1935 | Py_DECREF(*callresult); |
| 1936 | /* switch to next unicode()/repr() result */ |
| 1937 | ++callresult; |
| 1938 | break; |
| 1939 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1940 | case '%': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1941 | PyUnicode_WRITE(kind, data, i++, '%'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1942 | break; |
| 1943 | default: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1944 | for (; *p; ++p, ++i) |
| 1945 | PyUnicode_WRITE(kind, data, i, *p); |
| 1946 | assert(i == PyUnicode_GET_LENGTH(string)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1947 | goto end; |
| 1948 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 1949 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1950 | else { |
| 1951 | assert(i < PyUnicode_GET_LENGTH(string)); |
| 1952 | PyUnicode_WRITE(kind, data, i++, *f); |
| 1953 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1954 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1955 | assert(i == PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1956 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1957 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1958 | if (callresults) |
| 1959 | PyObject_Free(callresults); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1960 | if (numberresults) |
| 1961 | PyObject_Free(numberresults); |
| 1962 | return (PyObject *)string; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1963 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1964 | if (callresults) { |
| 1965 | PyObject **callresult2 = callresults; |
| 1966 | while (callresult2 < callresult) { |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1967 | Py_XDECREF(*callresult2); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1968 | ++callresult2; |
| 1969 | } |
| 1970 | PyObject_Free(callresults); |
| 1971 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1972 | if (numberresults) |
| 1973 | PyObject_Free(numberresults); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1974 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1975 | } |
| 1976 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1977 | PyObject * |
| 1978 | PyUnicode_FromFormat(const char *format, ...) |
| 1979 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1980 | PyObject* ret; |
| 1981 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1982 | |
| 1983 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1984 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1985 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1986 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1987 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1988 | ret = PyUnicode_FromFormatV(format, vargs); |
| 1989 | va_end(vargs); |
| 1990 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1991 | } |
| 1992 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1993 | #ifdef HAVE_WCHAR_H |
| 1994 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1995 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 1996 | convert a Unicode object to a wide character string. |
| 1997 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 1998 | - 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] | 1999 | character) required to convert the unicode object. Ignore size argument. |
| 2000 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2001 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2002 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2003 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2004 | static Py_ssize_t |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2005 | unicode_aswidechar(PyUnicodeObject *unicode, |
| 2006 | wchar_t *w, |
| 2007 | Py_ssize_t size) |
| 2008 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2009 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2010 | const wchar_t *wstr; |
| 2011 | |
| 2012 | wstr = PyUnicode_AsUnicodeAndSize((PyObject *)unicode, &res); |
| 2013 | if (wstr == NULL) |
| 2014 | return -1; |
| 2015 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2016 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2017 | if (size > res) |
| 2018 | size = res + 1; |
| 2019 | else |
| 2020 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2021 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2022 | return res; |
| 2023 | } |
| 2024 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2025 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
| 2028 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2029 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2030 | wchar_t *w, |
| 2031 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2032 | { |
| 2033 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2034 | PyErr_BadInternalCall(); |
| 2035 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2036 | } |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2037 | return unicode_aswidechar((PyUnicodeObject*)unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2038 | } |
| 2039 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2040 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2041 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2042 | Py_ssize_t *size) |
| 2043 | { |
| 2044 | wchar_t* buffer; |
| 2045 | Py_ssize_t buflen; |
| 2046 | |
| 2047 | if (unicode == NULL) { |
| 2048 | PyErr_BadInternalCall(); |
| 2049 | return NULL; |
| 2050 | } |
| 2051 | |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2052 | buflen = unicode_aswidechar((PyUnicodeObject *)unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2053 | if (buflen == -1) |
| 2054 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2055 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2056 | PyErr_NoMemory(); |
| 2057 | return NULL; |
| 2058 | } |
| 2059 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2060 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2061 | if (buffer == NULL) { |
| 2062 | PyErr_NoMemory(); |
| 2063 | return NULL; |
| 2064 | } |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2065 | buflen = unicode_aswidechar((PyUnicodeObject *)unicode, buffer, buflen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2066 | if (buflen == -1) |
| 2067 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2068 | if (size != NULL) |
| 2069 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2070 | return buffer; |
| 2071 | } |
| 2072 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2073 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2074 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2075 | PyObject * |
| 2076 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2077 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2078 | PyObject *v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2079 | if (ordinal < 0 || ordinal > 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2080 | PyErr_SetString(PyExc_ValueError, |
| 2081 | "chr() arg not in range(0x110000)"); |
| 2082 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2083 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2084 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2085 | if (ordinal < 256) |
| 2086 | return get_latin1_char(ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2087 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2088 | v = PyUnicode_New(1, ordinal); |
| 2089 | if (v == NULL) |
| 2090 | return NULL; |
| 2091 | PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal); |
| 2092 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2093 | } |
| 2094 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2095 | PyObject * |
| 2096 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2097 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2098 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2099 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2100 | if (PyUnicode_CheckExact(obj)) { |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2101 | if (PyUnicode_READY(obj)) |
| 2102 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2103 | Py_INCREF(obj); |
| 2104 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2105 | } |
| 2106 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2107 | /* For a Unicode subtype that's not a Unicode object, |
| 2108 | return a true Unicode object with the same data. */ |
Victor Stinner | 2219e0a | 2011-10-01 01:16:59 +0200 | [diff] [blame] | 2109 | return PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2110 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2111 | PyErr_Format(PyExc_TypeError, |
| 2112 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2113 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2114 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2115 | } |
| 2116 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2117 | PyObject * |
| 2118 | PyUnicode_FromEncodedObject(register PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2119 | const char *encoding, |
| 2120 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2121 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2122 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2123 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2124 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2125 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2126 | PyErr_BadInternalCall(); |
| 2127 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2128 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2129 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2130 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2131 | if (PyBytes_Check(obj)) { |
| 2132 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2133 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2134 | v = unicode_empty; |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2135 | } |
| 2136 | else { |
| 2137 | v = PyUnicode_Decode( |
| 2138 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 2139 | encoding, errors); |
| 2140 | } |
| 2141 | return v; |
| 2142 | } |
| 2143 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2144 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2145 | PyErr_SetString(PyExc_TypeError, |
| 2146 | "decoding str is not supported"); |
| 2147 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2148 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2149 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2150 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 2151 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 2152 | PyErr_Format(PyExc_TypeError, |
| 2153 | "coercing to str: need bytes, bytearray " |
| 2154 | "or buffer-like object, %.80s found", |
| 2155 | Py_TYPE(obj)->tp_name); |
| 2156 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2157 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2158 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2159 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2160 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2161 | v = unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2162 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2163 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2164 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2165 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2166 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2167 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2168 | } |
| 2169 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2170 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2171 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 2172 | 1 on success. */ |
| 2173 | static int |
| 2174 | normalize_encoding(const char *encoding, |
| 2175 | char *lower, |
| 2176 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2177 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2178 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2179 | char *l; |
| 2180 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2181 | |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2182 | e = encoding; |
| 2183 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2184 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2185 | while (*e) { |
| 2186 | if (l == l_end) |
| 2187 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2188 | if (Py_ISUPPER(*e)) { |
| 2189 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2190 | } |
| 2191 | else if (*e == '_') { |
| 2192 | *l++ = '-'; |
| 2193 | e++; |
| 2194 | } |
| 2195 | else { |
| 2196 | *l++ = *e++; |
| 2197 | } |
| 2198 | } |
| 2199 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2200 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2201 | } |
| 2202 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2203 | PyObject * |
| 2204 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2205 | Py_ssize_t size, |
| 2206 | const char *encoding, |
| 2207 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2208 | { |
| 2209 | PyObject *buffer = NULL, *unicode; |
| 2210 | Py_buffer info; |
| 2211 | char lower[11]; /* Enough for any encoding shortcut */ |
| 2212 | |
| 2213 | if (encoding == NULL) |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2214 | return PyUnicode_DecodeUTF8(s, size, errors); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2215 | |
| 2216 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2217 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2218 | if ((strcmp(lower, "utf-8") == 0) || |
| 2219 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2220 | return PyUnicode_DecodeUTF8(s, size, errors); |
| 2221 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2222 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2223 | (strcmp(lower, "iso-8859-1") == 0)) |
| 2224 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2225 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2226 | else if (strcmp(lower, "mbcs") == 0) |
| 2227 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2228 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2229 | else if (strcmp(lower, "ascii") == 0) |
| 2230 | return PyUnicode_DecodeASCII(s, size, errors); |
| 2231 | else if (strcmp(lower, "utf-16") == 0) |
| 2232 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 2233 | else if (strcmp(lower, "utf-32") == 0) |
| 2234 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 2235 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2236 | |
| 2237 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 2238 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 2239 | 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] | 2240 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 2241 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2242 | if (buffer == NULL) |
| 2243 | goto onError; |
| 2244 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 2245 | if (unicode == NULL) |
| 2246 | goto onError; |
| 2247 | if (!PyUnicode_Check(unicode)) { |
| 2248 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2249 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2250 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2251 | Py_DECREF(unicode); |
| 2252 | goto onError; |
| 2253 | } |
| 2254 | Py_DECREF(buffer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2255 | if (PyUnicode_READY(unicode)) { |
| 2256 | Py_DECREF(unicode); |
| 2257 | return NULL; |
| 2258 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2259 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2260 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2261 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2262 | Py_XDECREF(buffer); |
| 2263 | return NULL; |
| 2264 | } |
| 2265 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2266 | PyObject * |
| 2267 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2268 | const char *encoding, |
| 2269 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2270 | { |
| 2271 | PyObject *v; |
| 2272 | |
| 2273 | if (!PyUnicode_Check(unicode)) { |
| 2274 | PyErr_BadArgument(); |
| 2275 | goto onError; |
| 2276 | } |
| 2277 | |
| 2278 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2279 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2280 | |
| 2281 | /* Decode via the codec registry */ |
| 2282 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2283 | if (v == NULL) |
| 2284 | goto onError; |
| 2285 | return v; |
| 2286 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2287 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2288 | return NULL; |
| 2289 | } |
| 2290 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2291 | PyObject * |
| 2292 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2293 | const char *encoding, |
| 2294 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2295 | { |
| 2296 | PyObject *v; |
| 2297 | |
| 2298 | if (!PyUnicode_Check(unicode)) { |
| 2299 | PyErr_BadArgument(); |
| 2300 | goto onError; |
| 2301 | } |
| 2302 | |
| 2303 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2304 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2305 | |
| 2306 | /* Decode via the codec registry */ |
| 2307 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2308 | if (v == NULL) |
| 2309 | goto onError; |
| 2310 | if (!PyUnicode_Check(v)) { |
| 2311 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2312 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2313 | Py_TYPE(v)->tp_name); |
| 2314 | Py_DECREF(v); |
| 2315 | goto onError; |
| 2316 | } |
| 2317 | return v; |
| 2318 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2319 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2320 | return NULL; |
| 2321 | } |
| 2322 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2323 | PyObject * |
| 2324 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2325 | Py_ssize_t size, |
| 2326 | const char *encoding, |
| 2327 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2328 | { |
| 2329 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2330 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2331 | unicode = PyUnicode_FromUnicode(s, size); |
| 2332 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2333 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2334 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 2335 | Py_DECREF(unicode); |
| 2336 | return v; |
| 2337 | } |
| 2338 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2339 | PyObject * |
| 2340 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2341 | const char *encoding, |
| 2342 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2343 | { |
| 2344 | PyObject *v; |
| 2345 | |
| 2346 | if (!PyUnicode_Check(unicode)) { |
| 2347 | PyErr_BadArgument(); |
| 2348 | goto onError; |
| 2349 | } |
| 2350 | |
| 2351 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2352 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2353 | |
| 2354 | /* Encode via the codec registry */ |
| 2355 | v = PyCodec_Encode(unicode, encoding, errors); |
| 2356 | if (v == NULL) |
| 2357 | goto onError; |
| 2358 | return v; |
| 2359 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2360 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2361 | return NULL; |
| 2362 | } |
| 2363 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2364 | PyObject * |
| 2365 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2366 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2367 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2368 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 2369 | PyUnicode_GET_SIZE(unicode), |
| 2370 | NULL); |
| 2371 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2372 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2373 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 2374 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 2375 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 2376 | cannot use it to encode and decode filenames before it is loaded. Load |
| 2377 | the Python codec requires to encode at least its own filename. Use the C |
| 2378 | version of the locale codec until the codec registry is initialized and |
| 2379 | the Python codec is loaded. |
| 2380 | |
| 2381 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 2382 | cannot only rely on it: check also interp->fscodec_initialized for |
| 2383 | subinterpreters. */ |
| 2384 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2385 | return PyUnicode_AsEncodedString(unicode, |
| 2386 | Py_FileSystemDefaultEncoding, |
| 2387 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 2388 | } |
| 2389 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2390 | /* locale encoding with surrogateescape */ |
| 2391 | wchar_t *wchar; |
| 2392 | char *bytes; |
| 2393 | PyObject *bytes_obj; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 2394 | size_t error_pos; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2395 | |
| 2396 | wchar = PyUnicode_AsWideCharString(unicode, NULL); |
| 2397 | if (wchar == NULL) |
| 2398 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 2399 | bytes = _Py_wchar2char(wchar, &error_pos); |
| 2400 | if (bytes == NULL) { |
| 2401 | if (error_pos != (size_t)-1) { |
| 2402 | char *errmsg = strerror(errno); |
| 2403 | PyObject *exc = NULL; |
| 2404 | if (errmsg == NULL) |
| 2405 | errmsg = "Py_wchar2char() failed"; |
| 2406 | raise_encode_exception(&exc, |
| 2407 | "filesystemencoding", |
| 2408 | PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), |
| 2409 | error_pos, error_pos+1, |
| 2410 | errmsg); |
| 2411 | Py_XDECREF(exc); |
| 2412 | } |
| 2413 | else |
| 2414 | PyErr_NoMemory(); |
| 2415 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2416 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 2417 | } |
| 2418 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2419 | |
| 2420 | bytes_obj = PyBytes_FromString(bytes); |
| 2421 | PyMem_Free(bytes); |
| 2422 | return bytes_obj; |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 2423 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2424 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2425 | } |
| 2426 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2427 | PyObject * |
| 2428 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2429 | const char *encoding, |
| 2430 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2431 | { |
| 2432 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2433 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2434 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2435 | if (!PyUnicode_Check(unicode)) { |
| 2436 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2437 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2438 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2439 | |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2440 | if (encoding == NULL) { |
| 2441 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2442 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2443 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2444 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2445 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2446 | |
| 2447 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2448 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2449 | if ((strcmp(lower, "utf-8") == 0) || |
| 2450 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 2451 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2452 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2453 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2454 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2455 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 2456 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2457 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2458 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2459 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2460 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2461 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2462 | else if (strcmp(lower, "mbcs") == 0) |
| 2463 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 2464 | PyUnicode_GET_SIZE(unicode), |
| 2465 | errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2466 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2467 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2468 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2469 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2470 | |
| 2471 | /* Encode via the codec registry */ |
| 2472 | v = PyCodec_Encode(unicode, encoding, errors); |
| 2473 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2474 | return NULL; |
| 2475 | |
| 2476 | /* The normal path */ |
| 2477 | if (PyBytes_Check(v)) |
| 2478 | return v; |
| 2479 | |
| 2480 | /* 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] | 2481 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2482 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2483 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2484 | |
| 2485 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 2486 | "encoder %s returned bytearray instead of bytes", |
| 2487 | encoding); |
| 2488 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2489 | Py_DECREF(v); |
| 2490 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2491 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2492 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2493 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 2494 | Py_DECREF(v); |
| 2495 | return b; |
| 2496 | } |
| 2497 | |
| 2498 | PyErr_Format(PyExc_TypeError, |
| 2499 | "encoder did not return a bytes object (type=%.400s)", |
| 2500 | Py_TYPE(v)->tp_name); |
| 2501 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2502 | return NULL; |
| 2503 | } |
| 2504 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2505 | PyObject * |
| 2506 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2507 | const char *encoding, |
| 2508 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2509 | { |
| 2510 | PyObject *v; |
| 2511 | |
| 2512 | if (!PyUnicode_Check(unicode)) { |
| 2513 | PyErr_BadArgument(); |
| 2514 | goto onError; |
| 2515 | } |
| 2516 | |
| 2517 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2518 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2519 | |
| 2520 | /* Encode via the codec registry */ |
| 2521 | v = PyCodec_Encode(unicode, encoding, errors); |
| 2522 | if (v == NULL) |
| 2523 | goto onError; |
| 2524 | if (!PyUnicode_Check(v)) { |
| 2525 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2526 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2527 | Py_TYPE(v)->tp_name); |
| 2528 | Py_DECREF(v); |
| 2529 | goto onError; |
| 2530 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2531 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2532 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2533 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2534 | return NULL; |
| 2535 | } |
| 2536 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2537 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 2538 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2539 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 2540 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 2541 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2542 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 2543 | PyObject* |
| 2544 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 2545 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2546 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2547 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 2548 | #elif defined(__APPLE__) |
| 2549 | return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); |
| 2550 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 2551 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 2552 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 2553 | cannot use it to encode and decode filenames before it is loaded. Load |
| 2554 | the Python codec requires to encode at least its own filename. Use the C |
| 2555 | version of the locale codec until the codec registry is initialized and |
| 2556 | the Python codec is loaded. |
| 2557 | |
| 2558 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 2559 | cannot only rely on it: check also interp->fscodec_initialized for |
| 2560 | subinterpreters. */ |
| 2561 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2562 | return PyUnicode_Decode(s, size, |
| 2563 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 2564 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2565 | } |
| 2566 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2567 | /* locale encoding with surrogateescape */ |
| 2568 | wchar_t *wchar; |
| 2569 | PyObject *unicode; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 2570 | size_t len; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2571 | |
| 2572 | if (s[size] != '\0' || size != strlen(s)) { |
| 2573 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 2574 | return NULL; |
| 2575 | } |
| 2576 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 2577 | wchar = _Py_char2wchar(s, &len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2578 | if (wchar == NULL) |
Victor Stinner | d5af0a5 | 2010-11-08 23:34:29 +0000 | [diff] [blame] | 2579 | return PyErr_NoMemory(); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2580 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 2581 | unicode = PyUnicode_FromWideChar(wchar, len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2582 | PyMem_Free(wchar); |
| 2583 | return unicode; |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2584 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2585 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2586 | } |
| 2587 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2588 | |
| 2589 | int |
| 2590 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 2591 | { |
| 2592 | PyObject *output = NULL; |
| 2593 | Py_ssize_t size; |
| 2594 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 2595 | if (arg == NULL) { |
| 2596 | Py_DECREF(*(PyObject**)addr); |
| 2597 | return 1; |
| 2598 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2599 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2600 | output = arg; |
| 2601 | Py_INCREF(output); |
| 2602 | } |
| 2603 | else { |
| 2604 | arg = PyUnicode_FromObject(arg); |
| 2605 | if (!arg) |
| 2606 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2607 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2608 | Py_DECREF(arg); |
| 2609 | if (!output) |
| 2610 | return 0; |
| 2611 | if (!PyBytes_Check(output)) { |
| 2612 | Py_DECREF(output); |
| 2613 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 2614 | return 0; |
| 2615 | } |
| 2616 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 2617 | size = PyBytes_GET_SIZE(output); |
| 2618 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2619 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 2620 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2621 | Py_DECREF(output); |
| 2622 | return 0; |
| 2623 | } |
| 2624 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 2625 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2626 | } |
| 2627 | |
| 2628 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 2629 | int |
| 2630 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 2631 | { |
| 2632 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 2633 | if (arg == NULL) { |
| 2634 | Py_DECREF(*(PyObject**)addr); |
| 2635 | return 1; |
| 2636 | } |
| 2637 | if (PyUnicode_Check(arg)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2638 | if (PyUnicode_READY(arg)) |
| 2639 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 2640 | output = arg; |
| 2641 | Py_INCREF(output); |
| 2642 | } |
| 2643 | else { |
| 2644 | arg = PyBytes_FromObject(arg); |
| 2645 | if (!arg) |
| 2646 | return 0; |
| 2647 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 2648 | PyBytes_GET_SIZE(arg)); |
| 2649 | Py_DECREF(arg); |
| 2650 | if (!output) |
| 2651 | return 0; |
| 2652 | if (!PyUnicode_Check(output)) { |
| 2653 | Py_DECREF(output); |
| 2654 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 2655 | return 0; |
| 2656 | } |
| 2657 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2658 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
| 2659 | PyUnicode_GET_LENGTH(output), 0, 1)) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 2660 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 2661 | Py_DECREF(output); |
| 2662 | return 0; |
| 2663 | } |
| 2664 | *(PyObject**)addr = output; |
| 2665 | return Py_CLEANUP_SUPPORTED; |
| 2666 | } |
| 2667 | |
| 2668 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2669 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2670 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2671 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 2672 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2673 | PyUnicodeObject *u = (PyUnicodeObject *)unicode; |
| 2674 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 2675 | if (!PyUnicode_Check(unicode)) { |
| 2676 | PyErr_BadArgument(); |
| 2677 | return NULL; |
| 2678 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2679 | if (PyUnicode_READY(u) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2680 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2681 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 2682 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 2683 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2684 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 2685 | if (bytes == NULL) |
| 2686 | return NULL; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 2687 | _PyUnicode_UTF8(u) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 2688 | if (_PyUnicode_UTF8(u) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2689 | Py_DECREF(bytes); |
| 2690 | return NULL; |
| 2691 | } |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 2692 | _PyUnicode_UTF8_LENGTH(u) = PyBytes_GET_SIZE(bytes); |
| 2693 | 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] | 2694 | Py_DECREF(bytes); |
| 2695 | } |
| 2696 | |
| 2697 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 2698 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 2699 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 2700 | } |
| 2701 | |
| 2702 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2703 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 2704 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2705 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 2706 | } |
| 2707 | |
| 2708 | #ifdef Py_DEBUG |
| 2709 | int unicode_as_unicode_calls = 0; |
| 2710 | #endif |
| 2711 | |
| 2712 | |
| 2713 | Py_UNICODE * |
| 2714 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 2715 | { |
| 2716 | PyUnicodeObject *u; |
| 2717 | const unsigned char *one_byte; |
| 2718 | #if SIZEOF_WCHAR_T == 4 |
| 2719 | const Py_UCS2 *two_bytes; |
| 2720 | #else |
| 2721 | const Py_UCS4 *four_bytes; |
| 2722 | const Py_UCS4 *ucs4_end; |
| 2723 | Py_ssize_t num_surrogates; |
| 2724 | #endif |
| 2725 | wchar_t *w; |
| 2726 | wchar_t *wchar_end; |
| 2727 | |
| 2728 | if (!PyUnicode_Check(unicode)) { |
| 2729 | PyErr_BadArgument(); |
| 2730 | return NULL; |
| 2731 | } |
| 2732 | u = (PyUnicodeObject*)unicode; |
| 2733 | if (_PyUnicode_WSTR(u) == NULL) { |
| 2734 | /* Non-ASCII compact unicode object */ |
| 2735 | assert(_PyUnicode_KIND(u) != 0); |
| 2736 | assert(PyUnicode_IS_READY(u)); |
| 2737 | |
| 2738 | #ifdef Py_DEBUG |
| 2739 | ++unicode_as_unicode_calls; |
| 2740 | #endif |
| 2741 | |
| 2742 | if (PyUnicode_KIND(u) == PyUnicode_4BYTE_KIND) { |
| 2743 | #if SIZEOF_WCHAR_T == 2 |
| 2744 | four_bytes = PyUnicode_4BYTE_DATA(u); |
| 2745 | ucs4_end = four_bytes + _PyUnicode_LENGTH(u); |
| 2746 | num_surrogates = 0; |
| 2747 | |
| 2748 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 2749 | if (*four_bytes > 0xFFFF) |
| 2750 | ++num_surrogates; |
| 2751 | } |
| 2752 | |
| 2753 | _PyUnicode_WSTR(u) = (wchar_t *) PyObject_MALLOC( |
| 2754 | sizeof(wchar_t) * (_PyUnicode_LENGTH(u) + 1 + num_surrogates)); |
| 2755 | if (!_PyUnicode_WSTR(u)) { |
| 2756 | PyErr_NoMemory(); |
| 2757 | return NULL; |
| 2758 | } |
| 2759 | _PyUnicode_WSTR_LENGTH(u) = _PyUnicode_LENGTH(u) + num_surrogates; |
| 2760 | |
| 2761 | w = _PyUnicode_WSTR(u); |
| 2762 | wchar_end = w + _PyUnicode_WSTR_LENGTH(u); |
| 2763 | four_bytes = PyUnicode_4BYTE_DATA(u); |
| 2764 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 2765 | if (*four_bytes > 0xFFFF) { |
| 2766 | /* encode surrogate pair in this case */ |
| 2767 | *w++ = 0xD800 | ((*four_bytes - 0x10000) >> 10); |
| 2768 | *w = 0xDC00 | ((*four_bytes - 0x10000) & 0x3FF); |
| 2769 | } |
| 2770 | else |
| 2771 | *w = *four_bytes; |
| 2772 | |
| 2773 | if (w > wchar_end) { |
| 2774 | assert(0 && "Miscalculated string end"); |
| 2775 | } |
| 2776 | } |
| 2777 | *w = 0; |
| 2778 | #else |
| 2779 | /* sizeof(wchar_t) == 4 */ |
| 2780 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 2781 | "should share memory already."); |
| 2782 | return NULL; |
| 2783 | #endif |
| 2784 | } |
| 2785 | else { |
| 2786 | _PyUnicode_WSTR(u) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 2787 | (_PyUnicode_LENGTH(u) + 1)); |
| 2788 | if (!_PyUnicode_WSTR(u)) { |
| 2789 | PyErr_NoMemory(); |
| 2790 | return NULL; |
| 2791 | } |
| 2792 | if (!PyUnicode_IS_COMPACT_ASCII(u)) |
| 2793 | _PyUnicode_WSTR_LENGTH(u) = _PyUnicode_LENGTH(u); |
| 2794 | w = _PyUnicode_WSTR(u); |
| 2795 | wchar_end = w + _PyUnicode_LENGTH(u); |
| 2796 | |
| 2797 | if (PyUnicode_KIND(u) == PyUnicode_1BYTE_KIND) { |
| 2798 | one_byte = PyUnicode_1BYTE_DATA(u); |
| 2799 | for (; w < wchar_end; ++one_byte, ++w) |
| 2800 | *w = *one_byte; |
| 2801 | /* null-terminate the wstr */ |
| 2802 | *w = 0; |
| 2803 | } |
| 2804 | else if (PyUnicode_KIND(u) == PyUnicode_2BYTE_KIND) { |
| 2805 | #if SIZEOF_WCHAR_T == 4 |
| 2806 | two_bytes = PyUnicode_2BYTE_DATA(u); |
| 2807 | for (; w < wchar_end; ++two_bytes, ++w) |
| 2808 | *w = *two_bytes; |
| 2809 | /* null-terminate the wstr */ |
| 2810 | *w = 0; |
| 2811 | #else |
| 2812 | /* sizeof(wchar_t) == 2 */ |
| 2813 | PyObject_FREE(_PyUnicode_WSTR(u)); |
| 2814 | _PyUnicode_WSTR(u) = NULL; |
| 2815 | Py_FatalError("Impossible unicode object state, wstr " |
| 2816 | "and str should share memory already."); |
| 2817 | return NULL; |
| 2818 | #endif |
| 2819 | } |
| 2820 | else { |
| 2821 | assert(0 && "This should never happen."); |
| 2822 | } |
| 2823 | } |
| 2824 | } |
| 2825 | if (size != NULL) |
| 2826 | *size = PyUnicode_WSTR_LENGTH(u); |
| 2827 | return _PyUnicode_WSTR(u); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2828 | } |
| 2829 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2830 | Py_UNICODE * |
| 2831 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2832 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2833 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2834 | } |
| 2835 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2836 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2837 | Py_ssize_t |
| 2838 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2839 | { |
| 2840 | if (!PyUnicode_Check(unicode)) { |
| 2841 | PyErr_BadArgument(); |
| 2842 | goto onError; |
| 2843 | } |
| 2844 | return PyUnicode_GET_SIZE(unicode); |
| 2845 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2846 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2847 | return -1; |
| 2848 | } |
| 2849 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2850 | Py_ssize_t |
| 2851 | PyUnicode_GetLength(PyObject *unicode) |
| 2852 | { |
Victor Stinner | 5a706cf | 2011-10-02 00:36:53 +0200 | [diff] [blame] | 2853 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2854 | PyErr_BadArgument(); |
| 2855 | return -1; |
| 2856 | } |
| 2857 | |
| 2858 | return PyUnicode_GET_LENGTH(unicode); |
| 2859 | } |
| 2860 | |
| 2861 | Py_UCS4 |
| 2862 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 2863 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 2864 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 2865 | PyErr_BadArgument(); |
| 2866 | return (Py_UCS4)-1; |
| 2867 | } |
| 2868 | if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { |
| 2869 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2870 | return (Py_UCS4)-1; |
| 2871 | } |
| 2872 | return PyUnicode_READ_CHAR(unicode, index); |
| 2873 | } |
| 2874 | |
| 2875 | int |
| 2876 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 2877 | { |
| 2878 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 2879 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2880 | return -1; |
| 2881 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 2882 | if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { |
| 2883 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 2884 | return -1; |
| 2885 | } |
| 2886 | if (_PyUnicode_Dirty(unicode)) |
| 2887 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2888 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 2889 | index, ch); |
| 2890 | return 0; |
| 2891 | } |
| 2892 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2893 | const char * |
| 2894 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2895 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 2896 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2897 | } |
| 2898 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 2899 | /* create or adjust a UnicodeDecodeError */ |
| 2900 | static void |
| 2901 | make_decode_exception(PyObject **exceptionObject, |
| 2902 | const char *encoding, |
| 2903 | const char *input, Py_ssize_t length, |
| 2904 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 2905 | const char *reason) |
| 2906 | { |
| 2907 | if (*exceptionObject == NULL) { |
| 2908 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 2909 | encoding, input, length, startpos, endpos, reason); |
| 2910 | } |
| 2911 | else { |
| 2912 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 2913 | goto onError; |
| 2914 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 2915 | goto onError; |
| 2916 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 2917 | goto onError; |
| 2918 | } |
| 2919 | return; |
| 2920 | |
| 2921 | onError: |
| 2922 | Py_DECREF(*exceptionObject); |
| 2923 | *exceptionObject = NULL; |
| 2924 | } |
| 2925 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2926 | /* error handling callback helper: |
| 2927 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 2928 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2929 | and adjust various state variables. |
| 2930 | return 0 on success, -1 on error |
| 2931 | */ |
| 2932 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2933 | static int |
| 2934 | unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2935 | const char *encoding, const char *reason, |
| 2936 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 2937 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 2938 | PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2939 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2940 | 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] | 2941 | |
| 2942 | PyObject *restuple = NULL; |
| 2943 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2944 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2945 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2946 | Py_ssize_t requiredsize; |
| 2947 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2948 | const Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2949 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2950 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2951 | int res = -1; |
| 2952 | |
| 2953 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2954 | *errorHandler = PyCodec_LookupError(errors); |
| 2955 | if (*errorHandler == NULL) |
| 2956 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2957 | } |
| 2958 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 2959 | make_decode_exception(exceptionObject, |
| 2960 | encoding, |
| 2961 | *input, *inend - *input, |
| 2962 | *startinpos, *endinpos, |
| 2963 | reason); |
| 2964 | if (*exceptionObject == NULL) |
| 2965 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2966 | |
| 2967 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 2968 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2969 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2970 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 2971 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2972 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2973 | } |
| 2974 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2975 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2976 | |
| 2977 | /* Copy back the bytes variables, which might have been modified by the |
| 2978 | callback */ |
| 2979 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 2980 | if (!inputobj) |
| 2981 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2982 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2983 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2984 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2985 | *input = PyBytes_AS_STRING(inputobj); |
| 2986 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2987 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 2988 | /* we can DECREF safely, as the exception has another reference, |
| 2989 | so the object won't go away. */ |
| 2990 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2991 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2992 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2993 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 2994 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2995 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 2996 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 2997 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2998 | |
| 2999 | /* need more space? (at least enough for what we |
| 3000 | have+the replacement+the rest of the string (starting |
| 3001 | at the new input position), so we won't have to check space |
| 3002 | when there are no errors in the rest of the string) */ |
| 3003 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 3004 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3005 | requiredsize = *outpos + repsize + insize-newpos; |
| 3006 | if (requiredsize > outsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3007 | if (requiredsize<2*outsize) |
| 3008 | requiredsize = 2*outsize; |
| 3009 | if (_PyUnicode_Resize(output, requiredsize) < 0) |
| 3010 | goto onError; |
| 3011 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3012 | } |
| 3013 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3014 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3015 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 3016 | *outptr += repsize; |
| 3017 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3018 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3019 | /* we made it! */ |
| 3020 | res = 0; |
| 3021 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3022 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3023 | Py_XDECREF(restuple); |
| 3024 | return res; |
| 3025 | } |
| 3026 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3027 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 3028 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3029 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 3030 | |
| 3031 | /* Three simple macros defining base-64. */ |
| 3032 | |
| 3033 | /* Is c a base-64 character? */ |
| 3034 | |
| 3035 | #define IS_BASE64(c) \ |
| 3036 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 3037 | ((c) >= 'a' && (c) <= 'z') || \ |
| 3038 | ((c) >= '0' && (c) <= '9') || \ |
| 3039 | (c) == '+' || (c) == '/') |
| 3040 | |
| 3041 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 3042 | |
| 3043 | #define FROM_BASE64(c) \ |
| 3044 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 3045 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 3046 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 3047 | (c) == '+' ? 62 : 63) |
| 3048 | |
| 3049 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 3050 | |
| 3051 | #define TO_BASE64(n) \ |
| 3052 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 3053 | |
| 3054 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 3055 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 3056 | * byte not decoding to itself is the + which begins a base64 |
| 3057 | * string. */ |
| 3058 | |
| 3059 | #define DECODE_DIRECT(c) \ |
| 3060 | ((c) <= 127 && (c) != '+') |
| 3061 | |
| 3062 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 3063 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 3064 | * the above). See RFC2152. This array identifies these different |
| 3065 | * sets: |
| 3066 | * 0 : "Set D" |
| 3067 | * alphanumeric and '(),-./:? |
| 3068 | * 1 : "Set O" |
| 3069 | * !"#$%&*;<=>@[]^_`{|} |
| 3070 | * 2 : "whitespace" |
| 3071 | * ht nl cr sp |
| 3072 | * 3 : special (must be base64 encoded) |
| 3073 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 3074 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3075 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3076 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3077 | char utf7_category[128] = { |
| 3078 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 3079 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 3080 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 3081 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 3082 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 3083 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 3084 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 3085 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 3086 | /* @ A B C D E F G H I J K L M N O */ |
| 3087 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3088 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 3089 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 3090 | /* ` a b c d e f g h i j k l m n o */ |
| 3091 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3092 | /* p q r s t u v w x y z { | } ~ del */ |
| 3093 | 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] | 3094 | }; |
| 3095 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3096 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 3097 | * answer depends on whether we are encoding set O as itself, and also |
| 3098 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 3099 | * clear that the answers to these questions vary between |
| 3100 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 3101 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3102 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 3103 | ((c) < 128 && (c) > 0 && \ |
| 3104 | ((utf7_category[(c)] == 0) || \ |
| 3105 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 3106 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3107 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3108 | PyObject * |
| 3109 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3110 | Py_ssize_t size, |
| 3111 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3112 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3113 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 3114 | } |
| 3115 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3116 | /* The decoder. The only state we preserve is our read position, |
| 3117 | * i.e. how many characters we have consumed. So if we end in the |
| 3118 | * middle of a shift sequence we have to back off the read position |
| 3119 | * and the output to the beginning of the sequence, otherwise we lose |
| 3120 | * all the shift state (seen bits, number of bits seen, high |
| 3121 | * surrogate). */ |
| 3122 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3123 | PyObject * |
| 3124 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3125 | Py_ssize_t size, |
| 3126 | const char *errors, |
| 3127 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3128 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3129 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3130 | Py_ssize_t startinpos; |
| 3131 | Py_ssize_t endinpos; |
| 3132 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3133 | const char *e; |
| 3134 | PyUnicodeObject *unicode; |
| 3135 | Py_UNICODE *p; |
| 3136 | const char *errmsg = ""; |
| 3137 | int inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3138 | Py_UNICODE *shiftOutStart; |
| 3139 | unsigned int base64bits = 0; |
| 3140 | unsigned long base64buffer = 0; |
| 3141 | Py_UNICODE surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3142 | PyObject *errorHandler = NULL; |
| 3143 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3144 | |
| 3145 | unicode = _PyUnicode_New(size); |
| 3146 | if (!unicode) |
| 3147 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3148 | if (size == 0) { |
| 3149 | if (consumed) |
| 3150 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3151 | return (PyObject *)unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3152 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3153 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3154 | p = PyUnicode_AS_UNICODE(unicode); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3155 | shiftOutStart = p; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3156 | e = s + size; |
| 3157 | |
| 3158 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3159 | Py_UNICODE ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3160 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 3161 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3162 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3163 | if (inShift) { /* in a base-64 section */ |
| 3164 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 3165 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 3166 | base64bits += 6; |
| 3167 | s++; |
| 3168 | if (base64bits >= 16) { |
| 3169 | /* we have enough bits for a UTF-16 value */ |
| 3170 | Py_UNICODE outCh = (Py_UNICODE) |
| 3171 | (base64buffer >> (base64bits-16)); |
| 3172 | base64bits -= 16; |
| 3173 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 3174 | if (surrogate) { |
| 3175 | /* expecting a second surrogate */ |
| 3176 | if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 3177 | #ifdef Py_UNICODE_WIDE |
| 3178 | *p++ = (((surrogate & 0x3FF)<<10) |
| 3179 | | (outCh & 0x3FF)) + 0x10000; |
| 3180 | #else |
| 3181 | *p++ = surrogate; |
| 3182 | *p++ = outCh; |
| 3183 | #endif |
| 3184 | surrogate = 0; |
| 3185 | } |
| 3186 | else { |
| 3187 | surrogate = 0; |
| 3188 | errmsg = "second surrogate missing"; |
| 3189 | goto utf7Error; |
| 3190 | } |
| 3191 | } |
| 3192 | else if (outCh >= 0xD800 && outCh <= 0xDBFF) { |
| 3193 | /* first surrogate */ |
| 3194 | surrogate = outCh; |
| 3195 | } |
| 3196 | else if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 3197 | errmsg = "unexpected second surrogate"; |
| 3198 | goto utf7Error; |
| 3199 | } |
| 3200 | else { |
| 3201 | *p++ = outCh; |
| 3202 | } |
| 3203 | } |
| 3204 | } |
| 3205 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3206 | inShift = 0; |
| 3207 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3208 | if (surrogate) { |
| 3209 | errmsg = "second surrogate missing at end of shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3210 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3211 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3212 | if (base64bits > 0) { /* left-over bits */ |
| 3213 | if (base64bits >= 6) { |
| 3214 | /* We've seen at least one base-64 character */ |
| 3215 | errmsg = "partial character in shift sequence"; |
| 3216 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3217 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3218 | else { |
| 3219 | /* Some bits remain; they should be zero */ |
| 3220 | if (base64buffer != 0) { |
| 3221 | errmsg = "non-zero padding bits in shift sequence"; |
| 3222 | goto utf7Error; |
| 3223 | } |
| 3224 | } |
| 3225 | } |
| 3226 | if (ch != '-') { |
| 3227 | /* '-' is absorbed; other terminating |
| 3228 | characters are preserved */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3229 | *p++ = ch; |
| 3230 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3231 | } |
| 3232 | } |
| 3233 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3234 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3235 | s++; /* consume '+' */ |
| 3236 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3237 | s++; |
| 3238 | *p++ = '+'; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3239 | } |
| 3240 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3241 | inShift = 1; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3242 | shiftOutStart = p; |
| 3243 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3244 | } |
| 3245 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3246 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3247 | *p++ = ch; |
| 3248 | s++; |
| 3249 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3250 | else { |
| 3251 | startinpos = s-starts; |
| 3252 | s++; |
| 3253 | errmsg = "unexpected special character"; |
| 3254 | goto utf7Error; |
| 3255 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3256 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3257 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3258 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 3259 | endinpos = s-starts; |
| 3260 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3261 | errors, &errorHandler, |
| 3262 | "utf7", errmsg, |
| 3263 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 3264 | &unicode, &outpos, &p)) |
| 3265 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3266 | } |
| 3267 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3268 | /* end of string */ |
| 3269 | |
| 3270 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 3271 | /* if we're in an inconsistent state, that's an error */ |
| 3272 | if (surrogate || |
| 3273 | (base64bits >= 6) || |
| 3274 | (base64bits > 0 && base64buffer != 0)) { |
| 3275 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 3276 | endinpos = size; |
| 3277 | if (unicode_decode_call_errorhandler( |
| 3278 | errors, &errorHandler, |
| 3279 | "utf7", "unterminated shift sequence", |
| 3280 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 3281 | &unicode, &outpos, &p)) |
| 3282 | goto onError; |
| 3283 | if (s < e) |
| 3284 | goto restart; |
| 3285 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3286 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3287 | |
| 3288 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3289 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3290 | if (inShift) { |
| 3291 | p = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3292 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3293 | } |
| 3294 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3295 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3296 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3297 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3298 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 3299 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3300 | goto onError; |
| 3301 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3302 | Py_XDECREF(errorHandler); |
| 3303 | Py_XDECREF(exc); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3304 | if (PyUnicode_READY(unicode) == -1) { |
| 3305 | Py_DECREF(unicode); |
| 3306 | return NULL; |
| 3307 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3308 | return (PyObject *)unicode; |
| 3309 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3310 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3311 | Py_XDECREF(errorHandler); |
| 3312 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3313 | Py_DECREF(unicode); |
| 3314 | return NULL; |
| 3315 | } |
| 3316 | |
| 3317 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3318 | PyObject * |
| 3319 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3320 | Py_ssize_t size, |
| 3321 | int base64SetO, |
| 3322 | int base64WhiteSpace, |
| 3323 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3324 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3325 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3326 | /* It might be possible to tighten this worst case */ |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 3327 | Py_ssize_t allocated = 8 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3328 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3329 | Py_ssize_t i = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3330 | unsigned int base64bits = 0; |
| 3331 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3332 | char * out; |
| 3333 | char * start; |
| 3334 | |
| 3335 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3336 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3337 | |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 3338 | if (allocated / 8 != size) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3339 | return PyErr_NoMemory(); |
| 3340 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3341 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3342 | if (v == NULL) |
| 3343 | return NULL; |
| 3344 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3345 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3346 | for (;i < size; ++i) { |
| 3347 | Py_UNICODE ch = s[i]; |
| 3348 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3349 | if (inShift) { |
| 3350 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 3351 | /* shifting out */ |
| 3352 | if (base64bits) { /* output remaining bits */ |
| 3353 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 3354 | base64buffer = 0; |
| 3355 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3356 | } |
| 3357 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3358 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 3359 | so no '-' is required, except if the character is itself a '-' */ |
| 3360 | if (IS_BASE64(ch) || ch == '-') { |
| 3361 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3362 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3363 | *out++ = (char) ch; |
| 3364 | } |
| 3365 | else { |
| 3366 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3367 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3368 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3369 | else { /* not in a shift sequence */ |
| 3370 | if (ch == '+') { |
| 3371 | *out++ = '+'; |
| 3372 | *out++ = '-'; |
| 3373 | } |
| 3374 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 3375 | *out++ = (char) ch; |
| 3376 | } |
| 3377 | else { |
| 3378 | *out++ = '+'; |
| 3379 | inShift = 1; |
| 3380 | goto encode_char; |
| 3381 | } |
| 3382 | } |
| 3383 | continue; |
| 3384 | encode_char: |
| 3385 | #ifdef Py_UNICODE_WIDE |
| 3386 | if (ch >= 0x10000) { |
| 3387 | /* code first surrogate */ |
| 3388 | base64bits += 16; |
| 3389 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 3390 | while (base64bits >= 6) { |
| 3391 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 3392 | base64bits -= 6; |
| 3393 | } |
| 3394 | /* prepare second surrogate */ |
| 3395 | ch = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 3396 | } |
| 3397 | #endif |
| 3398 | base64bits += 16; |
| 3399 | base64buffer = (base64buffer << 16) | ch; |
| 3400 | while (base64bits >= 6) { |
| 3401 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 3402 | base64bits -= 6; |
| 3403 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 3404 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3405 | if (base64bits) |
| 3406 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 3407 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3408 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3409 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 3410 | return NULL; |
| 3411 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3412 | } |
| 3413 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3414 | #undef IS_BASE64 |
| 3415 | #undef FROM_BASE64 |
| 3416 | #undef TO_BASE64 |
| 3417 | #undef DECODE_DIRECT |
| 3418 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3419 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3420 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 3421 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3422 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3423 | char utf8_code_length[256] = { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3424 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 3425 | illegal prefix. See RFC 3629 for details */ |
| 3426 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 3427 | 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] | 3428 | 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] | 3429 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 3430 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 3431 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 3432 | 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] | 3433 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 3434 | 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] | 3435 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3436 | 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] | 3437 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 3438 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 3439 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 3440 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 3441 | 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] | 3442 | }; |
| 3443 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3444 | PyObject * |
| 3445 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3446 | Py_ssize_t size, |
| 3447 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3448 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3449 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 3450 | } |
| 3451 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3452 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 3453 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 3454 | |
| 3455 | /* Mask to quickly check whether a C 'long' contains a |
| 3456 | non-ASCII, UTF8-encoded char. */ |
| 3457 | #if (SIZEOF_LONG == 8) |
| 3458 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 3459 | #elif (SIZEOF_LONG == 4) |
| 3460 | # define ASCII_CHAR_MASK 0x80808080L |
| 3461 | #else |
| 3462 | # error C 'long' size should be either 4 or 8! |
| 3463 | #endif |
| 3464 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3465 | /* Scans a UTF-8 string and returns the maximum character to be expected, |
| 3466 | the size of the decoded unicode string and if any major errors were |
| 3467 | encountered. |
| 3468 | |
| 3469 | This function does check basic UTF-8 sanity, it does however NOT CHECK |
| 3470 | if the string contains surrogates, and if all continuation bytes are |
| 3471 | within the correct ranges, these checks are performed in |
| 3472 | PyUnicode_DecodeUTF8Stateful. |
| 3473 | |
| 3474 | If it sets has_errors to 1, it means the value of unicode_size and max_char |
| 3475 | will be bogus and you should not rely on useful information in them. |
| 3476 | */ |
| 3477 | static Py_UCS4 |
| 3478 | utf8_max_char_size_and_has_errors(const char *s, Py_ssize_t string_size, |
| 3479 | Py_ssize_t *unicode_size, Py_ssize_t* consumed, |
| 3480 | int *has_errors) |
| 3481 | { |
| 3482 | Py_ssize_t n; |
| 3483 | Py_ssize_t char_count = 0; |
| 3484 | Py_UCS4 max_char = 127, new_max; |
| 3485 | Py_UCS4 upper_bound; |
| 3486 | const unsigned char *p = (const unsigned char *)s; |
| 3487 | const unsigned char *end = p + string_size; |
| 3488 | const unsigned char *aligned_end = (const unsigned char *) ((size_t) end & ~LONG_PTR_MASK); |
| 3489 | int err = 0; |
| 3490 | |
| 3491 | for (; p < end && !err; ++p, ++char_count) { |
| 3492 | /* Only check value if it's not a ASCII char... */ |
| 3493 | if (*p < 0x80) { |
| 3494 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 3495 | an explanation. */ |
| 3496 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 3497 | /* Help register allocation */ |
| 3498 | register const unsigned char *_p = p; |
| 3499 | while (_p < aligned_end) { |
| 3500 | unsigned long value = *(unsigned long *) _p; |
| 3501 | if (value & ASCII_CHAR_MASK) |
| 3502 | break; |
| 3503 | _p += SIZEOF_LONG; |
| 3504 | char_count += SIZEOF_LONG; |
| 3505 | } |
| 3506 | p = _p; |
| 3507 | if (p == end) |
| 3508 | break; |
| 3509 | } |
| 3510 | } |
| 3511 | if (*p >= 0x80) { |
| 3512 | n = utf8_code_length[*p]; |
| 3513 | new_max = max_char; |
| 3514 | switch (n) { |
| 3515 | /* invalid start byte */ |
| 3516 | case 0: |
| 3517 | err = 1; |
| 3518 | break; |
| 3519 | case 2: |
| 3520 | /* Code points between 0x00FF and 0x07FF inclusive. |
| 3521 | Approximate the upper bound of the code point, |
| 3522 | if this flips over 255 we can be sure it will be more |
| 3523 | than 255 and the string will need 2 bytes per code coint, |
| 3524 | if it stays under or equal to 255, we can be sure 1 byte |
| 3525 | is enough. |
| 3526 | ((*p & 0b00011111) << 6) | 0b00111111 */ |
| 3527 | upper_bound = ((*p & 0x1F) << 6) | 0x3F; |
| 3528 | if (max_char < upper_bound) |
| 3529 | new_max = upper_bound; |
| 3530 | /* Ensure we track at least that we left ASCII space. */ |
| 3531 | if (new_max < 128) |
| 3532 | new_max = 128; |
| 3533 | break; |
| 3534 | case 3: |
| 3535 | /* Between 0x0FFF and 0xFFFF inclusive, so values are |
| 3536 | always > 255 and <= 65535 and will always need 2 bytes. */ |
| 3537 | if (max_char < 65535) |
| 3538 | new_max = 65535; |
| 3539 | break; |
| 3540 | case 4: |
| 3541 | /* Code point will be above 0xFFFF for sure in this case. */ |
| 3542 | new_max = 65537; |
| 3543 | break; |
| 3544 | /* Internal error, this should be caught by the first if */ |
| 3545 | case 1: |
| 3546 | default: |
| 3547 | assert(0 && "Impossible case in utf8_max_char_and_size"); |
| 3548 | err = 1; |
| 3549 | } |
| 3550 | /* Instead of number of overall bytes for this code point, |
| 3551 | n containts the number of following bytes: */ |
| 3552 | --n; |
| 3553 | /* Check if the follow up chars are all valid continuation bytes */ |
| 3554 | if (n >= 1) { |
| 3555 | const unsigned char *cont; |
| 3556 | if ((p + n) >= end) { |
| 3557 | if (consumed == 0) |
| 3558 | /* incomplete data, non-incremental decoding */ |
| 3559 | err = 1; |
| 3560 | break; |
| 3561 | } |
| 3562 | for (cont = p + 1; cont < (p + n); ++cont) { |
| 3563 | if ((*cont & 0xc0) != 0x80) { |
| 3564 | err = 1; |
| 3565 | break; |
| 3566 | } |
| 3567 | } |
| 3568 | p += n; |
| 3569 | } |
| 3570 | else |
| 3571 | err = 1; |
| 3572 | max_char = new_max; |
| 3573 | } |
| 3574 | } |
| 3575 | |
| 3576 | if (unicode_size) |
| 3577 | *unicode_size = char_count; |
| 3578 | if (has_errors) |
| 3579 | *has_errors = err; |
| 3580 | return max_char; |
| 3581 | } |
| 3582 | |
| 3583 | /* Similar to PyUnicode_WRITE but can also write into wstr field |
| 3584 | of the legacy unicode representation */ |
| 3585 | #define WRITE_FLEXIBLE_OR_WSTR(kind, buf, index, value) \ |
| 3586 | do { \ |
| 3587 | const int k_ = (kind); \ |
| 3588 | if (k_ == PyUnicode_WCHAR_KIND) \ |
| 3589 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \ |
| 3590 | else if (k_ == PyUnicode_1BYTE_KIND) \ |
| 3591 | ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \ |
| 3592 | else if (k_ == PyUnicode_2BYTE_KIND) \ |
| 3593 | ((Py_UCS2 *)(buf))[(index)] = (Py_UCS2)(value); \ |
| 3594 | else \ |
| 3595 | ((Py_UCS4 *)(buf))[(index)] = (Py_UCS4)(value); \ |
| 3596 | } while (0) |
| 3597 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3598 | PyObject * |
| 3599 | PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3600 | Py_ssize_t size, |
| 3601 | const char *errors, |
| 3602 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3603 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3604 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3605 | int n; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3606 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3607 | Py_ssize_t startinpos; |
| 3608 | Py_ssize_t endinpos; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3609 | const char *e, *aligned_end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3610 | PyUnicodeObject *unicode; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 3611 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3612 | PyObject *errorHandler = NULL; |
| 3613 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3614 | Py_UCS4 maxchar = 0; |
| 3615 | Py_ssize_t unicode_size; |
| 3616 | Py_ssize_t i; |
| 3617 | int kind; |
| 3618 | void *data; |
| 3619 | int has_errors; |
| 3620 | Py_UNICODE *error_outptr; |
| 3621 | #if SIZEOF_WCHAR_T == 2 |
| 3622 | Py_ssize_t wchar_offset = 0; |
| 3623 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3624 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3625 | if (size == 0) { |
| 3626 | if (consumed) |
| 3627 | *consumed = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3628 | return (PyObject *)PyUnicode_New(0, 0); |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3629 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3630 | maxchar = utf8_max_char_size_and_has_errors(s, size, &unicode_size, |
| 3631 | consumed, &has_errors); |
| 3632 | if (has_errors) { |
| 3633 | unicode = _PyUnicode_New(size); |
| 3634 | if (!unicode) |
| 3635 | return NULL; |
| 3636 | kind = PyUnicode_WCHAR_KIND; |
| 3637 | data = PyUnicode_AS_UNICODE(unicode); |
| 3638 | assert(data != NULL); |
| 3639 | } |
| 3640 | else { |
| 3641 | unicode = (PyUnicodeObject *)PyUnicode_New(unicode_size, maxchar); |
| 3642 | if (!unicode) |
| 3643 | return NULL; |
| 3644 | /* When the string is ASCII only, just use memcpy and return. |
| 3645 | unicode_size may be != size if there is an incomplete UTF-8 |
| 3646 | sequence at the end of the ASCII block. */ |
| 3647 | if (maxchar < 128 && size == unicode_size) { |
| 3648 | Py_MEMCPY(PyUnicode_1BYTE_DATA(unicode), s, unicode_size); |
| 3649 | return (PyObject *)unicode; |
| 3650 | } |
| 3651 | kind = PyUnicode_KIND(unicode); |
| 3652 | data = PyUnicode_DATA(unicode); |
| 3653 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3654 | /* Unpack UTF-8 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3655 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3656 | e = s + size; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3657 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3658 | |
| 3659 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 3660 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3661 | |
| 3662 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3663 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 3664 | input will consist of an overwhelming majority of ASCII |
| 3665 | characters, we try to optimize for this case by checking |
| 3666 | as many characters as a C 'long' can contain. |
| 3667 | First, check if we can do an aligned read, as most CPUs have |
| 3668 | a penalty for unaligned reads. |
| 3669 | */ |
| 3670 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 3671 | /* Help register allocation */ |
| 3672 | register const char *_s = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3673 | register Py_ssize_t _i = i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3674 | while (_s < aligned_end) { |
| 3675 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 3676 | and do a fast unrolled copy if it only contains ASCII |
| 3677 | characters. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3678 | unsigned long value = *(unsigned long *) _s; |
| 3679 | if (value & ASCII_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3680 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3681 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+0, _s[0]); |
| 3682 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+1, _s[1]); |
| 3683 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+2, _s[2]); |
| 3684 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+3, _s[3]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3685 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3686 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+4, _s[4]); |
| 3687 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+5, _s[5]); |
| 3688 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+6, _s[6]); |
| 3689 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+7, _s[7]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3690 | #endif |
| 3691 | _s += SIZEOF_LONG; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3692 | _i += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3693 | } |
| 3694 | s = _s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3695 | i = _i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3696 | if (s == e) |
| 3697 | break; |
| 3698 | ch = (unsigned char)*s; |
| 3699 | } |
| 3700 | } |
| 3701 | |
| 3702 | if (ch < 0x80) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3703 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3704 | s++; |
| 3705 | continue; |
| 3706 | } |
| 3707 | |
| 3708 | n = utf8_code_length[ch]; |
| 3709 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 3710 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3711 | if (consumed) |
| 3712 | break; |
| 3713 | else { |
| 3714 | errmsg = "unexpected end of data"; |
| 3715 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3716 | endinpos = startinpos+1; |
| 3717 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 3718 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3719 | goto utf8Error; |
| 3720 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3721 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3722 | |
| 3723 | switch (n) { |
| 3724 | |
| 3725 | case 0: |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3726 | errmsg = "invalid start byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3727 | startinpos = s-starts; |
| 3728 | endinpos = startinpos+1; |
| 3729 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3730 | |
| 3731 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 3732 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3733 | startinpos = s-starts; |
| 3734 | endinpos = startinpos+1; |
| 3735 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3736 | |
| 3737 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 3738 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3739 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3740 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3741 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3742 | goto utf8Error; |
| 3743 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3744 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3745 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3746 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3747 | break; |
| 3748 | |
| 3749 | case 3: |
Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 3750 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 3751 | will result in surrogates in range d800-dfff. Surrogates are |
| 3752 | not valid UTF-8 so they are rejected. |
| 3753 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 3754 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3755 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3756 | (s[2] & 0xc0) != 0x80 || |
| 3757 | ((unsigned char)s[0] == 0xE0 && |
| 3758 | (unsigned char)s[1] < 0xA0) || |
| 3759 | ((unsigned char)s[0] == 0xED && |
| 3760 | (unsigned char)s[1] > 0x9F)) { |
| 3761 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3762 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3763 | endinpos = startinpos + 1; |
| 3764 | |
| 3765 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 3766 | continuation byte is s[2], so increment endinpos by 1, |
| 3767 | if not, s[1] is invalid and endinpos doesn't need to |
| 3768 | be incremented. */ |
| 3769 | if ((s[1] & 0xC0) == 0x80) |
| 3770 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3771 | goto utf8Error; |
| 3772 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3773 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3774 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3775 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 3776 | break; |
| 3777 | |
| 3778 | case 4: |
| 3779 | if ((s[1] & 0xc0) != 0x80 || |
| 3780 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3781 | (s[3] & 0xc0) != 0x80 || |
| 3782 | ((unsigned char)s[0] == 0xF0 && |
| 3783 | (unsigned char)s[1] < 0x90) || |
| 3784 | ((unsigned char)s[0] == 0xF4 && |
| 3785 | (unsigned char)s[1] > 0x8F)) { |
| 3786 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3787 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3788 | endinpos = startinpos + 1; |
| 3789 | if ((s[1] & 0xC0) == 0x80) { |
| 3790 | endinpos++; |
| 3791 | if ((s[2] & 0xC0) == 0x80) |
| 3792 | endinpos++; |
| 3793 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3794 | goto utf8Error; |
| 3795 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 3796 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3797 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 3798 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 3799 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3800 | /* If the string is flexible or we have native UCS-4, write |
| 3801 | directly.. */ |
| 3802 | if (sizeof(Py_UNICODE) > 2 || kind != PyUnicode_WCHAR_KIND) |
| 3803 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3804 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3805 | else { |
| 3806 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3807 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3808 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 3809 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3810 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3811 | /* high surrogate = top 10 bits added to D800 */ |
| 3812 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, |
| 3813 | (Py_UNICODE)(0xD800 + (ch >> 10))); |
| 3814 | |
| 3815 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 3816 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, |
| 3817 | (Py_UNICODE)(0xDC00 + (ch & 0x03FF))); |
| 3818 | } |
| 3819 | #if SIZEOF_WCHAR_T == 2 |
| 3820 | wchar_offset++; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3821 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3822 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3823 | } |
| 3824 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3825 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3826 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3827 | utf8Error: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3828 | /* If this is not yet a resizable string, make it one.. */ |
| 3829 | if (kind != PyUnicode_WCHAR_KIND) { |
| 3830 | const Py_UNICODE *u; |
| 3831 | PyUnicodeObject *new_unicode = _PyUnicode_New(size); |
| 3832 | if (!new_unicode) |
| 3833 | goto onError; |
| 3834 | u = PyUnicode_AsUnicode((PyObject *)unicode); |
| 3835 | if (!u) |
| 3836 | goto onError; |
| 3837 | #if SIZEOF_WCHAR_T == 2 |
| 3838 | i += wchar_offset; |
| 3839 | #endif |
| 3840 | Py_UNICODE_COPY(PyUnicode_AS_UNICODE(new_unicode), u, i); |
| 3841 | Py_DECREF(unicode); |
| 3842 | unicode = new_unicode; |
| 3843 | kind = 0; |
| 3844 | data = PyUnicode_AS_UNICODE(new_unicode); |
| 3845 | assert(data != NULL); |
| 3846 | } |
| 3847 | error_outptr = PyUnicode_AS_UNICODE(unicode) + i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3848 | if (unicode_decode_call_errorhandler( |
| 3849 | errors, &errorHandler, |
| 3850 | "utf8", errmsg, |
| 3851 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3852 | &unicode, &i, &error_outptr)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3853 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3854 | /* Update data because unicode_decode_call_errorhandler might have |
| 3855 | re-created or resized the unicode object. */ |
| 3856 | data = PyUnicode_AS_UNICODE(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3857 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3858 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3859 | /* Ensure the unicode_size calculation above was correct: */ |
| 3860 | assert(kind == PyUnicode_WCHAR_KIND || i == unicode_size); |
| 3861 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3862 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3863 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3864 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3865 | /* Adjust length and ready string when it contained errors and |
| 3866 | is of the old resizable kind. */ |
| 3867 | if (kind == PyUnicode_WCHAR_KIND) { |
| 3868 | if (_PyUnicode_Resize(&unicode, i) < 0 || |
| 3869 | PyUnicode_READY(unicode) == -1) |
| 3870 | goto onError; |
| 3871 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3872 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3873 | Py_XDECREF(errorHandler); |
| 3874 | Py_XDECREF(exc); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3875 | if (PyUnicode_READY(unicode) == -1) { |
| 3876 | Py_DECREF(unicode); |
| 3877 | return NULL; |
| 3878 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3879 | return (PyObject *)unicode; |
| 3880 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3881 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3882 | Py_XDECREF(errorHandler); |
| 3883 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3884 | Py_DECREF(unicode); |
| 3885 | return NULL; |
| 3886 | } |
| 3887 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3888 | #undef WRITE_FLEXIBLE_OR_WSTR |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3889 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 3890 | #ifdef __APPLE__ |
| 3891 | |
| 3892 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
| 3893 | used to decode the command line arguments on Mac OS X. */ |
| 3894 | |
| 3895 | wchar_t* |
| 3896 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 3897 | { |
| 3898 | int n; |
| 3899 | const char *e; |
| 3900 | wchar_t *unicode, *p; |
| 3901 | |
| 3902 | /* Note: size will always be longer than the resulting Unicode |
| 3903 | character count */ |
| 3904 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) { |
| 3905 | PyErr_NoMemory(); |
| 3906 | return NULL; |
| 3907 | } |
| 3908 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 3909 | if (!unicode) |
| 3910 | return NULL; |
| 3911 | |
| 3912 | /* Unpack UTF-8 encoded data */ |
| 3913 | p = unicode; |
| 3914 | e = s + size; |
| 3915 | while (s < e) { |
| 3916 | Py_UCS4 ch = (unsigned char)*s; |
| 3917 | |
| 3918 | if (ch < 0x80) { |
| 3919 | *p++ = (wchar_t)ch; |
| 3920 | s++; |
| 3921 | continue; |
| 3922 | } |
| 3923 | |
| 3924 | n = utf8_code_length[ch]; |
| 3925 | if (s + n > e) { |
| 3926 | goto surrogateescape; |
| 3927 | } |
| 3928 | |
| 3929 | switch (n) { |
| 3930 | case 0: |
| 3931 | case 1: |
| 3932 | goto surrogateescape; |
| 3933 | |
| 3934 | case 2: |
| 3935 | if ((s[1] & 0xc0) != 0x80) |
| 3936 | goto surrogateescape; |
| 3937 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
| 3938 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 3939 | *p++ = (wchar_t)ch; |
| 3940 | break; |
| 3941 | |
| 3942 | case 3: |
| 3943 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 3944 | will result in surrogates in range d800-dfff. Surrogates are |
| 3945 | not valid UTF-8 so they are rejected. |
| 3946 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 3947 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
| 3948 | if ((s[1] & 0xc0) != 0x80 || |
| 3949 | (s[2] & 0xc0) != 0x80 || |
| 3950 | ((unsigned char)s[0] == 0xE0 && |
| 3951 | (unsigned char)s[1] < 0xA0) || |
| 3952 | ((unsigned char)s[0] == 0xED && |
| 3953 | (unsigned char)s[1] > 0x9F)) { |
| 3954 | |
| 3955 | goto surrogateescape; |
| 3956 | } |
| 3957 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
| 3958 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3959 | *p++ = (wchar_t)ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 3960 | break; |
| 3961 | |
| 3962 | case 4: |
| 3963 | if ((s[1] & 0xc0) != 0x80 || |
| 3964 | (s[2] & 0xc0) != 0x80 || |
| 3965 | (s[3] & 0xc0) != 0x80 || |
| 3966 | ((unsigned char)s[0] == 0xF0 && |
| 3967 | (unsigned char)s[1] < 0x90) || |
| 3968 | ((unsigned char)s[0] == 0xF4 && |
| 3969 | (unsigned char)s[1] > 0x8F)) { |
| 3970 | goto surrogateescape; |
| 3971 | } |
| 3972 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 3973 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 3974 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 3975 | |
| 3976 | #if SIZEOF_WCHAR_T == 4 |
| 3977 | *p++ = (wchar_t)ch; |
| 3978 | #else |
| 3979 | /* compute and append the two surrogates: */ |
| 3980 | |
| 3981 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 3982 | ch -= 0x10000; |
| 3983 | |
| 3984 | /* high surrogate = top 10 bits added to D800 */ |
| 3985 | *p++ = (wchar_t)(0xD800 + (ch >> 10)); |
| 3986 | |
| 3987 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 3988 | *p++ = (wchar_t)(0xDC00 + (ch & 0x03FF)); |
| 3989 | #endif |
| 3990 | break; |
| 3991 | } |
| 3992 | s += n; |
| 3993 | continue; |
| 3994 | |
| 3995 | surrogateescape: |
| 3996 | *p++ = 0xDC00 + ch; |
| 3997 | s++; |
| 3998 | } |
| 3999 | *p = L'\0'; |
| 4000 | return unicode; |
| 4001 | } |
| 4002 | |
| 4003 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4004 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4005 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 4006 | |
| 4007 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4008 | and allocate exactly as much space needed at the end. Else allocate the |
| 4009 | maximum possible needed (4 result bytes per Unicode character), and return |
| 4010 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4011 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4012 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4013 | _PyUnicode_AsUTF8String(PyObject *obj, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4014 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4015 | #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] | 4016 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4017 | Py_ssize_t i; /* index into s of next input byte */ |
| 4018 | PyObject *result; /* result string object */ |
| 4019 | char *p; /* next free byte in output buffer */ |
| 4020 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 4021 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4022 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4023 | PyObject *errorHandler = NULL; |
| 4024 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4025 | int kind; |
| 4026 | void *data; |
| 4027 | Py_ssize_t size; |
| 4028 | PyUnicodeObject *unicode = (PyUnicodeObject *)obj; |
| 4029 | #if SIZEOF_WCHAR_T == 2 |
| 4030 | Py_ssize_t wchar_offset = 0; |
| 4031 | #endif |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4032 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4033 | if (!PyUnicode_Check(unicode)) { |
| 4034 | PyErr_BadArgument(); |
| 4035 | return NULL; |
| 4036 | } |
| 4037 | |
| 4038 | if (PyUnicode_READY(unicode) == -1) |
| 4039 | return NULL; |
| 4040 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 4041 | if (PyUnicode_UTF8(unicode)) |
| 4042 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 4043 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4044 | |
| 4045 | kind = PyUnicode_KIND(unicode); |
| 4046 | data = PyUnicode_DATA(unicode); |
| 4047 | size = PyUnicode_GET_LENGTH(unicode); |
| 4048 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4049 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4050 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4051 | if (size <= MAX_SHORT_UNICHARS) { |
| 4052 | /* Write into the stack buffer; nallocated can't overflow. |
| 4053 | * At the end, we'll allocate exactly as much heap space as it |
| 4054 | * turns out we need. |
| 4055 | */ |
| 4056 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4057 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4058 | p = stackbuf; |
| 4059 | } |
| 4060 | else { |
| 4061 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 4062 | nallocated = size * 4; |
| 4063 | if (nallocated / 4 != size) /* overflow! */ |
| 4064 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4065 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4066 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4067 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4068 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4069 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4070 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4071 | for (i = 0; i < size;) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4072 | Py_UCS4 ch = PyUnicode_READ(kind, data, i++); |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4073 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4074 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4075 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4076 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4077 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4078 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4079 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 4080 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 4081 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4082 | } else if (0xD800 <= ch && ch <= 0xDFFF) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4083 | Py_ssize_t newpos; |
| 4084 | PyObject *rep; |
| 4085 | Py_ssize_t repsize, k, startpos; |
| 4086 | startpos = i-1; |
| 4087 | #if SIZEOF_WCHAR_T == 2 |
| 4088 | startpos += wchar_offset; |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 4089 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4090 | rep = unicode_encode_call_errorhandler( |
| 4091 | errors, &errorHandler, "utf-8", "surrogates not allowed", |
| 4092 | PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), |
| 4093 | &exc, startpos, startpos+1, &newpos); |
| 4094 | if (!rep) |
| 4095 | goto error; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4096 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4097 | if (PyBytes_Check(rep)) |
| 4098 | repsize = PyBytes_GET_SIZE(rep); |
| 4099 | else |
| 4100 | repsize = PyUnicode_GET_SIZE(rep); |
| 4101 | |
| 4102 | if (repsize > 4) { |
| 4103 | Py_ssize_t offset; |
| 4104 | |
| 4105 | if (result == NULL) |
| 4106 | offset = p - stackbuf; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4107 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4108 | offset = p - PyBytes_AS_STRING(result); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4109 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4110 | if (nallocated > PY_SSIZE_T_MAX - repsize + 4) { |
| 4111 | /* integer overflow */ |
| 4112 | PyErr_NoMemory(); |
| 4113 | goto error; |
| 4114 | } |
| 4115 | nallocated += repsize - 4; |
| 4116 | if (result != NULL) { |
| 4117 | if (_PyBytes_Resize(&result, nallocated) < 0) |
| 4118 | goto error; |
| 4119 | } else { |
| 4120 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4121 | if (result == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4122 | goto error; |
| 4123 | Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset); |
| 4124 | } |
| 4125 | p = PyBytes_AS_STRING(result) + offset; |
| 4126 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4127 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4128 | if (PyBytes_Check(rep)) { |
| 4129 | char *prep = PyBytes_AS_STRING(rep); |
| 4130 | for(k = repsize; k > 0; k--) |
| 4131 | *p++ = *prep++; |
| 4132 | } else /* rep is unicode */ { |
| 4133 | const Py_UNICODE *prep = PyUnicode_AS_UNICODE(rep); |
| 4134 | Py_UNICODE c; |
| 4135 | |
| 4136 | for(k=0; k<repsize; k++) { |
| 4137 | c = prep[k]; |
| 4138 | if (0x80 <= c) { |
| 4139 | raise_encode_exception(&exc, "utf-8", |
| 4140 | PyUnicode_AS_UNICODE(unicode), |
| 4141 | size, i-1, i, |
| 4142 | "surrogates not allowed"); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4143 | goto error; |
| 4144 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4145 | *p++ = (char)prep[k]; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4146 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4147 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4148 | Py_DECREF(rep); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4149 | } else if (ch < 0x10000) { |
| 4150 | *p++ = (char)(0xe0 | (ch >> 12)); |
| 4151 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4152 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 4153 | } else /* ch >= 0x10000 */ { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4154 | /* Encode UCS4 Unicode ordinals */ |
| 4155 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 4156 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 4157 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4158 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4159 | #if SIZEOF_WCHAR_T == 2 |
| 4160 | wchar_offset++; |
| 4161 | #endif |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4162 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4163 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 4164 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4165 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4166 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4167 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4168 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4169 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4170 | } |
| 4171 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 4172 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4173 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4174 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4175 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4176 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4177 | |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4178 | Py_XDECREF(errorHandler); |
| 4179 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4180 | return result; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4181 | error: |
| 4182 | Py_XDECREF(errorHandler); |
| 4183 | Py_XDECREF(exc); |
| 4184 | Py_XDECREF(result); |
| 4185 | return NULL; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4186 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4187 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4188 | } |
| 4189 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4190 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4191 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 4192 | Py_ssize_t size, |
| 4193 | const char *errors) |
| 4194 | { |
| 4195 | PyObject *v, *unicode; |
| 4196 | |
| 4197 | unicode = PyUnicode_FromUnicode(s, size); |
| 4198 | if (unicode == NULL) |
| 4199 | return NULL; |
| 4200 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 4201 | Py_DECREF(unicode); |
| 4202 | return v; |
| 4203 | } |
| 4204 | |
| 4205 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4206 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4207 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4208 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4209 | } |
| 4210 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4211 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 4212 | |
| 4213 | PyObject * |
| 4214 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4215 | Py_ssize_t size, |
| 4216 | const char *errors, |
| 4217 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4218 | { |
| 4219 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 4220 | } |
| 4221 | |
| 4222 | PyObject * |
| 4223 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4224 | Py_ssize_t size, |
| 4225 | const char *errors, |
| 4226 | int *byteorder, |
| 4227 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4228 | { |
| 4229 | const char *starts = s; |
| 4230 | Py_ssize_t startinpos; |
| 4231 | Py_ssize_t endinpos; |
| 4232 | Py_ssize_t outpos; |
| 4233 | PyUnicodeObject *unicode; |
| 4234 | Py_UNICODE *p; |
| 4235 | #ifndef Py_UNICODE_WIDE |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4236 | int pairs = 0; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4237 | const unsigned char *qq; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4238 | #else |
| 4239 | const int pairs = 0; |
| 4240 | #endif |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4241 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4242 | int bo = 0; /* assume native ordering by default */ |
| 4243 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4244 | /* Offsets from q for retrieving bytes in the right order. */ |
| 4245 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4246 | int iorder[] = {0, 1, 2, 3}; |
| 4247 | #else |
| 4248 | int iorder[] = {3, 2, 1, 0}; |
| 4249 | #endif |
| 4250 | PyObject *errorHandler = NULL; |
| 4251 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4252 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4253 | q = (unsigned char *)s; |
| 4254 | e = q + size; |
| 4255 | |
| 4256 | if (byteorder) |
| 4257 | bo = *byteorder; |
| 4258 | |
| 4259 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4260 | byte order setting accordingly. In native mode, the leading BOM |
| 4261 | mark is skipped, in all other modes, it is copied to the output |
| 4262 | stream as-is (giving a ZWNBSP character). */ |
| 4263 | if (bo == 0) { |
| 4264 | if (size >= 4) { |
| 4265 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4266 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4267 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4268 | if (bom == 0x0000FEFF) { |
| 4269 | q += 4; |
| 4270 | bo = -1; |
| 4271 | } |
| 4272 | else if (bom == 0xFFFE0000) { |
| 4273 | q += 4; |
| 4274 | bo = 1; |
| 4275 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4276 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4277 | if (bom == 0x0000FEFF) { |
| 4278 | q += 4; |
| 4279 | bo = 1; |
| 4280 | } |
| 4281 | else if (bom == 0xFFFE0000) { |
| 4282 | q += 4; |
| 4283 | bo = -1; |
| 4284 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4285 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4286 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4287 | } |
| 4288 | |
| 4289 | if (bo == -1) { |
| 4290 | /* force LE */ |
| 4291 | iorder[0] = 0; |
| 4292 | iorder[1] = 1; |
| 4293 | iorder[2] = 2; |
| 4294 | iorder[3] = 3; |
| 4295 | } |
| 4296 | else if (bo == 1) { |
| 4297 | /* force BE */ |
| 4298 | iorder[0] = 3; |
| 4299 | iorder[1] = 2; |
| 4300 | iorder[2] = 1; |
| 4301 | iorder[3] = 0; |
| 4302 | } |
| 4303 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4304 | /* On narrow builds we split characters outside the BMP into two |
| 4305 | codepoints => count how much extra space we need. */ |
| 4306 | #ifndef Py_UNICODE_WIDE |
| 4307 | for (qq = q; qq < e; qq += 4) |
| 4308 | if (qq[iorder[2]] != 0 || qq[iorder[3]] != 0) |
| 4309 | pairs++; |
| 4310 | #endif |
| 4311 | |
| 4312 | /* This might be one to much, because of a BOM */ |
| 4313 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 4314 | if (!unicode) |
| 4315 | return NULL; |
| 4316 | if (size == 0) |
| 4317 | return (PyObject *)unicode; |
| 4318 | |
| 4319 | /* Unpack UTF-32 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4320 | p = PyUnicode_AS_UNICODE(unicode); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4321 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4322 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4323 | Py_UCS4 ch; |
| 4324 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 4325 | if (e-q<4) { |
| 4326 | if (consumed) |
| 4327 | break; |
| 4328 | errmsg = "truncated data"; |
| 4329 | startinpos = ((const char *)q)-starts; |
| 4330 | endinpos = ((const char *)e)-starts; |
| 4331 | goto utf32Error; |
| 4332 | /* The remaining input chars are ignored if the callback |
| 4333 | chooses to skip the input */ |
| 4334 | } |
| 4335 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 4336 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4337 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4338 | if (ch >= 0x110000) |
| 4339 | { |
| 4340 | errmsg = "codepoint not in range(0x110000)"; |
| 4341 | startinpos = ((const char *)q)-starts; |
| 4342 | endinpos = startinpos+4; |
| 4343 | goto utf32Error; |
| 4344 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4345 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4346 | if (ch >= 0x10000) |
| 4347 | { |
| 4348 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 4349 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 4350 | } |
| 4351 | else |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4352 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4353 | *p++ = ch; |
| 4354 | q += 4; |
| 4355 | continue; |
| 4356 | utf32Error: |
| 4357 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 4358 | if (unicode_decode_call_errorhandler( |
| 4359 | errors, &errorHandler, |
| 4360 | "utf32", errmsg, |
| 4361 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
| 4362 | &unicode, &outpos, &p)) |
| 4363 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4364 | } |
| 4365 | |
| 4366 | if (byteorder) |
| 4367 | *byteorder = bo; |
| 4368 | |
| 4369 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4370 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4371 | |
| 4372 | /* Adjust length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4373 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4374 | goto onError; |
| 4375 | |
| 4376 | Py_XDECREF(errorHandler); |
| 4377 | Py_XDECREF(exc); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4378 | if (PyUnicode_READY(unicode) == -1) { |
| 4379 | Py_DECREF(unicode); |
| 4380 | return NULL; |
| 4381 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4382 | return (PyObject *)unicode; |
| 4383 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4384 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4385 | Py_DECREF(unicode); |
| 4386 | Py_XDECREF(errorHandler); |
| 4387 | Py_XDECREF(exc); |
| 4388 | return NULL; |
| 4389 | } |
| 4390 | |
| 4391 | PyObject * |
| 4392 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4393 | Py_ssize_t size, |
| 4394 | const char *errors, |
| 4395 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4396 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4397 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4398 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4399 | Py_ssize_t nsize, bytesize; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4400 | #ifndef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4401 | Py_ssize_t i, pairs; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4402 | #else |
| 4403 | const int pairs = 0; |
| 4404 | #endif |
| 4405 | /* Offsets from p for storing byte pairs in the right order. */ |
| 4406 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4407 | int iorder[] = {0, 1, 2, 3}; |
| 4408 | #else |
| 4409 | int iorder[] = {3, 2, 1, 0}; |
| 4410 | #endif |
| 4411 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4412 | #define STORECHAR(CH) \ |
| 4413 | do { \ |
| 4414 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 4415 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 4416 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 4417 | p[iorder[0]] = (CH) & 0xff; \ |
| 4418 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4419 | } while(0) |
| 4420 | |
| 4421 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 4422 | so we need less space. */ |
| 4423 | #ifndef Py_UNICODE_WIDE |
| 4424 | for (i = pairs = 0; i < size-1; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4425 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 4426 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 4427 | pairs++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4428 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4429 | nsize = (size - pairs + (byteorder == 0)); |
| 4430 | bytesize = nsize * 4; |
| 4431 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4432 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4433 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4434 | if (v == NULL) |
| 4435 | return NULL; |
| 4436 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4437 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4438 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4439 | STORECHAR(0xFEFF); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4440 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4441 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4442 | |
| 4443 | if (byteorder == -1) { |
| 4444 | /* force LE */ |
| 4445 | iorder[0] = 0; |
| 4446 | iorder[1] = 1; |
| 4447 | iorder[2] = 2; |
| 4448 | iorder[3] = 3; |
| 4449 | } |
| 4450 | else if (byteorder == 1) { |
| 4451 | /* force BE */ |
| 4452 | iorder[0] = 3; |
| 4453 | iorder[1] = 2; |
| 4454 | iorder[2] = 1; |
| 4455 | iorder[3] = 0; |
| 4456 | } |
| 4457 | |
| 4458 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4459 | Py_UCS4 ch = *s++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4460 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4461 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 4462 | Py_UCS4 ch2 = *s; |
| 4463 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 4464 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 4465 | s++; |
| 4466 | size--; |
| 4467 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4468 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4469 | #endif |
| 4470 | STORECHAR(ch); |
| 4471 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4472 | |
| 4473 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4474 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4475 | #undef STORECHAR |
| 4476 | } |
| 4477 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4478 | PyObject * |
| 4479 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4480 | { |
| 4481 | if (!PyUnicode_Check(unicode)) { |
| 4482 | PyErr_BadArgument(); |
| 4483 | return NULL; |
| 4484 | } |
| 4485 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4486 | PyUnicode_GET_SIZE(unicode), |
| 4487 | NULL, |
| 4488 | 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4489 | } |
| 4490 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4491 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 4492 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4493 | PyObject * |
| 4494 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4495 | Py_ssize_t size, |
| 4496 | const char *errors, |
| 4497 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4498 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4499 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 4500 | } |
| 4501 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4502 | /* Two masks for fast checking of whether a C 'long' may contain |
| 4503 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 4504 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 4505 | rare in most input. |
| 4506 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 4507 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4508 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4509 | #if (SIZEOF_LONG == 8) |
| 4510 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 4511 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 4512 | #elif (SIZEOF_LONG == 4) |
| 4513 | # define FAST_CHAR_MASK 0x80008000L |
| 4514 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 4515 | #else |
| 4516 | # error C 'long' size should be either 4 or 8! |
| 4517 | #endif |
| 4518 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4519 | PyObject * |
| 4520 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4521 | Py_ssize_t size, |
| 4522 | const char *errors, |
| 4523 | int *byteorder, |
| 4524 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4525 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4526 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4527 | Py_ssize_t startinpos; |
| 4528 | Py_ssize_t endinpos; |
| 4529 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4530 | PyUnicodeObject *unicode; |
| 4531 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4532 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4533 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4534 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4535 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4536 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 4537 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4538 | int ihi = 1, ilo = 0; |
| 4539 | #else |
| 4540 | int ihi = 0, ilo = 1; |
| 4541 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4542 | PyObject *errorHandler = NULL; |
| 4543 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4544 | |
| 4545 | /* Note: size will always be longer than the resulting Unicode |
| 4546 | character count */ |
| 4547 | unicode = _PyUnicode_New(size); |
| 4548 | if (!unicode) |
| 4549 | return NULL; |
| 4550 | if (size == 0) |
| 4551 | return (PyObject *)unicode; |
| 4552 | |
| 4553 | /* Unpack UTF-16 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4554 | p = PyUnicode_AS_UNICODE(unicode); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4555 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4556 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4557 | |
| 4558 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4559 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4560 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 4561 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4562 | byte order setting accordingly. In native mode, the leading BOM |
| 4563 | mark is skipped, in all other modes, it is copied to the output |
| 4564 | stream as-is (giving a ZWNBSP character). */ |
| 4565 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4566 | if (size >= 2) { |
| 4567 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 4568 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4569 | if (bom == 0xFEFF) { |
| 4570 | q += 2; |
| 4571 | bo = -1; |
| 4572 | } |
| 4573 | else if (bom == 0xFFFE) { |
| 4574 | q += 2; |
| 4575 | bo = 1; |
| 4576 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4577 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4578 | if (bom == 0xFEFF) { |
| 4579 | q += 2; |
| 4580 | bo = 1; |
| 4581 | } |
| 4582 | else if (bom == 0xFFFE) { |
| 4583 | q += 2; |
| 4584 | bo = -1; |
| 4585 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 4586 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4587 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 4588 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4589 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4590 | if (bo == -1) { |
| 4591 | /* force LE */ |
| 4592 | ihi = 1; |
| 4593 | ilo = 0; |
| 4594 | } |
| 4595 | else if (bo == 1) { |
| 4596 | /* force BE */ |
| 4597 | ihi = 0; |
| 4598 | ilo = 1; |
| 4599 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4600 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4601 | native_ordering = ilo < ihi; |
| 4602 | #else |
| 4603 | native_ordering = ilo > ihi; |
| 4604 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4605 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4606 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4607 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4608 | Py_UNICODE ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4609 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 4610 | reads are more expensive, better to defer to another iteration. */ |
| 4611 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 4612 | /* Fast path for runs of non-surrogate chars. */ |
| 4613 | register const unsigned char *_q = q; |
| 4614 | Py_UNICODE *_p = p; |
| 4615 | if (native_ordering) { |
| 4616 | /* Native ordering is simple: as long as the input cannot |
| 4617 | possibly contain a surrogate char, do an unrolled copy |
| 4618 | of several 16-bit code points to the target object. |
| 4619 | The non-surrogate check is done on several input bytes |
| 4620 | at a time (as many as a C 'long' can contain). */ |
| 4621 | while (_q < aligned_end) { |
| 4622 | unsigned long data = * (unsigned long *) _q; |
| 4623 | if (data & FAST_CHAR_MASK) |
| 4624 | break; |
| 4625 | _p[0] = ((unsigned short *) _q)[0]; |
| 4626 | _p[1] = ((unsigned short *) _q)[1]; |
| 4627 | #if (SIZEOF_LONG == 8) |
| 4628 | _p[2] = ((unsigned short *) _q)[2]; |
| 4629 | _p[3] = ((unsigned short *) _q)[3]; |
| 4630 | #endif |
| 4631 | _q += SIZEOF_LONG; |
| 4632 | _p += SIZEOF_LONG / 2; |
| 4633 | } |
| 4634 | } |
| 4635 | else { |
| 4636 | /* Byteswapped ordering is similar, but we must decompose |
| 4637 | the copy bytewise, and take care of zero'ing out the |
| 4638 | upper bytes if the target object is in 32-bit units |
| 4639 | (that is, in UCS-4 builds). */ |
| 4640 | while (_q < aligned_end) { |
| 4641 | unsigned long data = * (unsigned long *) _q; |
| 4642 | if (data & SWAPPED_FAST_CHAR_MASK) |
| 4643 | break; |
| 4644 | /* Zero upper bytes in UCS-4 builds */ |
| 4645 | #if (Py_UNICODE_SIZE > 2) |
| 4646 | _p[0] = 0; |
| 4647 | _p[1] = 0; |
| 4648 | #if (SIZEOF_LONG == 8) |
| 4649 | _p[2] = 0; |
| 4650 | _p[3] = 0; |
| 4651 | #endif |
| 4652 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 4653 | /* Issue #4916; UCS-4 builds on big endian machines must |
| 4654 | fill the two last bytes of each 4-byte unit. */ |
| 4655 | #if (!defined(BYTEORDER_IS_LITTLE_ENDIAN) && Py_UNICODE_SIZE > 2) |
| 4656 | # define OFF 2 |
| 4657 | #else |
| 4658 | # define OFF 0 |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4659 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 4660 | ((unsigned char *) _p)[OFF + 1] = _q[0]; |
| 4661 | ((unsigned char *) _p)[OFF + 0] = _q[1]; |
| 4662 | ((unsigned char *) _p)[OFF + 1 + Py_UNICODE_SIZE] = _q[2]; |
| 4663 | ((unsigned char *) _p)[OFF + 0 + Py_UNICODE_SIZE] = _q[3]; |
| 4664 | #if (SIZEOF_LONG == 8) |
| 4665 | ((unsigned char *) _p)[OFF + 1 + 2 * Py_UNICODE_SIZE] = _q[4]; |
| 4666 | ((unsigned char *) _p)[OFF + 0 + 2 * Py_UNICODE_SIZE] = _q[5]; |
| 4667 | ((unsigned char *) _p)[OFF + 1 + 3 * Py_UNICODE_SIZE] = _q[6]; |
| 4668 | ((unsigned char *) _p)[OFF + 0 + 3 * Py_UNICODE_SIZE] = _q[7]; |
| 4669 | #endif |
| 4670 | #undef OFF |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4671 | _q += SIZEOF_LONG; |
| 4672 | _p += SIZEOF_LONG / 2; |
| 4673 | } |
| 4674 | } |
| 4675 | p = _p; |
| 4676 | q = _q; |
| 4677 | if (q >= e) |
| 4678 | break; |
| 4679 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4680 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4681 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4682 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4683 | |
| 4684 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 4685 | *p++ = ch; |
| 4686 | continue; |
| 4687 | } |
| 4688 | |
| 4689 | /* UTF-16 code pair: */ |
| 4690 | if (q > e) { |
| 4691 | errmsg = "unexpected end of data"; |
| 4692 | startinpos = (((const char *)q) - 2) - starts; |
| 4693 | endinpos = ((const char *)e) + 1 - starts; |
| 4694 | goto utf16Error; |
| 4695 | } |
| 4696 | if (0xD800 <= ch && ch <= 0xDBFF) { |
| 4697 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 4698 | q += 2; |
| 4699 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 4700 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4701 | *p++ = ch; |
| 4702 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4703 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4704 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4705 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4706 | continue; |
| 4707 | } |
| 4708 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4709 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4710 | startinpos = (((const char *)q)-4)-starts; |
| 4711 | endinpos = startinpos+2; |
| 4712 | goto utf16Error; |
| 4713 | } |
| 4714 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4715 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4716 | errmsg = "illegal encoding"; |
| 4717 | startinpos = (((const char *)q)-2)-starts; |
| 4718 | endinpos = startinpos+2; |
| 4719 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4720 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4721 | utf16Error: |
| 4722 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 4723 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4724 | errors, |
| 4725 | &errorHandler, |
| 4726 | "utf16", errmsg, |
| 4727 | &starts, |
| 4728 | (const char **)&e, |
| 4729 | &startinpos, |
| 4730 | &endinpos, |
| 4731 | &exc, |
| 4732 | (const char **)&q, |
| 4733 | &unicode, |
| 4734 | &outpos, |
| 4735 | &p)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4736 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4737 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4738 | /* remaining byte at the end? (size should be even) */ |
| 4739 | if (e == q) { |
| 4740 | if (!consumed) { |
| 4741 | errmsg = "truncated data"; |
| 4742 | startinpos = ((const char *)q) - starts; |
| 4743 | endinpos = ((const char *)e) + 1 - starts; |
| 4744 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 4745 | if (unicode_decode_call_errorhandler( |
| 4746 | errors, |
| 4747 | &errorHandler, |
| 4748 | "utf16", errmsg, |
| 4749 | &starts, |
| 4750 | (const char **)&e, |
| 4751 | &startinpos, |
| 4752 | &endinpos, |
| 4753 | &exc, |
| 4754 | (const char **)&q, |
| 4755 | &unicode, |
| 4756 | &outpos, |
| 4757 | &p)) |
| 4758 | goto onError; |
| 4759 | /* The remaining input chars are ignored if the callback |
| 4760 | chooses to skip the input */ |
| 4761 | } |
| 4762 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4763 | |
| 4764 | if (byteorder) |
| 4765 | *byteorder = bo; |
| 4766 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4767 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4768 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4769 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4770 | /* Adjust length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4771 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4772 | goto onError; |
| 4773 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4774 | Py_XDECREF(errorHandler); |
| 4775 | Py_XDECREF(exc); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4776 | if (PyUnicode_READY(unicode) == -1) { |
| 4777 | Py_DECREF(unicode); |
| 4778 | return NULL; |
| 4779 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4780 | return (PyObject *)unicode; |
| 4781 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4782 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4783 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4784 | Py_XDECREF(errorHandler); |
| 4785 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4786 | return NULL; |
| 4787 | } |
| 4788 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4789 | #undef FAST_CHAR_MASK |
| 4790 | #undef SWAPPED_FAST_CHAR_MASK |
| 4791 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4792 | PyObject * |
| 4793 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4794 | Py_ssize_t size, |
| 4795 | const char *errors, |
| 4796 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4797 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4798 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4799 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4800 | Py_ssize_t nsize, bytesize; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4801 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4802 | Py_ssize_t i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4803 | #else |
| 4804 | const int pairs = 0; |
| 4805 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4806 | /* Offsets from p for storing byte pairs in the right order. */ |
| 4807 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4808 | int ihi = 1, ilo = 0; |
| 4809 | #else |
| 4810 | int ihi = 0, ilo = 1; |
| 4811 | #endif |
| 4812 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4813 | #define STORECHAR(CH) \ |
| 4814 | do { \ |
| 4815 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 4816 | p[ilo] = (CH) & 0xff; \ |
| 4817 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4818 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4819 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4820 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4821 | for (i = pairs = 0; i < size; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4822 | if (s[i] >= 0x10000) |
| 4823 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4824 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4825 | /* 2 * (size + pairs + (byteorder == 0)) */ |
| 4826 | if (size > PY_SSIZE_T_MAX || |
| 4827 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4828 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4829 | nsize = size + pairs + (byteorder == 0); |
| 4830 | bytesize = nsize * 2; |
| 4831 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4832 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4833 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4834 | if (v == NULL) |
| 4835 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4836 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4837 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4838 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4839 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 4840 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4841 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4842 | |
| 4843 | if (byteorder == -1) { |
| 4844 | /* force LE */ |
| 4845 | ihi = 1; |
| 4846 | ilo = 0; |
| 4847 | } |
| 4848 | else if (byteorder == 1) { |
| 4849 | /* force BE */ |
| 4850 | ihi = 0; |
| 4851 | ilo = 1; |
| 4852 | } |
| 4853 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4854 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4855 | Py_UNICODE ch = *s++; |
| 4856 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4857 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4858 | if (ch >= 0x10000) { |
| 4859 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 4860 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 4861 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4862 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4863 | STORECHAR(ch); |
| 4864 | if (ch2) |
| 4865 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4866 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4867 | |
| 4868 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4869 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4870 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4871 | } |
| 4872 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4873 | PyObject * |
| 4874 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4875 | { |
| 4876 | if (!PyUnicode_Check(unicode)) { |
| 4877 | PyErr_BadArgument(); |
| 4878 | return NULL; |
| 4879 | } |
| 4880 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4881 | PyUnicode_GET_SIZE(unicode), |
| 4882 | NULL, |
| 4883 | 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4884 | } |
| 4885 | |
| 4886 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 4887 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4888 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 4889 | if all the escapes in the string make it still a valid ASCII string. |
| 4890 | Returns -1 if any escapes were found which cause the string to |
| 4891 | pop out of ASCII range. Otherwise returns the length of the |
| 4892 | required buffer to hold the string. |
| 4893 | */ |
| 4894 | Py_ssize_t |
| 4895 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 4896 | { |
| 4897 | const unsigned char *p = (const unsigned char *)s; |
| 4898 | const unsigned char *end = p + size; |
| 4899 | Py_ssize_t length = 0; |
| 4900 | |
| 4901 | if (size < 0) |
| 4902 | return -1; |
| 4903 | |
| 4904 | for (; p < end; ++p) { |
| 4905 | if (*p > 127) { |
| 4906 | /* Non-ASCII */ |
| 4907 | return -1; |
| 4908 | } |
| 4909 | else if (*p != '\\') { |
| 4910 | /* Normal character */ |
| 4911 | ++length; |
| 4912 | } |
| 4913 | else { |
| 4914 | /* Backslash-escape, check next char */ |
| 4915 | ++p; |
| 4916 | /* Escape sequence reaches till end of string or |
| 4917 | non-ASCII follow-up. */ |
| 4918 | if (p >= end || *p > 127) |
| 4919 | return -1; |
| 4920 | switch (*p) { |
| 4921 | case '\n': |
| 4922 | /* backslash + \n result in zero characters */ |
| 4923 | break; |
| 4924 | case '\\': case '\'': case '\"': |
| 4925 | case 'b': case 'f': case 't': |
| 4926 | case 'n': case 'r': case 'v': case 'a': |
| 4927 | ++length; |
| 4928 | break; |
| 4929 | case '0': case '1': case '2': case '3': |
| 4930 | case '4': case '5': case '6': case '7': |
| 4931 | case 'x': case 'u': case 'U': case 'N': |
| 4932 | /* these do not guarantee ASCII characters */ |
| 4933 | return -1; |
| 4934 | default: |
| 4935 | /* count the backslash + the other character */ |
| 4936 | length += 2; |
| 4937 | } |
| 4938 | } |
| 4939 | } |
| 4940 | return length; |
| 4941 | } |
| 4942 | |
| 4943 | /* Similar to PyUnicode_WRITE but either write into wstr field |
| 4944 | or treat string as ASCII. */ |
| 4945 | #define WRITE_ASCII_OR_WSTR(kind, buf, index, value) \ |
| 4946 | do { \ |
| 4947 | if ((kind) != PyUnicode_WCHAR_KIND) \ |
| 4948 | ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \ |
| 4949 | else \ |
| 4950 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \ |
| 4951 | } while (0) |
| 4952 | |
| 4953 | #define WRITE_WSTR(buf, index, value) \ |
| 4954 | assert(kind == PyUnicode_WCHAR_KIND), \ |
| 4955 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value) |
| 4956 | |
| 4957 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 4958 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 4959 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4960 | PyObject * |
| 4961 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4962 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 4963 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4964 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4965 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4966 | Py_ssize_t startinpos; |
| 4967 | Py_ssize_t endinpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4968 | int j; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4969 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4970 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4971 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 4972 | char* message; |
| 4973 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4974 | PyObject *errorHandler = NULL; |
| 4975 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4976 | Py_ssize_t ascii_length; |
| 4977 | Py_ssize_t i; |
| 4978 | int kind; |
| 4979 | void *data; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 4980 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4981 | ascii_length = length_of_escaped_ascii_string(s, size); |
| 4982 | |
| 4983 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 4984 | either the string is pure ASCII with named escapes like \n, etc. |
| 4985 | and we determined it's exact size (common case) |
| 4986 | or it contains \x, \u, ... escape sequences. then we create a |
| 4987 | legacy wchar string and resize it at the end of this function. */ |
| 4988 | if (ascii_length >= 0) { |
| 4989 | v = (PyUnicodeObject *)PyUnicode_New(ascii_length, 127); |
| 4990 | if (!v) |
| 4991 | goto onError; |
| 4992 | assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND); |
| 4993 | kind = PyUnicode_1BYTE_KIND; |
| 4994 | data = PyUnicode_DATA(v); |
| 4995 | } |
| 4996 | else { |
| 4997 | /* Escaped strings will always be longer than the resulting |
| 4998 | Unicode string, so we start with size here and then reduce the |
| 4999 | length after conversion to the true value. |
| 5000 | (but if the error callback returns a long replacement string |
| 5001 | we'll have to allocate more space) */ |
| 5002 | v = _PyUnicode_New(size); |
| 5003 | if (!v) |
| 5004 | goto onError; |
| 5005 | kind = PyUnicode_WCHAR_KIND; |
| 5006 | data = PyUnicode_AS_UNICODE(v); |
| 5007 | } |
| 5008 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5009 | if (size == 0) |
| 5010 | return (PyObject *)v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5011 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5012 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5013 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5014 | while (s < end) { |
| 5015 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 5016 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5017 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5018 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5019 | if (kind == PyUnicode_WCHAR_KIND) { |
| 5020 | assert(i < _PyUnicode_WSTR_LENGTH(v)); |
| 5021 | } |
| 5022 | else { |
| 5023 | /* The only case in which i == ascii_length is a backslash |
| 5024 | followed by a newline. */ |
| 5025 | assert(i <= ascii_length); |
| 5026 | } |
| 5027 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5028 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5029 | if (*s != '\\') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5030 | WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char) *s++); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5031 | continue; |
| 5032 | } |
| 5033 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5034 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5035 | /* \ - Escapes */ |
| 5036 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5037 | c = *s++; |
| 5038 | if (s > end) |
| 5039 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5040 | |
| 5041 | if (kind == PyUnicode_WCHAR_KIND) { |
| 5042 | assert(i < _PyUnicode_WSTR_LENGTH(v)); |
| 5043 | } |
| 5044 | else { |
| 5045 | /* The only case in which i == ascii_length is a backslash |
| 5046 | followed by a newline. */ |
| 5047 | assert(i < ascii_length || (i == ascii_length && c == '\n')); |
| 5048 | } |
| 5049 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5050 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5051 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5052 | /* \x escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5053 | case '\n': break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5054 | case '\\': WRITE_ASCII_OR_WSTR(kind, data, i++, '\\'); break; |
| 5055 | case '\'': WRITE_ASCII_OR_WSTR(kind, data, i++, '\''); break; |
| 5056 | case '\"': WRITE_ASCII_OR_WSTR(kind, data, i++, '\"'); break; |
| 5057 | case 'b': WRITE_ASCII_OR_WSTR(kind, data, i++, '\b'); break; |
| 5058 | /* FF */ |
| 5059 | case 'f': WRITE_ASCII_OR_WSTR(kind, data, i++, '\014'); break; |
| 5060 | case 't': WRITE_ASCII_OR_WSTR(kind, data, i++, '\t'); break; |
| 5061 | case 'n': WRITE_ASCII_OR_WSTR(kind, data, i++, '\n'); break; |
| 5062 | case 'r': WRITE_ASCII_OR_WSTR(kind, data, i++, '\r'); break; |
| 5063 | /* VT */ |
| 5064 | case 'v': WRITE_ASCII_OR_WSTR(kind, data, i++, '\013'); break; |
| 5065 | /* BEL, not classic C */ |
| 5066 | case 'a': WRITE_ASCII_OR_WSTR(kind, data, i++, '\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5067 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5068 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5069 | case '0': case '1': case '2': case '3': |
| 5070 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5071 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5072 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5073 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5074 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5075 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5076 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5077 | WRITE_WSTR(data, i++, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5078 | break; |
| 5079 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5080 | /* hex escapes */ |
| 5081 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5082 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5083 | digits = 2; |
| 5084 | message = "truncated \\xXX escape"; |
| 5085 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5086 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5087 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5088 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5089 | digits = 4; |
| 5090 | message = "truncated \\uXXXX escape"; |
| 5091 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5092 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5093 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5094 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5095 | digits = 8; |
| 5096 | message = "truncated \\UXXXXXXXX escape"; |
| 5097 | hexescape: |
| 5098 | chr = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5099 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5100 | if (s+digits>end) { |
| 5101 | endinpos = size; |
| 5102 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5103 | errors, &errorHandler, |
| 5104 | "unicodeescape", "end of string in escape sequence", |
| 5105 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5106 | &v, &i, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5107 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5108 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5109 | goto nextByte; |
| 5110 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5111 | for (j = 0; j < digits; ++j) { |
| 5112 | c = (unsigned char) s[j]; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5113 | if (!Py_ISXDIGIT(c)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5114 | endinpos = (s+j+1)-starts; |
| 5115 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5116 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5117 | errors, &errorHandler, |
| 5118 | "unicodeescape", message, |
| 5119 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5120 | &v, &i, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5121 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5122 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5123 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5124 | } |
| 5125 | chr = (chr<<4) & ~0xF; |
| 5126 | if (c >= '0' && c <= '9') |
| 5127 | chr += c - '0'; |
| 5128 | else if (c >= 'a' && c <= 'f') |
| 5129 | chr += 10 + c - 'a'; |
| 5130 | else |
| 5131 | chr += 10 + c - 'A'; |
| 5132 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5133 | s += j; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5134 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5135 | /* _decoding_error will have already written into the |
| 5136 | target buffer. */ |
| 5137 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5138 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5139 | /* when we get here, chr is a 32-bit unicode character */ |
| 5140 | if (chr <= 0xffff) |
| 5141 | /* UCS-2 character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5142 | WRITE_WSTR(data, i++, chr); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5143 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5144 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5145 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 5146 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5147 | WRITE_WSTR(data, i++, chr); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5148 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5149 | chr -= 0x10000L; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5150 | WRITE_WSTR(data, i++, 0xD800 + (Py_UNICODE) (chr >> 10)); |
| 5151 | WRITE_WSTR(data, i++, 0xDC00 + (Py_UNICODE) (chr & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5152 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5153 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5154 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5155 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5156 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5157 | errors, &errorHandler, |
| 5158 | "unicodeescape", "illegal Unicode character", |
| 5159 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5160 | &v, &i, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5161 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5162 | data = PyUnicode_AS_UNICODE(v); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5163 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5164 | break; |
| 5165 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5166 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5167 | case 'N': |
| 5168 | message = "malformed \\N character escape"; |
| 5169 | if (ucnhash_CAPI == NULL) { |
| 5170 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5171 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5172 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5173 | if (ucnhash_CAPI == NULL) |
| 5174 | goto ucnhashError; |
| 5175 | } |
| 5176 | if (*s == '{') { |
| 5177 | const char *start = s+1; |
| 5178 | /* look for the closing brace */ |
| 5179 | while (*s != '}' && s < end) |
| 5180 | s++; |
| 5181 | if (s > start && s < end && *s == '}') { |
| 5182 | /* found a name. look it up in the unicode database */ |
| 5183 | message = "unknown Unicode character name"; |
| 5184 | s++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5185 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
| 5186 | &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5187 | goto store; |
| 5188 | } |
| 5189 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5190 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5191 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5192 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5193 | errors, &errorHandler, |
| 5194 | "unicodeescape", message, |
| 5195 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5196 | &v, &i, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5197 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5198 | data = PyUnicode_AS_UNICODE(v); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5199 | break; |
| 5200 | |
| 5201 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5202 | if (s > end) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5203 | assert(kind == PyUnicode_WCHAR_KIND); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5204 | message = "\\ at end of string"; |
| 5205 | s--; |
| 5206 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5207 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5208 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5209 | errors, &errorHandler, |
| 5210 | "unicodeescape", message, |
| 5211 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5212 | &v, &i, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5213 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5214 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5215 | } |
| 5216 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5217 | WRITE_ASCII_OR_WSTR(kind, data, i++, '\\'); |
| 5218 | WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char)s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5219 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5220 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5221 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5222 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5223 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5224 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5225 | /* Ensure the length prediction worked in case of ASCII strings */ |
| 5226 | assert(kind == PyUnicode_WCHAR_KIND || i == ascii_length); |
| 5227 | |
| 5228 | if (kind == PyUnicode_WCHAR_KIND && (_PyUnicode_Resize(&v, i) < 0 || |
| 5229 | PyUnicode_READY(v) == -1)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5230 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5231 | Py_XDECREF(errorHandler); |
| 5232 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5233 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5234 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5235 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5236 | PyErr_SetString( |
| 5237 | PyExc_UnicodeError, |
| 5238 | "\\N escapes not supported (can't load unicodedata module)" |
| 5239 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5240 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5241 | Py_XDECREF(errorHandler); |
| 5242 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5243 | return NULL; |
| 5244 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5245 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5246 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5247 | Py_XDECREF(errorHandler); |
| 5248 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5249 | return NULL; |
| 5250 | } |
| 5251 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5252 | #undef WRITE_ASCII_OR_WSTR |
| 5253 | #undef WRITE_WSTR |
| 5254 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5255 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5256 | |
| 5257 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5258 | appropriate. |
| 5259 | |
| 5260 | */ |
| 5261 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5262 | static const char *hexdigits = "0123456789abcdef"; |
| 5263 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5264 | PyObject * |
| 5265 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5266 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5267 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5268 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5269 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5270 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5271 | #ifdef Py_UNICODE_WIDE |
| 5272 | const Py_ssize_t expandsize = 10; |
| 5273 | #else |
| 5274 | const Py_ssize_t expandsize = 6; |
| 5275 | #endif |
| 5276 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5277 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 5278 | better to choose a different scheme. Perhaps scan the |
| 5279 | first N-chars of the string and allocate based on that size. |
| 5280 | */ |
| 5281 | /* Initial allocation is based on the longest-possible unichr |
| 5282 | escape. |
| 5283 | |
| 5284 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 5285 | unichr, so in this case it's the longest unichr escape. In |
| 5286 | narrow (UTF-16) builds this is five chars per source unichr |
| 5287 | since there are two unichrs in the surrogate pair, so in narrow |
| 5288 | (UTF-16) builds it's not the longest unichr escape. |
| 5289 | |
| 5290 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 5291 | so in the narrow (UTF-16) build case it's the longest unichr |
| 5292 | escape. |
| 5293 | */ |
| 5294 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5295 | if (size == 0) |
| 5296 | return PyBytes_FromStringAndSize(NULL, 0); |
| 5297 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5298 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5299 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5300 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5301 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5302 | 2 |
| 5303 | + expandsize*size |
| 5304 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5305 | if (repr == NULL) |
| 5306 | return NULL; |
| 5307 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5308 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5309 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5310 | while (size-- > 0) { |
| 5311 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5312 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5313 | /* Escape backslashes */ |
| 5314 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5315 | *p++ = '\\'; |
| 5316 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5317 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5318 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5319 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 5320 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5321 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 5322 | else if (ch >= 0x10000) { |
| 5323 | *p++ = '\\'; |
| 5324 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5325 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 5326 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 5327 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 5328 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 5329 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 5330 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 5331 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 5332 | *p++ = hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5333 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5334 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5335 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5336 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 5337 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 5338 | Py_UNICODE ch2; |
| 5339 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5340 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5341 | ch2 = *s++; |
| 5342 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 5343 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5344 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 5345 | *p++ = '\\'; |
| 5346 | *p++ = 'U'; |
| 5347 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 5348 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 5349 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 5350 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 5351 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 5352 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 5353 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 5354 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 5355 | continue; |
| 5356 | } |
| 5357 | /* Fall through: isolated surrogates are copied as-is */ |
| 5358 | s--; |
| 5359 | size++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5360 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5361 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5362 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5363 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5364 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5365 | *p++ = '\\'; |
| 5366 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5367 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 5368 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 5369 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 5370 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5371 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5372 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5373 | /* Map special whitespace to '\t', \n', '\r' */ |
| 5374 | else if (ch == '\t') { |
| 5375 | *p++ = '\\'; |
| 5376 | *p++ = 't'; |
| 5377 | } |
| 5378 | else if (ch == '\n') { |
| 5379 | *p++ = '\\'; |
| 5380 | *p++ = 'n'; |
| 5381 | } |
| 5382 | else if (ch == '\r') { |
| 5383 | *p++ = '\\'; |
| 5384 | *p++ = 'r'; |
| 5385 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5386 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5387 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 5388 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5389 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5390 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5391 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 5392 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5393 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5394 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5395 | /* Copy everything else as-is */ |
| 5396 | else |
| 5397 | *p++ = (char) ch; |
| 5398 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5399 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5400 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 5401 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 5402 | return NULL; |
| 5403 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5404 | } |
| 5405 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5406 | PyObject * |
| 5407 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5408 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 5409 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5410 | if (!PyUnicode_Check(unicode)) { |
| 5411 | PyErr_BadArgument(); |
| 5412 | return NULL; |
| 5413 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5414 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 5415 | PyUnicode_GET_SIZE(unicode)); |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 5416 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5417 | } |
| 5418 | |
| 5419 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 5420 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5421 | PyObject * |
| 5422 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5423 | Py_ssize_t size, |
| 5424 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5425 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5426 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5427 | Py_ssize_t startinpos; |
| 5428 | Py_ssize_t endinpos; |
| 5429 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5430 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5431 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5432 | const char *end; |
| 5433 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5434 | PyObject *errorHandler = NULL; |
| 5435 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5436 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5437 | /* Escaped strings will always be longer than the resulting |
| 5438 | 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] | 5439 | length after conversion to the true value. (But decoding error |
| 5440 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5441 | v = _PyUnicode_New(size); |
| 5442 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5443 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5444 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5445 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5446 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5447 | end = s + size; |
| 5448 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5449 | unsigned char c; |
| 5450 | Py_UCS4 x; |
| 5451 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5452 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5453 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5454 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5455 | if (*s != '\\') { |
| 5456 | *p++ = (unsigned char)*s++; |
| 5457 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5458 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5459 | startinpos = s-starts; |
| 5460 | |
| 5461 | /* \u-escapes are only interpreted iff the number of leading |
| 5462 | backslashes if odd */ |
| 5463 | bs = s; |
| 5464 | for (;s < end;) { |
| 5465 | if (*s != '\\') |
| 5466 | break; |
| 5467 | *p++ = (unsigned char)*s++; |
| 5468 | } |
| 5469 | if (((s - bs) & 1) == 0 || |
| 5470 | s >= end || |
| 5471 | (*s != 'u' && *s != 'U')) { |
| 5472 | continue; |
| 5473 | } |
| 5474 | p--; |
| 5475 | count = *s=='u' ? 4 : 8; |
| 5476 | s++; |
| 5477 | |
| 5478 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
| 5479 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 5480 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 5481 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5482 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5483 | endinpos = s-starts; |
| 5484 | if (unicode_decode_call_errorhandler( |
| 5485 | errors, &errorHandler, |
| 5486 | "rawunicodeescape", "truncated \\uXXXX", |
| 5487 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 5488 | &v, &outpos, &p)) |
| 5489 | goto onError; |
| 5490 | goto nextByte; |
| 5491 | } |
| 5492 | x = (x<<4) & ~0xF; |
| 5493 | if (c >= '0' && c <= '9') |
| 5494 | x += c - '0'; |
| 5495 | else if (c >= 'a' && c <= 'f') |
| 5496 | x += 10 + c - 'a'; |
| 5497 | else |
| 5498 | x += 10 + c - 'A'; |
| 5499 | } |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5500 | if (x <= 0xffff) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5501 | /* UCS-2 character */ |
| 5502 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5503 | else if (x <= 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5504 | /* UCS-4 character. Either store directly, or as |
| 5505 | surrogate pair. */ |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5506 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5507 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5508 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5509 | x -= 0x10000L; |
| 5510 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 5511 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5512 | #endif |
| 5513 | } else { |
| 5514 | endinpos = s-starts; |
| 5515 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5516 | if (unicode_decode_call_errorhandler( |
| 5517 | errors, &errorHandler, |
| 5518 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5519 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 5520 | &v, &outpos, &p)) |
| 5521 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5522 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5523 | nextByte: |
| 5524 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5525 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5526 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5527 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5528 | Py_XDECREF(errorHandler); |
| 5529 | Py_XDECREF(exc); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5530 | if (PyUnicode_READY(v) == -1) { |
| 5531 | Py_DECREF(v); |
| 5532 | return NULL; |
| 5533 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5534 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5535 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5536 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5537 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5538 | Py_XDECREF(errorHandler); |
| 5539 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5540 | return NULL; |
| 5541 | } |
| 5542 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5543 | PyObject * |
| 5544 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5545 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5546 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5547 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5548 | char *p; |
| 5549 | char *q; |
| 5550 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5551 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5552 | const Py_ssize_t expandsize = 10; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5553 | #else |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5554 | const Py_ssize_t expandsize = 6; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5555 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5556 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5557 | if (size > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5558 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5559 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5560 | repr = PyBytes_FromStringAndSize(NULL, expandsize * size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5561 | if (repr == NULL) |
| 5562 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 5563 | if (size == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5564 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5565 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5566 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5567 | while (size-- > 0) { |
| 5568 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5569 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5570 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 5571 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5572 | *p++ = '\\'; |
| 5573 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 5574 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 5575 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 5576 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 5577 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 5578 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 5579 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 5580 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 5581 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5582 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5583 | else |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5584 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5585 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 5586 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 5587 | Py_UNICODE ch2; |
| 5588 | Py_UCS4 ucs; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5589 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5590 | ch2 = *s++; |
| 5591 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 5592 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5593 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 5594 | *p++ = '\\'; |
| 5595 | *p++ = 'U'; |
| 5596 | *p++ = hexdigits[(ucs >> 28) & 0xf]; |
| 5597 | *p++ = hexdigits[(ucs >> 24) & 0xf]; |
| 5598 | *p++ = hexdigits[(ucs >> 20) & 0xf]; |
| 5599 | *p++ = hexdigits[(ucs >> 16) & 0xf]; |
| 5600 | *p++ = hexdigits[(ucs >> 12) & 0xf]; |
| 5601 | *p++ = hexdigits[(ucs >> 8) & 0xf]; |
| 5602 | *p++ = hexdigits[(ucs >> 4) & 0xf]; |
| 5603 | *p++ = hexdigits[ucs & 0xf]; |
| 5604 | continue; |
| 5605 | } |
| 5606 | /* Fall through: isolated surrogates are copied as-is */ |
| 5607 | s--; |
| 5608 | size++; |
| 5609 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5610 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5611 | /* Map 16-bit characters to '\uxxxx' */ |
| 5612 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5613 | *p++ = '\\'; |
| 5614 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 5615 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 5616 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 5617 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 5618 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5619 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5620 | /* Copy everything else as-is */ |
| 5621 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5622 | *p++ = (char) ch; |
| 5623 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5624 | size = p - q; |
| 5625 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5626 | assert(size > 0); |
| 5627 | if (_PyBytes_Resize(&repr, size) < 0) |
| 5628 | return NULL; |
| 5629 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5630 | } |
| 5631 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5632 | PyObject * |
| 5633 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5634 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 5635 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5636 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 5637 | PyErr_BadArgument(); |
| 5638 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5639 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 5640 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 5641 | PyUnicode_GET_SIZE(unicode)); |
| 5642 | |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 5643 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5644 | } |
| 5645 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5646 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 5647 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5648 | PyObject * |
| 5649 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5650 | Py_ssize_t size, |
| 5651 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5652 | { |
| 5653 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5654 | Py_ssize_t startinpos; |
| 5655 | Py_ssize_t endinpos; |
| 5656 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5657 | PyUnicodeObject *v; |
| 5658 | Py_UNICODE *p; |
| 5659 | const char *end; |
| 5660 | const char *reason; |
| 5661 | PyObject *errorHandler = NULL; |
| 5662 | PyObject *exc = NULL; |
| 5663 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 5664 | #ifdef Py_UNICODE_WIDE |
| 5665 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 5666 | #endif |
| 5667 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5668 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5669 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 5670 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5671 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5672 | /* Intentionally PyUnicode_GET_SIZE instead of PyUnicode_GET_LENGTH |
| 5673 | as string was created with the old API. */ |
| 5674 | if (PyUnicode_GET_SIZE(v) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5675 | return (PyObject *)v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5676 | p = PyUnicode_AS_UNICODE(v); |
| 5677 | end = s + size; |
| 5678 | |
| 5679 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5680 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5681 | /* We have to sanity check the raw data, otherwise doom looms for |
| 5682 | some malformed UCS-4 data. */ |
| 5683 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5684 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5685 | *p > unimax || *p < 0 || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5686 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5687 | end-s < Py_UNICODE_SIZE |
| 5688 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5689 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5690 | startinpos = s - starts; |
| 5691 | if (end-s < Py_UNICODE_SIZE) { |
| 5692 | endinpos = end-starts; |
| 5693 | reason = "truncated input"; |
| 5694 | } |
| 5695 | else { |
| 5696 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 5697 | reason = "illegal code point (> 0x10FFFF)"; |
| 5698 | } |
| 5699 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 5700 | if (unicode_decode_call_errorhandler( |
| 5701 | errors, &errorHandler, |
| 5702 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 5703 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 5704 | &v, &outpos, &p)) { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5705 | goto onError; |
| 5706 | } |
| 5707 | } |
| 5708 | else { |
| 5709 | p++; |
| 5710 | s += Py_UNICODE_SIZE; |
| 5711 | } |
| 5712 | } |
| 5713 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5714 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5715 | goto onError; |
| 5716 | Py_XDECREF(errorHandler); |
| 5717 | Py_XDECREF(exc); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5718 | if (PyUnicode_READY(v) == -1) { |
| 5719 | Py_DECREF(v); |
| 5720 | return NULL; |
| 5721 | } |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5722 | return (PyObject *)v; |
| 5723 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5724 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5725 | Py_XDECREF(v); |
| 5726 | Py_XDECREF(errorHandler); |
| 5727 | Py_XDECREF(exc); |
| 5728 | return NULL; |
| 5729 | } |
| 5730 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5731 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 5732 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5733 | PyObject * |
| 5734 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5735 | Py_ssize_t size, |
| 5736 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5737 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5738 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 5739 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5740 | } |
| 5741 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5742 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5743 | static void |
| 5744 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5745 | const char *encoding, |
| 5746 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5747 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5748 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5749 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5750 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5751 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 5752 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5753 | } |
| 5754 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5755 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 5756 | goto onError; |
| 5757 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 5758 | goto onError; |
| 5759 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 5760 | goto onError; |
| 5761 | return; |
| 5762 | onError: |
| 5763 | Py_DECREF(*exceptionObject); |
| 5764 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5765 | } |
| 5766 | } |
| 5767 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5768 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5769 | static void |
| 5770 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5771 | const char *encoding, |
| 5772 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5773 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5774 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5775 | { |
| 5776 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5777 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5778 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5779 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5780 | } |
| 5781 | |
| 5782 | /* error handling callback helper: |
| 5783 | build arguments, call the callback and check the arguments, |
| 5784 | put the result into newpos and return the replacement string, which |
| 5785 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5786 | static PyObject * |
| 5787 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5788 | PyObject **errorHandler, |
| 5789 | const char *encoding, const char *reason, |
| 5790 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 5791 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5792 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5793 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5794 | 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] | 5795 | |
| 5796 | PyObject *restuple; |
| 5797 | PyObject *resunicode; |
| 5798 | |
| 5799 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5800 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5801 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5802 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5803 | } |
| 5804 | |
| 5805 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5806 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5807 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5808 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5809 | |
| 5810 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5811 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5812 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5813 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5814 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5815 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5816 | Py_DECREF(restuple); |
| 5817 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5818 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5819 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5820 | &resunicode, newpos)) { |
| 5821 | Py_DECREF(restuple); |
| 5822 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5823 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5824 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 5825 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 5826 | Py_DECREF(restuple); |
| 5827 | return NULL; |
| 5828 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5829 | if (*newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5830 | *newpos = size+*newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5831 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5832 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 5833 | Py_DECREF(restuple); |
| 5834 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5835 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5836 | Py_INCREF(resunicode); |
| 5837 | Py_DECREF(restuple); |
| 5838 | return resunicode; |
| 5839 | } |
| 5840 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5841 | static PyObject * |
| 5842 | unicode_encode_ucs1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5843 | Py_ssize_t size, |
| 5844 | const char *errors, |
| 5845 | int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5846 | { |
| 5847 | /* output object */ |
| 5848 | PyObject *res; |
| 5849 | /* pointers to the beginning and end+1 of input */ |
| 5850 | const Py_UNICODE *startp = p; |
| 5851 | const Py_UNICODE *endp = p + size; |
| 5852 | /* pointer to the beginning of the unencodable characters */ |
| 5853 | /* const Py_UNICODE *badp = NULL; */ |
| 5854 | /* pointer into the output */ |
| 5855 | char *str; |
| 5856 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5857 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5858 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 5859 | 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] | 5860 | PyObject *errorHandler = NULL; |
| 5861 | PyObject *exc = NULL; |
| 5862 | /* the following variable is used for caching string comparisons |
| 5863 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 5864 | int known_errorHandler = -1; |
| 5865 | |
| 5866 | /* allocate enough for a simple encoding without |
| 5867 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5868 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5869 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5870 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5871 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5872 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5873 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5874 | ressize = size; |
| 5875 | |
| 5876 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5877 | Py_UNICODE c = *p; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5878 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5879 | /* can we encode this? */ |
| 5880 | if (c<limit) { |
| 5881 | /* no overflow check, because we know that the space is enough */ |
| 5882 | *str++ = (char)c; |
| 5883 | ++p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5884 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5885 | else { |
| 5886 | Py_ssize_t unicodepos = p-startp; |
| 5887 | Py_ssize_t requiredsize; |
| 5888 | PyObject *repunicode; |
| 5889 | Py_ssize_t repsize; |
| 5890 | Py_ssize_t newpos; |
| 5891 | Py_ssize_t respos; |
| 5892 | Py_UNICODE *uni2; |
| 5893 | /* startpos for collecting unencodable chars */ |
| 5894 | const Py_UNICODE *collstart = p; |
| 5895 | const Py_UNICODE *collend = p; |
| 5896 | /* find all unecodable characters */ |
| 5897 | while ((collend < endp) && ((*collend)>=limit)) |
| 5898 | ++collend; |
| 5899 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 5900 | if (known_errorHandler==-1) { |
| 5901 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5902 | known_errorHandler = 1; |
| 5903 | else if (!strcmp(errors, "replace")) |
| 5904 | known_errorHandler = 2; |
| 5905 | else if (!strcmp(errors, "ignore")) |
| 5906 | known_errorHandler = 3; |
| 5907 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5908 | known_errorHandler = 4; |
| 5909 | else |
| 5910 | known_errorHandler = 0; |
| 5911 | } |
| 5912 | switch (known_errorHandler) { |
| 5913 | case 1: /* strict */ |
| 5914 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 5915 | goto onError; |
| 5916 | case 2: /* replace */ |
| 5917 | while (collstart++<collend) |
| 5918 | *str++ = '?'; /* fall through */ |
| 5919 | case 3: /* ignore */ |
| 5920 | p = collend; |
| 5921 | break; |
| 5922 | case 4: /* xmlcharrefreplace */ |
| 5923 | respos = str - PyBytes_AS_STRING(res); |
| 5924 | /* determine replacement size (temporarily (mis)uses p) */ |
| 5925 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 5926 | if (*p<10) |
| 5927 | repsize += 2+1+1; |
| 5928 | else if (*p<100) |
| 5929 | repsize += 2+2+1; |
| 5930 | else if (*p<1000) |
| 5931 | repsize += 2+3+1; |
| 5932 | else if (*p<10000) |
| 5933 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 5934 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5935 | else |
| 5936 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 5937 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5938 | else if (*p<100000) |
| 5939 | repsize += 2+5+1; |
| 5940 | else if (*p<1000000) |
| 5941 | repsize += 2+6+1; |
| 5942 | else |
| 5943 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5944 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5945 | } |
| 5946 | requiredsize = respos+repsize+(endp-collend); |
| 5947 | if (requiredsize > ressize) { |
| 5948 | if (requiredsize<2*ressize) |
| 5949 | requiredsize = 2*ressize; |
| 5950 | if (_PyBytes_Resize(&res, requiredsize)) |
| 5951 | goto onError; |
| 5952 | str = PyBytes_AS_STRING(res) + respos; |
| 5953 | ressize = requiredsize; |
| 5954 | } |
| 5955 | /* generate replacement (temporarily (mis)uses p) */ |
| 5956 | for (p = collstart; p < collend; ++p) { |
| 5957 | str += sprintf(str, "&#%d;", (int)*p); |
| 5958 | } |
| 5959 | p = collend; |
| 5960 | break; |
| 5961 | default: |
| 5962 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 5963 | encoding, reason, startp, size, &exc, |
| 5964 | collstart-startp, collend-startp, &newpos); |
| 5965 | if (repunicode == NULL) |
| 5966 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5967 | if (PyBytes_Check(repunicode)) { |
| 5968 | /* Directly copy bytes result to output. */ |
| 5969 | repsize = PyBytes_Size(repunicode); |
| 5970 | if (repsize > 1) { |
| 5971 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 5972 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5973 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 5974 | Py_DECREF(repunicode); |
| 5975 | goto onError; |
| 5976 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 5977 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5978 | ressize += repsize-1; |
| 5979 | } |
| 5980 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 5981 | str += repsize; |
| 5982 | p = startp + newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5983 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5984 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5985 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5986 | /* need more space? (at least enough for what we |
| 5987 | have+the replacement+the rest of the string, so |
| 5988 | we won't have to check space for encodable characters) */ |
| 5989 | respos = str - PyBytes_AS_STRING(res); |
| 5990 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5991 | requiredsize = respos+repsize+(endp-collend); |
| 5992 | if (requiredsize > ressize) { |
| 5993 | if (requiredsize<2*ressize) |
| 5994 | requiredsize = 2*ressize; |
| 5995 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 5996 | Py_DECREF(repunicode); |
| 5997 | goto onError; |
| 5998 | } |
| 5999 | str = PyBytes_AS_STRING(res) + respos; |
| 6000 | ressize = requiredsize; |
| 6001 | } |
| 6002 | /* check if there is anything unencodable in the replacement |
| 6003 | and copy it to the output */ |
| 6004 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 6005 | c = *uni2; |
| 6006 | if (c >= limit) { |
| 6007 | raise_encode_exception(&exc, encoding, startp, size, |
| 6008 | unicodepos, unicodepos+1, reason); |
| 6009 | Py_DECREF(repunicode); |
| 6010 | goto onError; |
| 6011 | } |
| 6012 | *str = (char)c; |
| 6013 | } |
| 6014 | p = startp + newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6015 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6016 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6017 | } |
| 6018 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6019 | /* Resize if we allocated to much */ |
| 6020 | size = str - PyBytes_AS_STRING(res); |
| 6021 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6022 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6023 | if (_PyBytes_Resize(&res, size) < 0) |
| 6024 | goto onError; |
| 6025 | } |
| 6026 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6027 | Py_XDECREF(errorHandler); |
| 6028 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6029 | return res; |
| 6030 | |
| 6031 | onError: |
| 6032 | Py_XDECREF(res); |
| 6033 | Py_XDECREF(errorHandler); |
| 6034 | Py_XDECREF(exc); |
| 6035 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6036 | } |
| 6037 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6038 | PyObject * |
| 6039 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6040 | Py_ssize_t size, |
| 6041 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6042 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6043 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6044 | } |
| 6045 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6046 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6047 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6048 | { |
| 6049 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6050 | PyErr_BadArgument(); |
| 6051 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6052 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6053 | if (PyUnicode_READY(unicode) == -1) |
| 6054 | return NULL; |
| 6055 | /* Fast path: if it is a one-byte string, construct |
| 6056 | bytes object directly. */ |
| 6057 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6058 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6059 | PyUnicode_GET_LENGTH(unicode)); |
| 6060 | /* Non-Latin-1 characters present. Defer to above function to |
| 6061 | raise the exception. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6062 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6063 | PyUnicode_GET_SIZE(unicode), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6064 | errors); |
| 6065 | } |
| 6066 | |
| 6067 | PyObject* |
| 6068 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6069 | { |
| 6070 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6071 | } |
| 6072 | |
| 6073 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6074 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6075 | PyObject * |
| 6076 | PyUnicode_DecodeASCII(const char *s, |
| 6077 | Py_ssize_t size, |
| 6078 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6079 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6080 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6081 | PyUnicodeObject *v; |
| 6082 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6083 | Py_ssize_t startinpos; |
| 6084 | Py_ssize_t endinpos; |
| 6085 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6086 | const char *e; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6087 | unsigned char* d; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6088 | PyObject *errorHandler = NULL; |
| 6089 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6090 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6091 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6092 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6093 | if (size == 1 && *(unsigned char*)s < 128) |
| 6094 | return PyUnicode_FromOrdinal(*(unsigned char*)s); |
| 6095 | |
| 6096 | /* Fast path. Assume the input actually *is* ASCII, and allocate |
| 6097 | a single-block Unicode object with that assumption. If there is |
| 6098 | an error, drop the object and start over. */ |
| 6099 | v = (PyUnicodeObject*)PyUnicode_New(size, 127); |
| 6100 | if (v == NULL) |
| 6101 | goto onError; |
| 6102 | d = PyUnicode_1BYTE_DATA(v); |
| 6103 | for (i = 0; i < size; i++) { |
| 6104 | unsigned char ch = ((unsigned char*)s)[i]; |
| 6105 | if (ch < 128) |
| 6106 | d[i] = ch; |
| 6107 | else |
| 6108 | break; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6109 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6110 | if (i == size) |
| 6111 | return (PyObject*)v; |
| 6112 | Py_DECREF(v); /* start over */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6113 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6114 | v = _PyUnicode_New(size); |
| 6115 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6116 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6117 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6118 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6119 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6120 | e = s + size; |
| 6121 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6122 | register unsigned char c = (unsigned char)*s; |
| 6123 | if (c < 128) { |
| 6124 | *p++ = c; |
| 6125 | ++s; |
| 6126 | } |
| 6127 | else { |
| 6128 | startinpos = s-starts; |
| 6129 | endinpos = startinpos + 1; |
| 6130 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
| 6131 | if (unicode_decode_call_errorhandler( |
| 6132 | errors, &errorHandler, |
| 6133 | "ascii", "ordinal not in range(128)", |
| 6134 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 6135 | &v, &outpos, &p)) |
| 6136 | goto onError; |
| 6137 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6138 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6139 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6140 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 6141 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6142 | Py_XDECREF(errorHandler); |
| 6143 | Py_XDECREF(exc); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6144 | if (PyUnicode_READY(v) == -1) { |
| 6145 | Py_DECREF(v); |
| 6146 | return NULL; |
| 6147 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6148 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6149 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6150 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6151 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6152 | Py_XDECREF(errorHandler); |
| 6153 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6154 | return NULL; |
| 6155 | } |
| 6156 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6157 | PyObject * |
| 6158 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6159 | Py_ssize_t size, |
| 6160 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6161 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6162 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6163 | } |
| 6164 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6165 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6166 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6167 | { |
| 6168 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6169 | PyErr_BadArgument(); |
| 6170 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6171 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6172 | if (PyUnicode_READY(unicode) == -1) |
| 6173 | return NULL; |
| 6174 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6175 | directly. Else defer to above function to raise the exception. */ |
| 6176 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 6177 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6178 | PyUnicode_GET_LENGTH(unicode)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6179 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6180 | PyUnicode_GET_SIZE(unicode), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6181 | errors); |
| 6182 | } |
| 6183 | |
| 6184 | PyObject * |
| 6185 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6186 | { |
| 6187 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6188 | } |
| 6189 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6190 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6191 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6192 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6193 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6194 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6195 | #define NEED_RETRY |
| 6196 | #endif |
| 6197 | |
| 6198 | /* XXX This code is limited to "true" double-byte encodings, as |
| 6199 | a) it assumes an incomplete character consists of a single byte, and |
| 6200 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6201 | encodings, see IsDBCSLeadByteEx documentation. */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6202 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6203 | static int |
| 6204 | is_dbcs_lead_byte(const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6205 | { |
| 6206 | const char *curr = s + offset; |
| 6207 | |
| 6208 | if (IsDBCSLeadByte(*curr)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6209 | const char *prev = CharPrev(s, curr); |
| 6210 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6211 | } |
| 6212 | return 0; |
| 6213 | } |
| 6214 | |
| 6215 | /* |
| 6216 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 6217 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 6218 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6219 | static int |
| 6220 | decode_mbcs(PyUnicodeObject **v, |
| 6221 | const char *s, /* MBCS string */ |
| 6222 | int size, /* sizeof MBCS string */ |
| 6223 | int final, |
| 6224 | const char *errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6225 | { |
| 6226 | Py_UNICODE *p; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6227 | Py_ssize_t n; |
| 6228 | DWORD usize; |
| 6229 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6230 | |
| 6231 | assert(size >= 0); |
| 6232 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6233 | /* check and handle 'errors' arg */ |
| 6234 | if (errors==NULL || strcmp(errors, "strict")==0) |
| 6235 | flags = MB_ERR_INVALID_CHARS; |
| 6236 | else if (strcmp(errors, "ignore")==0) |
| 6237 | flags = 0; |
| 6238 | else { |
| 6239 | PyErr_Format(PyExc_ValueError, |
| 6240 | "mbcs encoding does not support errors='%s'", |
| 6241 | errors); |
| 6242 | return -1; |
| 6243 | } |
| 6244 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6245 | /* Skip trailing lead-byte unless 'final' is set */ |
| 6246 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6247 | --size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6248 | |
| 6249 | /* First get the size of the result */ |
| 6250 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6251 | usize = MultiByteToWideChar(CP_ACP, flags, s, size, NULL, 0); |
| 6252 | if (usize==0) |
| 6253 | goto mbcs_decode_error; |
| 6254 | } else |
| 6255 | usize = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6256 | |
| 6257 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6258 | /* Create unicode object */ |
| 6259 | *v = _PyUnicode_New(usize); |
| 6260 | if (*v == NULL) |
| 6261 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6262 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6263 | } |
| 6264 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6265 | /* Extend unicode object */ |
| 6266 | n = PyUnicode_GET_SIZE(*v); |
| 6267 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 6268 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6269 | } |
| 6270 | |
| 6271 | /* Do the conversion */ |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6272 | if (usize > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6273 | p = PyUnicode_AS_UNICODE(*v) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6274 | if (0 == MultiByteToWideChar(CP_ACP, flags, s, size, p, usize)) { |
| 6275 | goto mbcs_decode_error; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6276 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6277 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6278 | return size; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6279 | |
| 6280 | mbcs_decode_error: |
| 6281 | /* If the last error was ERROR_NO_UNICODE_TRANSLATION, then |
| 6282 | we raise a UnicodeDecodeError - else it is a 'generic' |
| 6283 | windows error |
| 6284 | */ |
| 6285 | if (GetLastError()==ERROR_NO_UNICODE_TRANSLATION) { |
| 6286 | /* Ideally, we should get reason from FormatMessage - this |
| 6287 | is the Windows 2000 English version of the message |
| 6288 | */ |
| 6289 | PyObject *exc = NULL; |
| 6290 | const char *reason = "No mapping for the Unicode character exists " |
| 6291 | "in the target multi-byte code page."; |
| 6292 | make_decode_exception(&exc, "mbcs", s, size, 0, 0, reason); |
| 6293 | if (exc != NULL) { |
| 6294 | PyCodec_StrictErrors(exc); |
| 6295 | Py_DECREF(exc); |
| 6296 | } |
| 6297 | } else { |
| 6298 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 6299 | } |
| 6300 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6301 | } |
| 6302 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6303 | PyObject * |
| 6304 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 6305 | Py_ssize_t size, |
| 6306 | const char *errors, |
| 6307 | Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6308 | { |
| 6309 | PyUnicodeObject *v = NULL; |
| 6310 | int done; |
| 6311 | |
| 6312 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6313 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6314 | |
| 6315 | #ifdef NEED_RETRY |
| 6316 | retry: |
| 6317 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6318 | done = decode_mbcs(&v, s, INT_MAX, 0, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6319 | else |
| 6320 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6321 | done = decode_mbcs(&v, s, (int)size, !consumed, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6322 | |
| 6323 | if (done < 0) { |
| 6324 | Py_XDECREF(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6325 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6326 | } |
| 6327 | |
| 6328 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6329 | *consumed += done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6330 | |
| 6331 | #ifdef NEED_RETRY |
| 6332 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6333 | s += done; |
| 6334 | size -= done; |
| 6335 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6336 | } |
| 6337 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6338 | if (PyUnicode_READY(v) == -1) { |
| 6339 | Py_DECREF(v); |
| 6340 | return NULL; |
| 6341 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6342 | return (PyObject *)v; |
| 6343 | } |
| 6344 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6345 | PyObject * |
| 6346 | PyUnicode_DecodeMBCS(const char *s, |
| 6347 | Py_ssize_t size, |
| 6348 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6349 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6350 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 6351 | } |
| 6352 | |
| 6353 | /* |
| 6354 | * Convert unicode into string object (MBCS). |
| 6355 | * Returns 0 if succeed, -1 otherwise. |
| 6356 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6357 | static int |
| 6358 | encode_mbcs(PyObject **repr, |
| 6359 | const Py_UNICODE *p, /* unicode */ |
| 6360 | int size, /* size of unicode */ |
| 6361 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6362 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6363 | BOOL usedDefaultChar = FALSE; |
| 6364 | BOOL *pusedDefaultChar; |
| 6365 | int mbcssize; |
| 6366 | Py_ssize_t n; |
| 6367 | PyObject *exc = NULL; |
| 6368 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6369 | |
| 6370 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6371 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6372 | /* check and handle 'errors' arg */ |
| 6373 | if (errors==NULL || strcmp(errors, "strict")==0) { |
| 6374 | flags = WC_NO_BEST_FIT_CHARS; |
| 6375 | pusedDefaultChar = &usedDefaultChar; |
| 6376 | } else if (strcmp(errors, "replace")==0) { |
| 6377 | flags = 0; |
| 6378 | pusedDefaultChar = NULL; |
| 6379 | } else { |
| 6380 | PyErr_Format(PyExc_ValueError, |
| 6381 | "mbcs encoding does not support errors='%s'", |
| 6382 | errors); |
| 6383 | return -1; |
| 6384 | } |
| 6385 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6386 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6387 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6388 | mbcssize = WideCharToMultiByte(CP_ACP, flags, p, size, NULL, 0, |
| 6389 | NULL, pusedDefaultChar); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6390 | if (mbcssize == 0) { |
| 6391 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 6392 | return -1; |
| 6393 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6394 | /* If we used a default char, then we failed! */ |
| 6395 | if (pusedDefaultChar && *pusedDefaultChar) |
| 6396 | goto mbcs_encode_error; |
| 6397 | } else { |
| 6398 | mbcssize = 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6399 | } |
| 6400 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6401 | if (*repr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6402 | /* Create string object */ |
| 6403 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); |
| 6404 | if (*repr == NULL) |
| 6405 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6406 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6407 | } |
| 6408 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6409 | /* Extend string object */ |
| 6410 | n = PyBytes_Size(*repr); |
| 6411 | if (_PyBytes_Resize(repr, n + mbcssize) < 0) |
| 6412 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6413 | } |
| 6414 | |
| 6415 | /* Do the conversion */ |
| 6416 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6417 | char *s = PyBytes_AS_STRING(*repr) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6418 | if (0 == WideCharToMultiByte(CP_ACP, flags, p, size, s, mbcssize, |
| 6419 | NULL, pusedDefaultChar)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6420 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 6421 | return -1; |
| 6422 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6423 | if (pusedDefaultChar && *pusedDefaultChar) |
| 6424 | goto mbcs_encode_error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6425 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6426 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6427 | |
| 6428 | mbcs_encode_error: |
| 6429 | raise_encode_exception(&exc, "mbcs", p, size, 0, 0, "invalid character"); |
| 6430 | Py_XDECREF(exc); |
| 6431 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6432 | } |
| 6433 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6434 | PyObject * |
| 6435 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 6436 | Py_ssize_t size, |
| 6437 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6438 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6439 | PyObject *repr = NULL; |
| 6440 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 6441 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6442 | #ifdef NEED_RETRY |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6443 | retry: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6444 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6445 | ret = encode_mbcs(&repr, p, INT_MAX, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6446 | else |
| 6447 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6448 | ret = encode_mbcs(&repr, p, (int)size, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6449 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6450 | if (ret < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6451 | Py_XDECREF(repr); |
| 6452 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6453 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6454 | |
| 6455 | #ifdef NEED_RETRY |
| 6456 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6457 | p += INT_MAX; |
| 6458 | size -= INT_MAX; |
| 6459 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6460 | } |
| 6461 | #endif |
| 6462 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6463 | return repr; |
| 6464 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6465 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6466 | PyObject * |
| 6467 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 6468 | { |
| 6469 | if (!PyUnicode_Check(unicode)) { |
| 6470 | PyErr_BadArgument(); |
| 6471 | return NULL; |
| 6472 | } |
| 6473 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6474 | PyUnicode_GET_SIZE(unicode), |
| 6475 | NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 6476 | } |
| 6477 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6478 | #undef NEED_RETRY |
| 6479 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6480 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6481 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6482 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 6483 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6484 | PyObject * |
| 6485 | PyUnicode_DecodeCharmap(const char *s, |
| 6486 | Py_ssize_t size, |
| 6487 | PyObject *mapping, |
| 6488 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6489 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6490 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6491 | Py_ssize_t startinpos; |
| 6492 | Py_ssize_t endinpos; |
| 6493 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6494 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6495 | PyUnicodeObject *v; |
| 6496 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6497 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6498 | PyObject *errorHandler = NULL; |
| 6499 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 6500 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6501 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6502 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6503 | /* Default to Latin-1 */ |
| 6504 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6505 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6506 | |
| 6507 | v = _PyUnicode_New(size); |
| 6508 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6509 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6510 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6511 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6512 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6513 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 6514 | if (PyUnicode_CheckExact(mapping)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6515 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 6516 | maplen = PyUnicode_GET_SIZE(mapping); |
| 6517 | while (s < e) { |
| 6518 | unsigned char ch = *s; |
| 6519 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6520 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6521 | if (ch < maplen) |
| 6522 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6523 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6524 | if (x == 0xfffe) { |
| 6525 | /* undefined mapping */ |
| 6526 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 6527 | startinpos = s-starts; |
| 6528 | endinpos = startinpos+1; |
| 6529 | if (unicode_decode_call_errorhandler( |
| 6530 | errors, &errorHandler, |
| 6531 | "charmap", "character maps to <undefined>", |
| 6532 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 6533 | &v, &outpos, &p)) { |
| 6534 | goto onError; |
| 6535 | } |
| 6536 | continue; |
| 6537 | } |
| 6538 | *p++ = x; |
| 6539 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6540 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 6541 | } |
| 6542 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6543 | while (s < e) { |
| 6544 | unsigned char ch = *s; |
| 6545 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 6546 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6547 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 6548 | w = PyLong_FromLong((long)ch); |
| 6549 | if (w == NULL) |
| 6550 | goto onError; |
| 6551 | x = PyObject_GetItem(mapping, w); |
| 6552 | Py_DECREF(w); |
| 6553 | if (x == NULL) { |
| 6554 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 6555 | /* No mapping found means: mapping is undefined. */ |
| 6556 | PyErr_Clear(); |
| 6557 | x = Py_None; |
| 6558 | Py_INCREF(x); |
| 6559 | } else |
| 6560 | goto onError; |
| 6561 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6562 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6563 | /* Apply mapping */ |
| 6564 | if (PyLong_Check(x)) { |
| 6565 | long value = PyLong_AS_LONG(x); |
| 6566 | if (value < 0 || value > 65535) { |
| 6567 | PyErr_SetString(PyExc_TypeError, |
| 6568 | "character mapping must be in range(65536)"); |
| 6569 | Py_DECREF(x); |
| 6570 | goto onError; |
| 6571 | } |
| 6572 | *p++ = (Py_UNICODE)value; |
| 6573 | } |
| 6574 | else if (x == Py_None) { |
| 6575 | /* undefined mapping */ |
| 6576 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 6577 | startinpos = s-starts; |
| 6578 | endinpos = startinpos+1; |
| 6579 | if (unicode_decode_call_errorhandler( |
| 6580 | errors, &errorHandler, |
| 6581 | "charmap", "character maps to <undefined>", |
| 6582 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 6583 | &v, &outpos, &p)) { |
| 6584 | Py_DECREF(x); |
| 6585 | goto onError; |
| 6586 | } |
| 6587 | Py_DECREF(x); |
| 6588 | continue; |
| 6589 | } |
| 6590 | else if (PyUnicode_Check(x)) { |
| 6591 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6592 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6593 | if (targetsize == 1) |
| 6594 | /* 1-1 mapping */ |
| 6595 | *p++ = *PyUnicode_AS_UNICODE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6596 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6597 | else if (targetsize > 1) { |
| 6598 | /* 1-n mapping */ |
| 6599 | if (targetsize > extrachars) { |
| 6600 | /* resize first */ |
| 6601 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 6602 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 6603 | (targetsize << 2); |
| 6604 | extrachars += needed; |
| 6605 | /* XXX overflow detection missing */ |
| 6606 | if (_PyUnicode_Resize(&v, |
| 6607 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 6608 | Py_DECREF(x); |
| 6609 | goto onError; |
| 6610 | } |
| 6611 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 6612 | } |
| 6613 | Py_UNICODE_COPY(p, |
| 6614 | PyUnicode_AS_UNICODE(x), |
| 6615 | targetsize); |
| 6616 | p += targetsize; |
| 6617 | extrachars -= targetsize; |
| 6618 | } |
| 6619 | /* 1-0 mapping: skip the character */ |
| 6620 | } |
| 6621 | else { |
| 6622 | /* wrong return value */ |
| 6623 | PyErr_SetString(PyExc_TypeError, |
| 6624 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6625 | Py_DECREF(x); |
| 6626 | goto onError; |
| 6627 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6628 | Py_DECREF(x); |
| 6629 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6630 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6631 | } |
| 6632 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6633 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 6634 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6635 | Py_XDECREF(errorHandler); |
| 6636 | Py_XDECREF(exc); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6637 | if (PyUnicode_READY(v) == -1) { |
| 6638 | Py_DECREF(v); |
| 6639 | return NULL; |
| 6640 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6641 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6642 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6643 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6644 | Py_XDECREF(errorHandler); |
| 6645 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6646 | Py_XDECREF(v); |
| 6647 | return NULL; |
| 6648 | } |
| 6649 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6650 | /* Charmap encoding: the lookup table */ |
| 6651 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6652 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6653 | PyObject_HEAD |
| 6654 | unsigned char level1[32]; |
| 6655 | int count2, count3; |
| 6656 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6657 | }; |
| 6658 | |
| 6659 | static PyObject* |
| 6660 | encoding_map_size(PyObject *obj, PyObject* args) |
| 6661 | { |
| 6662 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6663 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6664 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6665 | } |
| 6666 | |
| 6667 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6668 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6669 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 6670 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6671 | }; |
| 6672 | |
| 6673 | static void |
| 6674 | encoding_map_dealloc(PyObject* o) |
| 6675 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6676 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6677 | } |
| 6678 | |
| 6679 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6680 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6681 | "EncodingMap", /*tp_name*/ |
| 6682 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 6683 | 0, /*tp_itemsize*/ |
| 6684 | /* methods */ |
| 6685 | encoding_map_dealloc, /*tp_dealloc*/ |
| 6686 | 0, /*tp_print*/ |
| 6687 | 0, /*tp_getattr*/ |
| 6688 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 6689 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6690 | 0, /*tp_repr*/ |
| 6691 | 0, /*tp_as_number*/ |
| 6692 | 0, /*tp_as_sequence*/ |
| 6693 | 0, /*tp_as_mapping*/ |
| 6694 | 0, /*tp_hash*/ |
| 6695 | 0, /*tp_call*/ |
| 6696 | 0, /*tp_str*/ |
| 6697 | 0, /*tp_getattro*/ |
| 6698 | 0, /*tp_setattro*/ |
| 6699 | 0, /*tp_as_buffer*/ |
| 6700 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 6701 | 0, /*tp_doc*/ |
| 6702 | 0, /*tp_traverse*/ |
| 6703 | 0, /*tp_clear*/ |
| 6704 | 0, /*tp_richcompare*/ |
| 6705 | 0, /*tp_weaklistoffset*/ |
| 6706 | 0, /*tp_iter*/ |
| 6707 | 0, /*tp_iternext*/ |
| 6708 | encoding_map_methods, /*tp_methods*/ |
| 6709 | 0, /*tp_members*/ |
| 6710 | 0, /*tp_getset*/ |
| 6711 | 0, /*tp_base*/ |
| 6712 | 0, /*tp_dict*/ |
| 6713 | 0, /*tp_descr_get*/ |
| 6714 | 0, /*tp_descr_set*/ |
| 6715 | 0, /*tp_dictoffset*/ |
| 6716 | 0, /*tp_init*/ |
| 6717 | 0, /*tp_alloc*/ |
| 6718 | 0, /*tp_new*/ |
| 6719 | 0, /*tp_free*/ |
| 6720 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6721 | }; |
| 6722 | |
| 6723 | PyObject* |
| 6724 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 6725 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6726 | PyObject *result; |
| 6727 | struct encoding_map *mresult; |
| 6728 | int i; |
| 6729 | int need_dict = 0; |
| 6730 | unsigned char level1[32]; |
| 6731 | unsigned char level2[512]; |
| 6732 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 6733 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6734 | int kind; |
| 6735 | void *data; |
| 6736 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6737 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6738 | if (!PyUnicode_Check(string) || PyUnicode_GET_LENGTH(string) != 256) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6739 | PyErr_BadArgument(); |
| 6740 | return NULL; |
| 6741 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6742 | kind = PyUnicode_KIND(string); |
| 6743 | data = PyUnicode_DATA(string); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6744 | memset(level1, 0xFF, sizeof level1); |
| 6745 | memset(level2, 0xFF, sizeof level2); |
| 6746 | |
| 6747 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 6748 | or if there are non-BMP characters, we need to use |
| 6749 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6750 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6751 | need_dict = 1; |
| 6752 | for (i = 1; i < 256; i++) { |
| 6753 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6754 | ch = PyUnicode_READ(kind, data, i); |
| 6755 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6756 | need_dict = 1; |
| 6757 | break; |
| 6758 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6759 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6760 | /* unmapped character */ |
| 6761 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6762 | l1 = ch >> 11; |
| 6763 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6764 | if (level1[l1] == 0xFF) |
| 6765 | level1[l1] = count2++; |
| 6766 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6767 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6768 | } |
| 6769 | |
| 6770 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 6771 | need_dict = 1; |
| 6772 | |
| 6773 | if (need_dict) { |
| 6774 | PyObject *result = PyDict_New(); |
| 6775 | PyObject *key, *value; |
| 6776 | if (!result) |
| 6777 | return NULL; |
| 6778 | for (i = 0; i < 256; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6779 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6780 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6781 | if (!key || !value) |
| 6782 | goto failed1; |
| 6783 | if (PyDict_SetItem(result, key, value) == -1) |
| 6784 | goto failed1; |
| 6785 | Py_DECREF(key); |
| 6786 | Py_DECREF(value); |
| 6787 | } |
| 6788 | return result; |
| 6789 | failed1: |
| 6790 | Py_XDECREF(key); |
| 6791 | Py_XDECREF(value); |
| 6792 | Py_DECREF(result); |
| 6793 | return NULL; |
| 6794 | } |
| 6795 | |
| 6796 | /* Create a three-level trie */ |
| 6797 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 6798 | 16*count2 + 128*count3 - 1); |
| 6799 | if (!result) |
| 6800 | return PyErr_NoMemory(); |
| 6801 | PyObject_Init(result, &EncodingMapType); |
| 6802 | mresult = (struct encoding_map*)result; |
| 6803 | mresult->count2 = count2; |
| 6804 | mresult->count3 = count3; |
| 6805 | mlevel1 = mresult->level1; |
| 6806 | mlevel2 = mresult->level23; |
| 6807 | mlevel3 = mresult->level23 + 16*count2; |
| 6808 | memcpy(mlevel1, level1, 32); |
| 6809 | memset(mlevel2, 0xFF, 16*count2); |
| 6810 | memset(mlevel3, 0, 128*count3); |
| 6811 | count3 = 0; |
| 6812 | for (i = 1; i < 256; i++) { |
| 6813 | int o1, o2, o3, i2, i3; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6814 | if (PyUnicode_READ(kind, data, i) == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6815 | /* unmapped character */ |
| 6816 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6817 | o1 = PyUnicode_READ(kind, data, i)>>11; |
| 6818 | o2 = (PyUnicode_READ(kind, data, i)>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6819 | i2 = 16*mlevel1[o1] + o2; |
| 6820 | if (mlevel2[i2] == 0xFF) |
| 6821 | mlevel2[i2] = count3++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6822 | o3 = PyUnicode_READ(kind, data, i) & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6823 | i3 = 128*mlevel2[i2] + o3; |
| 6824 | mlevel3[i3] = i; |
| 6825 | } |
| 6826 | return result; |
| 6827 | } |
| 6828 | |
| 6829 | static int |
| 6830 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 6831 | { |
| 6832 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 6833 | int l1 = c>>11; |
| 6834 | int l2 = (c>>7) & 0xF; |
| 6835 | int l3 = c & 0x7F; |
| 6836 | int i; |
| 6837 | |
| 6838 | #ifdef Py_UNICODE_WIDE |
| 6839 | if (c > 0xFFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6840 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6841 | } |
| 6842 | #endif |
| 6843 | if (c == 0) |
| 6844 | return 0; |
| 6845 | /* level 1*/ |
| 6846 | i = map->level1[l1]; |
| 6847 | if (i == 0xFF) { |
| 6848 | return -1; |
| 6849 | } |
| 6850 | /* level 2*/ |
| 6851 | i = map->level23[16*i+l2]; |
| 6852 | if (i == 0xFF) { |
| 6853 | return -1; |
| 6854 | } |
| 6855 | /* level 3 */ |
| 6856 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 6857 | if (i == 0) { |
| 6858 | return -1; |
| 6859 | } |
| 6860 | return i; |
| 6861 | } |
| 6862 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6863 | /* Lookup the character ch in the mapping. If the character |
| 6864 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 6865 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6866 | static PyObject * |
| 6867 | charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6868 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6869 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6870 | PyObject *x; |
| 6871 | |
| 6872 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6873 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6874 | x = PyObject_GetItem(mapping, w); |
| 6875 | Py_DECREF(w); |
| 6876 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6877 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 6878 | /* No mapping found means: mapping is undefined. */ |
| 6879 | PyErr_Clear(); |
| 6880 | x = Py_None; |
| 6881 | Py_INCREF(x); |
| 6882 | return x; |
| 6883 | } else |
| 6884 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6885 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 6886 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6887 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6888 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6889 | long value = PyLong_AS_LONG(x); |
| 6890 | if (value < 0 || value > 255) { |
| 6891 | PyErr_SetString(PyExc_TypeError, |
| 6892 | "character mapping must be in range(256)"); |
| 6893 | Py_DECREF(x); |
| 6894 | return NULL; |
| 6895 | } |
| 6896 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6897 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6898 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6899 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6900 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6901 | /* wrong return value */ |
| 6902 | PyErr_Format(PyExc_TypeError, |
| 6903 | "character mapping must return integer, bytes or None, not %.400s", |
| 6904 | x->ob_type->tp_name); |
| 6905 | Py_DECREF(x); |
| 6906 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6907 | } |
| 6908 | } |
| 6909 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6910 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6911 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6912 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6913 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 6914 | /* exponentially overallocate to minimize reallocations */ |
| 6915 | if (requiredsize < 2*outsize) |
| 6916 | requiredsize = 2*outsize; |
| 6917 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 6918 | return -1; |
| 6919 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6920 | } |
| 6921 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6922 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6923 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6924 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6925 | /* 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] | 6926 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6927 | space is available. Return a new reference to the object that |
| 6928 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 6929 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 6930 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6931 | static charmapencode_result |
| 6932 | charmapencode_output(Py_UNICODE c, PyObject *mapping, |
| 6933 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6934 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6935 | PyObject *rep; |
| 6936 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6937 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6938 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 6939 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6940 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6941 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6942 | if (res == -1) |
| 6943 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6944 | if (outsize<requiredsize) |
| 6945 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 6946 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6947 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6948 | outstart[(*outpos)++] = (char)res; |
| 6949 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6950 | } |
| 6951 | |
| 6952 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6953 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6954 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6955 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6956 | Py_DECREF(rep); |
| 6957 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6958 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6959 | if (PyLong_Check(rep)) { |
| 6960 | Py_ssize_t requiredsize = *outpos+1; |
| 6961 | if (outsize<requiredsize) |
| 6962 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 6963 | Py_DECREF(rep); |
| 6964 | return enc_EXCEPTION; |
| 6965 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6966 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6967 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6968 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6969 | else { |
| 6970 | const char *repchars = PyBytes_AS_STRING(rep); |
| 6971 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 6972 | Py_ssize_t requiredsize = *outpos+repsize; |
| 6973 | if (outsize<requiredsize) |
| 6974 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 6975 | Py_DECREF(rep); |
| 6976 | return enc_EXCEPTION; |
| 6977 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6978 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6979 | memcpy(outstart + *outpos, repchars, repsize); |
| 6980 | *outpos += repsize; |
| 6981 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6982 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6983 | Py_DECREF(rep); |
| 6984 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6985 | } |
| 6986 | |
| 6987 | /* handle an error in PyUnicode_EncodeCharmap |
| 6988 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6989 | static int |
| 6990 | charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6991 | 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] | 6992 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 6993 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6994 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6995 | { |
| 6996 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6997 | Py_ssize_t repsize; |
| 6998 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6999 | Py_UNICODE *uni2; |
| 7000 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7001 | Py_ssize_t collstartpos = *inpos; |
| 7002 | Py_ssize_t collendpos = *inpos+1; |
| 7003 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7004 | char *encoding = "charmap"; |
| 7005 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7006 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7007 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7008 | /* find all unencodable characters */ |
| 7009 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7010 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7011 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7012 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 7013 | if (res != -1) |
| 7014 | break; |
| 7015 | ++collendpos; |
| 7016 | continue; |
| 7017 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7018 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7019 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 7020 | if (rep==NULL) |
| 7021 | return -1; |
| 7022 | else if (rep!=Py_None) { |
| 7023 | Py_DECREF(rep); |
| 7024 | break; |
| 7025 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7026 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7027 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7028 | } |
| 7029 | /* cache callback name lookup |
| 7030 | * (if not done yet, i.e. it's the first error) */ |
| 7031 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7032 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 7033 | *known_errorHandler = 1; |
| 7034 | else if (!strcmp(errors, "replace")) |
| 7035 | *known_errorHandler = 2; |
| 7036 | else if (!strcmp(errors, "ignore")) |
| 7037 | *known_errorHandler = 3; |
| 7038 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 7039 | *known_errorHandler = 4; |
| 7040 | else |
| 7041 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7042 | } |
| 7043 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7044 | case 1: /* strict */ |
| 7045 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7046 | return -1; |
| 7047 | case 2: /* replace */ |
| 7048 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7049 | x = charmapencode_output('?', mapping, res, respos); |
| 7050 | if (x==enc_EXCEPTION) { |
| 7051 | return -1; |
| 7052 | } |
| 7053 | else if (x==enc_FAILED) { |
| 7054 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7055 | return -1; |
| 7056 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7057 | } |
| 7058 | /* fall through */ |
| 7059 | case 3: /* ignore */ |
| 7060 | *inpos = collendpos; |
| 7061 | break; |
| 7062 | case 4: /* xmlcharrefreplace */ |
| 7063 | /* generate replacement (temporarily (mis)uses p) */ |
| 7064 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7065 | char buffer[2+29+1+1]; |
| 7066 | char *cp; |
| 7067 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 7068 | for (cp = buffer; *cp; ++cp) { |
| 7069 | x = charmapencode_output(*cp, mapping, res, respos); |
| 7070 | if (x==enc_EXCEPTION) |
| 7071 | return -1; |
| 7072 | else if (x==enc_FAILED) { |
| 7073 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7074 | return -1; |
| 7075 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7076 | } |
| 7077 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7078 | *inpos = collendpos; |
| 7079 | break; |
| 7080 | default: |
| 7081 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7082 | encoding, reason, p, size, exceptionObject, |
| 7083 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7084 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7085 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7086 | if (PyBytes_Check(repunicode)) { |
| 7087 | /* Directly copy bytes result to output. */ |
| 7088 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 7089 | Py_ssize_t requiredsize; |
| 7090 | repsize = PyBytes_Size(repunicode); |
| 7091 | requiredsize = *respos + repsize; |
| 7092 | if (requiredsize > outsize) |
| 7093 | /* Make room for all additional bytes. */ |
| 7094 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 7095 | Py_DECREF(repunicode); |
| 7096 | return -1; |
| 7097 | } |
| 7098 | memcpy(PyBytes_AsString(*res) + *respos, |
| 7099 | PyBytes_AsString(repunicode), repsize); |
| 7100 | *respos += repsize; |
| 7101 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7102 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7103 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7104 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7105 | /* generate replacement */ |
| 7106 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 7107 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7108 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 7109 | if (x==enc_EXCEPTION) { |
| 7110 | return -1; |
| 7111 | } |
| 7112 | else if (x==enc_FAILED) { |
| 7113 | Py_DECREF(repunicode); |
| 7114 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7115 | return -1; |
| 7116 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7117 | } |
| 7118 | *inpos = newpos; |
| 7119 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7120 | } |
| 7121 | return 0; |
| 7122 | } |
| 7123 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7124 | PyObject * |
| 7125 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 7126 | Py_ssize_t size, |
| 7127 | PyObject *mapping, |
| 7128 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7129 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7130 | /* output object */ |
| 7131 | PyObject *res = NULL; |
| 7132 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7133 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7134 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7135 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7136 | PyObject *errorHandler = NULL; |
| 7137 | PyObject *exc = NULL; |
| 7138 | /* the following variable is used for caching string comparisons |
| 7139 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 7140 | * 3=ignore, 4=xmlcharrefreplace */ |
| 7141 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7142 | |
| 7143 | /* Default to Latin-1 */ |
| 7144 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7145 | return PyUnicode_EncodeLatin1(p, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7146 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7147 | /* allocate enough for a simple encoding without |
| 7148 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7149 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7150 | if (res == NULL) |
| 7151 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 7152 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7153 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7154 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7155 | while (inpos<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7156 | /* try to encode it */ |
| 7157 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 7158 | if (x==enc_EXCEPTION) /* error */ |
| 7159 | goto onError; |
| 7160 | if (x==enc_FAILED) { /* unencodable character */ |
| 7161 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 7162 | &exc, |
| 7163 | &known_errorHandler, &errorHandler, errors, |
| 7164 | &res, &respos)) { |
| 7165 | goto onError; |
| 7166 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7167 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7168 | else |
| 7169 | /* done with this character => adjust input position */ |
| 7170 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7171 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7172 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7173 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7174 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 7175 | if (_PyBytes_Resize(&res, respos) < 0) |
| 7176 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7177 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7178 | Py_XDECREF(exc); |
| 7179 | Py_XDECREF(errorHandler); |
| 7180 | return res; |
| 7181 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7182 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7183 | Py_XDECREF(res); |
| 7184 | Py_XDECREF(exc); |
| 7185 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7186 | return NULL; |
| 7187 | } |
| 7188 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7189 | PyObject * |
| 7190 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 7191 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7192 | { |
| 7193 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7194 | PyErr_BadArgument(); |
| 7195 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7196 | } |
| 7197 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7198 | PyUnicode_GET_SIZE(unicode), |
| 7199 | mapping, |
| 7200 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7201 | } |
| 7202 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7203 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7204 | static void |
| 7205 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7206 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7207 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7208 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7209 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7210 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7211 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 7212 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7213 | } |
| 7214 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7215 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 7216 | goto onError; |
| 7217 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 7218 | goto onError; |
| 7219 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 7220 | goto onError; |
| 7221 | return; |
| 7222 | onError: |
| 7223 | Py_DECREF(*exceptionObject); |
| 7224 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7225 | } |
| 7226 | } |
| 7227 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7228 | /* raises a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7229 | static void |
| 7230 | raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7231 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7232 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7233 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7234 | { |
| 7235 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7236 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7237 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7238 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7239 | } |
| 7240 | |
| 7241 | /* error handling callback helper: |
| 7242 | build arguments, call the callback and check the arguments, |
| 7243 | put the result into newpos and return the replacement string, which |
| 7244 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7245 | static PyObject * |
| 7246 | unicode_translate_call_errorhandler(const char *errors, |
| 7247 | PyObject **errorHandler, |
| 7248 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7249 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7250 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7251 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7252 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7253 | 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] | 7254 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7255 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7256 | PyObject *restuple; |
| 7257 | PyObject *resunicode; |
| 7258 | |
| 7259 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7260 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7261 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7262 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7263 | } |
| 7264 | |
| 7265 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7266 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7267 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7268 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7269 | |
| 7270 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7271 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7272 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7273 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7274 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 7275 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7276 | Py_DECREF(restuple); |
| 7277 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7278 | } |
| 7279 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7280 | &resunicode, &i_newpos)) { |
| 7281 | Py_DECREF(restuple); |
| 7282 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7283 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7284 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7285 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7286 | else |
| 7287 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7288 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7289 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 7290 | Py_DECREF(restuple); |
| 7291 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 7292 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7293 | Py_INCREF(resunicode); |
| 7294 | Py_DECREF(restuple); |
| 7295 | return resunicode; |
| 7296 | } |
| 7297 | |
| 7298 | /* Lookup the character ch in the mapping and put the result in result, |
| 7299 | which must be decrefed by the caller. |
| 7300 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7301 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7302 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7303 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7304 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7305 | PyObject *x; |
| 7306 | |
| 7307 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7308 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7309 | x = PyObject_GetItem(mapping, w); |
| 7310 | Py_DECREF(w); |
| 7311 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7312 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7313 | /* No mapping found means: use 1:1 mapping. */ |
| 7314 | PyErr_Clear(); |
| 7315 | *result = NULL; |
| 7316 | return 0; |
| 7317 | } else |
| 7318 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7319 | } |
| 7320 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7321 | *result = x; |
| 7322 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7323 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7324 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7325 | long value = PyLong_AS_LONG(x); |
| 7326 | long max = PyUnicode_GetMax(); |
| 7327 | if (value < 0 || value > max) { |
| 7328 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 7329 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7330 | Py_DECREF(x); |
| 7331 | return -1; |
| 7332 | } |
| 7333 | *result = x; |
| 7334 | return 0; |
| 7335 | } |
| 7336 | else if (PyUnicode_Check(x)) { |
| 7337 | *result = x; |
| 7338 | return 0; |
| 7339 | } |
| 7340 | else { |
| 7341 | /* wrong return value */ |
| 7342 | PyErr_SetString(PyExc_TypeError, |
| 7343 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7344 | Py_DECREF(x); |
| 7345 | return -1; |
| 7346 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7347 | } |
| 7348 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7349 | if not reallocate and adjust various state variables. |
| 7350 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7351 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7352 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7353 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7354 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7355 | Py_ssize_t oldsize = *psize; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 7356 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7357 | /* exponentially overallocate to minimize reallocations */ |
| 7358 | if (requiredsize < 2 * oldsize) |
| 7359 | requiredsize = 2 * oldsize; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7360 | *outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 7361 | if (*outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7362 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7363 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7364 | } |
| 7365 | return 0; |
| 7366 | } |
| 7367 | /* lookup the character, put the result in the output string and adjust |
| 7368 | various state variables. Return a new reference to the object that |
| 7369 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 7370 | undefined (in which case no character was written). |
| 7371 | The called must decref result. |
| 7372 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7373 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7374 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 7375 | PyObject *mapping, Py_UCS4 **output, |
| 7376 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7377 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7378 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7379 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 7380 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7381 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7382 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7383 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7384 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7385 | } |
| 7386 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7387 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7388 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7389 | /* 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] | 7390 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7391 | } |
| 7392 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7393 | Py_ssize_t repsize; |
| 7394 | if (PyUnicode_READY(*res) == -1) |
| 7395 | return -1; |
| 7396 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7397 | if (repsize==1) { |
| 7398 | /* 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] | 7399 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7400 | } |
| 7401 | else if (repsize!=0) { |
| 7402 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7403 | Py_ssize_t requiredsize = *opos + |
| 7404 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7405 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7406 | Py_ssize_t i; |
| 7407 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7408 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7409 | for(i = 0; i < repsize; i++) |
| 7410 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7411 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7412 | } |
| 7413 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7414 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7415 | return 0; |
| 7416 | } |
| 7417 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7418 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7419 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 7420 | PyObject *mapping, |
| 7421 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7422 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7423 | /* input object */ |
| 7424 | char *idata; |
| 7425 | Py_ssize_t size, i; |
| 7426 | int kind; |
| 7427 | /* output buffer */ |
| 7428 | Py_UCS4 *output = NULL; |
| 7429 | Py_ssize_t osize; |
| 7430 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7431 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7432 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7433 | char *reason = "character maps to <undefined>"; |
| 7434 | PyObject *errorHandler = NULL; |
| 7435 | PyObject *exc = NULL; |
| 7436 | /* the following variable is used for caching string comparisons |
| 7437 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 7438 | * 3=ignore, 4=xmlcharrefreplace */ |
| 7439 | int known_errorHandler = -1; |
| 7440 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7441 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7442 | PyErr_BadArgument(); |
| 7443 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7444 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7445 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7446 | if (PyUnicode_READY(input) == -1) |
| 7447 | return NULL; |
| 7448 | idata = (char*)PyUnicode_DATA(input); |
| 7449 | kind = PyUnicode_KIND(input); |
| 7450 | size = PyUnicode_GET_LENGTH(input); |
| 7451 | i = 0; |
| 7452 | |
| 7453 | if (size == 0) { |
| 7454 | Py_INCREF(input); |
| 7455 | return input; |
| 7456 | } |
| 7457 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7458 | /* allocate enough for a simple 1:1 translation without |
| 7459 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7460 | osize = size; |
| 7461 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 7462 | opos = 0; |
| 7463 | if (output == NULL) { |
| 7464 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7465 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7466 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7467 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7468 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7469 | /* try to encode it */ |
| 7470 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7471 | if (charmaptranslate_output(input, i, mapping, |
| 7472 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7473 | Py_XDECREF(x); |
| 7474 | goto onError; |
| 7475 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7476 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7477 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7478 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7479 | else { /* untranslatable character */ |
| 7480 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 7481 | Py_ssize_t repsize; |
| 7482 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7483 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7484 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7485 | Py_ssize_t collstart = i; |
| 7486 | Py_ssize_t collend = i+1; |
| 7487 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7488 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7489 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7490 | while (collend < size) { |
| 7491 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7492 | goto onError; |
| 7493 | Py_XDECREF(x); |
| 7494 | if (x!=Py_None) |
| 7495 | break; |
| 7496 | ++collend; |
| 7497 | } |
| 7498 | /* cache callback name lookup |
| 7499 | * (if not done yet, i.e. it's the first error) */ |
| 7500 | if (known_errorHandler==-1) { |
| 7501 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 7502 | known_errorHandler = 1; |
| 7503 | else if (!strcmp(errors, "replace")) |
| 7504 | known_errorHandler = 2; |
| 7505 | else if (!strcmp(errors, "ignore")) |
| 7506 | known_errorHandler = 3; |
| 7507 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 7508 | known_errorHandler = 4; |
| 7509 | else |
| 7510 | known_errorHandler = 0; |
| 7511 | } |
| 7512 | switch (known_errorHandler) { |
| 7513 | case 1: /* strict */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7514 | raise_translate_exception(&exc, input, collstart, |
| 7515 | collend, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7516 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7517 | case 2: /* replace */ |
| 7518 | /* 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] | 7519 | for (coll = collstart; coll<collend; coll++) |
| 7520 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7521 | /* fall through */ |
| 7522 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7523 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7524 | break; |
| 7525 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7526 | /* generate replacement (temporarily (mis)uses i) */ |
| 7527 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7528 | char buffer[2+29+1+1]; |
| 7529 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7530 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 7531 | if (charmaptranslate_makespace(&output, &osize, |
| 7532 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7533 | goto onError; |
| 7534 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7535 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7536 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7537 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7538 | break; |
| 7539 | default: |
| 7540 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7541 | reason, input, &exc, |
| 7542 | collstart, collend, &newpos); |
| 7543 | if (repunicode == NULL || PyUnicode_READY(repunicode) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7544 | goto onError; |
| 7545 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7546 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 7547 | if (charmaptranslate_makespace(&output, &osize, |
| 7548 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7549 | Py_DECREF(repunicode); |
| 7550 | goto onError; |
| 7551 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7552 | for (uni2 = 0; repsize-->0; ++uni2) |
| 7553 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 7554 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7555 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7556 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7557 | } |
| 7558 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7559 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 7560 | if (!res) |
| 7561 | goto onError; |
| 7562 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7563 | Py_XDECREF(exc); |
| 7564 | Py_XDECREF(errorHandler); |
| 7565 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7566 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7567 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7568 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7569 | Py_XDECREF(exc); |
| 7570 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7571 | return NULL; |
| 7572 | } |
| 7573 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7574 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 7575 | PyObject * |
| 7576 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 7577 | Py_ssize_t size, |
| 7578 | PyObject *mapping, |
| 7579 | const char *errors) |
| 7580 | { |
| 7581 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 7582 | if (!unicode) |
| 7583 | return NULL; |
| 7584 | return _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 7585 | } |
| 7586 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7587 | PyObject * |
| 7588 | PyUnicode_Translate(PyObject *str, |
| 7589 | PyObject *mapping, |
| 7590 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7591 | { |
| 7592 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7593 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7594 | str = PyUnicode_FromObject(str); |
| 7595 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7596 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7597 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7598 | Py_DECREF(str); |
| 7599 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7600 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7601 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7602 | Py_XDECREF(str); |
| 7603 | return NULL; |
| 7604 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7605 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7606 | static Py_UCS4 |
| 7607 | fix_decimal_and_space_to_ascii(PyUnicodeObject *self) |
| 7608 | { |
| 7609 | /* No need to call PyUnicode_READY(self) because this function is only |
| 7610 | called as a callback from fixup() which does it already. */ |
| 7611 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 7612 | const int kind = PyUnicode_KIND(self); |
| 7613 | void *data = PyUnicode_DATA(self); |
| 7614 | Py_UCS4 maxchar = 0, ch, fixed; |
| 7615 | Py_ssize_t i; |
| 7616 | |
| 7617 | for (i = 0; i < len; ++i) { |
| 7618 | ch = PyUnicode_READ(kind, data, i); |
| 7619 | fixed = 0; |
| 7620 | if (ch > 127) { |
| 7621 | if (Py_UNICODE_ISSPACE(ch)) |
| 7622 | fixed = ' '; |
| 7623 | else { |
| 7624 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 7625 | if (decimal >= 0) |
| 7626 | fixed = '0' + decimal; |
| 7627 | } |
| 7628 | if (fixed != 0) { |
| 7629 | if (fixed > maxchar) |
| 7630 | maxchar = fixed; |
| 7631 | PyUnicode_WRITE(kind, data, i, fixed); |
| 7632 | } |
| 7633 | else if (ch > maxchar) |
| 7634 | maxchar = ch; |
| 7635 | } |
| 7636 | else if (ch > maxchar) |
| 7637 | maxchar = ch; |
| 7638 | } |
| 7639 | |
| 7640 | return maxchar; |
| 7641 | } |
| 7642 | |
| 7643 | PyObject * |
| 7644 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 7645 | { |
| 7646 | if (!PyUnicode_Check(unicode)) { |
| 7647 | PyErr_BadInternalCall(); |
| 7648 | return NULL; |
| 7649 | } |
| 7650 | if (PyUnicode_READY(unicode) == -1) |
| 7651 | return NULL; |
| 7652 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 7653 | /* If the string is already ASCII, just return the same string */ |
| 7654 | Py_INCREF(unicode); |
| 7655 | return unicode; |
| 7656 | } |
| 7657 | return fixup((PyUnicodeObject *)unicode, fix_decimal_and_space_to_ascii); |
| 7658 | } |
| 7659 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 7660 | PyObject * |
| 7661 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 7662 | Py_ssize_t length) |
| 7663 | { |
| 7664 | PyObject *result; |
| 7665 | Py_UNICODE *p; /* write pointer into result */ |
| 7666 | Py_ssize_t i; |
| 7667 | /* Copy to a new string */ |
| 7668 | result = (PyObject *)_PyUnicode_New(length); |
| 7669 | Py_UNICODE_COPY(PyUnicode_AS_UNICODE(result), s, length); |
| 7670 | if (result == NULL) |
| 7671 | return result; |
| 7672 | p = PyUnicode_AS_UNICODE(result); |
| 7673 | /* Iterate over code points */ |
| 7674 | for (i = 0; i < length; i++) { |
| 7675 | Py_UNICODE ch =s[i]; |
| 7676 | if (ch > 127) { |
| 7677 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 7678 | if (decimal >= 0) |
| 7679 | p[i] = '0' + decimal; |
| 7680 | } |
| 7681 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7682 | if (PyUnicode_READY((PyUnicodeObject*)result) == -1) { |
| 7683 | Py_DECREF(result); |
| 7684 | return NULL; |
| 7685 | } |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 7686 | return result; |
| 7687 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 7688 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 7689 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7690 | int |
| 7691 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 7692 | Py_ssize_t length, |
| 7693 | char *output, |
| 7694 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 7695 | { |
| 7696 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7697 | PyObject *errorHandler = NULL; |
| 7698 | PyObject *exc = NULL; |
| 7699 | const char *encoding = "decimal"; |
| 7700 | const char *reason = "invalid decimal Unicode string"; |
| 7701 | /* the following variable is used for caching string comparisons |
| 7702 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 7703 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 7704 | |
| 7705 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7706 | PyErr_BadArgument(); |
| 7707 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 7708 | } |
| 7709 | |
| 7710 | p = s; |
| 7711 | end = s + length; |
| 7712 | while (p < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7713 | register Py_UNICODE ch = *p; |
| 7714 | int decimal; |
| 7715 | PyObject *repunicode; |
| 7716 | Py_ssize_t repsize; |
| 7717 | Py_ssize_t newpos; |
| 7718 | Py_UNICODE *uni2; |
| 7719 | Py_UNICODE *collstart; |
| 7720 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7721 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7722 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7723 | *output++ = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7724 | ++p; |
| 7725 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7726 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7727 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 7728 | if (decimal >= 0) { |
| 7729 | *output++ = '0' + decimal; |
| 7730 | ++p; |
| 7731 | continue; |
| 7732 | } |
| 7733 | if (0 < ch && ch < 256) { |
| 7734 | *output++ = (char)ch; |
| 7735 | ++p; |
| 7736 | continue; |
| 7737 | } |
| 7738 | /* All other characters are considered unencodable */ |
| 7739 | collstart = p; |
| 7740 | collend = p+1; |
| 7741 | while (collend < end) { |
| 7742 | if ((0 < *collend && *collend < 256) || |
| 7743 | !Py_UNICODE_ISSPACE(*collend) || |
| 7744 | Py_UNICODE_TODECIMAL(*collend)) |
| 7745 | break; |
| 7746 | } |
| 7747 | /* cache callback name lookup |
| 7748 | * (if not done yet, i.e. it's the first error) */ |
| 7749 | if (known_errorHandler==-1) { |
| 7750 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 7751 | known_errorHandler = 1; |
| 7752 | else if (!strcmp(errors, "replace")) |
| 7753 | known_errorHandler = 2; |
| 7754 | else if (!strcmp(errors, "ignore")) |
| 7755 | known_errorHandler = 3; |
| 7756 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 7757 | known_errorHandler = 4; |
| 7758 | else |
| 7759 | known_errorHandler = 0; |
| 7760 | } |
| 7761 | switch (known_errorHandler) { |
| 7762 | case 1: /* strict */ |
| 7763 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 7764 | goto onError; |
| 7765 | case 2: /* replace */ |
| 7766 | for (p = collstart; p < collend; ++p) |
| 7767 | *output++ = '?'; |
| 7768 | /* fall through */ |
| 7769 | case 3: /* ignore */ |
| 7770 | p = collend; |
| 7771 | break; |
| 7772 | case 4: /* xmlcharrefreplace */ |
| 7773 | /* generate replacement (temporarily (mis)uses p) */ |
| 7774 | for (p = collstart; p < collend; ++p) |
| 7775 | output += sprintf(output, "&#%d;", (int)*p); |
| 7776 | p = collend; |
| 7777 | break; |
| 7778 | default: |
| 7779 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 7780 | encoding, reason, s, length, &exc, |
| 7781 | collstart-s, collend-s, &newpos); |
| 7782 | if (repunicode == NULL) |
| 7783 | goto onError; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7784 | if (!PyUnicode_Check(repunicode)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7785 | /* Byte results not supported, since they have no decimal property. */ |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7786 | PyErr_SetString(PyExc_TypeError, "error handler should return unicode"); |
| 7787 | Py_DECREF(repunicode); |
| 7788 | goto onError; |
| 7789 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7790 | /* generate replacement */ |
| 7791 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 7792 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 7793 | Py_UNICODE ch = *uni2; |
| 7794 | if (Py_UNICODE_ISSPACE(ch)) |
| 7795 | *output++ = ' '; |
| 7796 | else { |
| 7797 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 7798 | if (decimal >= 0) |
| 7799 | *output++ = '0' + decimal; |
| 7800 | else if (0 < ch && ch < 256) |
| 7801 | *output++ = (char)ch; |
| 7802 | else { |
| 7803 | Py_DECREF(repunicode); |
| 7804 | raise_encode_exception(&exc, encoding, |
| 7805 | s, length, collstart-s, collend-s, reason); |
| 7806 | goto onError; |
| 7807 | } |
| 7808 | } |
| 7809 | } |
| 7810 | p = s + newpos; |
| 7811 | Py_DECREF(repunicode); |
| 7812 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 7813 | } |
| 7814 | /* 0-terminate the output string */ |
| 7815 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7816 | Py_XDECREF(exc); |
| 7817 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 7818 | return 0; |
| 7819 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7820 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7821 | Py_XDECREF(exc); |
| 7822 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 7823 | return -1; |
| 7824 | } |
| 7825 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7826 | /* --- Helpers ------------------------------------------------------------ */ |
| 7827 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7828 | #include "stringlib/ucs1lib.h" |
| 7829 | #include "stringlib/fastsearch.h" |
| 7830 | #include "stringlib/partition.h" |
| 7831 | #include "stringlib/split.h" |
| 7832 | #include "stringlib/count.h" |
| 7833 | #include "stringlib/find.h" |
| 7834 | #include "stringlib/localeutil.h" |
| 7835 | #include "stringlib/undef.h" |
| 7836 | |
| 7837 | #include "stringlib/ucs2lib.h" |
| 7838 | #include "stringlib/fastsearch.h" |
| 7839 | #include "stringlib/partition.h" |
| 7840 | #include "stringlib/split.h" |
| 7841 | #include "stringlib/count.h" |
| 7842 | #include "stringlib/find.h" |
| 7843 | #include "stringlib/localeutil.h" |
| 7844 | #include "stringlib/undef.h" |
| 7845 | |
| 7846 | #include "stringlib/ucs4lib.h" |
| 7847 | #include "stringlib/fastsearch.h" |
| 7848 | #include "stringlib/partition.h" |
| 7849 | #include "stringlib/split.h" |
| 7850 | #include "stringlib/count.h" |
| 7851 | #include "stringlib/find.h" |
| 7852 | #include "stringlib/localeutil.h" |
| 7853 | #include "stringlib/undef.h" |
| 7854 | |
| 7855 | static Py_ssize_t |
| 7856 | any_find_slice(Py_ssize_t Py_LOCAL_CALLBACK(ucs1)(const Py_UCS1*, Py_ssize_t, |
| 7857 | const Py_UCS1*, Py_ssize_t, |
| 7858 | Py_ssize_t, Py_ssize_t), |
| 7859 | Py_ssize_t Py_LOCAL_CALLBACK(ucs2)(const Py_UCS2*, Py_ssize_t, |
| 7860 | const Py_UCS2*, Py_ssize_t, |
| 7861 | Py_ssize_t, Py_ssize_t), |
| 7862 | Py_ssize_t Py_LOCAL_CALLBACK(ucs4)(const Py_UCS4*, Py_ssize_t, |
| 7863 | const Py_UCS4*, Py_ssize_t, |
| 7864 | Py_ssize_t, Py_ssize_t), |
| 7865 | PyObject* s1, PyObject* s2, |
| 7866 | Py_ssize_t start, |
| 7867 | Py_ssize_t end) |
| 7868 | { |
| 7869 | int kind1, kind2, kind; |
| 7870 | void *buf1, *buf2; |
| 7871 | Py_ssize_t len1, len2, result; |
| 7872 | |
| 7873 | kind1 = PyUnicode_KIND(s1); |
| 7874 | kind2 = PyUnicode_KIND(s2); |
| 7875 | kind = kind1 > kind2 ? kind1 : kind2; |
| 7876 | buf1 = PyUnicode_DATA(s1); |
| 7877 | buf2 = PyUnicode_DATA(s2); |
| 7878 | if (kind1 != kind) |
| 7879 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 7880 | if (!buf1) |
| 7881 | return -2; |
| 7882 | if (kind2 != kind) |
| 7883 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 7884 | if (!buf2) { |
| 7885 | if (kind1 != kind) PyMem_Free(buf1); |
| 7886 | return -2; |
| 7887 | } |
| 7888 | len1 = PyUnicode_GET_LENGTH(s1); |
| 7889 | len2 = PyUnicode_GET_LENGTH(s2); |
| 7890 | |
| 7891 | switch(kind) { |
| 7892 | case PyUnicode_1BYTE_KIND: |
| 7893 | result = ucs1(buf1, len1, buf2, len2, start, end); |
| 7894 | break; |
| 7895 | case PyUnicode_2BYTE_KIND: |
| 7896 | result = ucs2(buf1, len1, buf2, len2, start, end); |
| 7897 | break; |
| 7898 | case PyUnicode_4BYTE_KIND: |
| 7899 | result = ucs4(buf1, len1, buf2, len2, start, end); |
| 7900 | break; |
| 7901 | default: |
| 7902 | assert(0); result = -2; |
| 7903 | } |
| 7904 | |
| 7905 | if (kind1 != kind) |
| 7906 | PyMem_Free(buf1); |
| 7907 | if (kind2 != kind) |
| 7908 | PyMem_Free(buf2); |
| 7909 | |
| 7910 | return result; |
| 7911 | } |
| 7912 | |
| 7913 | Py_ssize_t |
| 7914 | _PyUnicode_InsertThousandsGrouping(int kind, void *data, |
| 7915 | Py_ssize_t n_buffer, |
| 7916 | void *digits, Py_ssize_t n_digits, |
| 7917 | Py_ssize_t min_width, |
| 7918 | const char *grouping, |
| 7919 | const char *thousands_sep) |
| 7920 | { |
| 7921 | switch(kind) { |
| 7922 | case PyUnicode_1BYTE_KIND: |
| 7923 | return _PyUnicode_ucs1_InsertThousandsGrouping( |
| 7924 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 7925 | min_width, grouping, thousands_sep); |
| 7926 | case PyUnicode_2BYTE_KIND: |
| 7927 | return _PyUnicode_ucs2_InsertThousandsGrouping( |
| 7928 | (Py_UCS2*)data, n_buffer, (Py_UCS2*)digits, n_digits, |
| 7929 | min_width, grouping, thousands_sep); |
| 7930 | case PyUnicode_4BYTE_KIND: |
| 7931 | return _PyUnicode_ucs4_InsertThousandsGrouping( |
| 7932 | (Py_UCS4*)data, n_buffer, (Py_UCS4*)digits, n_digits, |
| 7933 | min_width, grouping, thousands_sep); |
| 7934 | } |
| 7935 | assert(0); |
| 7936 | return -1; |
| 7937 | } |
| 7938 | |
| 7939 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 7940 | #include "stringlib/unicodedefs.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7941 | #include "stringlib/fastsearch.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 7942 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7943 | #include "stringlib/count.h" |
| 7944 | #include "stringlib/find.h" |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 7945 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7946 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 7947 | #define ADJUST_INDICES(start, end, len) \ |
| 7948 | if (end > len) \ |
| 7949 | end = len; \ |
| 7950 | else if (end < 0) { \ |
| 7951 | end += len; \ |
| 7952 | if (end < 0) \ |
| 7953 | end = 0; \ |
| 7954 | } \ |
| 7955 | if (start < 0) { \ |
| 7956 | start += len; \ |
| 7957 | if (start < 0) \ |
| 7958 | start = 0; \ |
| 7959 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7960 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7961 | Py_ssize_t |
| 7962 | PyUnicode_Count(PyObject *str, |
| 7963 | PyObject *substr, |
| 7964 | Py_ssize_t start, |
| 7965 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7966 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7967 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7968 | PyUnicodeObject* str_obj; |
| 7969 | PyUnicodeObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7970 | int kind1, kind2, kind; |
| 7971 | void *buf1 = NULL, *buf2 = NULL; |
| 7972 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7973 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7974 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7975 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7976 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7977 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 7978 | if (!sub_obj || PyUnicode_READY(sub_obj) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7979 | Py_DECREF(str_obj); |
| 7980 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7981 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7982 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7983 | kind1 = PyUnicode_KIND(str_obj); |
| 7984 | kind2 = PyUnicode_KIND(sub_obj); |
| 7985 | kind = kind1 > kind2 ? kind1 : kind2; |
| 7986 | buf1 = PyUnicode_DATA(str_obj); |
| 7987 | if (kind1 != kind) |
| 7988 | buf1 = _PyUnicode_AsKind((PyObject*)str_obj, kind); |
| 7989 | if (!buf1) |
| 7990 | goto onError; |
| 7991 | buf2 = PyUnicode_DATA(sub_obj); |
| 7992 | if (kind2 != kind) |
| 7993 | buf2 = _PyUnicode_AsKind((PyObject*)sub_obj, kind); |
| 7994 | if (!buf2) |
| 7995 | goto onError; |
| 7996 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 7997 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 7998 | |
| 7999 | ADJUST_INDICES(start, end, len1); |
| 8000 | switch(kind) { |
| 8001 | case PyUnicode_1BYTE_KIND: |
| 8002 | result = ucs1lib_count( |
| 8003 | ((Py_UCS1*)buf1) + start, end - start, |
| 8004 | buf2, len2, PY_SSIZE_T_MAX |
| 8005 | ); |
| 8006 | break; |
| 8007 | case PyUnicode_2BYTE_KIND: |
| 8008 | result = ucs2lib_count( |
| 8009 | ((Py_UCS2*)buf1) + start, end - start, |
| 8010 | buf2, len2, PY_SSIZE_T_MAX |
| 8011 | ); |
| 8012 | break; |
| 8013 | case PyUnicode_4BYTE_KIND: |
| 8014 | result = ucs4lib_count( |
| 8015 | ((Py_UCS4*)buf1) + start, end - start, |
| 8016 | buf2, len2, PY_SSIZE_T_MAX |
| 8017 | ); |
| 8018 | break; |
| 8019 | default: |
| 8020 | assert(0); result = 0; |
| 8021 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8022 | |
| 8023 | Py_DECREF(sub_obj); |
| 8024 | Py_DECREF(str_obj); |
| 8025 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8026 | if (kind1 != kind) |
| 8027 | PyMem_Free(buf1); |
| 8028 | if (kind2 != kind) |
| 8029 | PyMem_Free(buf2); |
| 8030 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8031 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8032 | onError: |
| 8033 | Py_DECREF(sub_obj); |
| 8034 | Py_DECREF(str_obj); |
| 8035 | if (kind1 != kind && buf1) |
| 8036 | PyMem_Free(buf1); |
| 8037 | if (kind2 != kind && buf2) |
| 8038 | PyMem_Free(buf2); |
| 8039 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8040 | } |
| 8041 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8042 | Py_ssize_t |
| 8043 | PyUnicode_Find(PyObject *str, |
| 8044 | PyObject *sub, |
| 8045 | Py_ssize_t start, |
| 8046 | Py_ssize_t end, |
| 8047 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8048 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8049 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8050 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8051 | str = PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8052 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8053 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8054 | sub = PyUnicode_FromObject(sub); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8055 | if (!sub || PyUnicode_READY(sub) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8056 | Py_DECREF(str); |
| 8057 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8058 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8059 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8060 | if (direction > 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8061 | result = any_find_slice( |
| 8062 | ucs1lib_find_slice, ucs2lib_find_slice, ucs4lib_find_slice, |
| 8063 | str, sub, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8064 | ); |
| 8065 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8066 | result = any_find_slice( |
| 8067 | ucs1lib_rfind_slice, ucs2lib_rfind_slice, ucs4lib_rfind_slice, |
| 8068 | str, sub, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8069 | ); |
| 8070 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8071 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8072 | Py_DECREF(sub); |
| 8073 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8074 | return result; |
| 8075 | } |
| 8076 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8077 | Py_ssize_t |
| 8078 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 8079 | Py_ssize_t start, Py_ssize_t end, |
| 8080 | int direction) |
| 8081 | { |
| 8082 | char *result; |
| 8083 | int kind; |
| 8084 | if (PyUnicode_READY(str) == -1) |
| 8085 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 8086 | if (start < 0 || end < 0) { |
| 8087 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 8088 | return -2; |
| 8089 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8090 | if (end > PyUnicode_GET_LENGTH(str)) |
| 8091 | end = PyUnicode_GET_LENGTH(str); |
| 8092 | kind = PyUnicode_KIND(str); |
| 8093 | result = findchar(PyUnicode_1BYTE_DATA(str) |
| 8094 | + PyUnicode_KIND_SIZE(kind, start), |
| 8095 | kind, |
| 8096 | end-start, ch, direction); |
| 8097 | if (!result) |
| 8098 | return -1; |
| 8099 | return (result-(char*)PyUnicode_DATA(str)) >> (kind-1); |
| 8100 | } |
| 8101 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8102 | static int |
| 8103 | tailmatch(PyUnicodeObject *self, |
| 8104 | PyUnicodeObject *substring, |
| 8105 | Py_ssize_t start, |
| 8106 | Py_ssize_t end, |
| 8107 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8108 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8109 | int kind_self; |
| 8110 | int kind_sub; |
| 8111 | void *data_self; |
| 8112 | void *data_sub; |
| 8113 | Py_ssize_t offset; |
| 8114 | Py_ssize_t i; |
| 8115 | Py_ssize_t end_sub; |
| 8116 | |
| 8117 | if (PyUnicode_READY(self) == -1 || |
| 8118 | PyUnicode_READY(substring) == -1) |
| 8119 | return 0; |
| 8120 | |
| 8121 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8122 | return 1; |
| 8123 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8124 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 8125 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8126 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8127 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8128 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8129 | kind_self = PyUnicode_KIND(self); |
| 8130 | data_self = PyUnicode_DATA(self); |
| 8131 | kind_sub = PyUnicode_KIND(substring); |
| 8132 | data_sub = PyUnicode_DATA(substring); |
| 8133 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 8134 | |
| 8135 | if (direction > 0) |
| 8136 | offset = end; |
| 8137 | else |
| 8138 | offset = start; |
| 8139 | |
| 8140 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 8141 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 8142 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 8143 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 8144 | /* If both are of the same kind, memcmp is sufficient */ |
| 8145 | if (kind_self == kind_sub) { |
| 8146 | return ! memcmp((char *)data_self + |
| 8147 | (offset * PyUnicode_CHARACTER_SIZE(substring)), |
| 8148 | data_sub, |
| 8149 | PyUnicode_GET_LENGTH(substring) * |
| 8150 | PyUnicode_CHARACTER_SIZE(substring)); |
| 8151 | } |
| 8152 | /* otherwise we have to compare each character by first accesing it */ |
| 8153 | else { |
| 8154 | /* We do not need to compare 0 and len(substring)-1 because |
| 8155 | the if statement above ensured already that they are equal |
| 8156 | when we end up here. */ |
| 8157 | // TODO: honor direction and do a forward or backwards search |
| 8158 | for (i = 1; i < end_sub; ++i) { |
| 8159 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 8160 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 8161 | return 0; |
| 8162 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8163 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8164 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8165 | } |
| 8166 | |
| 8167 | return 0; |
| 8168 | } |
| 8169 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8170 | Py_ssize_t |
| 8171 | PyUnicode_Tailmatch(PyObject *str, |
| 8172 | PyObject *substr, |
| 8173 | Py_ssize_t start, |
| 8174 | Py_ssize_t end, |
| 8175 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8176 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8177 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8178 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8179 | str = PyUnicode_FromObject(str); |
| 8180 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8181 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8182 | substr = PyUnicode_FromObject(substr); |
| 8183 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8184 | Py_DECREF(str); |
| 8185 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8186 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8187 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8188 | result = tailmatch((PyUnicodeObject *)str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8189 | (PyUnicodeObject *)substr, |
| 8190 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8191 | Py_DECREF(str); |
| 8192 | Py_DECREF(substr); |
| 8193 | return result; |
| 8194 | } |
| 8195 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8196 | /* Apply fixfct filter to the Unicode object self and return a |
| 8197 | reference to the modified object */ |
| 8198 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8199 | static PyObject * |
| 8200 | fixup(PyUnicodeObject *self, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8201 | Py_UCS4 (*fixfct)(PyUnicodeObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8202 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8203 | PyObject *u; |
| 8204 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8205 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8206 | if (PyUnicode_READY(self) == -1) |
| 8207 | return NULL; |
| 8208 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(self); |
| 8209 | u = PyUnicode_New(PyUnicode_GET_LENGTH(self), |
| 8210 | maxchar_old); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8211 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8212 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8213 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8214 | Py_MEMCPY(PyUnicode_1BYTE_DATA(u), PyUnicode_1BYTE_DATA(self), |
| 8215 | PyUnicode_GET_LENGTH(u) * PyUnicode_CHARACTER_SIZE(u)); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8216 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8217 | /* fix functions return the new maximum character in a string, |
| 8218 | if the kind of the resulting unicode object does not change, |
| 8219 | everything is fine. Otherwise we need to change the string kind |
| 8220 | and re-run the fix function. */ |
| 8221 | maxchar_new = fixfct((PyUnicodeObject*)u); |
| 8222 | if (maxchar_new == 0) |
| 8223 | /* do nothing, keep maxchar_new at 0 which means no changes. */; |
| 8224 | else if (maxchar_new <= 127) |
| 8225 | maxchar_new = 127; |
| 8226 | else if (maxchar_new <= 255) |
| 8227 | maxchar_new = 255; |
| 8228 | else if (maxchar_new <= 65535) |
| 8229 | maxchar_new = 65535; |
| 8230 | else |
| 8231 | maxchar_new = 1114111; /* 0x10ffff */ |
| 8232 | |
| 8233 | if (!maxchar_new && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8234 | /* fixfct should return TRUE if it modified the buffer. If |
| 8235 | FALSE, return a reference to the original buffer instead |
| 8236 | (to save space, not time) */ |
| 8237 | Py_INCREF(self); |
| 8238 | Py_DECREF(u); |
| 8239 | return (PyObject*) self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8240 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8241 | else if (maxchar_new == maxchar_old) { |
| 8242 | return u; |
| 8243 | } |
| 8244 | else { |
| 8245 | /* In case the maximum character changed, we need to |
| 8246 | convert the string to the new category. */ |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8247 | PyObject *v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8248 | if (v == NULL) { |
| 8249 | Py_DECREF(u); |
| 8250 | return NULL; |
| 8251 | } |
| 8252 | if (maxchar_new > maxchar_old) { |
| 8253 | /* If the maxchar increased so that the kind changed, not all |
| 8254 | characters are representable anymore and we need to fix the |
| 8255 | string again. This only happens in very few cases. */ |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 8256 | if (PyUnicode_CopyCharacters(v, 0, |
| 8257 | (PyObject*)self, 0, |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8258 | PyUnicode_GET_LENGTH(self)) < 0) |
| 8259 | { |
| 8260 | Py_DECREF(u); |
| 8261 | return NULL; |
| 8262 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8263 | maxchar_old = fixfct((PyUnicodeObject*)v); |
| 8264 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
| 8265 | } |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8266 | else { |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 8267 | if (PyUnicode_CopyCharacters(v, 0, |
| 8268 | u, 0, |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8269 | PyUnicode_GET_LENGTH(self)) < 0) |
| 8270 | { |
| 8271 | Py_DECREF(u); |
| 8272 | return NULL; |
| 8273 | } |
| 8274 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8275 | |
| 8276 | Py_DECREF(u); |
| 8277 | return v; |
| 8278 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8279 | } |
| 8280 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8281 | static Py_UCS4 |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8282 | fixupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8283 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8284 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8285 | called as a callback from fixup() which does it already. */ |
| 8286 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8287 | const int kind = PyUnicode_KIND(self); |
| 8288 | void *data = PyUnicode_DATA(self); |
| 8289 | int touched = 0; |
| 8290 | Py_UCS4 maxchar = 0; |
| 8291 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8292 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8293 | for (i = 0; i < len; ++i) { |
| 8294 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8295 | const Py_UCS4 up = Py_UNICODE_TOUPPER(ch); |
| 8296 | if (up != ch) { |
| 8297 | if (up > maxchar) |
| 8298 | maxchar = up; |
| 8299 | PyUnicode_WRITE(kind, data, i, up); |
| 8300 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8301 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8302 | else if (ch > maxchar) |
| 8303 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8304 | } |
| 8305 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8306 | if (touched) |
| 8307 | return maxchar; |
| 8308 | else |
| 8309 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8310 | } |
| 8311 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8312 | static Py_UCS4 |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8313 | fixlower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8314 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8315 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8316 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8317 | const int kind = PyUnicode_KIND(self); |
| 8318 | void *data = PyUnicode_DATA(self); |
| 8319 | int touched = 0; |
| 8320 | Py_UCS4 maxchar = 0; |
| 8321 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8322 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8323 | for(i = 0; i < len; ++i) { |
| 8324 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8325 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 8326 | if (lo != ch) { |
| 8327 | if (lo > maxchar) |
| 8328 | maxchar = lo; |
| 8329 | PyUnicode_WRITE(kind, data, i, lo); |
| 8330 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8331 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8332 | else if (ch > maxchar) |
| 8333 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8334 | } |
| 8335 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8336 | if (touched) |
| 8337 | return maxchar; |
| 8338 | else |
| 8339 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8340 | } |
| 8341 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8342 | static Py_UCS4 |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8343 | fixswapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8344 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8345 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8346 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8347 | const int kind = PyUnicode_KIND(self); |
| 8348 | void *data = PyUnicode_DATA(self); |
| 8349 | int touched = 0; |
| 8350 | Py_UCS4 maxchar = 0; |
| 8351 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8352 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8353 | for(i = 0; i < len; ++i) { |
| 8354 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8355 | Py_UCS4 nu = 0; |
| 8356 | |
| 8357 | if (Py_UNICODE_ISUPPER(ch)) |
| 8358 | nu = Py_UNICODE_TOLOWER(ch); |
| 8359 | else if (Py_UNICODE_ISLOWER(ch)) |
| 8360 | nu = Py_UNICODE_TOUPPER(ch); |
| 8361 | |
| 8362 | if (nu != 0) { |
| 8363 | if (nu > maxchar) |
| 8364 | maxchar = nu; |
| 8365 | PyUnicode_WRITE(kind, data, i, nu); |
| 8366 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8367 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8368 | else if (ch > maxchar) |
| 8369 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8370 | } |
| 8371 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8372 | if (touched) |
| 8373 | return maxchar; |
| 8374 | else |
| 8375 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8376 | } |
| 8377 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8378 | static Py_UCS4 |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8379 | fixcapitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8380 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8381 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8382 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8383 | const int kind = PyUnicode_KIND(self); |
| 8384 | void *data = PyUnicode_DATA(self); |
| 8385 | int touched = 0; |
| 8386 | Py_UCS4 maxchar = 0; |
| 8387 | Py_ssize_t i = 0; |
| 8388 | Py_UCS4 ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8389 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 8390 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8391 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8392 | |
| 8393 | ch = PyUnicode_READ(kind, data, i); |
| 8394 | if (!Py_UNICODE_ISUPPER(ch)) { |
| 8395 | maxchar = Py_UNICODE_TOUPPER(ch); |
| 8396 | PyUnicode_WRITE(kind, data, i, maxchar); |
| 8397 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8398 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8399 | ++i; |
| 8400 | for(; i < len; ++i) { |
| 8401 | ch = PyUnicode_READ(kind, data, i); |
| 8402 | if (!Py_UNICODE_ISLOWER(ch)) { |
| 8403 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 8404 | if (lo > maxchar) |
| 8405 | maxchar = lo; |
| 8406 | PyUnicode_WRITE(kind, data, i, lo); |
| 8407 | touched = 1; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 8408 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8409 | else if (ch > maxchar) |
| 8410 | maxchar = ch; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 8411 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8412 | |
| 8413 | if (touched) |
| 8414 | return maxchar; |
| 8415 | else |
| 8416 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8417 | } |
| 8418 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8419 | static Py_UCS4 |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8420 | fixtitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8421 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8422 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8423 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8424 | const int kind = PyUnicode_KIND(self); |
| 8425 | void *data = PyUnicode_DATA(self); |
| 8426 | Py_UCS4 maxchar = 0; |
| 8427 | Py_ssize_t i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8428 | int previous_is_cased; |
| 8429 | |
| 8430 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8431 | if (len == 1) { |
| 8432 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8433 | const Py_UCS4 ti = Py_UNICODE_TOTITLE(ch); |
| 8434 | if (ti != ch) { |
| 8435 | PyUnicode_WRITE(kind, data, i, ti); |
| 8436 | return ti; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8437 | } |
| 8438 | else |
| 8439 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8440 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8441 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8442 | for(; i < len; ++i) { |
| 8443 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8444 | Py_UCS4 nu; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8445 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8446 | if (previous_is_cased) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8447 | nu = Py_UNICODE_TOLOWER(ch); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8448 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8449 | nu = Py_UNICODE_TOTITLE(ch); |
| 8450 | |
| 8451 | if (nu > maxchar) |
| 8452 | maxchar = nu; |
| 8453 | PyUnicode_WRITE(kind, data, i, nu); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8454 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8455 | if (Py_UNICODE_ISLOWER(ch) || |
| 8456 | Py_UNICODE_ISUPPER(ch) || |
| 8457 | Py_UNICODE_ISTITLE(ch)) |
| 8458 | previous_is_cased = 1; |
| 8459 | else |
| 8460 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8461 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8462 | return maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8463 | } |
| 8464 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8465 | PyObject * |
| 8466 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8467 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8468 | PyObject *sep = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8469 | Py_ssize_t seplen = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8470 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8471 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8472 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 8473 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8474 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8475 | Py_ssize_t sz, i, res_offset; |
| 8476 | Py_UCS4 maxchar = 0; |
| 8477 | Py_UCS4 item_maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8478 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8479 | fseq = PySequence_Fast(seq, ""); |
| 8480 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8481 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8482 | } |
| 8483 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8484 | /* NOTE: the following code can't call back into Python code, |
| 8485 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 8486 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8487 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8488 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 8489 | /* If empty sequence, return u"". */ |
| 8490 | if (seqlen == 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8491 | res = PyUnicode_New(0, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8492 | goto Done; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8493 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8494 | items = PySequence_Fast_ITEMS(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8495 | /* If singleton sequence with an exact Unicode, return that. */ |
| 8496 | if (seqlen == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8497 | item = items[0]; |
| 8498 | if (PyUnicode_CheckExact(item)) { |
| 8499 | Py_INCREF(item); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8500 | res = item; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8501 | goto Done; |
| 8502 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8503 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8504 | else { |
| 8505 | /* Set up sep and seplen */ |
| 8506 | if (separator == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8507 | /* fall back to a blank space separator */ |
| 8508 | sep = PyUnicode_FromOrdinal(' '); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 8509 | if (!sep) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8510 | goto onError; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8511 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8512 | else { |
| 8513 | if (!PyUnicode_Check(separator)) { |
| 8514 | PyErr_Format(PyExc_TypeError, |
| 8515 | "separator: expected str instance," |
| 8516 | " %.80s found", |
| 8517 | Py_TYPE(separator)->tp_name); |
| 8518 | goto onError; |
| 8519 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8520 | if (PyUnicode_READY(separator) == -1) |
| 8521 | goto onError; |
| 8522 | sep = separator; |
| 8523 | seplen = PyUnicode_GET_LENGTH(separator); |
| 8524 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 8525 | /* inc refcount to keep this code path symetric with the |
| 8526 | above case of a blank separator */ |
| 8527 | Py_INCREF(sep); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8528 | } |
| 8529 | } |
| 8530 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8531 | /* There are at least two things to join, or else we have a subclass |
| 8532 | * of str in the sequence. |
| 8533 | * Do a pre-pass to figure out the total amount of space we'll |
| 8534 | * need (sz), and see whether all argument are strings. |
| 8535 | */ |
| 8536 | sz = 0; |
| 8537 | for (i = 0; i < seqlen; i++) { |
| 8538 | const Py_ssize_t old_sz = sz; |
| 8539 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8540 | if (!PyUnicode_Check(item)) { |
| 8541 | PyErr_Format(PyExc_TypeError, |
| 8542 | "sequence item %zd: expected str instance," |
| 8543 | " %.80s found", |
| 8544 | i, Py_TYPE(item)->tp_name); |
| 8545 | goto onError; |
| 8546 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8547 | if (PyUnicode_READY(item) == -1) |
| 8548 | goto onError; |
| 8549 | sz += PyUnicode_GET_LENGTH(item); |
| 8550 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
| 8551 | if (item_maxchar > maxchar) |
| 8552 | maxchar = item_maxchar; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8553 | if (i != 0) |
| 8554 | sz += seplen; |
| 8555 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 8556 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8557 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8558 | goto onError; |
| 8559 | } |
| 8560 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8561 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8562 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8563 | if (res == NULL) |
| 8564 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 8565 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8566 | /* Catenate everything. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8567 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8568 | Py_ssize_t itemlen; |
| 8569 | item = items[i]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8570 | itemlen = PyUnicode_GET_LENGTH(item); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8571 | /* Copy item, and maybe the separator. */ |
| 8572 | if (i) { |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8573 | if (PyUnicode_CopyCharacters(res, res_offset, |
| 8574 | sep, 0, seplen) < 0) |
| 8575 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8576 | res_offset += seplen; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8577 | } |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8578 | if (PyUnicode_CopyCharacters(res, res_offset, |
| 8579 | item, 0, itemlen) < 0) |
| 8580 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8581 | res_offset += itemlen; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8582 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8583 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8584 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8585 | Done: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8586 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8587 | Py_XDECREF(sep); |
| 8588 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8589 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8590 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8591 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8592 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8593 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8594 | return NULL; |
| 8595 | } |
| 8596 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8597 | #define FILL(kind, data, value, start, length) \ |
| 8598 | do { \ |
| 8599 | Py_ssize_t i_ = 0; \ |
| 8600 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 8601 | switch ((kind)) { \ |
| 8602 | case PyUnicode_1BYTE_KIND: { \ |
| 8603 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
| 8604 | memset(to_, (unsigned char)value, length); \ |
| 8605 | break; \ |
| 8606 | } \ |
| 8607 | case PyUnicode_2BYTE_KIND: { \ |
| 8608 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 8609 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 8610 | break; \ |
| 8611 | } \ |
| 8612 | default: { \ |
| 8613 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 8614 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 8615 | break; \ |
| 8616 | } \ |
| 8617 | } \ |
| 8618 | } while (0) |
| 8619 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8620 | static PyUnicodeObject * |
| 8621 | pad(PyUnicodeObject *self, |
| 8622 | Py_ssize_t left, |
| 8623 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8624 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8625 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8626 | PyObject *u; |
| 8627 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8628 | int kind; |
| 8629 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8630 | |
| 8631 | if (left < 0) |
| 8632 | left = 0; |
| 8633 | if (right < 0) |
| 8634 | right = 0; |
| 8635 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 8636 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8637 | Py_INCREF(self); |
| 8638 | return self; |
| 8639 | } |
| 8640 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8641 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 8642 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 8643 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 8644 | return NULL; |
| 8645 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8646 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 8647 | if (fill > maxchar) |
| 8648 | maxchar = fill; |
| 8649 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8650 | if (!u) |
| 8651 | return NULL; |
| 8652 | |
| 8653 | kind = PyUnicode_KIND(u); |
| 8654 | data = PyUnicode_DATA(u); |
| 8655 | if (left) |
| 8656 | FILL(kind, data, fill, 0, left); |
| 8657 | if (right) |
| 8658 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 8659 | if (PyUnicode_CopyCharacters(u, left, |
| 8660 | (PyObject*)self, 0, |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8661 | _PyUnicode_LENGTH(self)) < 0) |
| 8662 | { |
| 8663 | Py_DECREF(u); |
| 8664 | return NULL; |
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 | return (PyUnicodeObject*)u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8668 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8669 | #undef FILL |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8670 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8671 | PyObject * |
| 8672 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8673 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8674 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8675 | |
| 8676 | string = PyUnicode_FromObject(string); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8677 | if (string == NULL || PyUnicode_READY(string) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8678 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8679 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8680 | switch(PyUnicode_KIND(string)) { |
| 8681 | case PyUnicode_1BYTE_KIND: |
| 8682 | list = ucs1lib_splitlines( |
| 8683 | (PyObject*) string, PyUnicode_1BYTE_DATA(string), |
| 8684 | PyUnicode_GET_LENGTH(string), keepends); |
| 8685 | break; |
| 8686 | case PyUnicode_2BYTE_KIND: |
| 8687 | list = ucs2lib_splitlines( |
| 8688 | (PyObject*) string, PyUnicode_2BYTE_DATA(string), |
| 8689 | PyUnicode_GET_LENGTH(string), keepends); |
| 8690 | break; |
| 8691 | case PyUnicode_4BYTE_KIND: |
| 8692 | list = ucs4lib_splitlines( |
| 8693 | (PyObject*) string, PyUnicode_4BYTE_DATA(string), |
| 8694 | PyUnicode_GET_LENGTH(string), keepends); |
| 8695 | break; |
| 8696 | default: |
| 8697 | assert(0); |
| 8698 | list = 0; |
| 8699 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8700 | Py_DECREF(string); |
| 8701 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8702 | } |
| 8703 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8704 | static PyObject * |
| 8705 | split(PyUnicodeObject *self, |
| 8706 | PyUnicodeObject *substring, |
| 8707 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8708 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8709 | int kind1, kind2, kind; |
| 8710 | void *buf1, *buf2; |
| 8711 | Py_ssize_t len1, len2; |
| 8712 | PyObject* out; |
| 8713 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8714 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8715 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8716 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8717 | if (PyUnicode_READY(self) == -1) |
| 8718 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8719 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8720 | if (substring == NULL) |
| 8721 | switch(PyUnicode_KIND(self)) { |
| 8722 | case PyUnicode_1BYTE_KIND: |
| 8723 | return ucs1lib_split_whitespace( |
| 8724 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 8725 | PyUnicode_GET_LENGTH(self), maxcount |
| 8726 | ); |
| 8727 | case PyUnicode_2BYTE_KIND: |
| 8728 | return ucs2lib_split_whitespace( |
| 8729 | (PyObject*) self, PyUnicode_2BYTE_DATA(self), |
| 8730 | PyUnicode_GET_LENGTH(self), maxcount |
| 8731 | ); |
| 8732 | case PyUnicode_4BYTE_KIND: |
| 8733 | return ucs4lib_split_whitespace( |
| 8734 | (PyObject*) self, PyUnicode_4BYTE_DATA(self), |
| 8735 | PyUnicode_GET_LENGTH(self), maxcount |
| 8736 | ); |
| 8737 | default: |
| 8738 | assert(0); |
| 8739 | return NULL; |
| 8740 | } |
| 8741 | |
| 8742 | if (PyUnicode_READY(substring) == -1) |
| 8743 | return NULL; |
| 8744 | |
| 8745 | kind1 = PyUnicode_KIND(self); |
| 8746 | kind2 = PyUnicode_KIND(substring); |
| 8747 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8748 | buf1 = PyUnicode_DATA(self); |
| 8749 | buf2 = PyUnicode_DATA(substring); |
| 8750 | if (kind1 != kind) |
| 8751 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 8752 | if (!buf1) |
| 8753 | return NULL; |
| 8754 | if (kind2 != kind) |
| 8755 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 8756 | if (!buf2) { |
| 8757 | if (kind1 != kind) PyMem_Free(buf1); |
| 8758 | return NULL; |
| 8759 | } |
| 8760 | len1 = PyUnicode_GET_LENGTH(self); |
| 8761 | len2 = PyUnicode_GET_LENGTH(substring); |
| 8762 | |
| 8763 | switch(kind) { |
| 8764 | case PyUnicode_1BYTE_KIND: |
| 8765 | out = ucs1lib_split( |
| 8766 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 8767 | break; |
| 8768 | case PyUnicode_2BYTE_KIND: |
| 8769 | out = ucs2lib_split( |
| 8770 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 8771 | break; |
| 8772 | case PyUnicode_4BYTE_KIND: |
| 8773 | out = ucs4lib_split( |
| 8774 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 8775 | break; |
| 8776 | default: |
| 8777 | out = NULL; |
| 8778 | } |
| 8779 | if (kind1 != kind) |
| 8780 | PyMem_Free(buf1); |
| 8781 | if (kind2 != kind) |
| 8782 | PyMem_Free(buf2); |
| 8783 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8784 | } |
| 8785 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8786 | static PyObject * |
| 8787 | rsplit(PyUnicodeObject *self, |
| 8788 | PyUnicodeObject *substring, |
| 8789 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8790 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8791 | int kind1, kind2, kind; |
| 8792 | void *buf1, *buf2; |
| 8793 | Py_ssize_t len1, len2; |
| 8794 | PyObject* out; |
| 8795 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8796 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8797 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8798 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8799 | if (PyUnicode_READY(self) == -1) |
| 8800 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8801 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8802 | if (substring == NULL) |
| 8803 | switch(PyUnicode_KIND(self)) { |
| 8804 | case PyUnicode_1BYTE_KIND: |
| 8805 | return ucs1lib_rsplit_whitespace( |
| 8806 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 8807 | PyUnicode_GET_LENGTH(self), maxcount |
| 8808 | ); |
| 8809 | case PyUnicode_2BYTE_KIND: |
| 8810 | return ucs2lib_rsplit_whitespace( |
| 8811 | (PyObject*) self, PyUnicode_2BYTE_DATA(self), |
| 8812 | PyUnicode_GET_LENGTH(self), maxcount |
| 8813 | ); |
| 8814 | case PyUnicode_4BYTE_KIND: |
| 8815 | return ucs4lib_rsplit_whitespace( |
| 8816 | (PyObject*) self, PyUnicode_4BYTE_DATA(self), |
| 8817 | PyUnicode_GET_LENGTH(self), maxcount |
| 8818 | ); |
| 8819 | default: |
| 8820 | assert(0); |
| 8821 | return NULL; |
| 8822 | } |
| 8823 | |
| 8824 | if (PyUnicode_READY(substring) == -1) |
| 8825 | return NULL; |
| 8826 | |
| 8827 | kind1 = PyUnicode_KIND(self); |
| 8828 | kind2 = PyUnicode_KIND(substring); |
| 8829 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8830 | buf1 = PyUnicode_DATA(self); |
| 8831 | buf2 = PyUnicode_DATA(substring); |
| 8832 | if (kind1 != kind) |
| 8833 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 8834 | if (!buf1) |
| 8835 | return NULL; |
| 8836 | if (kind2 != kind) |
| 8837 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 8838 | if (!buf2) { |
| 8839 | if (kind1 != kind) PyMem_Free(buf1); |
| 8840 | return NULL; |
| 8841 | } |
| 8842 | len1 = PyUnicode_GET_LENGTH(self); |
| 8843 | len2 = PyUnicode_GET_LENGTH(substring); |
| 8844 | |
| 8845 | switch(kind) { |
| 8846 | case PyUnicode_1BYTE_KIND: |
| 8847 | out = ucs1lib_rsplit( |
| 8848 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 8849 | break; |
| 8850 | case PyUnicode_2BYTE_KIND: |
| 8851 | out = ucs2lib_rsplit( |
| 8852 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 8853 | break; |
| 8854 | case PyUnicode_4BYTE_KIND: |
| 8855 | out = ucs4lib_rsplit( |
| 8856 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 8857 | break; |
| 8858 | default: |
| 8859 | out = NULL; |
| 8860 | } |
| 8861 | if (kind1 != kind) |
| 8862 | PyMem_Free(buf1); |
| 8863 | if (kind2 != kind) |
| 8864 | PyMem_Free(buf2); |
| 8865 | return out; |
| 8866 | } |
| 8867 | |
| 8868 | static Py_ssize_t |
| 8869 | anylib_find(int kind, void *buf1, Py_ssize_t len1, |
| 8870 | void *buf2, Py_ssize_t len2, Py_ssize_t offset) |
| 8871 | { |
| 8872 | switch(kind) { |
| 8873 | case PyUnicode_1BYTE_KIND: |
| 8874 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
| 8875 | case PyUnicode_2BYTE_KIND: |
| 8876 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 8877 | case PyUnicode_4BYTE_KIND: |
| 8878 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 8879 | } |
| 8880 | assert(0); |
| 8881 | return -1; |
| 8882 | } |
| 8883 | |
| 8884 | static Py_ssize_t |
| 8885 | anylib_count(int kind, void* sbuf, Py_ssize_t slen, |
| 8886 | void *buf1, Py_ssize_t len1, Py_ssize_t maxcount) |
| 8887 | { |
| 8888 | switch(kind) { |
| 8889 | case PyUnicode_1BYTE_KIND: |
| 8890 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
| 8891 | case PyUnicode_2BYTE_KIND: |
| 8892 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 8893 | case PyUnicode_4BYTE_KIND: |
| 8894 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 8895 | } |
| 8896 | assert(0); |
| 8897 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8898 | } |
| 8899 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8900 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8901 | replace(PyObject *self, PyObject *str1, |
| 8902 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8903 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8904 | PyObject *u; |
| 8905 | char *sbuf = PyUnicode_DATA(self); |
| 8906 | char *buf1 = PyUnicode_DATA(str1); |
| 8907 | char *buf2 = PyUnicode_DATA(str2); |
| 8908 | int srelease = 0, release1 = 0, release2 = 0; |
| 8909 | int skind = PyUnicode_KIND(self); |
| 8910 | int kind1 = PyUnicode_KIND(str1); |
| 8911 | int kind2 = PyUnicode_KIND(str2); |
| 8912 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 8913 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 8914 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8915 | |
| 8916 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8917 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8918 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8919 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8920 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8921 | if (skind < kind1) |
| 8922 | /* substring too wide to be present */ |
| 8923 | goto nothing; |
| 8924 | |
| 8925 | if (len1 == len2) { |
Antoine Pitrou | cbfdee3 | 2010-01-13 08:58:08 +0000 | [diff] [blame] | 8926 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8927 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8928 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8929 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8930 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8931 | /* replace characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8932 | Py_UCS4 u1, u2, maxchar; |
| 8933 | int mayshrink, rkind; |
| 8934 | u1 = PyUnicode_READ_CHAR(str1, 0); |
| 8935 | if (!findchar(sbuf, PyUnicode_KIND(self), |
| 8936 | slen, u1, 1)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8937 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8938 | u2 = PyUnicode_READ_CHAR(str2, 0); |
| 8939 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 8940 | /* Replacing u1 with u2 may cause a maxchar reduction in the |
| 8941 | result string. */ |
| 8942 | mayshrink = maxchar > 127; |
| 8943 | if (u2 > maxchar) { |
| 8944 | maxchar = u2; |
| 8945 | mayshrink = 0; |
| 8946 | } |
| 8947 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8948 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8949 | goto error; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8950 | if (PyUnicode_CopyCharacters(u, 0, |
| 8951 | (PyObject*)self, 0, slen) < 0) |
| 8952 | { |
| 8953 | Py_DECREF(u); |
| 8954 | return NULL; |
| 8955 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8956 | rkind = PyUnicode_KIND(u); |
| 8957 | for (i = 0; i < PyUnicode_GET_LENGTH(u); i++) |
| 8958 | if (PyUnicode_READ(rkind, PyUnicode_DATA(u), i) == u1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8959 | if (--maxcount < 0) |
| 8960 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8961 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), i, u2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8962 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8963 | if (mayshrink) { |
| 8964 | PyObject *tmp = u; |
| 8965 | u = PyUnicode_FromKindAndData(rkind, PyUnicode_DATA(tmp), |
| 8966 | PyUnicode_GET_LENGTH(tmp)); |
| 8967 | Py_DECREF(tmp); |
| 8968 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8969 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8970 | int rkind = skind; |
| 8971 | char *res; |
| 8972 | if (kind1 < rkind) { |
| 8973 | /* widen substring */ |
| 8974 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 8975 | if (!buf1) goto error; |
| 8976 | release1 = 1; |
| 8977 | } |
| 8978 | i = anylib_find(rkind, sbuf, slen, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8979 | if (i < 0) |
| 8980 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8981 | if (rkind > kind2) { |
| 8982 | /* widen replacement */ |
| 8983 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 8984 | if (!buf2) goto error; |
| 8985 | release2 = 1; |
| 8986 | } |
| 8987 | else if (rkind < kind2) { |
| 8988 | /* widen self and buf1 */ |
| 8989 | rkind = kind2; |
| 8990 | if (release1) PyMem_Free(buf1); |
| 8991 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 8992 | if (!sbuf) goto error; |
| 8993 | srelease = 1; |
| 8994 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 8995 | if (!buf1) goto error; |
| 8996 | release1 = 1; |
| 8997 | } |
| 8998 | res = PyMem_Malloc(PyUnicode_KIND_SIZE(rkind, slen)); |
| 8999 | if (!res) { |
| 9000 | PyErr_NoMemory(); |
| 9001 | goto error; |
| 9002 | } |
| 9003 | memcpy(res, sbuf, PyUnicode_KIND_SIZE(rkind, slen)); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9004 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9005 | memcpy(res + PyUnicode_KIND_SIZE(rkind, i), |
| 9006 | buf2, |
| 9007 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 9008 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9009 | |
| 9010 | while ( --maxcount > 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9011 | i = anylib_find(rkind, sbuf+PyUnicode_KIND_SIZE(rkind, i), |
| 9012 | slen-i, |
| 9013 | buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9014 | if (i == -1) |
| 9015 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9016 | memcpy(res + PyUnicode_KIND_SIZE(rkind, i), |
| 9017 | buf2, |
| 9018 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 9019 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9020 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9021 | |
| 9022 | u = PyUnicode_FromKindAndData(rkind, res, slen); |
| 9023 | PyMem_Free(res); |
| 9024 | if (!u) goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9025 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9026 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9027 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9028 | Py_ssize_t n, i, j, ires; |
| 9029 | Py_ssize_t product, new_size; |
| 9030 | int rkind = skind; |
| 9031 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9032 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9033 | if (kind1 < rkind) { |
| 9034 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9035 | if (!buf1) goto error; |
| 9036 | release1 = 1; |
| 9037 | } |
| 9038 | n = anylib_count(rkind, sbuf, slen, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9039 | if (n == 0) |
| 9040 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9041 | if (kind2 < rkind) { |
| 9042 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 9043 | if (!buf2) goto error; |
| 9044 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9045 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9046 | else if (kind2 > rkind) { |
| 9047 | rkind = kind2; |
| 9048 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 9049 | if (!sbuf) goto error; |
| 9050 | srelease = 1; |
| 9051 | if (release1) PyMem_Free(buf1); |
| 9052 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9053 | if (!buf1) goto error; |
| 9054 | release1 = 1; |
| 9055 | } |
| 9056 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 9057 | PyUnicode_GET_LENGTH(str1))); */ |
| 9058 | product = n * (len2-len1); |
| 9059 | if ((product / (len2-len1)) != n) { |
| 9060 | PyErr_SetString(PyExc_OverflowError, |
| 9061 | "replace string is too long"); |
| 9062 | goto error; |
| 9063 | } |
| 9064 | new_size = slen + product; |
| 9065 | if (new_size < 0 || new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
| 9066 | PyErr_SetString(PyExc_OverflowError, |
| 9067 | "replace string is too long"); |
| 9068 | goto error; |
| 9069 | } |
| 9070 | res = PyMem_Malloc(PyUnicode_KIND_SIZE(rkind, new_size)); |
| 9071 | if (!res) |
| 9072 | goto error; |
| 9073 | ires = i = 0; |
| 9074 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9075 | while (n-- > 0) { |
| 9076 | /* look for next match */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9077 | j = anylib_find(rkind, |
| 9078 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9079 | slen-i, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9080 | if (j == -1) |
| 9081 | break; |
| 9082 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9083 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9084 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9085 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9086 | PyUnicode_KIND_SIZE(rkind, j-i)); |
| 9087 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9088 | } |
| 9089 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9090 | if (len2 > 0) { |
| 9091 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9092 | buf2, |
| 9093 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 9094 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9095 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9096 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9097 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9098 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9099 | /* copy tail [i:] */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9100 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9101 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9102 | PyUnicode_KIND_SIZE(rkind, slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9103 | } else { |
| 9104 | /* interleave */ |
| 9105 | while (n > 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9106 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9107 | buf2, |
| 9108 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 9109 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9110 | if (--n <= 0) |
| 9111 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9112 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9113 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9114 | PyUnicode_KIND_SIZE(rkind, 1)); |
| 9115 | ires++; |
| 9116 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9117 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9118 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9119 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9120 | PyUnicode_KIND_SIZE(rkind, slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9121 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9122 | u = PyUnicode_FromKindAndData(rkind, res, new_size); |
Martin v. Löwis | 0b1d348 | 2011-10-01 16:35:40 +0200 | [diff] [blame] | 9123 | PyMem_Free(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9124 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9125 | if (srelease) |
| 9126 | PyMem_FREE(sbuf); |
| 9127 | if (release1) |
| 9128 | PyMem_FREE(buf1); |
| 9129 | if (release2) |
| 9130 | PyMem_FREE(buf2); |
| 9131 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9132 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9133 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9134 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9135 | if (srelease) |
| 9136 | PyMem_FREE(sbuf); |
| 9137 | if (release1) |
| 9138 | PyMem_FREE(buf1); |
| 9139 | if (release2) |
| 9140 | PyMem_FREE(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9141 | if (PyUnicode_CheckExact(self)) { |
| 9142 | Py_INCREF(self); |
| 9143 | return (PyObject *) self; |
| 9144 | } |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 9145 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9146 | error: |
| 9147 | if (srelease && sbuf) |
| 9148 | PyMem_FREE(sbuf); |
| 9149 | if (release1 && buf1) |
| 9150 | PyMem_FREE(buf1); |
| 9151 | if (release2 && buf2) |
| 9152 | PyMem_FREE(buf2); |
| 9153 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9154 | } |
| 9155 | |
| 9156 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 9157 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9158 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9159 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9160 | \n\ |
| 9161 | 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] | 9162 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9163 | |
| 9164 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9165 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9166 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9167 | return fixup(self, fixtitle); |
| 9168 | } |
| 9169 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9170 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9171 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9172 | \n\ |
| 9173 | 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] | 9174 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9175 | |
| 9176 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9177 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9178 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9179 | return fixup(self, fixcapitalize); |
| 9180 | } |
| 9181 | |
| 9182 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9183 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9184 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9185 | \n\ |
| 9186 | 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] | 9187 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9188 | |
| 9189 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9190 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9191 | { |
| 9192 | PyObject *list; |
| 9193 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9194 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9195 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9196 | /* Split into words */ |
| 9197 | list = split(self, NULL, -1); |
| 9198 | if (!list) |
| 9199 | return NULL; |
| 9200 | |
| 9201 | /* Capitalize each word */ |
| 9202 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 9203 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9204 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9205 | if (item == NULL) |
| 9206 | goto onError; |
| 9207 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 9208 | PyList_SET_ITEM(list, i, item); |
| 9209 | } |
| 9210 | |
| 9211 | /* Join the words to form a new string */ |
| 9212 | item = PyUnicode_Join(NULL, list); |
| 9213 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9214 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9215 | Py_DECREF(list); |
| 9216 | return (PyObject *)item; |
| 9217 | } |
| 9218 | #endif |
| 9219 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9220 | /* Argument converter. Coerces to a single unicode character */ |
| 9221 | |
| 9222 | static int |
| 9223 | convert_uc(PyObject *obj, void *addr) |
| 9224 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9225 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9226 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9227 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9228 | uniobj = PyUnicode_FromObject(obj); |
| 9229 | if (uniobj == NULL) { |
| 9230 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9231 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9232 | return 0; |
| 9233 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9234 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9235 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9236 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9237 | Py_DECREF(uniobj); |
| 9238 | return 0; |
| 9239 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9240 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9241 | Py_DECREF(uniobj); |
| 9242 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9243 | } |
| 9244 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9245 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9246 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9247 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 9248 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9249 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9250 | |
| 9251 | static PyObject * |
| 9252 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 9253 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9254 | Py_ssize_t marg, left; |
| 9255 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9256 | Py_UCS4 fillchar = ' '; |
| 9257 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 9258 | 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] | 9259 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9260 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 9261 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9262 | return NULL; |
| 9263 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9264 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9265 | Py_INCREF(self); |
| 9266 | return (PyObject*) self; |
| 9267 | } |
| 9268 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9269 | marg = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9270 | left = marg / 2 + (marg & width & 1); |
| 9271 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9272 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9273 | } |
| 9274 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9275 | #if 0 |
| 9276 | |
| 9277 | /* This code should go into some future Unicode collation support |
| 9278 | module. The basic comparison should compare ordinals on a naive |
Georg Brandl | c6c3178 | 2009-06-08 13:41:29 +0000 | [diff] [blame] | 9279 | basis (this is what Java does and thus Jython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9280 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9281 | /* speedy UTF-16 code point order comparison */ |
| 9282 | /* gleaned from: */ |
| 9283 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 9284 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 9285 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9286 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9287 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9288 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 9289 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 9290 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9291 | }; |
| 9292 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9293 | static int |
| 9294 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 9295 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9296 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9297 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9298 | Py_UNICODE *s1 = str1->str; |
| 9299 | Py_UNICODE *s2 = str2->str; |
| 9300 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9301 | len1 = str1->_base._base.length; |
| 9302 | len2 = str2->_base._base.length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9303 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9304 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9305 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9306 | |
| 9307 | c1 = *s1++; |
| 9308 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 9309 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9310 | if (c1 > (1<<11) * 26) |
| 9311 | c1 += utf16Fixup[c1>>11]; |
| 9312 | if (c2 > (1<<11) * 26) |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9313 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9314 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 9315 | |
| 9316 | if (c1 != c2) |
| 9317 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9318 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9319 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9320 | } |
| 9321 | |
| 9322 | return (len1 < len2) ? -1 : (len1 != len2); |
| 9323 | } |
| 9324 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9325 | #else |
| 9326 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9327 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 9328 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9329 | static int |
| 9330 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 9331 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9332 | int kind1, kind2; |
| 9333 | void *data1, *data2; |
| 9334 | Py_ssize_t len1, len2, i; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9335 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9336 | kind1 = PyUnicode_KIND(str1); |
| 9337 | kind2 = PyUnicode_KIND(str2); |
| 9338 | data1 = PyUnicode_DATA(str1); |
| 9339 | data2 = PyUnicode_DATA(str2); |
| 9340 | len1 = PyUnicode_GET_LENGTH(str1); |
| 9341 | len2 = PyUnicode_GET_LENGTH(str2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9342 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9343 | for (i = 0; i < len1 && i < len2; ++i) { |
| 9344 | Py_UCS4 c1, c2; |
| 9345 | c1 = PyUnicode_READ(kind1, data1, i); |
| 9346 | c2 = PyUnicode_READ(kind2, data2, i); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 9347 | |
| 9348 | if (c1 != c2) |
| 9349 | return (c1 < c2) ? -1 : 1; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9350 | } |
| 9351 | |
| 9352 | return (len1 < len2) ? -1 : (len1 != len2); |
| 9353 | } |
| 9354 | |
| 9355 | #endif |
| 9356 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9357 | int |
| 9358 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9359 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9360 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 9361 | if (PyUnicode_READY(left) == -1 || |
| 9362 | PyUnicode_READY(right) == -1) |
| 9363 | return -1; |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 9364 | return unicode_compare((PyUnicodeObject *)left, |
| 9365 | (PyUnicodeObject *)right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9366 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 9367 | PyErr_Format(PyExc_TypeError, |
| 9368 | "Can't compare %.100s and %.100s", |
| 9369 | left->ob_type->tp_name, |
| 9370 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9371 | return -1; |
| 9372 | } |
| 9373 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9374 | int |
| 9375 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 9376 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9377 | Py_ssize_t i; |
| 9378 | int kind; |
| 9379 | void *data; |
| 9380 | Py_UCS4 chr; |
| 9381 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9382 | assert(PyUnicode_Check(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9383 | if (PyUnicode_READY(uni) == -1) |
| 9384 | return -1; |
| 9385 | kind = PyUnicode_KIND(uni); |
| 9386 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9387 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9388 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 9389 | if (chr != str[i]) |
| 9390 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 9391 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 9392 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9393 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9394 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9395 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9396 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9397 | return 0; |
| 9398 | } |
| 9399 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9400 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9401 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9402 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9403 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9404 | PyObject * |
| 9405 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9406 | { |
| 9407 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9408 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9409 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 9410 | PyObject *v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9411 | if (PyUnicode_READY(left) == -1 || |
| 9412 | PyUnicode_READY(right) == -1) |
| 9413 | return NULL; |
| 9414 | if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) || |
| 9415 | PyUnicode_KIND(left) != PyUnicode_KIND(right)) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9416 | if (op == Py_EQ) { |
| 9417 | Py_INCREF(Py_False); |
| 9418 | return Py_False; |
| 9419 | } |
| 9420 | if (op == Py_NE) { |
| 9421 | Py_INCREF(Py_True); |
| 9422 | return Py_True; |
| 9423 | } |
| 9424 | } |
| 9425 | if (left == right) |
| 9426 | result = 0; |
| 9427 | else |
| 9428 | result = unicode_compare((PyUnicodeObject *)left, |
| 9429 | (PyUnicodeObject *)right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9430 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9431 | /* Convert the return value to a Boolean */ |
| 9432 | switch (op) { |
| 9433 | case Py_EQ: |
| 9434 | v = TEST_COND(result == 0); |
| 9435 | break; |
| 9436 | case Py_NE: |
| 9437 | v = TEST_COND(result != 0); |
| 9438 | break; |
| 9439 | case Py_LE: |
| 9440 | v = TEST_COND(result <= 0); |
| 9441 | break; |
| 9442 | case Py_GE: |
| 9443 | v = TEST_COND(result >= 0); |
| 9444 | break; |
| 9445 | case Py_LT: |
| 9446 | v = TEST_COND(result == -1); |
| 9447 | break; |
| 9448 | case Py_GT: |
| 9449 | v = TEST_COND(result == 1); |
| 9450 | break; |
| 9451 | default: |
| 9452 | PyErr_BadArgument(); |
| 9453 | return NULL; |
| 9454 | } |
| 9455 | Py_INCREF(v); |
| 9456 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9457 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9458 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 9459 | Py_RETURN_NOTIMPLEMENTED; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9460 | } |
| 9461 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9462 | int |
| 9463 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9464 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9465 | PyObject *str, *sub; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9466 | int kind1, kind2, kind; |
| 9467 | void *buf1, *buf2; |
| 9468 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9469 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9470 | |
| 9471 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9472 | sub = PyUnicode_FromObject(element); |
| 9473 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9474 | PyErr_Format(PyExc_TypeError, |
| 9475 | "'in <string>' requires string as left operand, not %s", |
| 9476 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9477 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9478 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9479 | if (PyUnicode_READY(sub) == -1) |
| 9480 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9481 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9482 | str = PyUnicode_FromObject(container); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 9483 | if (!str || PyUnicode_READY(str) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9484 | Py_DECREF(sub); |
| 9485 | return -1; |
| 9486 | } |
| 9487 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9488 | kind1 = PyUnicode_KIND(str); |
| 9489 | kind2 = PyUnicode_KIND(sub); |
| 9490 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9491 | buf1 = PyUnicode_DATA(str); |
| 9492 | buf2 = PyUnicode_DATA(sub); |
| 9493 | if (kind1 != kind) |
| 9494 | buf1 = _PyUnicode_AsKind((PyObject*)str, kind); |
| 9495 | if (!buf1) { |
| 9496 | Py_DECREF(sub); |
| 9497 | return -1; |
| 9498 | } |
| 9499 | if (kind2 != kind) |
| 9500 | buf2 = _PyUnicode_AsKind((PyObject*)sub, kind); |
| 9501 | if (!buf2) { |
| 9502 | Py_DECREF(sub); |
| 9503 | if (kind1 != kind) PyMem_Free(buf1); |
| 9504 | return -1; |
| 9505 | } |
| 9506 | len1 = PyUnicode_GET_LENGTH(str); |
| 9507 | len2 = PyUnicode_GET_LENGTH(sub); |
| 9508 | |
| 9509 | switch(kind) { |
| 9510 | case PyUnicode_1BYTE_KIND: |
| 9511 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 9512 | break; |
| 9513 | case PyUnicode_2BYTE_KIND: |
| 9514 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 9515 | break; |
| 9516 | case PyUnicode_4BYTE_KIND: |
| 9517 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 9518 | break; |
| 9519 | default: |
| 9520 | result = -1; |
| 9521 | assert(0); |
| 9522 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9523 | |
| 9524 | Py_DECREF(str); |
| 9525 | Py_DECREF(sub); |
| 9526 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9527 | if (kind1 != kind) |
| 9528 | PyMem_Free(buf1); |
| 9529 | if (kind2 != kind) |
| 9530 | PyMem_Free(buf2); |
| 9531 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9532 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9533 | } |
| 9534 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9535 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 9536 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9537 | PyObject * |
| 9538 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9539 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9540 | PyObject *u = NULL, *v = NULL, *w; |
| 9541 | Py_UCS4 maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9542 | |
| 9543 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9544 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9545 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9546 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9547 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9548 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9549 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9550 | |
| 9551 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 9552 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9553 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9554 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9555 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 9556 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9557 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9558 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9559 | } |
| 9560 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9561 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | ff9e50f | 2011-09-28 22:17:19 +0200 | [diff] [blame] | 9562 | maxchar = Py_MAX(maxchar, PyUnicode_MAX_CHAR_VALUE(v)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9563 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9564 | /* Concat the two Unicode strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9565 | w = PyUnicode_New( |
| 9566 | PyUnicode_GET_LENGTH(u) + PyUnicode_GET_LENGTH(v), |
| 9567 | maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9568 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9569 | goto onError; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9570 | if (PyUnicode_CopyCharacters(w, 0, u, 0, PyUnicode_GET_LENGTH(u)) < 0) |
| 9571 | goto onError; |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 9572 | if (PyUnicode_CopyCharacters(w, PyUnicode_GET_LENGTH(u), |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9573 | v, 0, |
| 9574 | PyUnicode_GET_LENGTH(v)) < 0) |
| 9575 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9576 | Py_DECREF(u); |
| 9577 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9578 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9579 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9580 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9581 | Py_XDECREF(u); |
| 9582 | Py_XDECREF(v); |
| 9583 | return NULL; |
| 9584 | } |
| 9585 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 9586 | void |
| 9587 | PyUnicode_Append(PyObject **pleft, PyObject *right) |
| 9588 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9589 | PyObject *new; |
| 9590 | if (*pleft == NULL) |
| 9591 | return; |
| 9592 | if (right == NULL || !PyUnicode_Check(*pleft)) { |
| 9593 | Py_DECREF(*pleft); |
| 9594 | *pleft = NULL; |
| 9595 | return; |
| 9596 | } |
| 9597 | new = PyUnicode_Concat(*pleft, right); |
| 9598 | Py_DECREF(*pleft); |
| 9599 | *pleft = new; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 9600 | } |
| 9601 | |
| 9602 | void |
| 9603 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 9604 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9605 | PyUnicode_Append(pleft, right); |
| 9606 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 9607 | } |
| 9608 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9609 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9610 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9611 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9612 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 9613 | 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] | 9614 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9615 | |
| 9616 | static PyObject * |
| 9617 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 9618 | { |
| 9619 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9620 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9621 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9622 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9623 | int kind1, kind2, kind; |
| 9624 | void *buf1, *buf2; |
| 9625 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9626 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 9627 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 9628 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9629 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9630 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9631 | kind1 = PyUnicode_KIND(self); |
| 9632 | kind2 = PyUnicode_KIND(substring); |
| 9633 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9634 | buf1 = PyUnicode_DATA(self); |
| 9635 | buf2 = PyUnicode_DATA(substring); |
| 9636 | if (kind1 != kind) |
| 9637 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 9638 | if (!buf1) { |
| 9639 | Py_DECREF(substring); |
| 9640 | return NULL; |
| 9641 | } |
| 9642 | if (kind2 != kind) |
| 9643 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 9644 | if (!buf2) { |
| 9645 | Py_DECREF(substring); |
| 9646 | if (kind1 != kind) PyMem_Free(buf1); |
| 9647 | return NULL; |
| 9648 | } |
| 9649 | len1 = PyUnicode_GET_LENGTH(self); |
| 9650 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9651 | |
| 9652 | ADJUST_INDICES(start, end, len1); |
| 9653 | switch(kind) { |
| 9654 | case PyUnicode_1BYTE_KIND: |
| 9655 | iresult = ucs1lib_count( |
| 9656 | ((Py_UCS1*)buf1) + start, end - start, |
| 9657 | buf2, len2, PY_SSIZE_T_MAX |
| 9658 | ); |
| 9659 | break; |
| 9660 | case PyUnicode_2BYTE_KIND: |
| 9661 | iresult = ucs2lib_count( |
| 9662 | ((Py_UCS2*)buf1) + start, end - start, |
| 9663 | buf2, len2, PY_SSIZE_T_MAX |
| 9664 | ); |
| 9665 | break; |
| 9666 | case PyUnicode_4BYTE_KIND: |
| 9667 | iresult = ucs4lib_count( |
| 9668 | ((Py_UCS4*)buf1) + start, end - start, |
| 9669 | buf2, len2, PY_SSIZE_T_MAX |
| 9670 | ); |
| 9671 | break; |
| 9672 | default: |
| 9673 | assert(0); iresult = 0; |
| 9674 | } |
| 9675 | |
| 9676 | result = PyLong_FromSsize_t(iresult); |
| 9677 | |
| 9678 | if (kind1 != kind) |
| 9679 | PyMem_Free(buf1); |
| 9680 | if (kind2 != kind) |
| 9681 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9682 | |
| 9683 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9684 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9685 | return result; |
| 9686 | } |
| 9687 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9688 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 9689 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9690 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 9691 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 9692 | 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] | 9693 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 9694 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 9695 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 9696 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9697 | |
| 9698 | static PyObject * |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 9699 | unicode_encode(PyUnicodeObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9700 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 9701 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9702 | char *encoding = NULL; |
| 9703 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 9704 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 9705 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 9706 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9707 | return NULL; |
Georg Brandl | 3b9406b | 2010-12-03 07:54:09 +0000 | [diff] [blame] | 9708 | return PyUnicode_AsEncodedString((PyObject *)self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 9709 | } |
| 9710 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9711 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9712 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9713 | \n\ |
| 9714 | 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] | 9715 | 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] | 9716 | |
| 9717 | static PyObject* |
| 9718 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 9719 | { |
| 9720 | Py_UNICODE *e; |
| 9721 | Py_UNICODE *p; |
| 9722 | Py_UNICODE *q; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9723 | Py_UNICODE *qe; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9724 | Py_ssize_t i, j, incr, wstr_length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9725 | PyUnicodeObject *u; |
| 9726 | int tabsize = 8; |
| 9727 | |
| 9728 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9729 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9730 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9731 | if (PyUnicode_AsUnicodeAndSize((PyObject *)self, &wstr_length) == NULL) |
| 9732 | return NULL; |
| 9733 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 9734 | /* First pass: determine size of output string */ |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9735 | i = 0; /* chars up to and including most recent \n or \r */ |
| 9736 | 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] | 9737 | e = _PyUnicode_WSTR(self) + wstr_length; /* end of input */ |
| 9738 | for (p = _PyUnicode_WSTR(self); p < e; p++) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9739 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9740 | if (tabsize > 0) { |
| 9741 | incr = tabsize - (j % tabsize); /* cannot overflow */ |
| 9742 | if (j > PY_SSIZE_T_MAX - incr) |
| 9743 | goto overflow1; |
| 9744 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9745 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9746 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9747 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9748 | if (j > PY_SSIZE_T_MAX - 1) |
| 9749 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9750 | j++; |
| 9751 | if (*p == '\n' || *p == '\r') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9752 | if (i > PY_SSIZE_T_MAX - j) |
| 9753 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9754 | i += j; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9755 | j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9756 | } |
| 9757 | } |
| 9758 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9759 | if (i > PY_SSIZE_T_MAX - j) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9760 | goto overflow1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 9761 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9762 | /* Second pass: create output string and fill it */ |
| 9763 | u = _PyUnicode_New(i + j); |
| 9764 | if (!u) |
| 9765 | return NULL; |
| 9766 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9767 | j = 0; /* same as in first pass */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9768 | q = _PyUnicode_WSTR(u); /* next output char */ |
| 9769 | qe = _PyUnicode_WSTR(u) + PyUnicode_GET_SIZE(u); /* end of output */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9770 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9771 | for (p = _PyUnicode_WSTR(self); p < e; p++) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9772 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9773 | if (tabsize > 0) { |
| 9774 | i = tabsize - (j % tabsize); |
| 9775 | j += i; |
| 9776 | while (i--) { |
| 9777 | if (q >= qe) |
| 9778 | goto overflow2; |
| 9779 | *q++ = ' '; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9780 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9781 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9782 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9783 | else { |
| 9784 | if (q >= qe) |
| 9785 | goto overflow2; |
| 9786 | *q++ = *p; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9787 | j++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9788 | if (*p == '\n' || *p == '\r') |
| 9789 | j = 0; |
| 9790 | } |
| 9791 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9792 | if (PyUnicode_READY(u) == -1) { |
| 9793 | Py_DECREF(u); |
| 9794 | return NULL; |
| 9795 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9796 | return (PyObject*) u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9797 | |
| 9798 | overflow2: |
| 9799 | Py_DECREF(u); |
| 9800 | overflow1: |
| 9801 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 9802 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9803 | } |
| 9804 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9805 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9806 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9807 | \n\ |
| 9808 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 9809 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9810 | arguments start and end are interpreted as in slice notation.\n\ |
| 9811 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9812 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9813 | |
| 9814 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9815 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9816 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 9817 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 9818 | Py_ssize_t start; |
| 9819 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9820 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9821 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 9822 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 9823 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9824 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9825 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9826 | if (PyUnicode_READY(self) == -1) |
| 9827 | return NULL; |
| 9828 | if (PyUnicode_READY(substring) == -1) |
| 9829 | return NULL; |
| 9830 | |
| 9831 | result = any_find_slice( |
| 9832 | ucs1lib_find_slice, ucs2lib_find_slice, ucs4lib_find_slice, |
| 9833 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9834 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9835 | |
| 9836 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9837 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9838 | if (result == -2) |
| 9839 | return NULL; |
| 9840 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9841 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9842 | } |
| 9843 | |
| 9844 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 9845 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9846 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 9847 | Py_UCS4 ch = PyUnicode_ReadChar(self, index); |
| 9848 | if (ch == (Py_UCS4)-1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9849 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9850 | return PyUnicode_FromOrdinal(ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9851 | } |
| 9852 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 9853 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 9854 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 9855 | static Py_hash_t |
Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 9856 | unicode_hash(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9857 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 9858 | Py_ssize_t len; |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 9859 | Py_uhash_t x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 9860 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9861 | if (_PyUnicode_HASH(self) != -1) |
| 9862 | return _PyUnicode_HASH(self); |
| 9863 | if (PyUnicode_READY(self) == -1) |
| 9864 | return -1; |
| 9865 | len = PyUnicode_GET_LENGTH(self); |
| 9866 | |
| 9867 | /* The hash function as a macro, gets expanded three times below. */ |
| 9868 | #define HASH(P) \ |
| 9869 | x = (Py_uhash_t)*P << 7; \ |
| 9870 | while (--len >= 0) \ |
| 9871 | x = (1000003*x) ^ (Py_uhash_t)*P++; |
| 9872 | |
| 9873 | switch (PyUnicode_KIND(self)) { |
| 9874 | case PyUnicode_1BYTE_KIND: { |
| 9875 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 9876 | HASH(c); |
| 9877 | break; |
| 9878 | } |
| 9879 | case PyUnicode_2BYTE_KIND: { |
| 9880 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 9881 | HASH(s); |
| 9882 | break; |
| 9883 | } |
| 9884 | default: { |
| 9885 | Py_UCS4 *l; |
| 9886 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 9887 | "Impossible switch case in unicode_hash"); |
| 9888 | l = PyUnicode_4BYTE_DATA(self); |
| 9889 | HASH(l); |
| 9890 | break; |
| 9891 | } |
| 9892 | } |
| 9893 | x ^= (Py_uhash_t)PyUnicode_GET_LENGTH(self); |
| 9894 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 9895 | if (x == -1) |
| 9896 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9897 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 9898 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9899 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9900 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9901 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9902 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9903 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9904 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9905 | 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] | 9906 | |
| 9907 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9908 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9909 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9910 | Py_ssize_t result; |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 9911 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 9912 | Py_ssize_t start; |
| 9913 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9914 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 9915 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 9916 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9917 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9918 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9919 | if (PyUnicode_READY(self) == -1) |
| 9920 | return NULL; |
| 9921 | if (PyUnicode_READY(substring) == -1) |
| 9922 | return NULL; |
| 9923 | |
| 9924 | result = any_find_slice( |
| 9925 | ucs1lib_find_slice, ucs2lib_find_slice, ucs4lib_find_slice, |
| 9926 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9927 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9928 | |
| 9929 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9930 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9931 | if (result == -2) |
| 9932 | return NULL; |
| 9933 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9934 | if (result < 0) { |
| 9935 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 9936 | return NULL; |
| 9937 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9938 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9939 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9940 | } |
| 9941 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9942 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9943 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9944 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 9945 | 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] | 9946 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9947 | |
| 9948 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9949 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9950 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9951 | Py_ssize_t i, length; |
| 9952 | int kind; |
| 9953 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9954 | int cased; |
| 9955 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9956 | if (PyUnicode_READY(self) == -1) |
| 9957 | return NULL; |
| 9958 | length = PyUnicode_GET_LENGTH(self); |
| 9959 | kind = PyUnicode_KIND(self); |
| 9960 | data = PyUnicode_DATA(self); |
| 9961 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9962 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9963 | if (length == 1) |
| 9964 | return PyBool_FromLong( |
| 9965 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9966 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 9967 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9968 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9969 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 9970 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9971 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9972 | for (i = 0; i < length; i++) { |
| 9973 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9974 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9975 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 9976 | return PyBool_FromLong(0); |
| 9977 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 9978 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9979 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 9980 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9981 | } |
| 9982 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9983 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9984 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9985 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 9986 | 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] | 9987 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9988 | |
| 9989 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9990 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9991 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9992 | Py_ssize_t i, length; |
| 9993 | int kind; |
| 9994 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9995 | int cased; |
| 9996 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9997 | if (PyUnicode_READY(self) == -1) |
| 9998 | return NULL; |
| 9999 | length = PyUnicode_GET_LENGTH(self); |
| 10000 | kind = PyUnicode_KIND(self); |
| 10001 | data = PyUnicode_DATA(self); |
| 10002 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10003 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10004 | if (length == 1) |
| 10005 | return PyBool_FromLong( |
| 10006 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10007 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10008 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10009 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10010 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10011 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10012 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10013 | for (i = 0; i < length; i++) { |
| 10014 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10015 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10016 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 10017 | return PyBool_FromLong(0); |
| 10018 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 10019 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10020 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10021 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10022 | } |
| 10023 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10024 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10025 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10026 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10027 | Return True if S is a titlecased string and there is at least one\n\ |
| 10028 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 10029 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 10030 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10031 | |
| 10032 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10033 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10034 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10035 | Py_ssize_t i, length; |
| 10036 | int kind; |
| 10037 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10038 | int cased, previous_is_cased; |
| 10039 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10040 | if (PyUnicode_READY(self) == -1) |
| 10041 | return NULL; |
| 10042 | length = PyUnicode_GET_LENGTH(self); |
| 10043 | kind = PyUnicode_KIND(self); |
| 10044 | data = PyUnicode_DATA(self); |
| 10045 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10046 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10047 | if (length == 1) { |
| 10048 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 10049 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 10050 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 10051 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10052 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10053 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10054 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10055 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10056 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10057 | cased = 0; |
| 10058 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10059 | for (i = 0; i < length; i++) { |
| 10060 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10061 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10062 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 10063 | if (previous_is_cased) |
| 10064 | return PyBool_FromLong(0); |
| 10065 | previous_is_cased = 1; |
| 10066 | cased = 1; |
| 10067 | } |
| 10068 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 10069 | if (!previous_is_cased) |
| 10070 | return PyBool_FromLong(0); |
| 10071 | previous_is_cased = 1; |
| 10072 | cased = 1; |
| 10073 | } |
| 10074 | else |
| 10075 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10076 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10077 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10078 | } |
| 10079 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10080 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10081 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10082 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10083 | Return True if all characters in S are whitespace\n\ |
| 10084 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10085 | |
| 10086 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10087 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10088 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10089 | Py_ssize_t i, length; |
| 10090 | int kind; |
| 10091 | void *data; |
| 10092 | |
| 10093 | if (PyUnicode_READY(self) == -1) |
| 10094 | return NULL; |
| 10095 | length = PyUnicode_GET_LENGTH(self); |
| 10096 | kind = PyUnicode_KIND(self); |
| 10097 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10098 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10099 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10100 | if (length == 1) |
| 10101 | return PyBool_FromLong( |
| 10102 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10103 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10104 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10105 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10106 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10107 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10108 | for (i = 0; i < length; i++) { |
| 10109 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 10110 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10111 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10112 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10113 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10114 | } |
| 10115 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10116 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10117 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10118 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10119 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10120 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10121 | |
| 10122 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10123 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10124 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10125 | Py_ssize_t i, length; |
| 10126 | int kind; |
| 10127 | void *data; |
| 10128 | |
| 10129 | if (PyUnicode_READY(self) == -1) |
| 10130 | return NULL; |
| 10131 | length = PyUnicode_GET_LENGTH(self); |
| 10132 | kind = PyUnicode_KIND(self); |
| 10133 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10134 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10135 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10136 | if (length == 1) |
| 10137 | return PyBool_FromLong( |
| 10138 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10139 | |
| 10140 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10141 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10142 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10143 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10144 | for (i = 0; i < length; i++) { |
| 10145 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10146 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10147 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10148 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10149 | } |
| 10150 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10151 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10152 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10153 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10154 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10155 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10156 | |
| 10157 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10158 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10159 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10160 | int kind; |
| 10161 | void *data; |
| 10162 | Py_ssize_t len, i; |
| 10163 | |
| 10164 | if (PyUnicode_READY(self) == -1) |
| 10165 | return NULL; |
| 10166 | |
| 10167 | kind = PyUnicode_KIND(self); |
| 10168 | data = PyUnicode_DATA(self); |
| 10169 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10170 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10171 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10172 | if (len == 1) { |
| 10173 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 10174 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 10175 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10176 | |
| 10177 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10178 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10179 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10180 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10181 | for (i = 0; i < len; i++) { |
| 10182 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 10183 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10184 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10185 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10186 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10187 | } |
| 10188 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10189 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10190 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10191 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10192 | 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] | 10193 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10194 | |
| 10195 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10196 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10197 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10198 | Py_ssize_t i, length; |
| 10199 | int kind; |
| 10200 | void *data; |
| 10201 | |
| 10202 | if (PyUnicode_READY(self) == -1) |
| 10203 | return NULL; |
| 10204 | length = PyUnicode_GET_LENGTH(self); |
| 10205 | kind = PyUnicode_KIND(self); |
| 10206 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10207 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10208 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10209 | if (length == 1) |
| 10210 | return PyBool_FromLong( |
| 10211 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10212 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10213 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10214 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10215 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10216 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10217 | for (i = 0; i < length; i++) { |
| 10218 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10219 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10220 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10221 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10222 | } |
| 10223 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10224 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10225 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10226 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10227 | Return True if all characters in S are digits\n\ |
| 10228 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10229 | |
| 10230 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10231 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10232 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10233 | Py_ssize_t i, length; |
| 10234 | int kind; |
| 10235 | void *data; |
| 10236 | |
| 10237 | if (PyUnicode_READY(self) == -1) |
| 10238 | return NULL; |
| 10239 | length = PyUnicode_GET_LENGTH(self); |
| 10240 | kind = PyUnicode_KIND(self); |
| 10241 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10242 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10243 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10244 | if (length == 1) { |
| 10245 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 10246 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 10247 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10248 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10249 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10250 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10251 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10252 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10253 | for (i = 0; i < length; i++) { |
| 10254 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10255 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10256 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10257 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10258 | } |
| 10259 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10260 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10261 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10262 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10263 | 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] | 10264 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10265 | |
| 10266 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10267 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10268 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10269 | Py_ssize_t i, length; |
| 10270 | int kind; |
| 10271 | void *data; |
| 10272 | |
| 10273 | if (PyUnicode_READY(self) == -1) |
| 10274 | return NULL; |
| 10275 | length = PyUnicode_GET_LENGTH(self); |
| 10276 | kind = PyUnicode_KIND(self); |
| 10277 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10278 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10279 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10280 | if (length == 1) |
| 10281 | return PyBool_FromLong( |
| 10282 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10283 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10284 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10285 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10286 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10287 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10288 | for (i = 0; i < length; i++) { |
| 10289 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10290 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10291 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10292 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10293 | } |
| 10294 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10295 | int |
| 10296 | PyUnicode_IsIdentifier(PyObject *self) |
| 10297 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10298 | int kind; |
| 10299 | void *data; |
| 10300 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 10301 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10302 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10303 | if (PyUnicode_READY(self) == -1) { |
| 10304 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10305 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10306 | } |
| 10307 | |
| 10308 | /* Special case for empty strings */ |
| 10309 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 10310 | return 0; |
| 10311 | kind = PyUnicode_KIND(self); |
| 10312 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10313 | |
| 10314 | /* PEP 3131 says that the first character must be in |
| 10315 | XID_Start and subsequent characters in XID_Continue, |
| 10316 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10317 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10318 | letters, digits, underscore). However, given the current |
| 10319 | definition of XID_Start and XID_Continue, it is sufficient |
| 10320 | to check just for these, except that _ must be allowed |
| 10321 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10322 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 10323 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10324 | return 0; |
| 10325 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 10326 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10327 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10328 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10329 | return 1; |
| 10330 | } |
| 10331 | |
| 10332 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10333 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10334 | \n\ |
| 10335 | Return True if S is a valid identifier according\n\ |
| 10336 | to the language definition."); |
| 10337 | |
| 10338 | static PyObject* |
| 10339 | unicode_isidentifier(PyObject *self) |
| 10340 | { |
| 10341 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 10342 | } |
| 10343 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10344 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10345 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10346 | \n\ |
| 10347 | Return True if all characters in S are considered\n\ |
| 10348 | printable in repr() or S is empty, False otherwise."); |
| 10349 | |
| 10350 | static PyObject* |
| 10351 | unicode_isprintable(PyObject *self) |
| 10352 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10353 | Py_ssize_t i, length; |
| 10354 | int kind; |
| 10355 | void *data; |
| 10356 | |
| 10357 | if (PyUnicode_READY(self) == -1) |
| 10358 | return NULL; |
| 10359 | length = PyUnicode_GET_LENGTH(self); |
| 10360 | kind = PyUnicode_KIND(self); |
| 10361 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10362 | |
| 10363 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10364 | if (length == 1) |
| 10365 | return PyBool_FromLong( |
| 10366 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10367 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10368 | for (i = 0; i < length; i++) { |
| 10369 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10370 | Py_RETURN_FALSE; |
| 10371 | } |
| 10372 | } |
| 10373 | Py_RETURN_TRUE; |
| 10374 | } |
| 10375 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10376 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 10377 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10378 | \n\ |
| 10379 | 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] | 10380 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10381 | |
| 10382 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10383 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10384 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10385 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10386 | } |
| 10387 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10388 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10389 | unicode_length(PyUnicodeObject *self) |
| 10390 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10391 | if (PyUnicode_READY(self) == -1) |
| 10392 | return -1; |
| 10393 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10394 | } |
| 10395 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10396 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10397 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10398 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 10399 | 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] | 10400 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10401 | |
| 10402 | static PyObject * |
| 10403 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 10404 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10405 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10406 | Py_UCS4 fillchar = ' '; |
| 10407 | |
| 10408 | if (PyUnicode_READY(self) == -1) |
| 10409 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10410 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10411 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10412 | return NULL; |
| 10413 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10414 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10415 | Py_INCREF(self); |
| 10416 | return (PyObject*) self; |
| 10417 | } |
| 10418 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10419 | return (PyObject*) pad(self, 0, width - _PyUnicode_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10420 | } |
| 10421 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10422 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10423 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10424 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10425 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10426 | |
| 10427 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10428 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10429 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10430 | return fixup(self, fixlower); |
| 10431 | } |
| 10432 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10433 | #define LEFTSTRIP 0 |
| 10434 | #define RIGHTSTRIP 1 |
| 10435 | #define BOTHSTRIP 2 |
| 10436 | |
| 10437 | /* Arrays indexed by above */ |
| 10438 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 10439 | |
| 10440 | #define STRIPNAME(i) (stripformat[i]+3) |
| 10441 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10442 | /* externally visible for str.strip(unicode) */ |
| 10443 | PyObject * |
| 10444 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 10445 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10446 | void *data; |
| 10447 | int kind; |
| 10448 | Py_ssize_t i, j, len; |
| 10449 | BLOOM_MASK sepmask; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10450 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10451 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 10452 | return NULL; |
| 10453 | |
| 10454 | kind = PyUnicode_KIND(self); |
| 10455 | data = PyUnicode_DATA(self); |
| 10456 | len = PyUnicode_GET_LENGTH(self); |
| 10457 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 10458 | PyUnicode_DATA(sepobj), |
| 10459 | PyUnicode_GET_LENGTH(sepobj)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10460 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10461 | i = 0; |
| 10462 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10463 | while (i < len && |
| 10464 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10465 | i++; |
| 10466 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10467 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10468 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10469 | j = len; |
| 10470 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10471 | do { |
| 10472 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10473 | } while (j >= i && |
| 10474 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10475 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10476 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10477 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10478 | return PyUnicode_Substring((PyObject*)self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10479 | } |
| 10480 | |
| 10481 | PyObject* |
| 10482 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 10483 | { |
| 10484 | unsigned char *data; |
| 10485 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10486 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10487 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 10488 | if (PyUnicode_READY(self) == -1) |
| 10489 | return NULL; |
| 10490 | |
| 10491 | end = Py_MIN(end, PyUnicode_GET_LENGTH(self)); |
| 10492 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10493 | if (start == 0 && end == PyUnicode_GET_LENGTH(self)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10494 | { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10495 | if (PyUnicode_CheckExact(self)) { |
| 10496 | Py_INCREF(self); |
| 10497 | return self; |
| 10498 | } |
| 10499 | else |
| 10500 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10501 | } |
| 10502 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10503 | length = end - start; |
| 10504 | if (length == 1) |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 10505 | return unicode_getitem(self, start); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10506 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 10507 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10508 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 10509 | return NULL; |
| 10510 | } |
| 10511 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10512 | kind = PyUnicode_KIND(self); |
| 10513 | data = PyUnicode_1BYTE_DATA(self); |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 10514 | return PyUnicode_FromKindAndData(kind, |
| 10515 | data + PyUnicode_KIND_SIZE(kind, start), |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10516 | length); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10517 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10518 | |
| 10519 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10520 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10521 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10522 | int kind; |
| 10523 | void *data; |
| 10524 | Py_ssize_t len, i, j; |
| 10525 | |
| 10526 | if (PyUnicode_READY(self) == -1) |
| 10527 | return NULL; |
| 10528 | |
| 10529 | kind = PyUnicode_KIND(self); |
| 10530 | data = PyUnicode_DATA(self); |
| 10531 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10532 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10533 | i = 0; |
| 10534 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10535 | while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10536 | i++; |
| 10537 | } |
| 10538 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10539 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10540 | j = len; |
| 10541 | if (striptype != LEFTSTRIP) { |
| 10542 | do { |
| 10543 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10544 | } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10545 | j++; |
| 10546 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10547 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10548 | return PyUnicode_Substring((PyObject*)self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10549 | } |
| 10550 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10551 | |
| 10552 | static PyObject * |
| 10553 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 10554 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10555 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10556 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10557 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 10558 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10559 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10560 | if (sep != NULL && sep != Py_None) { |
| 10561 | if (PyUnicode_Check(sep)) |
| 10562 | return _PyUnicode_XStrip(self, striptype, sep); |
| 10563 | else { |
| 10564 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10565 | "%s arg must be None or str", |
| 10566 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10567 | return NULL; |
| 10568 | } |
| 10569 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10570 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10571 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10572 | } |
| 10573 | |
| 10574 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10575 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10576 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10577 | \n\ |
| 10578 | Return a copy of the string S with leading and trailing\n\ |
| 10579 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10580 | 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] | 10581 | |
| 10582 | static PyObject * |
| 10583 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 10584 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10585 | if (PyTuple_GET_SIZE(args) == 0) |
| 10586 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 10587 | else |
| 10588 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10589 | } |
| 10590 | |
| 10591 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10592 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10593 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10594 | \n\ |
| 10595 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10596 | 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] | 10597 | |
| 10598 | static PyObject * |
| 10599 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 10600 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10601 | if (PyTuple_GET_SIZE(args) == 0) |
| 10602 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 10603 | else |
| 10604 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10605 | } |
| 10606 | |
| 10607 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10608 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10609 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10610 | \n\ |
| 10611 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10612 | 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] | 10613 | |
| 10614 | static PyObject * |
| 10615 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 10616 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10617 | if (PyTuple_GET_SIZE(args) == 0) |
| 10618 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 10619 | else |
| 10620 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10621 | } |
| 10622 | |
| 10623 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10624 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10625 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10626 | { |
| 10627 | PyUnicodeObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10628 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10629 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 10630 | if (len < 1) { |
| 10631 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10632 | return unicode_empty; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 10633 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10634 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 10635 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10636 | /* no repeat, return original string */ |
| 10637 | Py_INCREF(str); |
| 10638 | return (PyObject*) str; |
| 10639 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 10640 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10641 | if (PyUnicode_READY(str) == -1) |
| 10642 | return NULL; |
| 10643 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 10644 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 10645 | PyErr_SetString(PyExc_OverflowError, |
| 10646 | "repeated string is too long"); |
| 10647 | return NULL; |
| 10648 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10649 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 10650 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10651 | u = (PyUnicodeObject *)PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10652 | if (!u) |
| 10653 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 10654 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10655 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10656 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 10657 | const int kind = PyUnicode_KIND(str); |
| 10658 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
| 10659 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 10660 | if (kind == PyUnicode_1BYTE_KIND) |
| 10661 | memset(to, (unsigned char)fill_char, len); |
| 10662 | else { |
| 10663 | for (n = 0; n < len; ++n) |
| 10664 | PyUnicode_WRITE(kind, to, n, fill_char); |
| 10665 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10666 | } |
| 10667 | else { |
| 10668 | /* number of characters copied this far */ |
| 10669 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
| 10670 | const Py_ssize_t char_size = PyUnicode_CHARACTER_SIZE(str); |
| 10671 | char *to = (char *) PyUnicode_DATA(u); |
| 10672 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 10673 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10674 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10675 | n = (done <= nchars-done) ? done : nchars-done; |
| 10676 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10677 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10678 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10679 | } |
| 10680 | |
| 10681 | return (PyObject*) u; |
| 10682 | } |
| 10683 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10684 | PyObject * |
| 10685 | PyUnicode_Replace(PyObject *obj, |
| 10686 | PyObject *subobj, |
| 10687 | PyObject *replobj, |
| 10688 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10689 | { |
| 10690 | PyObject *self; |
| 10691 | PyObject *str1; |
| 10692 | PyObject *str2; |
| 10693 | PyObject *result; |
| 10694 | |
| 10695 | self = PyUnicode_FromObject(obj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10696 | if (self == NULL || PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10697 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10698 | str1 = PyUnicode_FromObject(subobj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10699 | if (str1 == NULL || PyUnicode_READY(str1) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10700 | Py_DECREF(self); |
| 10701 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10702 | } |
| 10703 | str2 = PyUnicode_FromObject(replobj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10704 | if (str2 == NULL || PyUnicode_READY(str2)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10705 | Py_DECREF(self); |
| 10706 | Py_DECREF(str1); |
| 10707 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10708 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10709 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10710 | Py_DECREF(self); |
| 10711 | Py_DECREF(str1); |
| 10712 | Py_DECREF(str2); |
| 10713 | return result; |
| 10714 | } |
| 10715 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10716 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 10717 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10718 | \n\ |
| 10719 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 10720 | old replaced by new. If the optional argument count is\n\ |
| 10721 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10722 | |
| 10723 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10724 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10725 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10726 | PyObject *str1; |
| 10727 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10728 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10729 | PyObject *result; |
| 10730 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10731 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10732 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10733 | if (!PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10734 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10735 | str1 = PyUnicode_FromObject(str1); |
| 10736 | if (str1 == NULL || PyUnicode_READY(str1) == -1) |
| 10737 | return NULL; |
| 10738 | str2 = PyUnicode_FromObject(str2); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10739 | if (str2 == NULL || PyUnicode_READY(str2) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10740 | Py_DECREF(str1); |
| 10741 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 10742 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10743 | |
| 10744 | result = replace(self, str1, str2, maxcount); |
| 10745 | |
| 10746 | Py_DECREF(str1); |
| 10747 | Py_DECREF(str2); |
| 10748 | return result; |
| 10749 | } |
| 10750 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10751 | static PyObject * |
| 10752 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10753 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10754 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10755 | Py_ssize_t isize; |
| 10756 | Py_ssize_t osize, squote, dquote, i, o; |
| 10757 | Py_UCS4 max, quote; |
| 10758 | int ikind, okind; |
| 10759 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10760 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10761 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10762 | return NULL; |
| 10763 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10764 | isize = PyUnicode_GET_LENGTH(unicode); |
| 10765 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10766 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10767 | /* Compute length of output, quote characters, and |
| 10768 | maximum character */ |
| 10769 | osize = 2; /* quotes */ |
| 10770 | max = 127; |
| 10771 | squote = dquote = 0; |
| 10772 | ikind = PyUnicode_KIND(unicode); |
| 10773 | for (i = 0; i < isize; i++) { |
| 10774 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 10775 | switch (ch) { |
| 10776 | case '\'': squote++; osize++; break; |
| 10777 | case '"': dquote++; osize++; break; |
| 10778 | case '\\': case '\t': case '\r': case '\n': |
| 10779 | osize += 2; break; |
| 10780 | default: |
| 10781 | /* Fast-path ASCII */ |
| 10782 | if (ch < ' ' || ch == 0x7f) |
| 10783 | osize += 4; /* \xHH */ |
| 10784 | else if (ch < 0x7f) |
| 10785 | osize++; |
| 10786 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 10787 | osize++; |
| 10788 | max = ch > max ? ch : max; |
| 10789 | } |
| 10790 | else if (ch < 0x100) |
| 10791 | osize += 4; /* \xHH */ |
| 10792 | else if (ch < 0x10000) |
| 10793 | osize += 6; /* \uHHHH */ |
| 10794 | else |
| 10795 | osize += 10; /* \uHHHHHHHH */ |
| 10796 | } |
| 10797 | } |
| 10798 | |
| 10799 | quote = '\''; |
| 10800 | if (squote) { |
| 10801 | if (dquote) |
| 10802 | /* Both squote and dquote present. Use squote, |
| 10803 | and escape them */ |
| 10804 | osize += squote; |
| 10805 | else |
| 10806 | quote = '"'; |
| 10807 | } |
| 10808 | |
| 10809 | repr = PyUnicode_New(osize, max); |
| 10810 | if (repr == NULL) |
| 10811 | return NULL; |
| 10812 | okind = PyUnicode_KIND(repr); |
| 10813 | odata = PyUnicode_DATA(repr); |
| 10814 | |
| 10815 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 10816 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
| 10817 | |
| 10818 | for (i = 0, o = 1; i < isize; i++) { |
| 10819 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10820 | |
| 10821 | /* Escape quotes and backslashes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10822 | if ((ch == quote) || (ch == '\\')) { |
| 10823 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 10824 | PyUnicode_WRITE(okind, odata, o++, ch); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10825 | continue; |
| 10826 | } |
| 10827 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10828 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10829 | if (ch == '\t') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10830 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 10831 | PyUnicode_WRITE(okind, odata, o++, 't'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10832 | } |
| 10833 | else if (ch == '\n') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10834 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 10835 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10836 | } |
| 10837 | else if (ch == '\r') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10838 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 10839 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10840 | } |
| 10841 | |
| 10842 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10843 | else if (ch < ' ' || ch == 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10844 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 10845 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 10846 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0x000F]); |
| 10847 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0x000F]); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10848 | } |
| 10849 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10850 | /* Copy ASCII characters as-is */ |
| 10851 | else if (ch < 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10852 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10853 | } |
| 10854 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10855 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10856 | else { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10857 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10858 | (categories Z* and C* except ASCII space) |
| 10859 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10860 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10861 | /* Map 8-bit characters to '\xhh' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10862 | if (ch <= 0xff) { |
| 10863 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 10864 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 10865 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0x000F]); |
| 10866 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0x000F]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10867 | } |
| 10868 | /* Map 21-bit characters to '\U00xxxxxx' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10869 | else if (ch >= 0x10000) { |
| 10870 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 10871 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
| 10872 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 28) & 0xF]); |
| 10873 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 24) & 0xF]); |
| 10874 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 20) & 0xF]); |
| 10875 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 16) & 0xF]); |
| 10876 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 12) & 0xF]); |
| 10877 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 8) & 0xF]); |
| 10878 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0xF]); |
| 10879 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10880 | } |
| 10881 | /* Map 16-bit characters to '\uxxxx' */ |
| 10882 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10883 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 10884 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
| 10885 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 12) & 0xF]); |
| 10886 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 8) & 0xF]); |
| 10887 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0xF]); |
| 10888 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10889 | } |
| 10890 | } |
| 10891 | /* Copy characters as-is */ |
| 10892 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10893 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10894 | } |
| 10895 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10896 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10897 | /* Closing quote already added at the beginning */ |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10898 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10899 | } |
| 10900 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10901 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10902 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10903 | \n\ |
| 10904 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 10905 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10906 | arguments start and end are interpreted as in slice notation.\n\ |
| 10907 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10908 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10909 | |
| 10910 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10911 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10912 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10913 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10914 | Py_ssize_t start; |
| 10915 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10916 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10917 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10918 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 10919 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10920 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10921 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10922 | if (PyUnicode_READY(self) == -1) |
| 10923 | return NULL; |
| 10924 | if (PyUnicode_READY(substring) == -1) |
| 10925 | return NULL; |
| 10926 | |
| 10927 | result = any_find_slice( |
| 10928 | ucs1lib_rfind_slice, ucs2lib_rfind_slice, ucs4lib_rfind_slice, |
| 10929 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10930 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10931 | |
| 10932 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10933 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10934 | if (result == -2) |
| 10935 | return NULL; |
| 10936 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10937 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10938 | } |
| 10939 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10940 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10941 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10942 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10943 | 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] | 10944 | |
| 10945 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10946 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10947 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10948 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10949 | Py_ssize_t start; |
| 10950 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10951 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10952 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10953 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 10954 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10955 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10956 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10957 | if (PyUnicode_READY(self) == -1) |
| 10958 | return NULL; |
| 10959 | if (PyUnicode_READY(substring) == -1) |
| 10960 | return NULL; |
| 10961 | |
| 10962 | result = any_find_slice( |
| 10963 | ucs1lib_rfind_slice, ucs2lib_rfind_slice, ucs4lib_rfind_slice, |
| 10964 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10965 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10966 | |
| 10967 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10968 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10969 | if (result == -2) |
| 10970 | return NULL; |
| 10971 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10972 | if (result < 0) { |
| 10973 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 10974 | return NULL; |
| 10975 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10976 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10977 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10978 | } |
| 10979 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10980 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10981 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10982 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 10983 | 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] | 10984 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10985 | |
| 10986 | static PyObject * |
| 10987 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 10988 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10989 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10990 | Py_UCS4 fillchar = ' '; |
| 10991 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10992 | 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] | 10993 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10994 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10995 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10996 | return NULL; |
| 10997 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10998 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10999 | Py_INCREF(self); |
| 11000 | return (PyObject*) self; |
| 11001 | } |
| 11002 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11003 | return (PyObject*) pad(self, width - _PyUnicode_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11004 | } |
| 11005 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11006 | PyObject * |
| 11007 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11008 | { |
| 11009 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11010 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11011 | s = PyUnicode_FromObject(s); |
| 11012 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11013 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11014 | if (sep != NULL) { |
| 11015 | sep = PyUnicode_FromObject(sep); |
| 11016 | if (sep == NULL) { |
| 11017 | Py_DECREF(s); |
| 11018 | return NULL; |
| 11019 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11020 | } |
| 11021 | |
| 11022 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 11023 | |
| 11024 | Py_DECREF(s); |
| 11025 | Py_XDECREF(sep); |
| 11026 | return result; |
| 11027 | } |
| 11028 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11029 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11030 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11031 | \n\ |
| 11032 | Return a list of the words in S, using sep as the\n\ |
| 11033 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 11034 | 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] | 11035 | whitespace string is a separator and empty strings are\n\ |
| 11036 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11037 | |
| 11038 | static PyObject* |
| 11039 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 11040 | { |
| 11041 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11042 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11043 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11044 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11045 | return NULL; |
| 11046 | |
| 11047 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11048 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11049 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11050 | return split(self, (PyUnicodeObject *)substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11051 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11052 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11053 | } |
| 11054 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11055 | PyObject * |
| 11056 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 11057 | { |
| 11058 | PyObject* str_obj; |
| 11059 | PyObject* sep_obj; |
| 11060 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11061 | int kind1, kind2, kind; |
| 11062 | void *buf1 = NULL, *buf2 = NULL; |
| 11063 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11064 | |
| 11065 | str_obj = PyUnicode_FromObject(str_in); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11066 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11067 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11068 | sep_obj = PyUnicode_FromObject(sep_in); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11069 | if (!sep_obj || PyUnicode_READY(sep_obj) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11070 | Py_DECREF(str_obj); |
| 11071 | return NULL; |
| 11072 | } |
| 11073 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11074 | kind1 = PyUnicode_KIND(str_in); |
| 11075 | kind2 = PyUnicode_KIND(sep_obj); |
| 11076 | kind = kind1 > kind2 ? kind1 : kind2; |
| 11077 | buf1 = PyUnicode_DATA(str_in); |
| 11078 | if (kind1 != kind) |
| 11079 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 11080 | if (!buf1) |
| 11081 | goto onError; |
| 11082 | buf2 = PyUnicode_DATA(sep_obj); |
| 11083 | if (kind2 != kind) |
| 11084 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 11085 | if (!buf2) |
| 11086 | goto onError; |
| 11087 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 11088 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 11089 | |
| 11090 | switch(PyUnicode_KIND(str_in)) { |
| 11091 | case PyUnicode_1BYTE_KIND: |
| 11092 | out = ucs1lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11093 | break; |
| 11094 | case PyUnicode_2BYTE_KIND: |
| 11095 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11096 | break; |
| 11097 | case PyUnicode_4BYTE_KIND: |
| 11098 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11099 | break; |
| 11100 | default: |
| 11101 | assert(0); |
| 11102 | out = 0; |
| 11103 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11104 | |
| 11105 | Py_DECREF(sep_obj); |
| 11106 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11107 | if (kind1 != kind) |
| 11108 | PyMem_Free(buf1); |
| 11109 | if (kind2 != kind) |
| 11110 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11111 | |
| 11112 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11113 | onError: |
| 11114 | Py_DECREF(sep_obj); |
| 11115 | Py_DECREF(str_obj); |
| 11116 | if (kind1 != kind && buf1) |
| 11117 | PyMem_Free(buf1); |
| 11118 | if (kind2 != kind && buf2) |
| 11119 | PyMem_Free(buf2); |
| 11120 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11121 | } |
| 11122 | |
| 11123 | |
| 11124 | PyObject * |
| 11125 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 11126 | { |
| 11127 | PyObject* str_obj; |
| 11128 | PyObject* sep_obj; |
| 11129 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11130 | int kind1, kind2, kind; |
| 11131 | void *buf1 = NULL, *buf2 = NULL; |
| 11132 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11133 | |
| 11134 | str_obj = PyUnicode_FromObject(str_in); |
| 11135 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11136 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11137 | sep_obj = PyUnicode_FromObject(sep_in); |
| 11138 | if (!sep_obj) { |
| 11139 | Py_DECREF(str_obj); |
| 11140 | return NULL; |
| 11141 | } |
| 11142 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11143 | kind1 = PyUnicode_KIND(str_in); |
| 11144 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 11145 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11146 | buf1 = PyUnicode_DATA(str_in); |
| 11147 | if (kind1 != kind) |
| 11148 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 11149 | if (!buf1) |
| 11150 | goto onError; |
| 11151 | buf2 = PyUnicode_DATA(sep_obj); |
| 11152 | if (kind2 != kind) |
| 11153 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 11154 | if (!buf2) |
| 11155 | goto onError; |
| 11156 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 11157 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 11158 | |
| 11159 | switch(PyUnicode_KIND(str_in)) { |
| 11160 | case PyUnicode_1BYTE_KIND: |
| 11161 | out = ucs1lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11162 | break; |
| 11163 | case PyUnicode_2BYTE_KIND: |
| 11164 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11165 | break; |
| 11166 | case PyUnicode_4BYTE_KIND: |
| 11167 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11168 | break; |
| 11169 | default: |
| 11170 | assert(0); |
| 11171 | out = 0; |
| 11172 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11173 | |
| 11174 | Py_DECREF(sep_obj); |
| 11175 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11176 | if (kind1 != kind) |
| 11177 | PyMem_Free(buf1); |
| 11178 | if (kind2 != kind) |
| 11179 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11180 | |
| 11181 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11182 | onError: |
| 11183 | Py_DECREF(sep_obj); |
| 11184 | Py_DECREF(str_obj); |
| 11185 | if (kind1 != kind && buf1) |
| 11186 | PyMem_Free(buf1); |
| 11187 | if (kind2 != kind && buf2) |
| 11188 | PyMem_Free(buf2); |
| 11189 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11190 | } |
| 11191 | |
| 11192 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11193 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11194 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 11195 | 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] | 11196 | 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] | 11197 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11198 | |
| 11199 | static PyObject* |
| 11200 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
| 11201 | { |
| 11202 | return PyUnicode_Partition((PyObject *)self, separator); |
| 11203 | } |
| 11204 | |
| 11205 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 11206 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11207 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 11208 | 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] | 11209 | 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] | 11210 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11211 | |
| 11212 | static PyObject* |
| 11213 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 11214 | { |
| 11215 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 11216 | } |
| 11217 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11218 | PyObject * |
| 11219 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11220 | { |
| 11221 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11222 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11223 | s = PyUnicode_FromObject(s); |
| 11224 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11225 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11226 | if (sep != NULL) { |
| 11227 | sep = PyUnicode_FromObject(sep); |
| 11228 | if (sep == NULL) { |
| 11229 | Py_DECREF(s); |
| 11230 | return NULL; |
| 11231 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11232 | } |
| 11233 | |
| 11234 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 11235 | |
| 11236 | Py_DECREF(s); |
| 11237 | Py_XDECREF(sep); |
| 11238 | return result; |
| 11239 | } |
| 11240 | |
| 11241 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11242 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11243 | \n\ |
| 11244 | Return a list of the words in S, using sep as the\n\ |
| 11245 | delimiter string, starting at the end of the string and\n\ |
| 11246 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 11247 | splits are done. If sep is not specified, any whitespace string\n\ |
| 11248 | is a separator."); |
| 11249 | |
| 11250 | static PyObject* |
| 11251 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 11252 | { |
| 11253 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11254 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11255 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11256 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11257 | return NULL; |
| 11258 | |
| 11259 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11260 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11261 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11262 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11263 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11264 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11265 | } |
| 11266 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11267 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11268 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11269 | \n\ |
| 11270 | 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] | 11271 | 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] | 11272 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11273 | |
| 11274 | static PyObject* |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 11275 | unicode_splitlines(PyUnicodeObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11276 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 11277 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 11278 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11279 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 11280 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 11281 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11282 | return NULL; |
| 11283 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 11284 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11285 | } |
| 11286 | |
| 11287 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 11288 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11289 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 11290 | if (PyUnicode_CheckExact(self)) { |
| 11291 | Py_INCREF(self); |
| 11292 | return self; |
| 11293 | } else |
| 11294 | /* Subtype -- return genuine unicode string with the same value. */ |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 11295 | return PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11296 | } |
| 11297 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11298 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11299 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11300 | \n\ |
| 11301 | 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] | 11302 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11303 | |
| 11304 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11305 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11306 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11307 | return fixup(self, fixswapcase); |
| 11308 | } |
| 11309 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11310 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11311 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11312 | \n\ |
| 11313 | Return a translation table usable for str.translate().\n\ |
| 11314 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 11315 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11316 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11317 | If there are two arguments, they must be strings of equal length, and\n\ |
| 11318 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 11319 | character at the same position in y. If there is a third argument, it\n\ |
| 11320 | must be a string, whose characters will be mapped to None in the result."); |
| 11321 | |
| 11322 | static PyObject* |
| 11323 | unicode_maketrans(PyUnicodeObject *null, PyObject *args) |
| 11324 | { |
| 11325 | PyObject *x, *y = NULL, *z = NULL; |
| 11326 | PyObject *new = NULL, *key, *value; |
| 11327 | Py_ssize_t i = 0; |
| 11328 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11329 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11330 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 11331 | return NULL; |
| 11332 | new = PyDict_New(); |
| 11333 | if (!new) |
| 11334 | return NULL; |
| 11335 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11336 | int x_kind, y_kind, z_kind; |
| 11337 | void *x_data, *y_data, *z_data; |
| 11338 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11339 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11340 | if (!PyUnicode_Check(x)) { |
| 11341 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 11342 | "be a string if there is a second argument"); |
| 11343 | goto err; |
| 11344 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11345 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11346 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 11347 | "arguments must have equal length"); |
| 11348 | goto err; |
| 11349 | } |
| 11350 | /* 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] | 11351 | x_kind = PyUnicode_KIND(x); |
| 11352 | y_kind = PyUnicode_KIND(y); |
| 11353 | x_data = PyUnicode_DATA(x); |
| 11354 | y_data = PyUnicode_DATA(y); |
| 11355 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 11356 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
| 11357 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11358 | if (!key || !value) |
| 11359 | goto err; |
| 11360 | res = PyDict_SetItem(new, key, value); |
| 11361 | Py_DECREF(key); |
| 11362 | Py_DECREF(value); |
| 11363 | if (res < 0) |
| 11364 | goto err; |
| 11365 | } |
| 11366 | /* create entries for deleting chars in z */ |
| 11367 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11368 | z_kind = PyUnicode_KIND(z); |
| 11369 | z_data = PyUnicode_DATA(z); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11370 | for (i = 0; i < PyUnicode_GET_SIZE(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11371 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11372 | if (!key) |
| 11373 | goto err; |
| 11374 | res = PyDict_SetItem(new, key, Py_None); |
| 11375 | Py_DECREF(key); |
| 11376 | if (res < 0) |
| 11377 | goto err; |
| 11378 | } |
| 11379 | } |
| 11380 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11381 | int kind; |
| 11382 | void *data; |
| 11383 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11384 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 11385 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11386 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 11387 | "to maketrans it must be a dict"); |
| 11388 | goto err; |
| 11389 | } |
| 11390 | /* copy entries into the new dict, converting string keys to int keys */ |
| 11391 | while (PyDict_Next(x, &i, &key, &value)) { |
| 11392 | if (PyUnicode_Check(key)) { |
| 11393 | /* convert string keys to integer keys */ |
| 11394 | PyObject *newkey; |
| 11395 | if (PyUnicode_GET_SIZE(key) != 1) { |
| 11396 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 11397 | "table must be of length 1"); |
| 11398 | goto err; |
| 11399 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11400 | kind = PyUnicode_KIND(key); |
| 11401 | data = PyUnicode_DATA(key); |
| 11402 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11403 | if (!newkey) |
| 11404 | goto err; |
| 11405 | res = PyDict_SetItem(new, newkey, value); |
| 11406 | Py_DECREF(newkey); |
| 11407 | if (res < 0) |
| 11408 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11409 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11410 | /* just keep integer keys */ |
| 11411 | if (PyDict_SetItem(new, key, value) < 0) |
| 11412 | goto err; |
| 11413 | } else { |
| 11414 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 11415 | "be strings or integers"); |
| 11416 | goto err; |
| 11417 | } |
| 11418 | } |
| 11419 | } |
| 11420 | return new; |
| 11421 | err: |
| 11422 | Py_DECREF(new); |
| 11423 | return NULL; |
| 11424 | } |
| 11425 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11426 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11427 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11428 | \n\ |
| 11429 | Return a copy of the string S, where all characters have been mapped\n\ |
| 11430 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11431 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 11432 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 11433 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11434 | |
| 11435 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11436 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11437 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11438 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11439 | } |
| 11440 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11441 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11442 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11443 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11444 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11445 | |
| 11446 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11447 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11448 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11449 | return fixup(self, fixupper); |
| 11450 | } |
| 11451 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11452 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11453 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11454 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 11455 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 11456 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11457 | |
| 11458 | static PyObject * |
| 11459 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 11460 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11461 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11462 | PyUnicodeObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11463 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11464 | int kind; |
| 11465 | void *data; |
| 11466 | Py_UCS4 chr; |
| 11467 | |
| 11468 | if (PyUnicode_READY(self) == -1) |
| 11469 | return NULL; |
| 11470 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11471 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11472 | return NULL; |
| 11473 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11474 | if (PyUnicode_GET_LENGTH(self) >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 11475 | if (PyUnicode_CheckExact(self)) { |
| 11476 | Py_INCREF(self); |
| 11477 | return (PyObject*) self; |
| 11478 | } |
| 11479 | else |
Victor Stinner | 2219e0a | 2011-10-01 01:16:59 +0200 | [diff] [blame] | 11480 | return PyUnicode_Copy((PyObject*)self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11481 | } |
| 11482 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11483 | fill = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11484 | |
| 11485 | u = pad(self, fill, 0, '0'); |
| 11486 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 11487 | if (u == NULL) |
| 11488 | return NULL; |
| 11489 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11490 | kind = PyUnicode_KIND(u); |
| 11491 | data = PyUnicode_DATA(u); |
| 11492 | chr = PyUnicode_READ(kind, data, fill); |
| 11493 | |
| 11494 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11495 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11496 | PyUnicode_WRITE(kind, data, 0, chr); |
| 11497 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11498 | } |
| 11499 | |
| 11500 | return (PyObject*) u; |
| 11501 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11502 | |
| 11503 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 11504 | static PyObject * |
| 11505 | unicode__decimal2ascii(PyObject *self) |
| 11506 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11507 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 11508 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11509 | #endif |
| 11510 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11511 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11512 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11513 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 11514 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 11515 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11516 | With optional end, stop comparing S at that position.\n\ |
| 11517 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11518 | |
| 11519 | static PyObject * |
| 11520 | unicode_startswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11521 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11522 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11523 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11524 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11525 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11526 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11527 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11528 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11529 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11530 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11531 | if (PyTuple_Check(subobj)) { |
| 11532 | Py_ssize_t i; |
| 11533 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 11534 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11535 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11536 | if (substring == NULL) |
| 11537 | return NULL; |
| 11538 | result = tailmatch(self, substring, start, end, -1); |
| 11539 | Py_DECREF(substring); |
| 11540 | if (result) { |
| 11541 | Py_RETURN_TRUE; |
| 11542 | } |
| 11543 | } |
| 11544 | /* nothing matched */ |
| 11545 | Py_RETURN_FALSE; |
| 11546 | } |
| 11547 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 11548 | if (substring == NULL) { |
| 11549 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 11550 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 11551 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11552 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 11553 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11554 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11555 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11556 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11557 | } |
| 11558 | |
| 11559 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11560 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11561 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11562 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 11563 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 11564 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11565 | With optional end, stop comparing S at that position.\n\ |
| 11566 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11567 | |
| 11568 | static PyObject * |
| 11569 | unicode_endswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11570 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11571 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11572 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11573 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11574 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11575 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11576 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11577 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11578 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11579 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11580 | if (PyTuple_Check(subobj)) { |
| 11581 | Py_ssize_t i; |
| 11582 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 11583 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11584 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11585 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11586 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11587 | result = tailmatch(self, substring, start, end, +1); |
| 11588 | Py_DECREF(substring); |
| 11589 | if (result) { |
| 11590 | Py_RETURN_TRUE; |
| 11591 | } |
| 11592 | } |
| 11593 | Py_RETURN_FALSE; |
| 11594 | } |
| 11595 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 11596 | if (substring == NULL) { |
| 11597 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 11598 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 11599 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11600 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 11601 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11602 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11603 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11604 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11605 | } |
| 11606 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11607 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 11608 | |
| 11609 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11610 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 11611 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 11612 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 11613 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 11614 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 11615 | PyDoc_STRVAR(format_map__doc__, |
| 11616 | "S.format_map(mapping) -> str\n\ |
| 11617 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 11618 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 11619 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 11620 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 11621 | static PyObject * |
| 11622 | unicode__format__(PyObject* self, PyObject* args) |
| 11623 | { |
| 11624 | PyObject *format_spec; |
| 11625 | |
| 11626 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 11627 | return NULL; |
| 11628 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11629 | return _PyUnicode_FormatAdvanced(self, format_spec, 0, |
| 11630 | PyUnicode_GET_LENGTH(format_spec)); |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 11631 | } |
| 11632 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 11633 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11634 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 11635 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 11636 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 11637 | |
| 11638 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 11639 | unicode__sizeof__(PyUnicodeObject *v) |
| 11640 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11641 | Py_ssize_t size; |
| 11642 | |
| 11643 | /* If it's a compact object, account for base structure + |
| 11644 | character data. */ |
| 11645 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 11646 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 11647 | else if (PyUnicode_IS_COMPACT(v)) |
| 11648 | size = sizeof(PyCompactUnicodeObject) + |
| 11649 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_CHARACTER_SIZE(v); |
| 11650 | else { |
| 11651 | /* If it is a two-block object, account for base object, and |
| 11652 | for character block if present. */ |
| 11653 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 11654 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11655 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
| 11656 | PyUnicode_CHARACTER_SIZE(v); |
| 11657 | } |
| 11658 | /* If the wstr pointer is present, account for it unless it is shared |
| 11659 | with the data pointer. Since PyUnicode_DATA will crash if the object |
| 11660 | is not ready, check whether it's either not ready (in which case the |
| 11661 | data is entirely in wstr) or if the data is not shared. */ |
| 11662 | if (_PyUnicode_WSTR(v) && |
| 11663 | (!PyUnicode_IS_READY(v) || |
| 11664 | (PyUnicode_DATA(v) != _PyUnicode_WSTR(v)))) |
| 11665 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 11666 | if (!PyUnicode_IS_COMPACT_ASCII(v) |
| 11667 | && _PyUnicode_UTF8(v) |
| 11668 | && _PyUnicode_UTF8(v) != PyUnicode_DATA(v)) |
| 11669 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11670 | |
| 11671 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 11672 | } |
| 11673 | |
| 11674 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11675 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 11676 | |
| 11677 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 11678 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 11679 | { |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 11680 | PyObject *copy = PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11681 | if (!copy) |
| 11682 | return NULL; |
| 11683 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 11684 | } |
| 11685 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11686 | static PyMethodDef unicode_methods[] = { |
| 11687 | |
| 11688 | /* Order is according to common usage: often used methods should |
| 11689 | appear first, since lookup is done sequentially. */ |
| 11690 | |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 11691 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11692 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 11693 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11694 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11695 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 11696 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 11697 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 11698 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 11699 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 11700 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 11701 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11702 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11703 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 11704 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 11705 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11706 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11707 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 11708 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 11709 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11710 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11711 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 11712 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11713 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11714 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 11715 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 11716 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 11717 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 11718 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 11719 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 11720 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 11721 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 11722 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 11723 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 11724 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 11725 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 11726 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 11727 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11728 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11729 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11730 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 11731 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 11732 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 11733 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11734 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 11735 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 11736 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 11737 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11738 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11739 | #endif |
| 11740 | |
| 11741 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 11742 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 11743 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11744 | #endif |
| 11745 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11746 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11747 | {NULL, NULL} |
| 11748 | }; |
| 11749 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 11750 | static PyObject * |
| 11751 | unicode_mod(PyObject *v, PyObject *w) |
| 11752 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 11753 | if (!PyUnicode_Check(v)) |
| 11754 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11755 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 11756 | } |
| 11757 | |
| 11758 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11759 | 0, /*nb_add*/ |
| 11760 | 0, /*nb_subtract*/ |
| 11761 | 0, /*nb_multiply*/ |
| 11762 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 11763 | }; |
| 11764 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11765 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11766 | (lenfunc) unicode_length, /* sq_length */ |
| 11767 | PyUnicode_Concat, /* sq_concat */ |
| 11768 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 11769 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 11770 | 0, /* sq_slice */ |
| 11771 | 0, /* sq_ass_item */ |
| 11772 | 0, /* sq_ass_slice */ |
| 11773 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11774 | }; |
| 11775 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11776 | static PyObject* |
| 11777 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 11778 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11779 | if (PyUnicode_READY(self) == -1) |
| 11780 | return NULL; |
| 11781 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 11782 | if (PyIndex_Check(item)) { |
| 11783 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11784 | if (i == -1 && PyErr_Occurred()) |
| 11785 | return NULL; |
| 11786 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11787 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11788 | return unicode_getitem((PyObject*)self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11789 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11790 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11791 | const Py_UNICODE* source_buf; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11792 | Py_UNICODE* result_buf; |
| 11793 | PyObject* result; |
| 11794 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11795 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11796 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11797 | return NULL; |
| 11798 | } |
| 11799 | |
| 11800 | if (slicelength <= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11801 | return PyUnicode_New(0, 0); |
| 11802 | } else if (start == 0 && step == 1 && |
| 11803 | slicelength == PyUnicode_GET_LENGTH(self) && |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 11804 | PyUnicode_CheckExact(self)) { |
| 11805 | Py_INCREF(self); |
| 11806 | return (PyObject *)self; |
| 11807 | } else if (step == 1) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11808 | return PyUnicode_Substring((PyObject*)self, |
| 11809 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11810 | } else { |
| 11811 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 11812 | result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* |
| 11813 | sizeof(Py_UNICODE)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11814 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11815 | if (result_buf == NULL) |
| 11816 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11817 | |
| 11818 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 11819 | result_buf[i] = source_buf[cur]; |
| 11820 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11821 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11822 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 11823 | PyObject_FREE(result_buf); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11824 | return result; |
| 11825 | } |
| 11826 | } else { |
| 11827 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 11828 | return NULL; |
| 11829 | } |
| 11830 | } |
| 11831 | |
| 11832 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11833 | (lenfunc)unicode_length, /* mp_length */ |
| 11834 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 11835 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11836 | }; |
| 11837 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11838 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11839 | /* Helpers for PyUnicode_Format() */ |
| 11840 | |
| 11841 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11842 | 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] | 11843 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11844 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11845 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11846 | (*p_argidx)++; |
| 11847 | if (arglen < 0) |
| 11848 | return args; |
| 11849 | else |
| 11850 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11851 | } |
| 11852 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11853 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11854 | return NULL; |
| 11855 | } |
| 11856 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 11857 | /* 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] | 11858 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 11859 | static PyObject * |
| 11860 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11861 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 11862 | char *p; |
| 11863 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11864 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11865 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11866 | x = PyFloat_AsDouble(v); |
| 11867 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 11868 | return NULL; |
| 11869 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11870 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11871 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 11872 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 11873 | p = PyOS_double_to_string(x, type, prec, |
| 11874 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 11875 | if (p == NULL) |
| 11876 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11877 | result = PyUnicode_DecodeASCII(p, strlen(p), NULL); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 11878 | PyMem_Free(p); |
| 11879 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11880 | } |
| 11881 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 11882 | static PyObject* |
| 11883 | formatlong(PyObject *val, int flags, int prec, int type) |
| 11884 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11885 | char *buf; |
| 11886 | int len; |
| 11887 | PyObject *str; /* temporary string object. */ |
| 11888 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 11889 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11890 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 11891 | if (!str) |
| 11892 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11893 | result = PyUnicode_DecodeASCII(buf, len, NULL); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11894 | Py_DECREF(str); |
| 11895 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 11896 | } |
| 11897 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11898 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11899 | formatchar(Py_UCS4 *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 11900 | size_t buflen, |
| 11901 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11902 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 11903 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 11904 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11905 | if (PyUnicode_GET_LENGTH(v) == 1) { |
| 11906 | buf[0] = PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11907 | buf[1] = '\0'; |
| 11908 | return 1; |
| 11909 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11910 | goto onError; |
| 11911 | } |
| 11912 | else { |
| 11913 | /* Integer input truncated to a character */ |
| 11914 | long x; |
| 11915 | x = PyLong_AsLong(v); |
| 11916 | if (x == -1 && PyErr_Occurred()) |
| 11917 | goto onError; |
| 11918 | |
| 11919 | if (x < 0 || x > 0x10ffff) { |
| 11920 | PyErr_SetString(PyExc_OverflowError, |
| 11921 | "%c arg not in range(0x110000)"); |
| 11922 | return -1; |
| 11923 | } |
| 11924 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11925 | buf[0] = (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11926 | buf[1] = '\0'; |
| 11927 | return 1; |
| 11928 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 11929 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11930 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 11931 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11932 | "%c requires int or char"); |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 11933 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11934 | } |
| 11935 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 11936 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 11937 | 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] | 11938 | */ |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 11939 | #define FORMATBUFLEN (size_t)10 |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 11940 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11941 | PyObject * |
| 11942 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11943 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11944 | void *fmt; |
| 11945 | int fmtkind; |
| 11946 | PyObject *result; |
| 11947 | Py_UCS4 *res, *res0; |
| 11948 | Py_UCS4 max; |
| 11949 | int kind; |
| 11950 | Py_ssize_t fmtcnt, fmtpos, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11951 | int args_owned = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11952 | PyObject *dict = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11953 | PyUnicodeObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11954 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11955 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11956 | PyErr_BadInternalCall(); |
| 11957 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11958 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11959 | uformat = (PyUnicodeObject*)PyUnicode_FromObject(format); |
| 11960 | if (uformat == NULL || PyUnicode_READY(uformat) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11961 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11962 | fmt = PyUnicode_DATA(uformat); |
| 11963 | fmtkind = PyUnicode_KIND(uformat); |
| 11964 | fmtcnt = PyUnicode_GET_LENGTH(uformat); |
| 11965 | fmtpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11966 | |
| 11967 | reslen = rescnt = fmtcnt + 100; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11968 | res = res0 = PyMem_Malloc(reslen * sizeof(Py_UCS4)); |
| 11969 | if (res0 == NULL) { |
| 11970 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11971 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11972 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11973 | |
| 11974 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11975 | arglen = PyTuple_Size(args); |
| 11976 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11977 | } |
| 11978 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11979 | arglen = -1; |
| 11980 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11981 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 11982 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 11983 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11984 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11985 | |
| 11986 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11987 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11988 | if (--rescnt < 0) { |
| 11989 | rescnt = fmtcnt + 100; |
| 11990 | reslen += rescnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11991 | res0 = PyMem_Realloc(res0, reslen*sizeof(Py_UCS4)); |
| 11992 | if (res0 == NULL){ |
| 11993 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11994 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11995 | } |
| 11996 | res = res0 + reslen - rescnt; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11997 | --rescnt; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11998 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11999 | *res++ = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12000 | } |
| 12001 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12002 | /* Got a format specifier */ |
| 12003 | int flags = 0; |
| 12004 | Py_ssize_t width = -1; |
| 12005 | int prec = -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12006 | Py_UCS4 c = '\0'; |
| 12007 | Py_UCS4 fill; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12008 | int isnumok; |
| 12009 | PyObject *v = NULL; |
| 12010 | PyObject *temp = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12011 | void *pbuf; |
| 12012 | Py_ssize_t pindex; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12013 | Py_UNICODE sign; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12014 | Py_ssize_t len, len1; |
| 12015 | Py_UCS4 formatbuf[FORMATBUFLEN]; /* For formatchar() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12016 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12017 | fmtpos++; |
| 12018 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') { |
| 12019 | Py_ssize_t keystart; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12020 | Py_ssize_t keylen; |
| 12021 | PyObject *key; |
| 12022 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 12023 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12024 | if (dict == NULL) { |
| 12025 | PyErr_SetString(PyExc_TypeError, |
| 12026 | "format requires a mapping"); |
| 12027 | goto onError; |
| 12028 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12029 | ++fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12030 | --fmtcnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12031 | keystart = fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12032 | /* Skip over balanced parentheses */ |
| 12033 | while (pcount > 0 && --fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12034 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == ')') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12035 | --pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12036 | else if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12037 | ++pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12038 | fmtpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12039 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12040 | keylen = fmtpos - keystart - 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12041 | if (fmtcnt < 0 || pcount > 0) { |
| 12042 | PyErr_SetString(PyExc_ValueError, |
| 12043 | "incomplete format key"); |
| 12044 | goto onError; |
| 12045 | } |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12046 | key = PyUnicode_Substring((PyObject*)uformat, |
| 12047 | keystart, keystart + keylen); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12048 | if (key == NULL) |
| 12049 | goto onError; |
| 12050 | if (args_owned) { |
| 12051 | Py_DECREF(args); |
| 12052 | args_owned = 0; |
| 12053 | } |
| 12054 | args = PyObject_GetItem(dict, key); |
| 12055 | Py_DECREF(key); |
| 12056 | if (args == NULL) { |
| 12057 | goto onError; |
| 12058 | } |
| 12059 | args_owned = 1; |
| 12060 | arglen = -1; |
| 12061 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12062 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12063 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12064 | switch (c = PyUnicode_READ(fmtkind, fmt, fmtpos++)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12065 | case '-': flags |= F_LJUST; continue; |
| 12066 | case '+': flags |= F_SIGN; continue; |
| 12067 | case ' ': flags |= F_BLANK; continue; |
| 12068 | case '#': flags |= F_ALT; continue; |
| 12069 | case '0': flags |= F_ZERO; continue; |
| 12070 | } |
| 12071 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12072 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12073 | if (c == '*') { |
| 12074 | v = getnextarg(args, arglen, &argidx); |
| 12075 | if (v == NULL) |
| 12076 | goto onError; |
| 12077 | if (!PyLong_Check(v)) { |
| 12078 | PyErr_SetString(PyExc_TypeError, |
| 12079 | "* wants int"); |
| 12080 | goto onError; |
| 12081 | } |
| 12082 | width = PyLong_AsLong(v); |
| 12083 | if (width == -1 && PyErr_Occurred()) |
| 12084 | goto onError; |
| 12085 | if (width < 0) { |
| 12086 | flags |= F_LJUST; |
| 12087 | width = -width; |
| 12088 | } |
| 12089 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12090 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12091 | } |
| 12092 | else if (c >= '0' && c <= '9') { |
| 12093 | width = c - '0'; |
| 12094 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12095 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12096 | if (c < '0' || c > '9') |
| 12097 | break; |
| 12098 | if ((width*10) / 10 != width) { |
| 12099 | PyErr_SetString(PyExc_ValueError, |
| 12100 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12101 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12102 | } |
| 12103 | width = width*10 + (c - '0'); |
| 12104 | } |
| 12105 | } |
| 12106 | if (c == '.') { |
| 12107 | prec = 0; |
| 12108 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12109 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12110 | if (c == '*') { |
| 12111 | v = getnextarg(args, arglen, &argidx); |
| 12112 | if (v == NULL) |
| 12113 | goto onError; |
| 12114 | if (!PyLong_Check(v)) { |
| 12115 | PyErr_SetString(PyExc_TypeError, |
| 12116 | "* wants int"); |
| 12117 | goto onError; |
| 12118 | } |
| 12119 | prec = PyLong_AsLong(v); |
| 12120 | if (prec == -1 && PyErr_Occurred()) |
| 12121 | goto onError; |
| 12122 | if (prec < 0) |
| 12123 | prec = 0; |
| 12124 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12125 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12126 | } |
| 12127 | else if (c >= '0' && c <= '9') { |
| 12128 | prec = c - '0'; |
| 12129 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12130 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12131 | if (c < '0' || c > '9') |
| 12132 | break; |
| 12133 | if ((prec*10) / 10 != prec) { |
| 12134 | PyErr_SetString(PyExc_ValueError, |
| 12135 | "prec too big"); |
| 12136 | goto onError; |
| 12137 | } |
| 12138 | prec = prec*10 + (c - '0'); |
| 12139 | } |
| 12140 | } |
| 12141 | } /* prec */ |
| 12142 | if (fmtcnt >= 0) { |
| 12143 | if (c == 'h' || c == 'l' || c == 'L') { |
| 12144 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12145 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12146 | } |
| 12147 | } |
| 12148 | if (fmtcnt < 0) { |
| 12149 | PyErr_SetString(PyExc_ValueError, |
| 12150 | "incomplete format"); |
| 12151 | goto onError; |
| 12152 | } |
| 12153 | if (c != '%') { |
| 12154 | v = getnextarg(args, arglen, &argidx); |
| 12155 | if (v == NULL) |
| 12156 | goto onError; |
| 12157 | } |
| 12158 | sign = 0; |
| 12159 | fill = ' '; |
| 12160 | switch (c) { |
| 12161 | |
| 12162 | case '%': |
| 12163 | pbuf = formatbuf; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12164 | kind = PyUnicode_4BYTE_KIND; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12165 | /* presume that buffer length is at least 1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12166 | PyUnicode_WRITE(kind, pbuf, 0, '%'); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12167 | len = 1; |
| 12168 | break; |
| 12169 | |
| 12170 | case 's': |
| 12171 | case 'r': |
| 12172 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 12173 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12174 | temp = v; |
| 12175 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12176 | } |
| 12177 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12178 | if (c == 's') |
| 12179 | temp = PyObject_Str(v); |
| 12180 | else if (c == 'r') |
| 12181 | temp = PyObject_Repr(v); |
| 12182 | else |
| 12183 | temp = PyObject_ASCII(v); |
| 12184 | if (temp == NULL) |
| 12185 | goto onError; |
| 12186 | if (PyUnicode_Check(temp)) |
| 12187 | /* nothing to do */; |
| 12188 | else { |
| 12189 | Py_DECREF(temp); |
| 12190 | PyErr_SetString(PyExc_TypeError, |
| 12191 | "%s argument has non-string str()"); |
| 12192 | goto onError; |
| 12193 | } |
| 12194 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12195 | if (PyUnicode_READY(temp) == -1) { |
| 12196 | Py_CLEAR(temp); |
| 12197 | goto onError; |
| 12198 | } |
| 12199 | pbuf = PyUnicode_DATA(temp); |
| 12200 | kind = PyUnicode_KIND(temp); |
| 12201 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12202 | if (prec >= 0 && len > prec) |
| 12203 | len = prec; |
| 12204 | break; |
| 12205 | |
| 12206 | case 'i': |
| 12207 | case 'd': |
| 12208 | case 'u': |
| 12209 | case 'o': |
| 12210 | case 'x': |
| 12211 | case 'X': |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12212 | isnumok = 0; |
| 12213 | if (PyNumber_Check(v)) { |
| 12214 | PyObject *iobj=NULL; |
| 12215 | |
| 12216 | if (PyLong_Check(v)) { |
| 12217 | iobj = v; |
| 12218 | Py_INCREF(iobj); |
| 12219 | } |
| 12220 | else { |
| 12221 | iobj = PyNumber_Long(v); |
| 12222 | } |
| 12223 | if (iobj!=NULL) { |
| 12224 | if (PyLong_Check(iobj)) { |
| 12225 | isnumok = 1; |
Senthil Kumaran | 9ebe08d | 2011-07-03 21:03:16 -0700 | [diff] [blame] | 12226 | temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12227 | Py_DECREF(iobj); |
| 12228 | if (!temp) |
| 12229 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12230 | if (PyUnicode_READY(temp) == -1) { |
| 12231 | Py_CLEAR(temp); |
| 12232 | goto onError; |
| 12233 | } |
| 12234 | pbuf = PyUnicode_DATA(temp); |
| 12235 | kind = PyUnicode_KIND(temp); |
| 12236 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12237 | sign = 1; |
| 12238 | } |
| 12239 | else { |
| 12240 | Py_DECREF(iobj); |
| 12241 | } |
| 12242 | } |
| 12243 | } |
| 12244 | if (!isnumok) { |
| 12245 | PyErr_Format(PyExc_TypeError, |
| 12246 | "%%%c format: a number is required, " |
| 12247 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 12248 | goto onError; |
| 12249 | } |
| 12250 | if (flags & F_ZERO) |
| 12251 | fill = '0'; |
| 12252 | break; |
| 12253 | |
| 12254 | case 'e': |
| 12255 | case 'E': |
| 12256 | case 'f': |
| 12257 | case 'F': |
| 12258 | case 'g': |
| 12259 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12260 | temp = formatfloat(v, flags, prec, c); |
| 12261 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12262 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12263 | if (PyUnicode_READY(temp) == -1) { |
| 12264 | Py_CLEAR(temp); |
| 12265 | goto onError; |
| 12266 | } |
| 12267 | pbuf = PyUnicode_DATA(temp); |
| 12268 | kind = PyUnicode_KIND(temp); |
| 12269 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12270 | sign = 1; |
| 12271 | if (flags & F_ZERO) |
| 12272 | fill = '0'; |
| 12273 | break; |
| 12274 | |
| 12275 | case 'c': |
| 12276 | pbuf = formatbuf; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12277 | kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | b9dcffb | 2011-09-29 00:39:24 +0200 | [diff] [blame] | 12278 | len = formatchar(pbuf, Py_ARRAY_LENGTH(formatbuf), v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12279 | if (len < 0) |
| 12280 | goto onError; |
| 12281 | break; |
| 12282 | |
| 12283 | default: |
| 12284 | PyErr_Format(PyExc_ValueError, |
| 12285 | "unsupported format character '%c' (0x%x) " |
| 12286 | "at index %zd", |
| 12287 | (31<=c && c<=126) ? (char)c : '?', |
| 12288 | (int)c, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12289 | fmtpos - 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12290 | goto onError; |
| 12291 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12292 | /* pbuf is initialized here. */ |
| 12293 | pindex = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12294 | if (sign) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12295 | if (PyUnicode_READ(kind, pbuf, pindex) == '-' || |
| 12296 | PyUnicode_READ(kind, pbuf, pindex) == '+') { |
| 12297 | sign = PyUnicode_READ(kind, pbuf, pindex++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12298 | len--; |
| 12299 | } |
| 12300 | else if (flags & F_SIGN) |
| 12301 | sign = '+'; |
| 12302 | else if (flags & F_BLANK) |
| 12303 | sign = ' '; |
| 12304 | else |
| 12305 | sign = 0; |
| 12306 | } |
| 12307 | if (width < len) |
| 12308 | width = len; |
| 12309 | if (rescnt - (sign != 0) < width) { |
| 12310 | reslen -= rescnt; |
| 12311 | rescnt = width + fmtcnt + 100; |
| 12312 | reslen += rescnt; |
| 12313 | if (reslen < 0) { |
| 12314 | Py_XDECREF(temp); |
| 12315 | PyErr_NoMemory(); |
| 12316 | goto onError; |
| 12317 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12318 | res0 = PyMem_Realloc(res0, reslen*sizeof(Py_UCS4)); |
| 12319 | if (res0 == 0) { |
| 12320 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12321 | Py_XDECREF(temp); |
| 12322 | goto onError; |
| 12323 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12324 | res = res0 + reslen - rescnt; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12325 | } |
| 12326 | if (sign) { |
| 12327 | if (fill != ' ') |
| 12328 | *res++ = sign; |
| 12329 | rescnt--; |
| 12330 | if (width > len) |
| 12331 | width--; |
| 12332 | } |
| 12333 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12334 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 12335 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12336 | if (fill != ' ') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12337 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
| 12338 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12339 | } |
| 12340 | rescnt -= 2; |
| 12341 | width -= 2; |
| 12342 | if (width < 0) |
| 12343 | width = 0; |
| 12344 | len -= 2; |
| 12345 | } |
| 12346 | if (width > len && !(flags & F_LJUST)) { |
| 12347 | do { |
| 12348 | --rescnt; |
| 12349 | *res++ = fill; |
| 12350 | } while (--width > len); |
| 12351 | } |
| 12352 | if (fill == ' ') { |
| 12353 | if (sign) |
| 12354 | *res++ = sign; |
| 12355 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12356 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 12357 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
| 12358 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
| 12359 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12360 | } |
| 12361 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12362 | /* Copy all characters, preserving len */ |
| 12363 | len1 = len; |
| 12364 | while (len1--) { |
| 12365 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
| 12366 | rescnt--; |
| 12367 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12368 | while (--width >= len) { |
| 12369 | --rescnt; |
| 12370 | *res++ = ' '; |
| 12371 | } |
| 12372 | if (dict && (argidx < arglen) && c != '%') { |
| 12373 | PyErr_SetString(PyExc_TypeError, |
| 12374 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 12375 | Py_XDECREF(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12376 | goto onError; |
| 12377 | } |
| 12378 | Py_XDECREF(temp); |
| 12379 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12380 | } /* until end */ |
| 12381 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12382 | PyErr_SetString(PyExc_TypeError, |
| 12383 | "not all arguments converted during string formatting"); |
| 12384 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12385 | } |
| 12386 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12387 | |
| 12388 | for (max=0, res = res0; res < res0+reslen-rescnt; res++) |
| 12389 | if (*res > max) |
| 12390 | max = *res; |
| 12391 | result = PyUnicode_New(reslen - rescnt, max); |
| 12392 | if (!result) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12393 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12394 | kind = PyUnicode_KIND(result); |
| 12395 | for (res = res0; res < res0+reslen-rescnt; res++) |
| 12396 | PyUnicode_WRITE(kind, PyUnicode_DATA(result), res-res0, *res); |
| 12397 | PyMem_Free(res0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12398 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12399 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12400 | } |
| 12401 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12402 | return (PyObject *)result; |
| 12403 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12404 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12405 | PyMem_Free(res0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12406 | Py_DECREF(uformat); |
| 12407 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12408 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12409 | } |
| 12410 | return NULL; |
| 12411 | } |
| 12412 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 12413 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 12414 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 12415 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12416 | static PyObject * |
| 12417 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 12418 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12419 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12420 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 12421 | char *encoding = NULL; |
| 12422 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12423 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12424 | if (type != &PyUnicode_Type) |
| 12425 | return unicode_subtype_new(type, args, kwds); |
| 12426 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12427 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12428 | return NULL; |
| 12429 | if (x == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12430 | return (PyObject *)PyUnicode_New(0, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12431 | if (encoding == NULL && errors == NULL) |
| 12432 | return PyObject_Str(x); |
| 12433 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12434 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12435 | } |
| 12436 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 12437 | static PyObject * |
| 12438 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 12439 | { |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12440 | PyUnicodeObject *unicode, *self; |
| 12441 | Py_ssize_t length, char_size; |
| 12442 | int share_wstr, share_utf8; |
| 12443 | unsigned int kind; |
| 12444 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 12445 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12446 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12447 | |
| 12448 | unicode = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 12449 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12450 | return NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12451 | assert(PyUnicode_Check(unicode)); |
| 12452 | if (PyUnicode_READY(unicode)) |
| 12453 | return NULL; |
| 12454 | |
| 12455 | self = (PyUnicodeObject *) type->tp_alloc(type, 0); |
| 12456 | if (self == NULL) { |
| 12457 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12458 | return NULL; |
| 12459 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12460 | kind = PyUnicode_KIND(unicode); |
| 12461 | length = PyUnicode_GET_LENGTH(unicode); |
| 12462 | |
| 12463 | _PyUnicode_LENGTH(self) = length; |
| 12464 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 12465 | _PyUnicode_STATE(self).interned = 0; |
| 12466 | _PyUnicode_STATE(self).kind = kind; |
| 12467 | _PyUnicode_STATE(self).compact = 0; |
| 12468 | _PyUnicode_STATE(self).ascii = 0; |
| 12469 | _PyUnicode_STATE(self).ready = 1; |
| 12470 | _PyUnicode_WSTR(self) = NULL; |
| 12471 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 12472 | _PyUnicode_UTF8(self) = NULL; |
| 12473 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 12474 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12475 | |
| 12476 | share_utf8 = 0; |
| 12477 | share_wstr = 0; |
| 12478 | if (kind == PyUnicode_1BYTE_KIND) { |
| 12479 | char_size = 1; |
| 12480 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 12481 | share_utf8 = 1; |
| 12482 | } |
| 12483 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 12484 | char_size = 2; |
| 12485 | if (sizeof(wchar_t) == 2) |
| 12486 | share_wstr = 1; |
| 12487 | } |
| 12488 | else { |
| 12489 | assert(kind == PyUnicode_4BYTE_KIND); |
| 12490 | char_size = 4; |
| 12491 | if (sizeof(wchar_t) == 4) |
| 12492 | share_wstr = 1; |
| 12493 | } |
| 12494 | |
| 12495 | /* Ensure we won't overflow the length. */ |
| 12496 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 12497 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12498 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12499 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12500 | data = PyObject_MALLOC((length + 1) * char_size); |
| 12501 | if (data == NULL) { |
| 12502 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12503 | goto onError; |
| 12504 | } |
| 12505 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 12506 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12507 | if (share_utf8) { |
| 12508 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 12509 | _PyUnicode_UTF8(self) = data; |
| 12510 | } |
| 12511 | if (share_wstr) { |
| 12512 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 12513 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 12514 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12515 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12516 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
| 12517 | PyUnicode_KIND_SIZE(kind, length + 1)); |
| 12518 | Py_DECREF(unicode); |
| 12519 | return (PyObject *)self; |
| 12520 | |
| 12521 | onError: |
| 12522 | Py_DECREF(unicode); |
| 12523 | Py_DECREF(self); |
| 12524 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 12525 | } |
| 12526 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12527 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12528 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12529 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 12530 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 12531 | encoding defaults to the current default string encoding.\n\ |
| 12532 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12533 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12534 | static PyObject *unicode_iter(PyObject *seq); |
| 12535 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12536 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 12537 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12538 | "str", /* tp_name */ |
| 12539 | sizeof(PyUnicodeObject), /* tp_size */ |
| 12540 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12541 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12542 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 12543 | 0, /* tp_print */ |
| 12544 | 0, /* tp_getattr */ |
| 12545 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 12546 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12547 | unicode_repr, /* tp_repr */ |
| 12548 | &unicode_as_number, /* tp_as_number */ |
| 12549 | &unicode_as_sequence, /* tp_as_sequence */ |
| 12550 | &unicode_as_mapping, /* tp_as_mapping */ |
| 12551 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 12552 | 0, /* tp_call*/ |
| 12553 | (reprfunc) unicode_str, /* tp_str */ |
| 12554 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 12555 | 0, /* tp_setattro */ |
| 12556 | 0, /* tp_as_buffer */ |
| 12557 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12558 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12559 | unicode_doc, /* tp_doc */ |
| 12560 | 0, /* tp_traverse */ |
| 12561 | 0, /* tp_clear */ |
| 12562 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 12563 | 0, /* tp_weaklistoffset */ |
| 12564 | unicode_iter, /* tp_iter */ |
| 12565 | 0, /* tp_iternext */ |
| 12566 | unicode_methods, /* tp_methods */ |
| 12567 | 0, /* tp_members */ |
| 12568 | 0, /* tp_getset */ |
| 12569 | &PyBaseObject_Type, /* tp_base */ |
| 12570 | 0, /* tp_dict */ |
| 12571 | 0, /* tp_descr_get */ |
| 12572 | 0, /* tp_descr_set */ |
| 12573 | 0, /* tp_dictoffset */ |
| 12574 | 0, /* tp_init */ |
| 12575 | 0, /* tp_alloc */ |
| 12576 | unicode_new, /* tp_new */ |
| 12577 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12578 | }; |
| 12579 | |
| 12580 | /* Initialize the Unicode implementation */ |
| 12581 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 12582 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12583 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 12584 | int i; |
| 12585 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12586 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12587 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12588 | 0x000A, /* LINE FEED */ |
| 12589 | 0x000D, /* CARRIAGE RETURN */ |
| 12590 | 0x001C, /* FILE SEPARATOR */ |
| 12591 | 0x001D, /* GROUP SEPARATOR */ |
| 12592 | 0x001E, /* RECORD SEPARATOR */ |
| 12593 | 0x0085, /* NEXT LINE */ |
| 12594 | 0x2028, /* LINE SEPARATOR */ |
| 12595 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 12596 | }; |
| 12597 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 12598 | /* Init the implementation */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 12599 | unicode_empty = PyUnicode_New(0, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12600 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12601 | Py_FatalError("Can't create empty string"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12602 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 12603 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12604 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 12605 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12606 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12607 | |
| 12608 | /* initialize the linebreak bloom filter */ |
| 12609 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12610 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 12611 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12612 | |
| 12613 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12614 | } |
| 12615 | |
| 12616 | /* Finalize the Unicode implementation */ |
| 12617 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 12618 | int |
| 12619 | PyUnicode_ClearFreeList(void) |
| 12620 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12621 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 12622 | } |
| 12623 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12624 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 12625 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12626 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 12627 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12628 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 12629 | Py_XDECREF(unicode_empty); |
| 12630 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 12631 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 12632 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12633 | if (unicode_latin1[i]) { |
| 12634 | Py_DECREF(unicode_latin1[i]); |
| 12635 | unicode_latin1[i] = NULL; |
| 12636 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 12637 | } |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 12638 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12639 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 12640 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12641 | void |
| 12642 | PyUnicode_InternInPlace(PyObject **p) |
| 12643 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12644 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 12645 | PyObject *t; |
| 12646 | if (s == NULL || !PyUnicode_Check(s)) |
| 12647 | Py_FatalError( |
| 12648 | "PyUnicode_InternInPlace: unicode strings only please!"); |
| 12649 | /* If it's a subclass, we don't really know what putting |
| 12650 | it in the interned dict might do. */ |
| 12651 | if (!PyUnicode_CheckExact(s)) |
| 12652 | return; |
| 12653 | if (PyUnicode_CHECK_INTERNED(s)) |
| 12654 | return; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12655 | if (PyUnicode_READY(s) == -1) { |
| 12656 | assert(0 && "ready fail in intern..."); |
| 12657 | return; |
| 12658 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12659 | if (interned == NULL) { |
| 12660 | interned = PyDict_New(); |
| 12661 | if (interned == NULL) { |
| 12662 | PyErr_Clear(); /* Don't leave an exception */ |
| 12663 | return; |
| 12664 | } |
| 12665 | } |
| 12666 | /* It might be that the GetItem call fails even |
| 12667 | though the key is present in the dictionary, |
| 12668 | namely when this happens during a stack overflow. */ |
| 12669 | Py_ALLOW_RECURSION |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12670 | t = PyDict_GetItem(interned, (PyObject *)s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12671 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 12672 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12673 | if (t) { |
| 12674 | Py_INCREF(t); |
| 12675 | Py_DECREF(*p); |
| 12676 | *p = t; |
| 12677 | return; |
| 12678 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12679 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12680 | PyThreadState_GET()->recursion_critical = 1; |
| 12681 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 12682 | PyErr_Clear(); |
| 12683 | PyThreadState_GET()->recursion_critical = 0; |
| 12684 | return; |
| 12685 | } |
| 12686 | PyThreadState_GET()->recursion_critical = 0; |
| 12687 | /* The two references in interned are not counted by refcnt. |
| 12688 | The deallocator will take care of this */ |
| 12689 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12690 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12691 | } |
| 12692 | |
| 12693 | void |
| 12694 | PyUnicode_InternImmortal(PyObject **p) |
| 12695 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12696 | PyUnicodeObject *u = (PyUnicodeObject *)*p; |
| 12697 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12698 | PyUnicode_InternInPlace(p); |
| 12699 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12700 | _PyUnicode_STATE(u).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12701 | Py_INCREF(*p); |
| 12702 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12703 | } |
| 12704 | |
| 12705 | PyObject * |
| 12706 | PyUnicode_InternFromString(const char *cp) |
| 12707 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12708 | PyObject *s = PyUnicode_FromString(cp); |
| 12709 | if (s == NULL) |
| 12710 | return NULL; |
| 12711 | PyUnicode_InternInPlace(&s); |
| 12712 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12713 | } |
| 12714 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12715 | void |
| 12716 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12717 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12718 | PyObject *keys; |
| 12719 | PyUnicodeObject *s; |
| 12720 | Py_ssize_t i, n; |
| 12721 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12722 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12723 | if (interned == NULL || !PyDict_Check(interned)) |
| 12724 | return; |
| 12725 | keys = PyDict_Keys(interned); |
| 12726 | if (keys == NULL || !PyList_Check(keys)) { |
| 12727 | PyErr_Clear(); |
| 12728 | return; |
| 12729 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12730 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12731 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 12732 | detector, interned unicode strings are not forcibly deallocated; |
| 12733 | rather, we give them their stolen references back, and then clear |
| 12734 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12735 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12736 | n = PyList_GET_SIZE(keys); |
| 12737 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12738 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12739 | for (i = 0; i < n; i++) { |
| 12740 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12741 | if (PyUnicode_READY(s) == -1) |
| 12742 | fprintf(stderr, "could not ready string\n"); |
| 12743 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12744 | case SSTATE_NOT_INTERNED: |
| 12745 | /* XXX Shouldn't happen */ |
| 12746 | break; |
| 12747 | case SSTATE_INTERNED_IMMORTAL: |
| 12748 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12749 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12750 | break; |
| 12751 | case SSTATE_INTERNED_MORTAL: |
| 12752 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12753 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12754 | break; |
| 12755 | default: |
| 12756 | Py_FatalError("Inconsistent interned string state."); |
| 12757 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12758 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12759 | } |
| 12760 | fprintf(stderr, "total size of all interned strings: " |
| 12761 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 12762 | "mortal/immortal\n", mortal_size, immortal_size); |
| 12763 | Py_DECREF(keys); |
| 12764 | PyDict_Clear(interned); |
| 12765 | Py_DECREF(interned); |
| 12766 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12767 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12768 | |
| 12769 | |
| 12770 | /********************* Unicode Iterator **************************/ |
| 12771 | |
| 12772 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12773 | PyObject_HEAD |
| 12774 | Py_ssize_t it_index; |
| 12775 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12776 | } unicodeiterobject; |
| 12777 | |
| 12778 | static void |
| 12779 | unicodeiter_dealloc(unicodeiterobject *it) |
| 12780 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12781 | _PyObject_GC_UNTRACK(it); |
| 12782 | Py_XDECREF(it->it_seq); |
| 12783 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12784 | } |
| 12785 | |
| 12786 | static int |
| 12787 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 12788 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12789 | Py_VISIT(it->it_seq); |
| 12790 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12791 | } |
| 12792 | |
| 12793 | static PyObject * |
| 12794 | unicodeiter_next(unicodeiterobject *it) |
| 12795 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12796 | PyUnicodeObject *seq; |
| 12797 | PyObject *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12798 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12799 | assert(it != NULL); |
| 12800 | seq = it->it_seq; |
| 12801 | if (seq == NULL) |
| 12802 | return NULL; |
| 12803 | assert(PyUnicode_Check(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12804 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12805 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 12806 | int kind = PyUnicode_KIND(seq); |
| 12807 | void *data = PyUnicode_DATA(seq); |
| 12808 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 12809 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12810 | if (item != NULL) |
| 12811 | ++it->it_index; |
| 12812 | return item; |
| 12813 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12814 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12815 | Py_DECREF(seq); |
| 12816 | it->it_seq = NULL; |
| 12817 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12818 | } |
| 12819 | |
| 12820 | static PyObject * |
| 12821 | unicodeiter_len(unicodeiterobject *it) |
| 12822 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12823 | Py_ssize_t len = 0; |
| 12824 | if (it->it_seq) |
| 12825 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
| 12826 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12827 | } |
| 12828 | |
| 12829 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 12830 | |
| 12831 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12832 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12833 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12834 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12835 | }; |
| 12836 | |
| 12837 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12838 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 12839 | "str_iterator", /* tp_name */ |
| 12840 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 12841 | 0, /* tp_itemsize */ |
| 12842 | /* methods */ |
| 12843 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 12844 | 0, /* tp_print */ |
| 12845 | 0, /* tp_getattr */ |
| 12846 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 12847 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12848 | 0, /* tp_repr */ |
| 12849 | 0, /* tp_as_number */ |
| 12850 | 0, /* tp_as_sequence */ |
| 12851 | 0, /* tp_as_mapping */ |
| 12852 | 0, /* tp_hash */ |
| 12853 | 0, /* tp_call */ |
| 12854 | 0, /* tp_str */ |
| 12855 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 12856 | 0, /* tp_setattro */ |
| 12857 | 0, /* tp_as_buffer */ |
| 12858 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 12859 | 0, /* tp_doc */ |
| 12860 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 12861 | 0, /* tp_clear */ |
| 12862 | 0, /* tp_richcompare */ |
| 12863 | 0, /* tp_weaklistoffset */ |
| 12864 | PyObject_SelfIter, /* tp_iter */ |
| 12865 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 12866 | unicodeiter_methods, /* tp_methods */ |
| 12867 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12868 | }; |
| 12869 | |
| 12870 | static PyObject * |
| 12871 | unicode_iter(PyObject *seq) |
| 12872 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12873 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12874 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12875 | if (!PyUnicode_Check(seq)) { |
| 12876 | PyErr_BadInternalCall(); |
| 12877 | return NULL; |
| 12878 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12879 | if (PyUnicode_READY(seq) == -1) |
| 12880 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12881 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 12882 | if (it == NULL) |
| 12883 | return NULL; |
| 12884 | it->it_index = 0; |
| 12885 | Py_INCREF(seq); |
| 12886 | it->it_seq = (PyUnicodeObject *)seq; |
| 12887 | _PyObject_GC_TRACK(it); |
| 12888 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12889 | } |
| 12890 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12891 | #define UNIOP(x) Py_UNICODE_##x |
| 12892 | #define UNIOP_t Py_UNICODE |
| 12893 | #include "uniops.h" |
| 12894 | #undef UNIOP |
| 12895 | #undef UNIOP_t |
| 12896 | #define UNIOP(x) Py_UCS4_##x |
| 12897 | #define UNIOP_t Py_UCS4 |
| 12898 | #include "uniops.h" |
| 12899 | #undef UNIOP |
| 12900 | #undef UNIOP_t |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 12901 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 12902 | Py_UNICODE* |
Victor Stinner | 4640860 | 2010-09-03 16:18:00 +0000 | [diff] [blame] | 12903 | PyUnicode_AsUnicodeCopy(PyObject *object) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 12904 | { |
| 12905 | PyUnicodeObject *unicode = (PyUnicodeObject *)object; |
| 12906 | Py_UNICODE *copy; |
| 12907 | Py_ssize_t size; |
| 12908 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12909 | if (!PyUnicode_Check(unicode)) { |
| 12910 | PyErr_BadArgument(); |
| 12911 | return NULL; |
| 12912 | } |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 12913 | /* Ensure we won't overflow the size. */ |
| 12914 | if (PyUnicode_GET_SIZE(unicode) > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 12915 | PyErr_NoMemory(); |
| 12916 | return NULL; |
| 12917 | } |
| 12918 | size = PyUnicode_GET_SIZE(unicode) + 1; /* copy the nul character */ |
| 12919 | size *= sizeof(Py_UNICODE); |
| 12920 | copy = PyMem_Malloc(size); |
| 12921 | if (copy == NULL) { |
| 12922 | PyErr_NoMemory(); |
| 12923 | return NULL; |
| 12924 | } |
| 12925 | memcpy(copy, PyUnicode_AS_UNICODE(unicode), size); |
| 12926 | return copy; |
| 12927 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 12928 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 12929 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 12930 | to the string.Formatter class implemented in Python. */ |
| 12931 | |
| 12932 | static PyMethodDef _string_methods[] = { |
| 12933 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 12934 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 12935 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 12936 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 12937 | {NULL, NULL} |
| 12938 | }; |
| 12939 | |
| 12940 | static struct PyModuleDef _string_module = { |
| 12941 | PyModuleDef_HEAD_INIT, |
| 12942 | "_string", |
| 12943 | PyDoc_STR("string helper module"), |
| 12944 | 0, |
| 12945 | _string_methods, |
| 12946 | NULL, |
| 12947 | NULL, |
| 12948 | NULL, |
| 12949 | NULL |
| 12950 | }; |
| 12951 | |
| 12952 | PyMODINIT_FUNC |
| 12953 | PyInit__string(void) |
| 12954 | { |
| 12955 | return PyModule_Create(&_string_module); |
| 12956 | } |
| 12957 | |
| 12958 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12959 | #ifdef __cplusplus |
| 12960 | } |
| 12961 | #endif |