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 | bc8b81b | 2011-09-29 19:31:34 +0200 | [diff] [blame] | 107 | #define _PyUnicode_UTF8(op) \ |
| 108 | (PyUnicode_IS_COMPACT_ASCII(op) ? \ |
| 109 | ((char*)((PyASCIIObject*)(op) + 1)) : \ |
| 110 | ((PyCompactUnicodeObject*)(op))->utf8) |
| 111 | #define _PyUnicode_UTF8_LENGTH(op) \ |
| 112 | (PyUnicode_IS_COMPACT_ASCII(op) ? \ |
| 113 | ((PyASCIIObject*)(op))->length : \ |
| 114 | ((PyCompactUnicodeObject*)(op))->utf8_length) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 115 | #define _PyUnicode_WSTR(op) (((PyASCIIObject*)(op))->wstr) |
| 116 | #define _PyUnicode_WSTR_LENGTH(op) (((PyCompactUnicodeObject*)(op))->wstr_length) |
| 117 | #define _PyUnicode_LENGTH(op) (((PyASCIIObject *)(op))->length) |
| 118 | #define _PyUnicode_STATE(op) (((PyASCIIObject *)(op))->state) |
| 119 | #define _PyUnicode_HASH(op) (((PyASCIIObject *)(op))->hash) |
| 120 | #define _PyUnicode_KIND(op) \ |
| 121 | (assert(PyUnicode_Check(op)), \ |
| 122 | ((PyASCIIObject *)(op))->state.kind) |
| 123 | #define _PyUnicode_GET_LENGTH(op) \ |
| 124 | (assert(PyUnicode_Check(op)), \ |
| 125 | ((PyASCIIObject *)(op))->length) |
| 126 | |
Victor Stinner | b15d4d8 | 2011-09-28 23:59:20 +0200 | [diff] [blame] | 127 | /* The Unicode string has been modified: reset the hash */ |
| 128 | #define _PyUnicode_DIRTY(op) do { _PyUnicode_HASH(op) = -1; } while (0) |
| 129 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 130 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 131 | /* This dictionary holds all interned unicode strings. Note that references |
| 132 | to strings in this dictionary are *not* counted in the string's ob_refcnt. |
| 133 | When the interned string reaches a refcnt of 0 the string deallocation |
| 134 | function will delete the reference from this dictionary. |
| 135 | |
| 136 | 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] | 137 | 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] | 138 | */ |
| 139 | static PyObject *interned; |
| 140 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 141 | /* The empty Unicode object is shared to improve performance. */ |
| 142 | static PyUnicodeObject *unicode_empty; |
| 143 | |
| 144 | /* Single character Unicode strings in the Latin-1 range are being |
| 145 | shared as well. */ |
| 146 | static PyUnicodeObject *unicode_latin1[256]; |
| 147 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 148 | /* Fast detection of the most frequent whitespace characters */ |
| 149 | const unsigned char _Py_ascii_whitespace[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 150 | 0, 0, 0, 0, 0, 0, 0, 0, |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 151 | /* case 0x0009: * CHARACTER TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 152 | /* case 0x000A: * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 153 | /* case 0x000B: * LINE TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 154 | /* case 0x000C: * FORM FEED */ |
| 155 | /* case 0x000D: * CARRIAGE RETURN */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 156 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 157 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 158 | /* case 0x001C: * FILE SEPARATOR */ |
| 159 | /* case 0x001D: * GROUP SEPARATOR */ |
| 160 | /* case 0x001E: * RECORD SEPARATOR */ |
| 161 | /* case 0x001F: * UNIT SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 162 | 0, 0, 0, 0, 1, 1, 1, 1, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 163 | /* case 0x0020: * SPACE */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 164 | 1, 0, 0, 0, 0, 0, 0, 0, |
| 165 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 166 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 167 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 168 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 169 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 170 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 171 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 172 | 0, 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, |
| 176 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 177 | }; |
| 178 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 179 | static PyObject * |
| 180 | unicode_encode_call_errorhandler(const char *errors, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 181 | PyObject **errorHandler,const char *encoding, const char *reason, |
| 182 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 183 | Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos); |
| 184 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 185 | static void |
| 186 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 187 | const char *encoding, |
| 188 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 189 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 190 | const char *reason); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 191 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 192 | /* Same for linebreaks */ |
| 193 | static unsigned char ascii_linebreak[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 194 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 195 | /* 0x000A, * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 196 | /* 0x000B, * LINE TABULATION */ |
| 197 | /* 0x000C, * FORM FEED */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 198 | /* 0x000D, * CARRIAGE RETURN */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 199 | 0, 0, 1, 1, 1, 1, 0, 0, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 200 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 201 | /* 0x001C, * FILE SEPARATOR */ |
| 202 | /* 0x001D, * GROUP SEPARATOR */ |
| 203 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 204 | 0, 0, 0, 0, 1, 1, 1, 0, |
| 205 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 206 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 207 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 208 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 209 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 210 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 211 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 212 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 213 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 214 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 215 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 216 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 217 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 218 | }; |
| 219 | |
Ezio Melotti | 48a2f8f | 2011-09-29 00:18:19 +0300 | [diff] [blame] | 220 | /* The max unicode value is always 0x10FFFF while using the PEP-393 API. |
| 221 | 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] | 222 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 223 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 224 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 225 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 226 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 227 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 228 | /* This is actually an illegal character, so it should |
| 229 | not be passed to unichr. */ |
| 230 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 231 | #endif |
| 232 | } |
| 233 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 234 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 235 | |
| 236 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 237 | to keep things simple, we use a single bitmask, using the least 5 |
| 238 | bits from each unicode characters as the bit index. */ |
| 239 | |
| 240 | /* the linebreak mask is set up by Unicode_Init below */ |
| 241 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 242 | #if LONG_BIT >= 128 |
| 243 | #define BLOOM_WIDTH 128 |
| 244 | #elif LONG_BIT >= 64 |
| 245 | #define BLOOM_WIDTH 64 |
| 246 | #elif LONG_BIT >= 32 |
| 247 | #define BLOOM_WIDTH 32 |
| 248 | #else |
| 249 | #error "LONG_BIT is smaller than 32" |
| 250 | #endif |
| 251 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 252 | #define BLOOM_MASK unsigned long |
| 253 | |
| 254 | static BLOOM_MASK bloom_linebreak; |
| 255 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 256 | #define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
| 257 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 258 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 259 | #define BLOOM_LINEBREAK(ch) \ |
| 260 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 261 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 262 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 263 | Py_LOCAL_INLINE(BLOOM_MASK) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 264 | make_bloom_mask(int kind, void* ptr, Py_ssize_t len) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 265 | { |
| 266 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 267 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 268 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 269 | Py_ssize_t i; |
| 270 | |
| 271 | mask = 0; |
| 272 | for (i = 0; i < len; i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 273 | BLOOM_ADD(mask, PyUnicode_READ(kind, ptr, i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 274 | |
| 275 | return mask; |
| 276 | } |
| 277 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 278 | #define BLOOM_MEMBER(mask, chr, str) \ |
| 279 | (BLOOM(mask, chr) \ |
| 280 | && (PyUnicode_FindChar(str, chr, 0, PyUnicode_GET_LENGTH(str), 1) >= 0)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 281 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 282 | /* --- Unicode Object ----------------------------------------------------- */ |
| 283 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 284 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 285 | fixup(PyUnicodeObject *self, Py_UCS4 (*fixfct)(PyUnicodeObject *s)); |
| 286 | |
| 287 | Py_LOCAL_INLINE(char *) findchar(void *s, int kind, |
| 288 | Py_ssize_t size, Py_UCS4 ch, |
| 289 | int direction) |
| 290 | { |
| 291 | /* like wcschr, but doesn't stop at NULL characters */ |
| 292 | Py_ssize_t i; |
| 293 | if (direction == 1) { |
| 294 | for(i = 0; i < size; i++) |
| 295 | if (PyUnicode_READ(kind, s, i) == ch) |
| 296 | return (char*)s + PyUnicode_KIND_SIZE(kind, i); |
| 297 | } |
| 298 | else { |
| 299 | for(i = size-1; i >= 0; i--) |
| 300 | if (PyUnicode_READ(kind, s, i) == ch) |
| 301 | return (char*)s + PyUnicode_KIND_SIZE(kind, i); |
| 302 | } |
| 303 | return NULL; |
| 304 | } |
| 305 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 306 | static int |
| 307 | unicode_resize(register PyUnicodeObject *unicode, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 308 | Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 309 | { |
| 310 | void *oldstr; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 311 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 312 | /* Resizing is only supported for old unicode objects. */ |
| 313 | assert(!PyUnicode_IS_COMPACT(unicode)); |
| 314 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 315 | |
| 316 | /* ... and only if they have not been readied yet, because |
| 317 | callees usually rely on the wstr representation when resizing. */ |
| 318 | assert(unicode->data.any == NULL); |
| 319 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 320 | /* Shortcut if there's nothing much to do. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 321 | if (_PyUnicode_WSTR_LENGTH(unicode) == length) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 322 | goto reset; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 323 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 324 | /* Resizing shared object (unicode_empty or single character |
| 325 | objects) in-place is not allowed. Use PyUnicode_Resize() |
| 326 | instead ! */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 327 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 328 | if (unicode == unicode_empty || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 329 | (_PyUnicode_WSTR_LENGTH(unicode) == 1 && |
| 330 | _PyUnicode_WSTR(unicode)[0] < 256U && |
| 331 | unicode_latin1[_PyUnicode_WSTR(unicode)[0]] == unicode)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 332 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 333 | "can't resize shared str objects"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 334 | return -1; |
| 335 | } |
| 336 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 337 | /* We allocate one more byte to make sure the string is Ux0000 terminated. |
| 338 | The overallocation is also used by fastsearch, which assumes that it's |
| 339 | safe to look at str[length] (without making any assumptions about what |
| 340 | it contains). */ |
| 341 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 342 | oldstr = _PyUnicode_WSTR(unicode); |
| 343 | _PyUnicode_WSTR(unicode) = PyObject_REALLOC(_PyUnicode_WSTR(unicode), |
| 344 | sizeof(Py_UNICODE) * (length + 1)); |
| 345 | if (!_PyUnicode_WSTR(unicode)) { |
| 346 | _PyUnicode_WSTR(unicode) = (Py_UNICODE *)oldstr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 347 | PyErr_NoMemory(); |
| 348 | return -1; |
| 349 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 350 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 351 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 352 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 353 | reset: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 354 | if (unicode->data.any != NULL) { |
| 355 | PyObject_FREE(unicode->data.any); |
| 356 | if (unicode->_base.utf8 && unicode->_base.utf8 != unicode->data.any) { |
| 357 | PyObject_FREE(unicode->_base.utf8); |
| 358 | } |
| 359 | unicode->_base.utf8 = NULL; |
| 360 | unicode->_base.utf8_length = 0; |
| 361 | unicode->data.any = NULL; |
| 362 | _PyUnicode_LENGTH(unicode) = 0; |
| 363 | _PyUnicode_STATE(unicode).interned = _PyUnicode_STATE(unicode).interned; |
| 364 | _PyUnicode_STATE(unicode).kind = PyUnicode_WCHAR_KIND; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 365 | } |
Victor Stinner | b15d4d8 | 2011-09-28 23:59:20 +0200 | [diff] [blame] | 366 | _PyUnicode_DIRTY(unicode); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 367 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | /* 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] | 372 | Ux0000 terminated; some code (e.g. new_identifier) |
| 373 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 374 | |
| 375 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 376 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 377 | |
| 378 | */ |
| 379 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 380 | #ifdef Py_DEBUG |
| 381 | int unicode_old_new_calls = 0; |
| 382 | #endif |
| 383 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 384 | static PyUnicodeObject * |
| 385 | _PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 386 | { |
| 387 | register PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 388 | size_t new_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 389 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 390 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 391 | if (length == 0 && unicode_empty != NULL) { |
| 392 | Py_INCREF(unicode_empty); |
| 393 | return unicode_empty; |
| 394 | } |
| 395 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 396 | /* Ensure we won't overflow the size. */ |
| 397 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 398 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 399 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 400 | if (length < 0) { |
| 401 | PyErr_SetString(PyExc_SystemError, |
| 402 | "Negative size passed to _PyUnicode_New"); |
| 403 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 406 | #ifdef Py_DEBUG |
| 407 | ++unicode_old_new_calls; |
| 408 | #endif |
| 409 | |
| 410 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
| 411 | if (unicode == NULL) |
| 412 | return NULL; |
| 413 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 414 | _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); |
| 415 | if (!_PyUnicode_WSTR(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 416 | PyErr_NoMemory(); |
| 417 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 418 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 419 | |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 420 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 421 | * the caller fails before initializing str -- unicode_resize() |
| 422 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 423 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 424 | * We don't want unicode_resize to read uninitialized memory in |
| 425 | * that case. |
| 426 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 427 | _PyUnicode_WSTR(unicode)[0] = 0; |
| 428 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 429 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 430 | _PyUnicode_HASH(unicode) = -1; |
| 431 | _PyUnicode_STATE(unicode).interned = 0; |
| 432 | _PyUnicode_STATE(unicode).kind = 0; |
| 433 | _PyUnicode_STATE(unicode).compact = 0; |
| 434 | _PyUnicode_STATE(unicode).ready = 0; |
| 435 | _PyUnicode_STATE(unicode).ascii = 0; |
| 436 | unicode->data.any = NULL; |
| 437 | _PyUnicode_LENGTH(unicode) = 0; |
| 438 | unicode->_base.utf8 = NULL; |
| 439 | unicode->_base.utf8_length = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 440 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 441 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 442 | onError: |
Amaury Forgeot d'Arc | 7888d08 | 2008-08-01 01:06:32 +0000 | [diff] [blame] | 443 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
| 444 | _Py_DEC_REFTOTAL; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 445 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 446 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 447 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 450 | #ifdef Py_DEBUG |
| 451 | int unicode_new_new_calls = 0; |
| 452 | |
| 453 | /* Functions wrapping macros for use in debugger */ |
| 454 | char *_PyUnicode_utf8(void *unicode){ |
| 455 | return _PyUnicode_UTF8(unicode); |
| 456 | } |
| 457 | |
| 458 | void *_PyUnicode_compact_data(void *unicode) { |
| 459 | return _PyUnicode_COMPACT_DATA(unicode); |
| 460 | } |
| 461 | void *_PyUnicode_data(void *unicode){ |
| 462 | printf("obj %p\n", unicode); |
| 463 | printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); |
| 464 | printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); |
| 465 | printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); |
| 466 | printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); |
| 467 | printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); |
| 468 | return PyUnicode_DATA(unicode); |
| 469 | } |
| 470 | #endif |
| 471 | |
| 472 | PyObject * |
| 473 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) |
| 474 | { |
| 475 | PyObject *obj; |
| 476 | PyCompactUnicodeObject *unicode; |
| 477 | void *data; |
| 478 | int kind_state; |
| 479 | int is_sharing = 0, is_ascii = 0; |
| 480 | Py_ssize_t char_size; |
| 481 | Py_ssize_t struct_size; |
| 482 | |
| 483 | /* Optimization for empty strings */ |
| 484 | if (size == 0 && unicode_empty != NULL) { |
| 485 | Py_INCREF(unicode_empty); |
| 486 | return (PyObject *)unicode_empty; |
| 487 | } |
| 488 | |
| 489 | #ifdef Py_DEBUG |
| 490 | ++unicode_new_new_calls; |
| 491 | #endif |
| 492 | |
| 493 | struct_size = sizeof(PyCompactUnicodeObject); |
| 494 | if (maxchar < 128) { |
| 495 | kind_state = PyUnicode_1BYTE_KIND; |
| 496 | char_size = 1; |
| 497 | is_ascii = 1; |
| 498 | struct_size = sizeof(PyASCIIObject); |
| 499 | } |
| 500 | else if (maxchar < 256) { |
| 501 | kind_state = PyUnicode_1BYTE_KIND; |
| 502 | char_size = 1; |
| 503 | } |
| 504 | else if (maxchar < 65536) { |
| 505 | kind_state = PyUnicode_2BYTE_KIND; |
| 506 | char_size = 2; |
| 507 | if (sizeof(wchar_t) == 2) |
| 508 | is_sharing = 1; |
| 509 | } |
| 510 | else { |
| 511 | kind_state = PyUnicode_4BYTE_KIND; |
| 512 | char_size = 4; |
| 513 | if (sizeof(wchar_t) == 4) |
| 514 | is_sharing = 1; |
| 515 | } |
| 516 | |
| 517 | /* Ensure we won't overflow the size. */ |
| 518 | if (size < 0) { |
| 519 | PyErr_SetString(PyExc_SystemError, |
| 520 | "Negative size passed to PyUnicode_New"); |
| 521 | return NULL; |
| 522 | } |
| 523 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) |
| 524 | return PyErr_NoMemory(); |
| 525 | |
| 526 | /* Duplicated allocation code from _PyObject_New() instead of a call to |
| 527 | * PyObject_New() so we are able to allocate space for the object and |
| 528 | * it's data buffer. |
| 529 | */ |
| 530 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); |
| 531 | if (obj == NULL) |
| 532 | return PyErr_NoMemory(); |
| 533 | obj = PyObject_INIT(obj, &PyUnicode_Type); |
| 534 | if (obj == NULL) |
| 535 | return NULL; |
| 536 | |
| 537 | unicode = (PyCompactUnicodeObject *)obj; |
| 538 | if (is_ascii) |
| 539 | data = ((PyASCIIObject*)obj) + 1; |
| 540 | else |
| 541 | data = unicode + 1; |
| 542 | _PyUnicode_LENGTH(unicode) = size; |
| 543 | _PyUnicode_HASH(unicode) = -1; |
| 544 | _PyUnicode_STATE(unicode).interned = 0; |
| 545 | _PyUnicode_STATE(unicode).kind = kind_state; |
| 546 | _PyUnicode_STATE(unicode).compact = 1; |
| 547 | _PyUnicode_STATE(unicode).ready = 1; |
| 548 | _PyUnicode_STATE(unicode).ascii = is_ascii; |
| 549 | if (is_ascii) { |
| 550 | ((char*)data)[size] = 0; |
| 551 | _PyUnicode_WSTR(unicode) = NULL; |
| 552 | } |
| 553 | else if (kind_state == PyUnicode_1BYTE_KIND) { |
| 554 | ((char*)data)[size] = 0; |
| 555 | _PyUnicode_WSTR(unicode) = NULL; |
| 556 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 557 | unicode->utf8_length = 0; |
| 558 | unicode->utf8 = NULL; |
| 559 | } |
| 560 | else { |
| 561 | unicode->utf8 = NULL; |
| 562 | if (kind_state == PyUnicode_2BYTE_KIND) |
| 563 | ((Py_UCS2*)data)[size] = 0; |
| 564 | else /* kind_state == PyUnicode_4BYTE_KIND */ |
| 565 | ((Py_UCS4*)data)[size] = 0; |
| 566 | if (is_sharing) { |
| 567 | _PyUnicode_WSTR_LENGTH(unicode) = size; |
| 568 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; |
| 569 | } |
| 570 | else { |
| 571 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 572 | _PyUnicode_WSTR(unicode) = NULL; |
| 573 | } |
| 574 | } |
| 575 | return obj; |
| 576 | } |
| 577 | |
| 578 | #if SIZEOF_WCHAR_T == 2 |
| 579 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this |
| 580 | will decode surrogate pairs, the other conversions are implemented as macros |
| 581 | for efficency. |
| 582 | |
| 583 | This function assumes that unicode can hold one more code point than wstr |
| 584 | characters for a terminating null character. */ |
| 585 | static int |
| 586 | unicode_convert_wchar_to_ucs4(const wchar_t *begin, const wchar_t *end, |
| 587 | PyUnicodeObject *unicode) |
| 588 | { |
| 589 | const wchar_t *iter; |
| 590 | Py_UCS4 *ucs4_out; |
| 591 | |
| 592 | assert(unicode && PyUnicode_Check(unicode)); |
| 593 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 594 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); |
| 595 | |
| 596 | for (iter = begin; iter < end; ) { |
| 597 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + |
| 598 | _PyUnicode_GET_LENGTH(unicode))); |
| 599 | if (*iter >= 0xD800 && *iter <= 0xDBFF |
| 600 | && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) |
| 601 | { |
| 602 | *ucs4_out++ = (((iter[0] & 0x3FF)<<10) | (iter[1] & 0x3FF)) + 0x10000; |
| 603 | iter += 2; |
| 604 | } |
| 605 | else { |
| 606 | *ucs4_out++ = *iter; |
| 607 | iter++; |
| 608 | } |
| 609 | } |
| 610 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 611 | _PyUnicode_GET_LENGTH(unicode))); |
| 612 | |
| 613 | return 0; |
| 614 | } |
| 615 | #endif |
| 616 | |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 617 | Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 618 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 619 | PyObject *from, Py_ssize_t from_start, |
| 620 | Py_ssize_t how_many) |
| 621 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 622 | unsigned int from_kind, to_kind; |
| 623 | void *from_data, *to_data; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 624 | |
Victor Stinner | b153615 | 2011-09-30 02:26:10 +0200 | [diff] [blame] | 625 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 626 | PyErr_BadInternalCall(); |
| 627 | return -1; |
| 628 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 629 | |
| 630 | if (PyUnicode_READY(from)) |
| 631 | return -1; |
| 632 | if (PyUnicode_READY(to)) |
| 633 | return -1; |
| 634 | |
Victor Stinner | ff9e50f | 2011-09-28 22:17:19 +0200 | [diff] [blame] | 635 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 636 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
| 637 | PyErr_Format(PyExc_ValueError, |
| 638 | "Cannot write %zi characters at %zi " |
| 639 | "in a string of %zi characters", |
| 640 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 641 | return -1; |
| 642 | } |
Victor Stinner | f5ca1a2 | 2011-09-28 23:54:59 +0200 | [diff] [blame] | 643 | if (how_many == 0) |
| 644 | return 0; |
| 645 | |
| 646 | if (Py_REFCNT(to) != 1) { |
| 647 | PyErr_SetString(PyExc_ValueError, |
| 648 | "Cannot modify a string having more than 1 reference"); |
| 649 | return -1; |
| 650 | } |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 651 | _PyUnicode_DIRTY(to); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 652 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 653 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 654 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 655 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 656 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 657 | |
| 658 | if (from_kind == to_kind) { |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 659 | /* fast path */ |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 660 | Py_MEMCPY((char*)to_data |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 661 | + PyUnicode_KIND_SIZE(to_kind, to_start), |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 662 | (char*)from_data |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 663 | + PyUnicode_KIND_SIZE(from_kind, from_start), |
| 664 | PyUnicode_KIND_SIZE(to_kind, how_many)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 665 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 666 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 667 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 668 | { |
| 669 | _PyUnicode_CONVERT_BYTES( |
| 670 | Py_UCS1, Py_UCS2, |
| 671 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 672 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 673 | PyUnicode_2BYTE_DATA(to) + to_start |
| 674 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 675 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 676 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 677 | && to_kind == PyUnicode_4BYTE_KIND) |
| 678 | { |
| 679 | _PyUnicode_CONVERT_BYTES( |
| 680 | Py_UCS1, Py_UCS4, |
| 681 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 682 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 683 | PyUnicode_4BYTE_DATA(to) + to_start |
| 684 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 685 | } |
| 686 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 687 | && to_kind == PyUnicode_4BYTE_KIND) |
| 688 | { |
| 689 | _PyUnicode_CONVERT_BYTES( |
| 690 | Py_UCS2, Py_UCS4, |
| 691 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 692 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 693 | PyUnicode_4BYTE_DATA(to) + to_start |
| 694 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 695 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 696 | else { |
| 697 | int invalid_kinds; |
| 698 | if (from_kind > to_kind) { |
| 699 | /* slow path to check for character overflow */ |
| 700 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 701 | Py_UCS4 ch, maxchar; |
| 702 | Py_ssize_t i; |
| 703 | |
| 704 | maxchar = 0; |
| 705 | invalid_kinds = 0; |
| 706 | for (i=0; i < how_many; i++) { |
| 707 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 708 | if (ch > maxchar) { |
| 709 | maxchar = ch; |
| 710 | if (maxchar > to_maxchar) { |
| 711 | invalid_kinds = 1; |
| 712 | break; |
| 713 | } |
| 714 | } |
| 715 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 716 | } |
| 717 | } |
| 718 | else |
| 719 | invalid_kinds = 1; |
| 720 | if (invalid_kinds) { |
| 721 | PyErr_Format(PyExc_ValueError, |
| 722 | "Cannot copy UCS%u characters " |
| 723 | "into a string of UCS%u characters", |
| 724 | 1 << (from_kind - 1), |
| 725 | 1 << (to_kind -1)); |
| 726 | return -1; |
| 727 | } |
| 728 | } |
| 729 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 730 | } |
| 731 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 732 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 733 | correct string length can be computed before converting a string to UCS4. |
| 734 | This function counts single surrogates as a character and not as a pair. |
| 735 | |
| 736 | Return 0 on success, or -1 on error. */ |
| 737 | static int |
| 738 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 739 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 740 | { |
| 741 | const wchar_t *iter; |
| 742 | |
| 743 | if (num_surrogates == NULL || maxchar == NULL) { |
| 744 | PyErr_SetString(PyExc_SystemError, |
| 745 | "unexpected NULL arguments to " |
| 746 | "PyUnicode_FindMaxCharAndNumSurrogatePairs"); |
| 747 | return -1; |
| 748 | } |
| 749 | |
| 750 | *num_surrogates = 0; |
| 751 | *maxchar = 0; |
| 752 | |
| 753 | for (iter = begin; iter < end; ) { |
| 754 | if (*iter > *maxchar) |
| 755 | *maxchar = *iter; |
| 756 | #if SIZEOF_WCHAR_T == 2 |
| 757 | if (*iter >= 0xD800 && *iter <= 0xDBFF |
| 758 | && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) |
| 759 | { |
| 760 | Py_UCS4 surrogate_val; |
| 761 | surrogate_val = (((iter[0] & 0x3FF)<<10) |
| 762 | | (iter[1] & 0x3FF)) + 0x10000; |
| 763 | ++(*num_surrogates); |
| 764 | if (surrogate_val > *maxchar) |
| 765 | *maxchar = surrogate_val; |
| 766 | iter += 2; |
| 767 | } |
| 768 | else |
| 769 | iter++; |
| 770 | #else |
| 771 | iter++; |
| 772 | #endif |
| 773 | } |
| 774 | return 0; |
| 775 | } |
| 776 | |
| 777 | #ifdef Py_DEBUG |
| 778 | int unicode_ready_calls = 0; |
| 779 | #endif |
| 780 | |
| 781 | int |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 782 | _PyUnicode_Ready(PyObject *obj) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 783 | { |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 784 | PyUnicodeObject *unicode = (PyUnicodeObject *)obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 785 | wchar_t *end; |
| 786 | Py_UCS4 maxchar = 0; |
| 787 | Py_ssize_t num_surrogates; |
| 788 | #if SIZEOF_WCHAR_T == 2 |
| 789 | Py_ssize_t length_wo_surrogates; |
| 790 | #endif |
| 791 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 792 | /* _PyUnicode_Ready() is only intented for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 793 | strings were created using _PyObject_New() and where no canonical |
| 794 | representation (the str field) has been set yet aka strings |
| 795 | which are not yet ready. */ |
| 796 | assert(PyUnicode_Check(obj)); |
| 797 | assert(!PyUnicode_IS_READY(obj)); |
| 798 | assert(!PyUnicode_IS_COMPACT(obj)); |
| 799 | assert(_PyUnicode_KIND(obj) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 800 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 801 | assert(unicode->data.any == NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 802 | assert(unicode->_base.utf8 == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 803 | /* Actually, it should neither be interned nor be anything else: */ |
| 804 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 805 | |
| 806 | #ifdef Py_DEBUG |
| 807 | ++unicode_ready_calls; |
| 808 | #endif |
| 809 | |
| 810 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 811 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 812 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 813 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 814 | |
| 815 | if (maxchar < 256) { |
| 816 | unicode->data.any = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 817 | if (!unicode->data.any) { |
| 818 | PyErr_NoMemory(); |
| 819 | return -1; |
| 820 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 821 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 822 | _PyUnicode_WSTR(unicode), end, |
| 823 | PyUnicode_1BYTE_DATA(unicode)); |
| 824 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 825 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 826 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 827 | if (maxchar < 128) { |
| 828 | unicode->_base.utf8 = unicode->data.any; |
| 829 | unicode->_base.utf8_length = _PyUnicode_WSTR_LENGTH(unicode); |
| 830 | } |
| 831 | else { |
| 832 | unicode->_base.utf8 = NULL; |
| 833 | unicode->_base.utf8_length = 0; |
| 834 | } |
| 835 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 836 | _PyUnicode_WSTR(unicode) = NULL; |
| 837 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 838 | } |
| 839 | /* In this case we might have to convert down from 4-byte native |
| 840 | wchar_t to 2-byte unicode. */ |
| 841 | else if (maxchar < 65536) { |
| 842 | assert(num_surrogates == 0 && |
| 843 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 844 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 845 | #if SIZEOF_WCHAR_T == 2 |
| 846 | /* We can share representations and are done. */ |
| 847 | unicode->data.any = _PyUnicode_WSTR(unicode); |
| 848 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 849 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 850 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
| 851 | unicode->_base.utf8 = NULL; |
| 852 | unicode->_base.utf8_length = 0; |
| 853 | #else |
| 854 | /* sizeof(wchar_t) == 4 */ |
| 855 | unicode->data.any = PyObject_MALLOC( |
| 856 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
| 857 | if (!unicode->data.any) { |
| 858 | PyErr_NoMemory(); |
| 859 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 860 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 861 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 862 | _PyUnicode_WSTR(unicode), end, |
| 863 | PyUnicode_2BYTE_DATA(unicode)); |
| 864 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 865 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 866 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
| 867 | unicode->_base.utf8 = NULL; |
| 868 | unicode->_base.utf8_length = 0; |
| 869 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 870 | _PyUnicode_WSTR(unicode) = NULL; |
| 871 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 872 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 873 | } |
| 874 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 875 | else { |
| 876 | #if SIZEOF_WCHAR_T == 2 |
| 877 | /* in case the native representation is 2-bytes, we need to allocate a |
| 878 | new normalized 4-byte version. */ |
| 879 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
| 880 | unicode->data.any = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 881 | if (!unicode->data.any) { |
| 882 | PyErr_NoMemory(); |
| 883 | return -1; |
| 884 | } |
| 885 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 886 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 887 | unicode->_base.utf8 = NULL; |
| 888 | unicode->_base.utf8_length = 0; |
| 889 | if (unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, |
| 890 | unicode) < 0) { |
| 891 | assert(0 && "ConvertWideCharToUCS4 failed"); |
| 892 | return -1; |
| 893 | } |
| 894 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 895 | _PyUnicode_WSTR(unicode) = NULL; |
| 896 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 897 | #else |
| 898 | assert(num_surrogates == 0); |
| 899 | |
| 900 | unicode->data.any = _PyUnicode_WSTR(unicode); |
| 901 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 902 | unicode->_base.utf8 = NULL; |
| 903 | unicode->_base.utf8_length = 0; |
| 904 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 905 | #endif |
| 906 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 907 | } |
| 908 | _PyUnicode_STATE(unicode).ready = 1; |
| 909 | return 0; |
| 910 | } |
| 911 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 912 | static void |
| 913 | unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 914 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 915 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 916 | case SSTATE_NOT_INTERNED: |
| 917 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 918 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 919 | case SSTATE_INTERNED_MORTAL: |
| 920 | /* revive dead object temporarily for DelItem */ |
| 921 | Py_REFCNT(unicode) = 3; |
| 922 | if (PyDict_DelItem(interned, (PyObject *)unicode) != 0) |
| 923 | Py_FatalError( |
| 924 | "deletion of interned string failed"); |
| 925 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 926 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 927 | case SSTATE_INTERNED_IMMORTAL: |
| 928 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 929 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 930 | default: |
| 931 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 932 | } |
| 933 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 934 | if (_PyUnicode_WSTR(unicode) && |
| 935 | (!PyUnicode_IS_READY(unicode) || |
| 936 | _PyUnicode_WSTR(unicode) != PyUnicode_DATA(unicode))) |
| 937 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
| 938 | if (_PyUnicode_UTF8(unicode) && _PyUnicode_UTF8(unicode) != PyUnicode_DATA(unicode)) |
| 939 | PyObject_DEL(unicode->_base.utf8); |
| 940 | |
| 941 | if (PyUnicode_IS_COMPACT(unicode)) { |
| 942 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 943 | } |
| 944 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 945 | if (unicode->data.any) |
| 946 | PyObject_DEL(unicode->data.any); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 947 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 948 | } |
| 949 | } |
| 950 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 951 | static int |
| 952 | _PyUnicode_Resize(PyUnicodeObject **unicode, Py_ssize_t length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 953 | { |
| 954 | register PyUnicodeObject *v; |
| 955 | |
| 956 | /* Argument checks */ |
| 957 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 958 | PyErr_BadInternalCall(); |
| 959 | return -1; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 960 | } |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 961 | v = *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 962 | if (v == NULL || !PyUnicode_Check(v) || Py_REFCNT(v) != 1 || length < 0 || |
| 963 | PyUnicode_IS_COMPACT(v) || _PyUnicode_WSTR(v) == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 964 | PyErr_BadInternalCall(); |
| 965 | return -1; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | /* Resizing unicode_empty and single character objects is not |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 969 | possible since these are being shared. |
| 970 | The same goes for new-representation unicode objects or objects which |
| 971 | have already been readied. |
| 972 | For these, we simply return a fresh copy with the same Unicode content. |
| 973 | */ |
| 974 | if ((_PyUnicode_WSTR_LENGTH(v) != length && |
| 975 | (v == unicode_empty || _PyUnicode_WSTR_LENGTH(v) == 1)) || |
| 976 | PyUnicode_IS_COMPACT(v) || v->data.any) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 977 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 978 | if (w == NULL) |
| 979 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 980 | Py_UNICODE_COPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(v), |
| 981 | length < _PyUnicode_WSTR_LENGTH(v) ? length : _PyUnicode_WSTR_LENGTH(v)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 982 | Py_DECREF(*unicode); |
| 983 | *unicode = w; |
| 984 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 988 | objects, since we can modify them in-place. */ |
| 989 | return unicode_resize(v, length); |
| 990 | } |
| 991 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 992 | int |
| 993 | PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 994 | { |
| 995 | return _PyUnicode_Resize((PyUnicodeObject **)unicode, length); |
| 996 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 997 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 998 | static PyObject* |
| 999 | get_latin1_char(unsigned char ch) |
| 1000 | { |
| 1001 | PyUnicodeObject *unicode = unicode_latin1[ch]; |
| 1002 | if (!unicode) { |
| 1003 | unicode = (PyUnicodeObject *)PyUnicode_New(1, ch); |
| 1004 | if (!unicode) |
| 1005 | return NULL; |
| 1006 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
| 1007 | unicode_latin1[ch] = unicode; |
| 1008 | } |
| 1009 | Py_INCREF(unicode); |
| 1010 | return (PyObject *)unicode; |
| 1011 | } |
| 1012 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1013 | PyObject * |
| 1014 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1015 | { |
| 1016 | PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1017 | Py_UCS4 maxchar = 0; |
| 1018 | Py_ssize_t num_surrogates; |
| 1019 | |
| 1020 | if (u == NULL) |
| 1021 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1022 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1023 | /* If the Unicode data is known at construction time, we can apply |
| 1024 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1025 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1026 | /* Optimization for empty strings */ |
| 1027 | if (size == 0 && unicode_empty != NULL) { |
| 1028 | Py_INCREF(unicode_empty); |
| 1029 | return (PyObject *)unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1030 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1031 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1032 | /* Single character Unicode objects in the Latin-1 range are |
| 1033 | shared when using this constructor */ |
| 1034 | if (size == 1 && *u < 256) |
| 1035 | return get_latin1_char((unsigned char)*u); |
| 1036 | |
| 1037 | /* If not empty and not single character, copy the Unicode data |
| 1038 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1039 | if (find_maxchar_surrogates(u, u + size, |
| 1040 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1041 | return NULL; |
| 1042 | |
| 1043 | unicode = (PyUnicodeObject *) PyUnicode_New(size - num_surrogates, |
| 1044 | maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1045 | if (!unicode) |
| 1046 | return NULL; |
| 1047 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1048 | switch (PyUnicode_KIND(unicode)) { |
| 1049 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1050 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1051 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1052 | break; |
| 1053 | case PyUnicode_2BYTE_KIND: |
| 1054 | #if Py_UNICODE_SIZE == 2 |
| 1055 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1056 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1057 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1058 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1059 | #endif |
| 1060 | break; |
| 1061 | case PyUnicode_4BYTE_KIND: |
| 1062 | #if SIZEOF_WCHAR_T == 2 |
| 1063 | /* This is the only case which has to process surrogates, thus |
| 1064 | a simple copy loop is not enough and we need a function. */ |
| 1065 | if (unicode_convert_wchar_to_ucs4(u, u + size, unicode) < 0) { |
| 1066 | Py_DECREF(unicode); |
| 1067 | return NULL; |
| 1068 | } |
| 1069 | #else |
| 1070 | assert(num_surrogates == 0); |
| 1071 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1072 | #endif |
| 1073 | break; |
| 1074 | default: |
| 1075 | assert(0 && "Impossible state"); |
| 1076 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1077 | |
| 1078 | return (PyObject *)unicode; |
| 1079 | } |
| 1080 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1081 | PyObject * |
| 1082 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1083 | { |
| 1084 | PyUnicodeObject *unicode; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1085 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1086 | if (size < 0) { |
| 1087 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1088 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1089 | return NULL; |
| 1090 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1091 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1092 | /* 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] | 1093 | some optimizations which share commonly used objects. |
| 1094 | Also, this means the input must be UTF-8, so fall back to the |
| 1095 | UTF-8 decoder at the end. */ |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1096 | if (u != NULL) { |
| 1097 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1098 | /* Optimization for empty strings */ |
| 1099 | if (size == 0 && unicode_empty != NULL) { |
| 1100 | Py_INCREF(unicode_empty); |
| 1101 | return (PyObject *)unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1102 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1103 | |
| 1104 | /* Single characters are shared when using this constructor. |
| 1105 | Restrict to ASCII, since the input must be UTF-8. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1106 | if (size == 1 && Py_CHARMASK(*u) < 128) |
| 1107 | return get_latin1_char(Py_CHARMASK(*u)); |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 1108 | |
| 1109 | return PyUnicode_DecodeUTF8(u, size, NULL); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
Walter Dörwald | 5550731 | 2007-05-18 13:12:10 +0000 | [diff] [blame] | 1112 | unicode = _PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1113 | if (!unicode) |
| 1114 | return NULL; |
| 1115 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1116 | return (PyObject *)unicode; |
| 1117 | } |
| 1118 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1119 | PyObject * |
| 1120 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1121 | { |
| 1122 | size_t size = strlen(u); |
| 1123 | if (size > PY_SSIZE_T_MAX) { |
| 1124 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1125 | return NULL; |
| 1126 | } |
| 1127 | |
| 1128 | return PyUnicode_FromStringAndSize(u, size); |
| 1129 | } |
| 1130 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1131 | static PyObject* |
| 1132 | _PyUnicode_FromUCS1(const unsigned char* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1133 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1134 | PyObject *res; |
| 1135 | unsigned char max = 127; |
| 1136 | Py_ssize_t i; |
| 1137 | for (i = 0; i < size; i++) { |
| 1138 | if (u[i] & 0x80) { |
| 1139 | max = 255; |
| 1140 | break; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1141 | } |
| 1142 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1143 | res = PyUnicode_New(size, max); |
| 1144 | if (!res) |
| 1145 | return NULL; |
| 1146 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
| 1147 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1150 | static PyObject* |
| 1151 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1152 | { |
| 1153 | PyObject *res; |
| 1154 | Py_UCS2 max = 0; |
| 1155 | Py_ssize_t i; |
| 1156 | for (i = 0; i < size; i++) |
| 1157 | if (u[i] > max) |
| 1158 | max = u[i]; |
| 1159 | res = PyUnicode_New(size, max); |
| 1160 | if (!res) |
| 1161 | return NULL; |
| 1162 | if (max >= 256) |
| 1163 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
| 1164 | else |
| 1165 | for (i = 0; i < size; i++) |
| 1166 | PyUnicode_1BYTE_DATA(res)[i] = (Py_UCS1)u[i]; |
| 1167 | return res; |
| 1168 | } |
| 1169 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1170 | static PyObject* |
| 1171 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1172 | { |
| 1173 | PyObject *res; |
| 1174 | Py_UCS4 max = 0; |
| 1175 | Py_ssize_t i; |
| 1176 | for (i = 0; i < size; i++) |
| 1177 | if (u[i] > max) |
| 1178 | max = u[i]; |
| 1179 | res = PyUnicode_New(size, max); |
| 1180 | if (!res) |
| 1181 | return NULL; |
| 1182 | if (max >= 0x10000) |
| 1183 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
| 1184 | else { |
| 1185 | int kind = PyUnicode_KIND(res); |
| 1186 | void *data = PyUnicode_DATA(res); |
| 1187 | for (i = 0; i < size; i++) |
| 1188 | PyUnicode_WRITE(kind, data, i, u[i]); |
| 1189 | } |
| 1190 | return res; |
| 1191 | } |
| 1192 | |
| 1193 | PyObject* |
| 1194 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 1195 | { |
| 1196 | switch(kind) { |
| 1197 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1198 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1199 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1200 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1201 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1202 | return _PyUnicode_FromUCS4(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1203 | } |
| 1204 | assert(0); |
| 1205 | return NULL; |
| 1206 | } |
| 1207 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1208 | PyObject* |
| 1209 | PyUnicode_Copy(PyObject *unicode) |
| 1210 | { |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1211 | Py_ssize_t size; |
| 1212 | PyObject *copy; |
| 1213 | void *data; |
| 1214 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1215 | if (!PyUnicode_Check(unicode)) { |
| 1216 | PyErr_BadInternalCall(); |
| 1217 | return NULL; |
| 1218 | } |
| 1219 | if (PyUnicode_READY(unicode)) |
| 1220 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1221 | |
| 1222 | size = PyUnicode_GET_LENGTH(unicode); |
| 1223 | copy = PyUnicode_New(size, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 1224 | if (!copy) |
| 1225 | return NULL; |
| 1226 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 1227 | |
| 1228 | data = PyUnicode_DATA(unicode); |
| 1229 | switch (PyUnicode_KIND(unicode)) |
| 1230 | { |
| 1231 | case PyUnicode_1BYTE_KIND: |
| 1232 | memcpy(PyUnicode_1BYTE_DATA(copy), data, size); |
| 1233 | break; |
| 1234 | case PyUnicode_2BYTE_KIND: |
| 1235 | memcpy(PyUnicode_2BYTE_DATA(copy), data, sizeof(Py_UCS2) * size); |
| 1236 | break; |
| 1237 | case PyUnicode_4BYTE_KIND: |
| 1238 | memcpy(PyUnicode_4BYTE_DATA(copy), data, sizeof(Py_UCS4) * size); |
| 1239 | break; |
| 1240 | default: |
| 1241 | assert(0); |
| 1242 | break; |
| 1243 | } |
| 1244 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1245 | } |
| 1246 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1247 | |
| 1248 | /* Widen Unicode objects to larger buffers. |
| 1249 | Return NULL if the string is too wide already. */ |
| 1250 | |
| 1251 | void* |
| 1252 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 1253 | { |
| 1254 | Py_ssize_t i; |
| 1255 | Py_ssize_t len = PyUnicode_GET_LENGTH(s); |
| 1256 | void *d = PyUnicode_DATA(s); |
| 1257 | unsigned int skind = PyUnicode_KIND(s); |
| 1258 | if (PyUnicode_KIND(s) >= kind) { |
| 1259 | PyErr_SetString(PyExc_RuntimeError, "invalid widening attempt"); |
| 1260 | return NULL; |
| 1261 | } |
| 1262 | switch(kind) { |
| 1263 | case PyUnicode_2BYTE_KIND: { |
| 1264 | Py_UCS2 *result = PyMem_Malloc(PyUnicode_GET_LENGTH(s) * sizeof(Py_UCS2)); |
| 1265 | if (!result) { |
| 1266 | PyErr_NoMemory(); |
| 1267 | return 0; |
| 1268 | } |
| 1269 | for (i = 0; i < len; i++) |
| 1270 | result[i] = ((Py_UCS1*)d)[i]; |
| 1271 | return result; |
| 1272 | } |
| 1273 | case PyUnicode_4BYTE_KIND: { |
| 1274 | Py_UCS4 *result = PyMem_Malloc(PyUnicode_GET_LENGTH(s) * sizeof(Py_UCS4)); |
| 1275 | if (!result) { |
| 1276 | PyErr_NoMemory(); |
| 1277 | return 0; |
| 1278 | } |
| 1279 | for (i = 0; i < len; i++) |
| 1280 | result[i] = PyUnicode_READ(skind, d, i); |
| 1281 | return result; |
| 1282 | } |
| 1283 | } |
| 1284 | Py_FatalError("invalid kind"); |
| 1285 | return NULL; |
| 1286 | } |
| 1287 | |
| 1288 | static Py_UCS4* |
| 1289 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 1290 | int copy_null) |
| 1291 | { |
| 1292 | int kind; |
| 1293 | void *data; |
| 1294 | Py_ssize_t len, targetlen; |
| 1295 | if (PyUnicode_READY(string) == -1) |
| 1296 | return NULL; |
| 1297 | kind = PyUnicode_KIND(string); |
| 1298 | data = PyUnicode_DATA(string); |
| 1299 | len = PyUnicode_GET_LENGTH(string); |
| 1300 | targetlen = len; |
| 1301 | if (copy_null) |
| 1302 | targetlen++; |
| 1303 | if (!target) { |
| 1304 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 1305 | PyErr_NoMemory(); |
| 1306 | return NULL; |
| 1307 | } |
| 1308 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 1309 | if (!target) { |
| 1310 | PyErr_NoMemory(); |
| 1311 | return NULL; |
| 1312 | } |
| 1313 | } |
| 1314 | else { |
| 1315 | if (targetsize < targetlen) { |
| 1316 | PyErr_Format(PyExc_SystemError, |
| 1317 | "string is longer than the buffer"); |
| 1318 | if (copy_null && 0 < targetsize) |
| 1319 | target[0] = 0; |
| 1320 | return NULL; |
| 1321 | } |
| 1322 | } |
| 1323 | if (kind != PyUnicode_4BYTE_KIND) { |
| 1324 | Py_ssize_t i; |
| 1325 | for (i = 0; i < len; i++) |
| 1326 | target[i] = PyUnicode_READ(kind, data, i); |
| 1327 | } |
| 1328 | else |
| 1329 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
| 1330 | if (copy_null) |
| 1331 | target[len] = 0; |
| 1332 | return target; |
| 1333 | } |
| 1334 | |
| 1335 | Py_UCS4* |
| 1336 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 1337 | int copy_null) |
| 1338 | { |
| 1339 | if (target == NULL || targetsize < 1) { |
| 1340 | PyErr_BadInternalCall(); |
| 1341 | return NULL; |
| 1342 | } |
| 1343 | return as_ucs4(string, target, targetsize, copy_null); |
| 1344 | } |
| 1345 | |
| 1346 | Py_UCS4* |
| 1347 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 1348 | { |
| 1349 | return as_ucs4(string, NULL, 0, 1); |
| 1350 | } |
| 1351 | |
| 1352 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1353 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1354 | PyObject * |
| 1355 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1356 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1357 | if (w == NULL) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 1358 | if (size == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1359 | return PyUnicode_New(0, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1360 | PyErr_BadInternalCall(); |
| 1361 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 1364 | if (size == -1) { |
| 1365 | size = wcslen(w); |
| 1366 | } |
| 1367 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1368 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1371 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1372 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1373 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1374 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 1375 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1376 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1377 | *fmt++ = '%'; |
| 1378 | if (width) { |
| 1379 | if (zeropad) |
| 1380 | *fmt++ = '0'; |
| 1381 | fmt += sprintf(fmt, "%d", width); |
| 1382 | } |
| 1383 | if (precision) |
| 1384 | fmt += sprintf(fmt, ".%d", precision); |
| 1385 | if (longflag) |
| 1386 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1387 | else if (longlongflag) { |
| 1388 | /* longlongflag should only ever be nonzero on machines with |
| 1389 | HAVE_LONG_LONG defined */ |
| 1390 | #ifdef HAVE_LONG_LONG |
| 1391 | char *f = PY_FORMAT_LONG_LONG; |
| 1392 | while (*f) |
| 1393 | *fmt++ = *f++; |
| 1394 | #else |
| 1395 | /* we shouldn't ever get here */ |
| 1396 | assert(0); |
| 1397 | *fmt++ = 'l'; |
| 1398 | #endif |
| 1399 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1400 | else if (size_tflag) { |
| 1401 | char *f = PY_FORMAT_SIZE_T; |
| 1402 | while (*f) |
| 1403 | *fmt++ = *f++; |
| 1404 | } |
| 1405 | *fmt++ = c; |
| 1406 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1407 | } |
| 1408 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1409 | /* helper for PyUnicode_FromFormatV() */ |
| 1410 | |
| 1411 | static const char* |
| 1412 | parse_format_flags(const char *f, |
| 1413 | int *p_width, int *p_precision, |
| 1414 | int *p_longflag, int *p_longlongflag, int *p_size_tflag) |
| 1415 | { |
| 1416 | int width, precision, longflag, longlongflag, size_tflag; |
| 1417 | |
| 1418 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ |
| 1419 | f++; |
| 1420 | width = 0; |
| 1421 | while (Py_ISDIGIT((unsigned)*f)) |
| 1422 | width = (width*10) + *f++ - '0'; |
| 1423 | precision = 0; |
| 1424 | if (*f == '.') { |
| 1425 | f++; |
| 1426 | while (Py_ISDIGIT((unsigned)*f)) |
| 1427 | precision = (precision*10) + *f++ - '0'; |
| 1428 | if (*f == '%') { |
| 1429 | /* "%.3%s" => f points to "3" */ |
| 1430 | f--; |
| 1431 | } |
| 1432 | } |
| 1433 | if (*f == '\0') { |
| 1434 | /* bogus format "%.1" => go backward, f points to "1" */ |
| 1435 | f--; |
| 1436 | } |
| 1437 | if (p_width != NULL) |
| 1438 | *p_width = width; |
| 1439 | if (p_precision != NULL) |
| 1440 | *p_precision = precision; |
| 1441 | |
| 1442 | /* Handle %ld, %lu, %lld and %llu. */ |
| 1443 | longflag = 0; |
| 1444 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 1445 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1446 | |
| 1447 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 1448 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1449 | longflag = 1; |
| 1450 | ++f; |
| 1451 | } |
| 1452 | #ifdef HAVE_LONG_LONG |
| 1453 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 1454 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1455 | longlongflag = 1; |
| 1456 | f += 2; |
| 1457 | } |
| 1458 | #endif |
| 1459 | } |
| 1460 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 1461 | 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] | 1462 | size_tflag = 1; |
| 1463 | ++f; |
| 1464 | } |
| 1465 | if (p_longflag != NULL) |
| 1466 | *p_longflag = longflag; |
| 1467 | if (p_longlongflag != NULL) |
| 1468 | *p_longlongflag = longlongflag; |
| 1469 | if (p_size_tflag != NULL) |
| 1470 | *p_size_tflag = size_tflag; |
| 1471 | return f; |
| 1472 | } |
| 1473 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1474 | /* maximum number of characters required for output of %ld. 21 characters |
| 1475 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 1476 | #define MAX_LONG_CHARS 21 |
| 1477 | /* maximum number of characters required for output of %lld. |
| 1478 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 1479 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 1480 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 1481 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1482 | PyObject * |
| 1483 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 1484 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1485 | va_list count; |
| 1486 | Py_ssize_t callcount = 0; |
| 1487 | PyObject **callresults = NULL; |
| 1488 | PyObject **callresult = NULL; |
| 1489 | Py_ssize_t n = 0; |
| 1490 | int width = 0; |
| 1491 | int precision = 0; |
| 1492 | int zeropad; |
| 1493 | const char* f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1494 | PyUnicodeObject *string; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1495 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1496 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1497 | Py_UCS4 maxchar = 127; /* result is ASCII by default */ |
| 1498 | Py_UCS4 argmaxchar; |
| 1499 | Py_ssize_t numbersize = 0; |
| 1500 | char *numberresults = NULL; |
| 1501 | char *numberresult = NULL; |
| 1502 | Py_ssize_t i; |
| 1503 | int kind; |
| 1504 | void *data; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1505 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 1506 | Py_VA_COPY(count, vargs); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1507 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 1508 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 1509 | * 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] | 1510 | * result in an array) |
| 1511 | * also esimate a upper bound for all the number formats in the string, |
| 1512 | * numbers will be formated in step 3 and be keept in a '\0'-separated |
| 1513 | * buffer before putting everything together. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1514 | for (f = format; *f; f++) { |
| 1515 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1516 | int longlongflag; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1517 | /* skip width or width.precision (eg. "1.2" of "%1.2f") */ |
| 1518 | f = parse_format_flags(f, &width, NULL, NULL, &longlongflag, NULL); |
| 1519 | if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V') |
| 1520 | ++callcount; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1521 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1522 | else if (*f == 'd' || *f=='u' || *f=='i' || *f=='x' || *f=='p') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1523 | #ifdef HAVE_LONG_LONG |
| 1524 | if (longlongflag) { |
| 1525 | if (width < MAX_LONG_LONG_CHARS) |
| 1526 | width = MAX_LONG_LONG_CHARS; |
| 1527 | } |
| 1528 | else |
| 1529 | #endif |
| 1530 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 1531 | including sign. Decimal takes the most space. This |
| 1532 | isn't enough for octal. If a width is specified we |
| 1533 | need more (which we allocate later). */ |
| 1534 | if (width < MAX_LONG_CHARS) |
| 1535 | width = MAX_LONG_CHARS; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1536 | |
| 1537 | /* account for the size + '\0' to separate numbers |
| 1538 | inside of the numberresults buffer */ |
| 1539 | numbersize += (width + 1); |
| 1540 | } |
| 1541 | } |
| 1542 | else if ((unsigned char)*f > 127) { |
| 1543 | PyErr_Format(PyExc_ValueError, |
| 1544 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 1545 | "string, got a non-ASCII byte: 0x%02x", |
| 1546 | (unsigned char)*f); |
| 1547 | return NULL; |
| 1548 | } |
| 1549 | } |
| 1550 | /* step 2: allocate memory for the results of |
| 1551 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
| 1552 | if (callcount) { |
| 1553 | callresults = PyObject_Malloc(sizeof(PyObject *) * callcount); |
| 1554 | if (!callresults) { |
| 1555 | PyErr_NoMemory(); |
| 1556 | return NULL; |
| 1557 | } |
| 1558 | callresult = callresults; |
| 1559 | } |
| 1560 | /* step 2.5: allocate memory for the results of formating numbers */ |
| 1561 | if (numbersize) { |
| 1562 | numberresults = PyObject_Malloc(numbersize); |
| 1563 | if (!numberresults) { |
| 1564 | PyErr_NoMemory(); |
| 1565 | goto fail; |
| 1566 | } |
| 1567 | numberresult = numberresults; |
| 1568 | } |
| 1569 | |
| 1570 | /* step 3: format numbers and figure out how large a buffer we need */ |
| 1571 | for (f = format; *f; f++) { |
| 1572 | if (*f == '%') { |
| 1573 | const char* p; |
| 1574 | int longflag; |
| 1575 | int longlongflag; |
| 1576 | int size_tflag; |
| 1577 | int numprinted; |
| 1578 | |
| 1579 | p = f; |
| 1580 | zeropad = (f[1] == '0'); |
| 1581 | f = parse_format_flags(f, &width, &precision, |
| 1582 | &longflag, &longlongflag, &size_tflag); |
| 1583 | switch (*f) { |
| 1584 | case 'c': |
| 1585 | { |
| 1586 | Py_UCS4 ordinal = va_arg(count, int); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 1587 | maxchar = Py_MAX(maxchar, ordinal); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1588 | n++; |
| 1589 | break; |
| 1590 | } |
| 1591 | case '%': |
| 1592 | n++; |
| 1593 | break; |
| 1594 | case 'i': |
| 1595 | case 'd': |
| 1596 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 1597 | width, precision, *f); |
| 1598 | if (longflag) |
| 1599 | numprinted = sprintf(numberresult, fmt, |
| 1600 | va_arg(count, long)); |
| 1601 | #ifdef HAVE_LONG_LONG |
| 1602 | else if (longlongflag) |
| 1603 | numprinted = sprintf(numberresult, fmt, |
| 1604 | va_arg(count, PY_LONG_LONG)); |
| 1605 | #endif |
| 1606 | else if (size_tflag) |
| 1607 | numprinted = sprintf(numberresult, fmt, |
| 1608 | va_arg(count, Py_ssize_t)); |
| 1609 | else |
| 1610 | numprinted = sprintf(numberresult, fmt, |
| 1611 | va_arg(count, int)); |
| 1612 | n += numprinted; |
| 1613 | /* advance by +1 to skip over the '\0' */ |
| 1614 | numberresult += (numprinted + 1); |
| 1615 | assert(*(numberresult - 1) == '\0'); |
| 1616 | assert(*(numberresult - 2) != '\0'); |
| 1617 | assert(numprinted >= 0); |
| 1618 | assert(numberresult <= numberresults + numbersize); |
| 1619 | break; |
| 1620 | case 'u': |
| 1621 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 1622 | width, precision, 'u'); |
| 1623 | if (longflag) |
| 1624 | numprinted = sprintf(numberresult, fmt, |
| 1625 | va_arg(count, unsigned long)); |
| 1626 | #ifdef HAVE_LONG_LONG |
| 1627 | else if (longlongflag) |
| 1628 | numprinted = sprintf(numberresult, fmt, |
| 1629 | va_arg(count, unsigned PY_LONG_LONG)); |
| 1630 | #endif |
| 1631 | else if (size_tflag) |
| 1632 | numprinted = sprintf(numberresult, fmt, |
| 1633 | va_arg(count, size_t)); |
| 1634 | else |
| 1635 | numprinted = sprintf(numberresult, fmt, |
| 1636 | va_arg(count, unsigned int)); |
| 1637 | n += numprinted; |
| 1638 | numberresult += (numprinted + 1); |
| 1639 | assert(*(numberresult - 1) == '\0'); |
| 1640 | assert(*(numberresult - 2) != '\0'); |
| 1641 | assert(numprinted >= 0); |
| 1642 | assert(numberresult <= numberresults + numbersize); |
| 1643 | break; |
| 1644 | case 'x': |
| 1645 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
| 1646 | numprinted = sprintf(numberresult, fmt, va_arg(count, int)); |
| 1647 | n += numprinted; |
| 1648 | numberresult += (numprinted + 1); |
| 1649 | assert(*(numberresult - 1) == '\0'); |
| 1650 | assert(*(numberresult - 2) != '\0'); |
| 1651 | assert(numprinted >= 0); |
| 1652 | assert(numberresult <= numberresults + numbersize); |
| 1653 | break; |
| 1654 | case 'p': |
| 1655 | numprinted = sprintf(numberresult, "%p", va_arg(count, void*)); |
| 1656 | /* %p is ill-defined: ensure leading 0x. */ |
| 1657 | if (numberresult[1] == 'X') |
| 1658 | numberresult[1] = 'x'; |
| 1659 | else if (numberresult[1] != 'x') { |
| 1660 | memmove(numberresult + 2, numberresult, |
| 1661 | strlen(numberresult) + 1); |
| 1662 | numberresult[0] = '0'; |
| 1663 | numberresult[1] = 'x'; |
| 1664 | numprinted += 2; |
| 1665 | } |
| 1666 | n += numprinted; |
| 1667 | numberresult += (numprinted + 1); |
| 1668 | assert(*(numberresult - 1) == '\0'); |
| 1669 | assert(*(numberresult - 2) != '\0'); |
| 1670 | assert(numprinted >= 0); |
| 1671 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1672 | break; |
| 1673 | case 's': |
| 1674 | { |
| 1675 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 1676 | const char *s = va_arg(count, const char*); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1677 | PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); |
| 1678 | if (!str) |
| 1679 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1680 | /* since PyUnicode_DecodeUTF8 returns already flexible |
| 1681 | unicode objects, there is no need to call ready on them */ |
| 1682 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 1683 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1684 | n += PyUnicode_GET_LENGTH(str); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1685 | /* Remember the str and switch to the next slot */ |
| 1686 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1687 | break; |
| 1688 | } |
| 1689 | case 'U': |
| 1690 | { |
| 1691 | PyObject *obj = va_arg(count, PyObject *); |
| 1692 | assert(obj && PyUnicode_Check(obj)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1693 | if (PyUnicode_READY(obj) == -1) |
| 1694 | goto fail; |
| 1695 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 1696 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1697 | n += PyUnicode_GET_LENGTH(obj); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1698 | break; |
| 1699 | } |
| 1700 | case 'V': |
| 1701 | { |
| 1702 | PyObject *obj = va_arg(count, PyObject *); |
| 1703 | const char *str = va_arg(count, const char *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1704 | PyObject *str_obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1705 | assert(obj || str); |
| 1706 | assert(!obj || PyUnicode_Check(obj)); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1707 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1708 | if (PyUnicode_READY(obj) == -1) |
| 1709 | goto fail; |
| 1710 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 1711 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1712 | n += PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1713 | *callresult++ = NULL; |
| 1714 | } |
| 1715 | else { |
| 1716 | str_obj = PyUnicode_DecodeUTF8(str, strlen(str), "replace"); |
| 1717 | if (!str_obj) |
| 1718 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1719 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 1720 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1721 | n += PyUnicode_GET_LENGTH(str_obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1722 | *callresult++ = str_obj; |
| 1723 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1724 | break; |
| 1725 | } |
| 1726 | case 'S': |
| 1727 | { |
| 1728 | PyObject *obj = va_arg(count, PyObject *); |
| 1729 | PyObject *str; |
| 1730 | assert(obj); |
| 1731 | str = PyObject_Str(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1732 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1733 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1734 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 1735 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1736 | n += PyUnicode_GET_LENGTH(str); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1737 | /* Remember the str and switch to the next slot */ |
| 1738 | *callresult++ = str; |
| 1739 | break; |
| 1740 | } |
| 1741 | case 'R': |
| 1742 | { |
| 1743 | PyObject *obj = va_arg(count, PyObject *); |
| 1744 | PyObject *repr; |
| 1745 | assert(obj); |
| 1746 | repr = PyObject_Repr(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1747 | if (!repr || PyUnicode_READY(repr) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1748 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1749 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(repr); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 1750 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1751 | n += PyUnicode_GET_LENGTH(repr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1752 | /* Remember the repr and switch to the next slot */ |
| 1753 | *callresult++ = repr; |
| 1754 | break; |
| 1755 | } |
| 1756 | case 'A': |
| 1757 | { |
| 1758 | PyObject *obj = va_arg(count, PyObject *); |
| 1759 | PyObject *ascii; |
| 1760 | assert(obj); |
| 1761 | ascii = PyObject_ASCII(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1762 | if (!ascii || PyUnicode_READY(ascii) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1763 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1764 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(ascii); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 1765 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1766 | n += PyUnicode_GET_LENGTH(ascii); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1767 | /* Remember the repr and switch to the next slot */ |
| 1768 | *callresult++ = ascii; |
| 1769 | break; |
| 1770 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1771 | default: |
| 1772 | /* if we stumble upon an unknown |
| 1773 | formatting code, copy the rest of |
| 1774 | the format string to the output |
| 1775 | string. (we cannot just skip the |
| 1776 | code, since there's no way to know |
| 1777 | what's in the argument list) */ |
| 1778 | n += strlen(p); |
| 1779 | goto expand; |
| 1780 | } |
| 1781 | } else |
| 1782 | n++; |
| 1783 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1784 | expand: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1785 | /* step 4: fill the buffer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1786 | /* Since we've analyzed how much space we need, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1787 | we don't have to resize the string. |
| 1788 | There can be no errors beyond this point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1789 | string = (PyUnicodeObject *)PyUnicode_New(n, maxchar); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1790 | if (!string) |
| 1791 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1792 | kind = PyUnicode_KIND(string); |
| 1793 | data = PyUnicode_DATA(string); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1794 | callresult = callresults; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1795 | numberresult = numberresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1796 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1797 | for (i = 0, f = format; *f; f++) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1798 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1799 | const char* p; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1800 | |
| 1801 | p = f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1802 | f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL); |
| 1803 | /* checking for == because the last argument could be a empty |
| 1804 | string, which causes i to point to end, the assert at the end of |
| 1805 | the loop */ |
| 1806 | assert(i <= PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1807 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1808 | switch (*f) { |
| 1809 | case 'c': |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 1810 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1811 | const int ordinal = va_arg(vargs, int); |
| 1812 | PyUnicode_WRITE(kind, data, i++, ordinal); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1813 | break; |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 1814 | } |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 1815 | case 'i': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1816 | case 'd': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1817 | case 'u': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1818 | case 'x': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1819 | case 'p': |
| 1820 | /* unused, since we already have the result */ |
| 1821 | if (*f == 'p') |
| 1822 | (void) va_arg(vargs, void *); |
| 1823 | else |
| 1824 | (void) va_arg(vargs, int); |
| 1825 | /* extract the result from numberresults and append. */ |
| 1826 | for (; *numberresult; ++i, ++numberresult) |
| 1827 | PyUnicode_WRITE(kind, data, i, *numberresult); |
| 1828 | /* skip over the separating '\0' */ |
| 1829 | assert(*numberresult == '\0'); |
| 1830 | numberresult++; |
| 1831 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1832 | break; |
| 1833 | case 's': |
| 1834 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1835 | /* unused, since we already have the result */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1836 | Py_ssize_t size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1837 | (void) va_arg(vargs, char *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1838 | size = PyUnicode_GET_LENGTH(*callresult); |
| 1839 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 1840 | if (PyUnicode_CopyCharacters((PyObject*)string, i, |
| 1841 | *callresult, 0, |
| 1842 | size) < 0) |
| 1843 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1844 | i += size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1845 | /* We're done with the unicode()/repr() => forget it */ |
| 1846 | Py_DECREF(*callresult); |
| 1847 | /* switch to next unicode()/repr() result */ |
| 1848 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1849 | break; |
| 1850 | } |
| 1851 | case 'U': |
| 1852 | { |
| 1853 | PyObject *obj = va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1854 | Py_ssize_t size; |
| 1855 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
| 1856 | size = PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 1857 | if (PyUnicode_CopyCharacters((PyObject*)string, i, |
| 1858 | obj, 0, |
| 1859 | size) < 0) |
| 1860 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1861 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1862 | break; |
| 1863 | } |
| 1864 | case 'V': |
| 1865 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1866 | Py_ssize_t size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1867 | PyObject *obj = va_arg(vargs, PyObject *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1868 | va_arg(vargs, const char *); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1869 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1870 | size = PyUnicode_GET_LENGTH(obj); |
| 1871 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 1872 | if (PyUnicode_CopyCharacters((PyObject*)string, i, |
| 1873 | obj, 0, |
| 1874 | size) < 0) |
| 1875 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1876 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1877 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1878 | size = PyUnicode_GET_LENGTH(*callresult); |
| 1879 | assert(PyUnicode_KIND(*callresult) <= |
| 1880 | PyUnicode_KIND(string)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 1881 | if (PyUnicode_CopyCharacters((PyObject*)string, i, |
| 1882 | *callresult, |
| 1883 | 0, size) < 0) |
| 1884 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1885 | i += size; |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1886 | Py_DECREF(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1887 | } |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1888 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1889 | break; |
| 1890 | } |
| 1891 | case 'S': |
| 1892 | case 'R': |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 1893 | case 'A': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1894 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1895 | /* unused, since we already have the result */ |
| 1896 | (void) va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1897 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 1898 | if (PyUnicode_CopyCharacters((PyObject*)string, i, |
| 1899 | *callresult, 0, |
| 1900 | PyUnicode_GET_LENGTH(*callresult)) < 0) |
| 1901 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1902 | i += PyUnicode_GET_LENGTH(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1903 | /* We're done with the unicode()/repr() => forget it */ |
| 1904 | Py_DECREF(*callresult); |
| 1905 | /* switch to next unicode()/repr() result */ |
| 1906 | ++callresult; |
| 1907 | break; |
| 1908 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1909 | case '%': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1910 | PyUnicode_WRITE(kind, data, i++, '%'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1911 | break; |
| 1912 | default: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1913 | for (; *p; ++p, ++i) |
| 1914 | PyUnicode_WRITE(kind, data, i, *p); |
| 1915 | assert(i == PyUnicode_GET_LENGTH(string)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1916 | goto end; |
| 1917 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 1918 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1919 | else { |
| 1920 | assert(i < PyUnicode_GET_LENGTH(string)); |
| 1921 | PyUnicode_WRITE(kind, data, i++, *f); |
| 1922 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1923 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1924 | assert(i == PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1925 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1926 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1927 | if (callresults) |
| 1928 | PyObject_Free(callresults); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1929 | if (numberresults) |
| 1930 | PyObject_Free(numberresults); |
| 1931 | return (PyObject *)string; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1932 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1933 | if (callresults) { |
| 1934 | PyObject **callresult2 = callresults; |
| 1935 | while (callresult2 < callresult) { |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1936 | Py_XDECREF(*callresult2); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1937 | ++callresult2; |
| 1938 | } |
| 1939 | PyObject_Free(callresults); |
| 1940 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1941 | if (numberresults) |
| 1942 | PyObject_Free(numberresults); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1943 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1944 | } |
| 1945 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1946 | PyObject * |
| 1947 | PyUnicode_FromFormat(const char *format, ...) |
| 1948 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1949 | PyObject* ret; |
| 1950 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1951 | |
| 1952 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1953 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1954 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1955 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1956 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1957 | ret = PyUnicode_FromFormatV(format, vargs); |
| 1958 | va_end(vargs); |
| 1959 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1960 | } |
| 1961 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1962 | #ifdef HAVE_WCHAR_H |
| 1963 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1964 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 1965 | convert a Unicode object to a wide character string. |
| 1966 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 1967 | - 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] | 1968 | character) required to convert the unicode object. Ignore size argument. |
| 1969 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 1970 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1971 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 1972 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1973 | static Py_ssize_t |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1974 | unicode_aswidechar(PyUnicodeObject *unicode, |
| 1975 | wchar_t *w, |
| 1976 | Py_ssize_t size) |
| 1977 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1978 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1979 | const wchar_t *wstr; |
| 1980 | |
| 1981 | wstr = PyUnicode_AsUnicodeAndSize((PyObject *)unicode, &res); |
| 1982 | if (wstr == NULL) |
| 1983 | return -1; |
| 1984 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1985 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1986 | if (size > res) |
| 1987 | size = res + 1; |
| 1988 | else |
| 1989 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1990 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1991 | return res; |
| 1992 | } |
| 1993 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1994 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1995 | } |
| 1996 | |
| 1997 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 1998 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1999 | wchar_t *w, |
| 2000 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2001 | { |
| 2002 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2003 | PyErr_BadInternalCall(); |
| 2004 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2005 | } |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2006 | return unicode_aswidechar((PyUnicodeObject*)unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2007 | } |
| 2008 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2009 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2010 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2011 | Py_ssize_t *size) |
| 2012 | { |
| 2013 | wchar_t* buffer; |
| 2014 | Py_ssize_t buflen; |
| 2015 | |
| 2016 | if (unicode == NULL) { |
| 2017 | PyErr_BadInternalCall(); |
| 2018 | return NULL; |
| 2019 | } |
| 2020 | |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2021 | buflen = unicode_aswidechar((PyUnicodeObject *)unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2022 | if (buflen == -1) |
| 2023 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2024 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2025 | PyErr_NoMemory(); |
| 2026 | return NULL; |
| 2027 | } |
| 2028 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2029 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2030 | if (buffer == NULL) { |
| 2031 | PyErr_NoMemory(); |
| 2032 | return NULL; |
| 2033 | } |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2034 | buflen = unicode_aswidechar((PyUnicodeObject *)unicode, buffer, buflen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2035 | if (buflen == -1) |
| 2036 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2037 | if (size != NULL) |
| 2038 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2039 | return buffer; |
| 2040 | } |
| 2041 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2042 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2043 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2044 | PyObject * |
| 2045 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2046 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2047 | PyObject *v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2048 | if (ordinal < 0 || ordinal > 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2049 | PyErr_SetString(PyExc_ValueError, |
| 2050 | "chr() arg not in range(0x110000)"); |
| 2051 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2052 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2053 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2054 | if (ordinal < 256) |
| 2055 | return get_latin1_char(ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2056 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2057 | v = PyUnicode_New(1, ordinal); |
| 2058 | if (v == NULL) |
| 2059 | return NULL; |
| 2060 | PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal); |
| 2061 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2062 | } |
| 2063 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2064 | PyObject * |
| 2065 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2066 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2067 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2068 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2069 | if (PyUnicode_CheckExact(obj)) { |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2070 | if (PyUnicode_READY(obj)) |
| 2071 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2072 | Py_INCREF(obj); |
| 2073 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2074 | } |
| 2075 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2076 | /* For a Unicode subtype that's not a Unicode object, |
| 2077 | return a true Unicode object with the same data. */ |
Victor Stinner | 2219e0a | 2011-10-01 01:16:59 +0200 | [diff] [blame] | 2078 | return PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2079 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2080 | PyErr_Format(PyExc_TypeError, |
| 2081 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2082 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2083 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2084 | } |
| 2085 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2086 | PyObject * |
| 2087 | PyUnicode_FromEncodedObject(register PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2088 | const char *encoding, |
| 2089 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2090 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2091 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2092 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2093 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2094 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2095 | PyErr_BadInternalCall(); |
| 2096 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2097 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2098 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2099 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2100 | if (PyBytes_Check(obj)) { |
| 2101 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2102 | Py_INCREF(unicode_empty); |
| 2103 | v = (PyObject *) unicode_empty; |
| 2104 | } |
| 2105 | else { |
| 2106 | v = PyUnicode_Decode( |
| 2107 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 2108 | encoding, errors); |
| 2109 | } |
| 2110 | return v; |
| 2111 | } |
| 2112 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2113 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2114 | PyErr_SetString(PyExc_TypeError, |
| 2115 | "decoding str is not supported"); |
| 2116 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2117 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2118 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2119 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 2120 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 2121 | PyErr_Format(PyExc_TypeError, |
| 2122 | "coercing to str: need bytes, bytearray " |
| 2123 | "or buffer-like object, %.80s found", |
| 2124 | Py_TYPE(obj)->tp_name); |
| 2125 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2126 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2127 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2128 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2129 | Py_INCREF(unicode_empty); |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2130 | v = (PyObject *) unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2131 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2132 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2133 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2134 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2135 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2136 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2137 | } |
| 2138 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2139 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2140 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 2141 | 1 on success. */ |
| 2142 | static int |
| 2143 | normalize_encoding(const char *encoding, |
| 2144 | char *lower, |
| 2145 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2146 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2147 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2148 | char *l; |
| 2149 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2150 | |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2151 | e = encoding; |
| 2152 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2153 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2154 | while (*e) { |
| 2155 | if (l == l_end) |
| 2156 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2157 | if (Py_ISUPPER(*e)) { |
| 2158 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2159 | } |
| 2160 | else if (*e == '_') { |
| 2161 | *l++ = '-'; |
| 2162 | e++; |
| 2163 | } |
| 2164 | else { |
| 2165 | *l++ = *e++; |
| 2166 | } |
| 2167 | } |
| 2168 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2169 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2170 | } |
| 2171 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2172 | PyObject * |
| 2173 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2174 | Py_ssize_t size, |
| 2175 | const char *encoding, |
| 2176 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2177 | { |
| 2178 | PyObject *buffer = NULL, *unicode; |
| 2179 | Py_buffer info; |
| 2180 | char lower[11]; /* Enough for any encoding shortcut */ |
| 2181 | |
| 2182 | if (encoding == NULL) |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2183 | return PyUnicode_DecodeUTF8(s, size, errors); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2184 | |
| 2185 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2186 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2187 | if ((strcmp(lower, "utf-8") == 0) || |
| 2188 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2189 | return PyUnicode_DecodeUTF8(s, size, errors); |
| 2190 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2191 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2192 | (strcmp(lower, "iso-8859-1") == 0)) |
| 2193 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2194 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2195 | else if (strcmp(lower, "mbcs") == 0) |
| 2196 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2197 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2198 | else if (strcmp(lower, "ascii") == 0) |
| 2199 | return PyUnicode_DecodeASCII(s, size, errors); |
| 2200 | else if (strcmp(lower, "utf-16") == 0) |
| 2201 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 2202 | else if (strcmp(lower, "utf-32") == 0) |
| 2203 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 2204 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2205 | |
| 2206 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 2207 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 2208 | 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] | 2209 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 2210 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2211 | if (buffer == NULL) |
| 2212 | goto onError; |
| 2213 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 2214 | if (unicode == NULL) |
| 2215 | goto onError; |
| 2216 | if (!PyUnicode_Check(unicode)) { |
| 2217 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2218 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2219 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2220 | Py_DECREF(unicode); |
| 2221 | goto onError; |
| 2222 | } |
| 2223 | Py_DECREF(buffer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2224 | if (PyUnicode_READY(unicode)) { |
| 2225 | Py_DECREF(unicode); |
| 2226 | return NULL; |
| 2227 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2228 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2229 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2230 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2231 | Py_XDECREF(buffer); |
| 2232 | return NULL; |
| 2233 | } |
| 2234 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2235 | PyObject * |
| 2236 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2237 | const char *encoding, |
| 2238 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2239 | { |
| 2240 | PyObject *v; |
| 2241 | |
| 2242 | if (!PyUnicode_Check(unicode)) { |
| 2243 | PyErr_BadArgument(); |
| 2244 | goto onError; |
| 2245 | } |
| 2246 | |
| 2247 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2248 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2249 | |
| 2250 | /* Decode via the codec registry */ |
| 2251 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2252 | if (v == NULL) |
| 2253 | goto onError; |
| 2254 | return v; |
| 2255 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2256 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2257 | return NULL; |
| 2258 | } |
| 2259 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2260 | PyObject * |
| 2261 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2262 | const char *encoding, |
| 2263 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2264 | { |
| 2265 | PyObject *v; |
| 2266 | |
| 2267 | if (!PyUnicode_Check(unicode)) { |
| 2268 | PyErr_BadArgument(); |
| 2269 | goto onError; |
| 2270 | } |
| 2271 | |
| 2272 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2273 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2274 | |
| 2275 | /* Decode via the codec registry */ |
| 2276 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2277 | if (v == NULL) |
| 2278 | goto onError; |
| 2279 | if (!PyUnicode_Check(v)) { |
| 2280 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2281 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2282 | Py_TYPE(v)->tp_name); |
| 2283 | Py_DECREF(v); |
| 2284 | goto onError; |
| 2285 | } |
| 2286 | return v; |
| 2287 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2288 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2289 | return NULL; |
| 2290 | } |
| 2291 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2292 | PyObject * |
| 2293 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2294 | Py_ssize_t size, |
| 2295 | const char *encoding, |
| 2296 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2297 | { |
| 2298 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2299 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2300 | unicode = PyUnicode_FromUnicode(s, size); |
| 2301 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2302 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2303 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 2304 | Py_DECREF(unicode); |
| 2305 | return v; |
| 2306 | } |
| 2307 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2308 | PyObject * |
| 2309 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2310 | const char *encoding, |
| 2311 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2312 | { |
| 2313 | PyObject *v; |
| 2314 | |
| 2315 | if (!PyUnicode_Check(unicode)) { |
| 2316 | PyErr_BadArgument(); |
| 2317 | goto onError; |
| 2318 | } |
| 2319 | |
| 2320 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2321 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2322 | |
| 2323 | /* Encode via the codec registry */ |
| 2324 | v = PyCodec_Encode(unicode, encoding, errors); |
| 2325 | if (v == NULL) |
| 2326 | goto onError; |
| 2327 | return v; |
| 2328 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2329 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2330 | return NULL; |
| 2331 | } |
| 2332 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2333 | PyObject * |
| 2334 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2335 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2336 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2337 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 2338 | PyUnicode_GET_SIZE(unicode), |
| 2339 | NULL); |
| 2340 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2341 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2342 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 2343 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 2344 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 2345 | cannot use it to encode and decode filenames before it is loaded. Load |
| 2346 | the Python codec requires to encode at least its own filename. Use the C |
| 2347 | version of the locale codec until the codec registry is initialized and |
| 2348 | the Python codec is loaded. |
| 2349 | |
| 2350 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 2351 | cannot only rely on it: check also interp->fscodec_initialized for |
| 2352 | subinterpreters. */ |
| 2353 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2354 | return PyUnicode_AsEncodedString(unicode, |
| 2355 | Py_FileSystemDefaultEncoding, |
| 2356 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 2357 | } |
| 2358 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2359 | /* locale encoding with surrogateescape */ |
| 2360 | wchar_t *wchar; |
| 2361 | char *bytes; |
| 2362 | PyObject *bytes_obj; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 2363 | size_t error_pos; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2364 | |
| 2365 | wchar = PyUnicode_AsWideCharString(unicode, NULL); |
| 2366 | if (wchar == NULL) |
| 2367 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 2368 | bytes = _Py_wchar2char(wchar, &error_pos); |
| 2369 | if (bytes == NULL) { |
| 2370 | if (error_pos != (size_t)-1) { |
| 2371 | char *errmsg = strerror(errno); |
| 2372 | PyObject *exc = NULL; |
| 2373 | if (errmsg == NULL) |
| 2374 | errmsg = "Py_wchar2char() failed"; |
| 2375 | raise_encode_exception(&exc, |
| 2376 | "filesystemencoding", |
| 2377 | PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), |
| 2378 | error_pos, error_pos+1, |
| 2379 | errmsg); |
| 2380 | Py_XDECREF(exc); |
| 2381 | } |
| 2382 | else |
| 2383 | PyErr_NoMemory(); |
| 2384 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2385 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 2386 | } |
| 2387 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2388 | |
| 2389 | bytes_obj = PyBytes_FromString(bytes); |
| 2390 | PyMem_Free(bytes); |
| 2391 | return bytes_obj; |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 2392 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2393 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2394 | } |
| 2395 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2396 | PyObject * |
| 2397 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2398 | const char *encoding, |
| 2399 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2400 | { |
| 2401 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2402 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2403 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2404 | if (!PyUnicode_Check(unicode)) { |
| 2405 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2406 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2407 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2408 | |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2409 | if (encoding == NULL) { |
| 2410 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2411 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2412 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2413 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2414 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2415 | |
| 2416 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2417 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2418 | if ((strcmp(lower, "utf-8") == 0) || |
| 2419 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 2420 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2421 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2422 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2423 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2424 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 2425 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2426 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2427 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2428 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2429 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2430 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2431 | else if (strcmp(lower, "mbcs") == 0) |
| 2432 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 2433 | PyUnicode_GET_SIZE(unicode), |
| 2434 | errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2435 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2436 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2437 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2438 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2439 | |
| 2440 | /* Encode via the codec registry */ |
| 2441 | v = PyCodec_Encode(unicode, encoding, errors); |
| 2442 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2443 | return NULL; |
| 2444 | |
| 2445 | /* The normal path */ |
| 2446 | if (PyBytes_Check(v)) |
| 2447 | return v; |
| 2448 | |
| 2449 | /* 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] | 2450 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2451 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2452 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2453 | |
| 2454 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 2455 | "encoder %s returned bytearray instead of bytes", |
| 2456 | encoding); |
| 2457 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2458 | Py_DECREF(v); |
| 2459 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2460 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2461 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2462 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 2463 | Py_DECREF(v); |
| 2464 | return b; |
| 2465 | } |
| 2466 | |
| 2467 | PyErr_Format(PyExc_TypeError, |
| 2468 | "encoder did not return a bytes object (type=%.400s)", |
| 2469 | Py_TYPE(v)->tp_name); |
| 2470 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2471 | return NULL; |
| 2472 | } |
| 2473 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2474 | PyObject * |
| 2475 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2476 | const char *encoding, |
| 2477 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2478 | { |
| 2479 | PyObject *v; |
| 2480 | |
| 2481 | if (!PyUnicode_Check(unicode)) { |
| 2482 | PyErr_BadArgument(); |
| 2483 | goto onError; |
| 2484 | } |
| 2485 | |
| 2486 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2487 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2488 | |
| 2489 | /* Encode via the codec registry */ |
| 2490 | v = PyCodec_Encode(unicode, encoding, errors); |
| 2491 | if (v == NULL) |
| 2492 | goto onError; |
| 2493 | if (!PyUnicode_Check(v)) { |
| 2494 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2495 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2496 | Py_TYPE(v)->tp_name); |
| 2497 | Py_DECREF(v); |
| 2498 | goto onError; |
| 2499 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2500 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2501 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2502 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2503 | return NULL; |
| 2504 | } |
| 2505 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2506 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 2507 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2508 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 2509 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 2510 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2511 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 2512 | PyObject* |
| 2513 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 2514 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2515 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2516 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 2517 | #elif defined(__APPLE__) |
| 2518 | return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); |
| 2519 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 2520 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 2521 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 2522 | cannot use it to encode and decode filenames before it is loaded. Load |
| 2523 | the Python codec requires to encode at least its own filename. Use the C |
| 2524 | version of the locale codec until the codec registry is initialized and |
| 2525 | the Python codec is loaded. |
| 2526 | |
| 2527 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 2528 | cannot only rely on it: check also interp->fscodec_initialized for |
| 2529 | subinterpreters. */ |
| 2530 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2531 | return PyUnicode_Decode(s, size, |
| 2532 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 2533 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2534 | } |
| 2535 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2536 | /* locale encoding with surrogateescape */ |
| 2537 | wchar_t *wchar; |
| 2538 | PyObject *unicode; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 2539 | size_t len; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2540 | |
| 2541 | if (s[size] != '\0' || size != strlen(s)) { |
| 2542 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 2543 | return NULL; |
| 2544 | } |
| 2545 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 2546 | wchar = _Py_char2wchar(s, &len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2547 | if (wchar == NULL) |
Victor Stinner | d5af0a5 | 2010-11-08 23:34:29 +0000 | [diff] [blame] | 2548 | return PyErr_NoMemory(); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2549 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 2550 | unicode = PyUnicode_FromWideChar(wchar, len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2551 | PyMem_Free(wchar); |
| 2552 | return unicode; |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2553 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2554 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2555 | } |
| 2556 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2557 | |
| 2558 | int |
| 2559 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 2560 | { |
| 2561 | PyObject *output = NULL; |
| 2562 | Py_ssize_t size; |
| 2563 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 2564 | if (arg == NULL) { |
| 2565 | Py_DECREF(*(PyObject**)addr); |
| 2566 | return 1; |
| 2567 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2568 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2569 | output = arg; |
| 2570 | Py_INCREF(output); |
| 2571 | } |
| 2572 | else { |
| 2573 | arg = PyUnicode_FromObject(arg); |
| 2574 | if (!arg) |
| 2575 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2576 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2577 | Py_DECREF(arg); |
| 2578 | if (!output) |
| 2579 | return 0; |
| 2580 | if (!PyBytes_Check(output)) { |
| 2581 | Py_DECREF(output); |
| 2582 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 2583 | return 0; |
| 2584 | } |
| 2585 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 2586 | size = PyBytes_GET_SIZE(output); |
| 2587 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2588 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 2589 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2590 | Py_DECREF(output); |
| 2591 | return 0; |
| 2592 | } |
| 2593 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 2594 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2595 | } |
| 2596 | |
| 2597 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 2598 | int |
| 2599 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 2600 | { |
| 2601 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 2602 | if (arg == NULL) { |
| 2603 | Py_DECREF(*(PyObject**)addr); |
| 2604 | return 1; |
| 2605 | } |
| 2606 | if (PyUnicode_Check(arg)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2607 | if (PyUnicode_READY(arg)) |
| 2608 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 2609 | output = arg; |
| 2610 | Py_INCREF(output); |
| 2611 | } |
| 2612 | else { |
| 2613 | arg = PyBytes_FromObject(arg); |
| 2614 | if (!arg) |
| 2615 | return 0; |
| 2616 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 2617 | PyBytes_GET_SIZE(arg)); |
| 2618 | Py_DECREF(arg); |
| 2619 | if (!output) |
| 2620 | return 0; |
| 2621 | if (!PyUnicode_Check(output)) { |
| 2622 | Py_DECREF(output); |
| 2623 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 2624 | return 0; |
| 2625 | } |
| 2626 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2627 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
| 2628 | PyUnicode_GET_LENGTH(output), 0, 1)) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 2629 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 2630 | Py_DECREF(output); |
| 2631 | return 0; |
| 2632 | } |
| 2633 | *(PyObject**)addr = output; |
| 2634 | return Py_CLEANUP_SUPPORTED; |
| 2635 | } |
| 2636 | |
| 2637 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2638 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2639 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2640 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 2641 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2642 | PyUnicodeObject *u = (PyUnicodeObject *)unicode; |
| 2643 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 2644 | if (!PyUnicode_Check(unicode)) { |
| 2645 | PyErr_BadArgument(); |
| 2646 | return NULL; |
| 2647 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2648 | if (PyUnicode_READY(u) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2649 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2650 | |
| 2651 | if (_PyUnicode_UTF8(unicode) == NULL) { |
| 2652 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 2653 | if (bytes == NULL) |
| 2654 | return NULL; |
| 2655 | u->_base.utf8 = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 2656 | if (u->_base.utf8 == NULL) { |
| 2657 | Py_DECREF(bytes); |
| 2658 | return NULL; |
| 2659 | } |
| 2660 | u->_base.utf8_length = PyBytes_GET_SIZE(bytes); |
| 2661 | Py_MEMCPY(u->_base.utf8, PyBytes_AS_STRING(bytes), u->_base.utf8_length + 1); |
| 2662 | Py_DECREF(bytes); |
| 2663 | } |
| 2664 | |
| 2665 | if (psize) |
| 2666 | *psize = _PyUnicode_UTF8_LENGTH(unicode); |
| 2667 | return _PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 2668 | } |
| 2669 | |
| 2670 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2671 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 2672 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2673 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 2674 | } |
| 2675 | |
| 2676 | #ifdef Py_DEBUG |
| 2677 | int unicode_as_unicode_calls = 0; |
| 2678 | #endif |
| 2679 | |
| 2680 | |
| 2681 | Py_UNICODE * |
| 2682 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 2683 | { |
| 2684 | PyUnicodeObject *u; |
| 2685 | const unsigned char *one_byte; |
| 2686 | #if SIZEOF_WCHAR_T == 4 |
| 2687 | const Py_UCS2 *two_bytes; |
| 2688 | #else |
| 2689 | const Py_UCS4 *four_bytes; |
| 2690 | const Py_UCS4 *ucs4_end; |
| 2691 | Py_ssize_t num_surrogates; |
| 2692 | #endif |
| 2693 | wchar_t *w; |
| 2694 | wchar_t *wchar_end; |
| 2695 | |
| 2696 | if (!PyUnicode_Check(unicode)) { |
| 2697 | PyErr_BadArgument(); |
| 2698 | return NULL; |
| 2699 | } |
| 2700 | u = (PyUnicodeObject*)unicode; |
| 2701 | if (_PyUnicode_WSTR(u) == NULL) { |
| 2702 | /* Non-ASCII compact unicode object */ |
| 2703 | assert(_PyUnicode_KIND(u) != 0); |
| 2704 | assert(PyUnicode_IS_READY(u)); |
| 2705 | |
| 2706 | #ifdef Py_DEBUG |
| 2707 | ++unicode_as_unicode_calls; |
| 2708 | #endif |
| 2709 | |
| 2710 | if (PyUnicode_KIND(u) == PyUnicode_4BYTE_KIND) { |
| 2711 | #if SIZEOF_WCHAR_T == 2 |
| 2712 | four_bytes = PyUnicode_4BYTE_DATA(u); |
| 2713 | ucs4_end = four_bytes + _PyUnicode_LENGTH(u); |
| 2714 | num_surrogates = 0; |
| 2715 | |
| 2716 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 2717 | if (*four_bytes > 0xFFFF) |
| 2718 | ++num_surrogates; |
| 2719 | } |
| 2720 | |
| 2721 | _PyUnicode_WSTR(u) = (wchar_t *) PyObject_MALLOC( |
| 2722 | sizeof(wchar_t) * (_PyUnicode_LENGTH(u) + 1 + num_surrogates)); |
| 2723 | if (!_PyUnicode_WSTR(u)) { |
| 2724 | PyErr_NoMemory(); |
| 2725 | return NULL; |
| 2726 | } |
| 2727 | _PyUnicode_WSTR_LENGTH(u) = _PyUnicode_LENGTH(u) + num_surrogates; |
| 2728 | |
| 2729 | w = _PyUnicode_WSTR(u); |
| 2730 | wchar_end = w + _PyUnicode_WSTR_LENGTH(u); |
| 2731 | four_bytes = PyUnicode_4BYTE_DATA(u); |
| 2732 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 2733 | if (*four_bytes > 0xFFFF) { |
| 2734 | /* encode surrogate pair in this case */ |
| 2735 | *w++ = 0xD800 | ((*four_bytes - 0x10000) >> 10); |
| 2736 | *w = 0xDC00 | ((*four_bytes - 0x10000) & 0x3FF); |
| 2737 | } |
| 2738 | else |
| 2739 | *w = *four_bytes; |
| 2740 | |
| 2741 | if (w > wchar_end) { |
| 2742 | assert(0 && "Miscalculated string end"); |
| 2743 | } |
| 2744 | } |
| 2745 | *w = 0; |
| 2746 | #else |
| 2747 | /* sizeof(wchar_t) == 4 */ |
| 2748 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 2749 | "should share memory already."); |
| 2750 | return NULL; |
| 2751 | #endif |
| 2752 | } |
| 2753 | else { |
| 2754 | _PyUnicode_WSTR(u) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 2755 | (_PyUnicode_LENGTH(u) + 1)); |
| 2756 | if (!_PyUnicode_WSTR(u)) { |
| 2757 | PyErr_NoMemory(); |
| 2758 | return NULL; |
| 2759 | } |
| 2760 | if (!PyUnicode_IS_COMPACT_ASCII(u)) |
| 2761 | _PyUnicode_WSTR_LENGTH(u) = _PyUnicode_LENGTH(u); |
| 2762 | w = _PyUnicode_WSTR(u); |
| 2763 | wchar_end = w + _PyUnicode_LENGTH(u); |
| 2764 | |
| 2765 | if (PyUnicode_KIND(u) == PyUnicode_1BYTE_KIND) { |
| 2766 | one_byte = PyUnicode_1BYTE_DATA(u); |
| 2767 | for (; w < wchar_end; ++one_byte, ++w) |
| 2768 | *w = *one_byte; |
| 2769 | /* null-terminate the wstr */ |
| 2770 | *w = 0; |
| 2771 | } |
| 2772 | else if (PyUnicode_KIND(u) == PyUnicode_2BYTE_KIND) { |
| 2773 | #if SIZEOF_WCHAR_T == 4 |
| 2774 | two_bytes = PyUnicode_2BYTE_DATA(u); |
| 2775 | for (; w < wchar_end; ++two_bytes, ++w) |
| 2776 | *w = *two_bytes; |
| 2777 | /* null-terminate the wstr */ |
| 2778 | *w = 0; |
| 2779 | #else |
| 2780 | /* sizeof(wchar_t) == 2 */ |
| 2781 | PyObject_FREE(_PyUnicode_WSTR(u)); |
| 2782 | _PyUnicode_WSTR(u) = NULL; |
| 2783 | Py_FatalError("Impossible unicode object state, wstr " |
| 2784 | "and str should share memory already."); |
| 2785 | return NULL; |
| 2786 | #endif |
| 2787 | } |
| 2788 | else { |
| 2789 | assert(0 && "This should never happen."); |
| 2790 | } |
| 2791 | } |
| 2792 | } |
| 2793 | if (size != NULL) |
| 2794 | *size = PyUnicode_WSTR_LENGTH(u); |
| 2795 | return _PyUnicode_WSTR(u); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2796 | } |
| 2797 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2798 | Py_UNICODE * |
| 2799 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2800 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2801 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2802 | } |
| 2803 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2804 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2805 | Py_ssize_t |
| 2806 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2807 | { |
| 2808 | if (!PyUnicode_Check(unicode)) { |
| 2809 | PyErr_BadArgument(); |
| 2810 | goto onError; |
| 2811 | } |
| 2812 | return PyUnicode_GET_SIZE(unicode); |
| 2813 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2814 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2815 | return -1; |
| 2816 | } |
| 2817 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2818 | Py_ssize_t |
| 2819 | PyUnicode_GetLength(PyObject *unicode) |
| 2820 | { |
| 2821 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) != -1) { |
| 2822 | PyErr_BadArgument(); |
| 2823 | return -1; |
| 2824 | } |
| 2825 | |
| 2826 | return PyUnicode_GET_LENGTH(unicode); |
| 2827 | } |
| 2828 | |
| 2829 | Py_UCS4 |
| 2830 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 2831 | { |
| 2832 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) != -1) { |
| 2833 | return PyErr_BadArgument(); |
| 2834 | return (Py_UCS4)-1; |
| 2835 | } |
| 2836 | return PyUnicode_READ_CHAR(unicode, index); |
| 2837 | } |
| 2838 | |
| 2839 | int |
| 2840 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 2841 | { |
| 2842 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
| 2843 | return PyErr_BadArgument(); |
| 2844 | return -1; |
| 2845 | } |
| 2846 | |
| 2847 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 2848 | index, ch); |
| 2849 | return 0; |
| 2850 | } |
| 2851 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2852 | const char * |
| 2853 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2854 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 2855 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2856 | } |
| 2857 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 2858 | /* create or adjust a UnicodeDecodeError */ |
| 2859 | static void |
| 2860 | make_decode_exception(PyObject **exceptionObject, |
| 2861 | const char *encoding, |
| 2862 | const char *input, Py_ssize_t length, |
| 2863 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 2864 | const char *reason) |
| 2865 | { |
| 2866 | if (*exceptionObject == NULL) { |
| 2867 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 2868 | encoding, input, length, startpos, endpos, reason); |
| 2869 | } |
| 2870 | else { |
| 2871 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 2872 | goto onError; |
| 2873 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 2874 | goto onError; |
| 2875 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 2876 | goto onError; |
| 2877 | } |
| 2878 | return; |
| 2879 | |
| 2880 | onError: |
| 2881 | Py_DECREF(*exceptionObject); |
| 2882 | *exceptionObject = NULL; |
| 2883 | } |
| 2884 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2885 | /* error handling callback helper: |
| 2886 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 2887 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2888 | and adjust various state variables. |
| 2889 | return 0 on success, -1 on error |
| 2890 | */ |
| 2891 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2892 | static int |
| 2893 | unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2894 | const char *encoding, const char *reason, |
| 2895 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 2896 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 2897 | PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2898 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2899 | 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] | 2900 | |
| 2901 | PyObject *restuple = NULL; |
| 2902 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2903 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2904 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2905 | Py_ssize_t requiredsize; |
| 2906 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2907 | const Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2908 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2909 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2910 | int res = -1; |
| 2911 | |
| 2912 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2913 | *errorHandler = PyCodec_LookupError(errors); |
| 2914 | if (*errorHandler == NULL) |
| 2915 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2916 | } |
| 2917 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 2918 | make_decode_exception(exceptionObject, |
| 2919 | encoding, |
| 2920 | *input, *inend - *input, |
| 2921 | *startinpos, *endinpos, |
| 2922 | reason); |
| 2923 | if (*exceptionObject == NULL) |
| 2924 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2925 | |
| 2926 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 2927 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2928 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2929 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 2930 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2931 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2932 | } |
| 2933 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2934 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2935 | |
| 2936 | /* Copy back the bytes variables, which might have been modified by the |
| 2937 | callback */ |
| 2938 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 2939 | if (!inputobj) |
| 2940 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2941 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2942 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2943 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2944 | *input = PyBytes_AS_STRING(inputobj); |
| 2945 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2946 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 2947 | /* we can DECREF safely, as the exception has another reference, |
| 2948 | so the object won't go away. */ |
| 2949 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2950 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2951 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2952 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 2953 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2954 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 2955 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 2956 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2957 | |
| 2958 | /* need more space? (at least enough for what we |
| 2959 | have+the replacement+the rest of the string (starting |
| 2960 | at the new input position), so we won't have to check space |
| 2961 | when there are no errors in the rest of the string) */ |
| 2962 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 2963 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 2964 | requiredsize = *outpos + repsize + insize-newpos; |
| 2965 | if (requiredsize > outsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2966 | if (requiredsize<2*outsize) |
| 2967 | requiredsize = 2*outsize; |
| 2968 | if (_PyUnicode_Resize(output, requiredsize) < 0) |
| 2969 | goto onError; |
| 2970 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2971 | } |
| 2972 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2973 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2974 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 2975 | *outptr += repsize; |
| 2976 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2977 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2978 | /* we made it! */ |
| 2979 | res = 0; |
| 2980 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2981 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2982 | Py_XDECREF(restuple); |
| 2983 | return res; |
| 2984 | } |
| 2985 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2986 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 2987 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2988 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 2989 | |
| 2990 | /* Three simple macros defining base-64. */ |
| 2991 | |
| 2992 | /* Is c a base-64 character? */ |
| 2993 | |
| 2994 | #define IS_BASE64(c) \ |
| 2995 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 2996 | ((c) >= 'a' && (c) <= 'z') || \ |
| 2997 | ((c) >= '0' && (c) <= '9') || \ |
| 2998 | (c) == '+' || (c) == '/') |
| 2999 | |
| 3000 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 3001 | |
| 3002 | #define FROM_BASE64(c) \ |
| 3003 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 3004 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 3005 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 3006 | (c) == '+' ? 62 : 63) |
| 3007 | |
| 3008 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 3009 | |
| 3010 | #define TO_BASE64(n) \ |
| 3011 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 3012 | |
| 3013 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 3014 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 3015 | * byte not decoding to itself is the + which begins a base64 |
| 3016 | * string. */ |
| 3017 | |
| 3018 | #define DECODE_DIRECT(c) \ |
| 3019 | ((c) <= 127 && (c) != '+') |
| 3020 | |
| 3021 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 3022 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 3023 | * the above). See RFC2152. This array identifies these different |
| 3024 | * sets: |
| 3025 | * 0 : "Set D" |
| 3026 | * alphanumeric and '(),-./:? |
| 3027 | * 1 : "Set O" |
| 3028 | * !"#$%&*;<=>@[]^_`{|} |
| 3029 | * 2 : "whitespace" |
| 3030 | * ht nl cr sp |
| 3031 | * 3 : special (must be base64 encoded) |
| 3032 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 3033 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3034 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3035 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3036 | char utf7_category[128] = { |
| 3037 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 3038 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 3039 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 3040 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 3041 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 3042 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 3043 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 3044 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 3045 | /* @ A B C D E F G H I J K L M N O */ |
| 3046 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3047 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 3048 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 3049 | /* ` a b c d e f g h i j k l m n o */ |
| 3050 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3051 | /* p q r s t u v w x y z { | } ~ del */ |
| 3052 | 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] | 3053 | }; |
| 3054 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3055 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 3056 | * answer depends on whether we are encoding set O as itself, and also |
| 3057 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 3058 | * clear that the answers to these questions vary between |
| 3059 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 3060 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3061 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 3062 | ((c) < 128 && (c) > 0 && \ |
| 3063 | ((utf7_category[(c)] == 0) || \ |
| 3064 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 3065 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3066 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3067 | PyObject * |
| 3068 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3069 | Py_ssize_t size, |
| 3070 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3071 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3072 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 3073 | } |
| 3074 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3075 | /* The decoder. The only state we preserve is our read position, |
| 3076 | * i.e. how many characters we have consumed. So if we end in the |
| 3077 | * middle of a shift sequence we have to back off the read position |
| 3078 | * and the output to the beginning of the sequence, otherwise we lose |
| 3079 | * all the shift state (seen bits, number of bits seen, high |
| 3080 | * surrogate). */ |
| 3081 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3082 | PyObject * |
| 3083 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3084 | Py_ssize_t size, |
| 3085 | const char *errors, |
| 3086 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3087 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3088 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3089 | Py_ssize_t startinpos; |
| 3090 | Py_ssize_t endinpos; |
| 3091 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3092 | const char *e; |
| 3093 | PyUnicodeObject *unicode; |
| 3094 | Py_UNICODE *p; |
| 3095 | const char *errmsg = ""; |
| 3096 | int inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3097 | Py_UNICODE *shiftOutStart; |
| 3098 | unsigned int base64bits = 0; |
| 3099 | unsigned long base64buffer = 0; |
| 3100 | Py_UNICODE surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3101 | PyObject *errorHandler = NULL; |
| 3102 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3103 | |
| 3104 | unicode = _PyUnicode_New(size); |
| 3105 | if (!unicode) |
| 3106 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3107 | if (size == 0) { |
| 3108 | if (consumed) |
| 3109 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3110 | return (PyObject *)unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3111 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3112 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3113 | p = PyUnicode_AS_UNICODE(unicode); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3114 | shiftOutStart = p; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3115 | e = s + size; |
| 3116 | |
| 3117 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3118 | Py_UNICODE ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3119 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 3120 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3121 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3122 | if (inShift) { /* in a base-64 section */ |
| 3123 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 3124 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 3125 | base64bits += 6; |
| 3126 | s++; |
| 3127 | if (base64bits >= 16) { |
| 3128 | /* we have enough bits for a UTF-16 value */ |
| 3129 | Py_UNICODE outCh = (Py_UNICODE) |
| 3130 | (base64buffer >> (base64bits-16)); |
| 3131 | base64bits -= 16; |
| 3132 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 3133 | if (surrogate) { |
| 3134 | /* expecting a second surrogate */ |
| 3135 | if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 3136 | #ifdef Py_UNICODE_WIDE |
| 3137 | *p++ = (((surrogate & 0x3FF)<<10) |
| 3138 | | (outCh & 0x3FF)) + 0x10000; |
| 3139 | #else |
| 3140 | *p++ = surrogate; |
| 3141 | *p++ = outCh; |
| 3142 | #endif |
| 3143 | surrogate = 0; |
| 3144 | } |
| 3145 | else { |
| 3146 | surrogate = 0; |
| 3147 | errmsg = "second surrogate missing"; |
| 3148 | goto utf7Error; |
| 3149 | } |
| 3150 | } |
| 3151 | else if (outCh >= 0xD800 && outCh <= 0xDBFF) { |
| 3152 | /* first surrogate */ |
| 3153 | surrogate = outCh; |
| 3154 | } |
| 3155 | else if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 3156 | errmsg = "unexpected second surrogate"; |
| 3157 | goto utf7Error; |
| 3158 | } |
| 3159 | else { |
| 3160 | *p++ = outCh; |
| 3161 | } |
| 3162 | } |
| 3163 | } |
| 3164 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3165 | inShift = 0; |
| 3166 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3167 | if (surrogate) { |
| 3168 | errmsg = "second surrogate missing at end of shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3169 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3170 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3171 | if (base64bits > 0) { /* left-over bits */ |
| 3172 | if (base64bits >= 6) { |
| 3173 | /* We've seen at least one base-64 character */ |
| 3174 | errmsg = "partial character in shift sequence"; |
| 3175 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3176 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3177 | else { |
| 3178 | /* Some bits remain; they should be zero */ |
| 3179 | if (base64buffer != 0) { |
| 3180 | errmsg = "non-zero padding bits in shift sequence"; |
| 3181 | goto utf7Error; |
| 3182 | } |
| 3183 | } |
| 3184 | } |
| 3185 | if (ch != '-') { |
| 3186 | /* '-' is absorbed; other terminating |
| 3187 | characters are preserved */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3188 | *p++ = ch; |
| 3189 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3190 | } |
| 3191 | } |
| 3192 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3193 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3194 | s++; /* consume '+' */ |
| 3195 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3196 | s++; |
| 3197 | *p++ = '+'; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3198 | } |
| 3199 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3200 | inShift = 1; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3201 | shiftOutStart = p; |
| 3202 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3203 | } |
| 3204 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3205 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3206 | *p++ = ch; |
| 3207 | s++; |
| 3208 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3209 | else { |
| 3210 | startinpos = s-starts; |
| 3211 | s++; |
| 3212 | errmsg = "unexpected special character"; |
| 3213 | goto utf7Error; |
| 3214 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3215 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3216 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3217 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 3218 | endinpos = s-starts; |
| 3219 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3220 | errors, &errorHandler, |
| 3221 | "utf7", errmsg, |
| 3222 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 3223 | &unicode, &outpos, &p)) |
| 3224 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3225 | } |
| 3226 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3227 | /* end of string */ |
| 3228 | |
| 3229 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 3230 | /* if we're in an inconsistent state, that's an error */ |
| 3231 | if (surrogate || |
| 3232 | (base64bits >= 6) || |
| 3233 | (base64bits > 0 && base64buffer != 0)) { |
| 3234 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 3235 | endinpos = size; |
| 3236 | if (unicode_decode_call_errorhandler( |
| 3237 | errors, &errorHandler, |
| 3238 | "utf7", "unterminated shift sequence", |
| 3239 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 3240 | &unicode, &outpos, &p)) |
| 3241 | goto onError; |
| 3242 | if (s < e) |
| 3243 | goto restart; |
| 3244 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3245 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3246 | |
| 3247 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3248 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3249 | if (inShift) { |
| 3250 | p = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3251 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3252 | } |
| 3253 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3254 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3255 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3256 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3257 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 3258 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3259 | goto onError; |
| 3260 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3261 | Py_XDECREF(errorHandler); |
| 3262 | Py_XDECREF(exc); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3263 | if (PyUnicode_READY(unicode) == -1) { |
| 3264 | Py_DECREF(unicode); |
| 3265 | return NULL; |
| 3266 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3267 | return (PyObject *)unicode; |
| 3268 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3269 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3270 | Py_XDECREF(errorHandler); |
| 3271 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3272 | Py_DECREF(unicode); |
| 3273 | return NULL; |
| 3274 | } |
| 3275 | |
| 3276 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3277 | PyObject * |
| 3278 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3279 | Py_ssize_t size, |
| 3280 | int base64SetO, |
| 3281 | int base64WhiteSpace, |
| 3282 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3283 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3284 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3285 | /* It might be possible to tighten this worst case */ |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 3286 | Py_ssize_t allocated = 8 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3287 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3288 | Py_ssize_t i = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3289 | unsigned int base64bits = 0; |
| 3290 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3291 | char * out; |
| 3292 | char * start; |
| 3293 | |
| 3294 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3295 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3296 | |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 3297 | if (allocated / 8 != size) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3298 | return PyErr_NoMemory(); |
| 3299 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3300 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3301 | if (v == NULL) |
| 3302 | return NULL; |
| 3303 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3304 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3305 | for (;i < size; ++i) { |
| 3306 | Py_UNICODE ch = s[i]; |
| 3307 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3308 | if (inShift) { |
| 3309 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 3310 | /* shifting out */ |
| 3311 | if (base64bits) { /* output remaining bits */ |
| 3312 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 3313 | base64buffer = 0; |
| 3314 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3315 | } |
| 3316 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3317 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 3318 | so no '-' is required, except if the character is itself a '-' */ |
| 3319 | if (IS_BASE64(ch) || ch == '-') { |
| 3320 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3321 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3322 | *out++ = (char) ch; |
| 3323 | } |
| 3324 | else { |
| 3325 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3326 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3327 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3328 | else { /* not in a shift sequence */ |
| 3329 | if (ch == '+') { |
| 3330 | *out++ = '+'; |
| 3331 | *out++ = '-'; |
| 3332 | } |
| 3333 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 3334 | *out++ = (char) ch; |
| 3335 | } |
| 3336 | else { |
| 3337 | *out++ = '+'; |
| 3338 | inShift = 1; |
| 3339 | goto encode_char; |
| 3340 | } |
| 3341 | } |
| 3342 | continue; |
| 3343 | encode_char: |
| 3344 | #ifdef Py_UNICODE_WIDE |
| 3345 | if (ch >= 0x10000) { |
| 3346 | /* code first surrogate */ |
| 3347 | base64bits += 16; |
| 3348 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 3349 | while (base64bits >= 6) { |
| 3350 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 3351 | base64bits -= 6; |
| 3352 | } |
| 3353 | /* prepare second surrogate */ |
| 3354 | ch = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 3355 | } |
| 3356 | #endif |
| 3357 | base64bits += 16; |
| 3358 | base64buffer = (base64buffer << 16) | ch; |
| 3359 | while (base64bits >= 6) { |
| 3360 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 3361 | base64bits -= 6; |
| 3362 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 3363 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3364 | if (base64bits) |
| 3365 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 3366 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3367 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3368 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 3369 | return NULL; |
| 3370 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3371 | } |
| 3372 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3373 | #undef IS_BASE64 |
| 3374 | #undef FROM_BASE64 |
| 3375 | #undef TO_BASE64 |
| 3376 | #undef DECODE_DIRECT |
| 3377 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3378 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3379 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 3380 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3381 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3382 | char utf8_code_length[256] = { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3383 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 3384 | illegal prefix. See RFC 3629 for details */ |
| 3385 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 3386 | 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] | 3387 | 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] | 3388 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 3389 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 3390 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 3391 | 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] | 3392 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 3393 | 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] | 3394 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3395 | 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] | 3396 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 3397 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 3398 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 3399 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 3400 | 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] | 3401 | }; |
| 3402 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3403 | PyObject * |
| 3404 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3405 | Py_ssize_t size, |
| 3406 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3407 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3408 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 3409 | } |
| 3410 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3411 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 3412 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 3413 | |
| 3414 | /* Mask to quickly check whether a C 'long' contains a |
| 3415 | non-ASCII, UTF8-encoded char. */ |
| 3416 | #if (SIZEOF_LONG == 8) |
| 3417 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 3418 | #elif (SIZEOF_LONG == 4) |
| 3419 | # define ASCII_CHAR_MASK 0x80808080L |
| 3420 | #else |
| 3421 | # error C 'long' size should be either 4 or 8! |
| 3422 | #endif |
| 3423 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3424 | /* Scans a UTF-8 string and returns the maximum character to be expected, |
| 3425 | the size of the decoded unicode string and if any major errors were |
| 3426 | encountered. |
| 3427 | |
| 3428 | This function does check basic UTF-8 sanity, it does however NOT CHECK |
| 3429 | if the string contains surrogates, and if all continuation bytes are |
| 3430 | within the correct ranges, these checks are performed in |
| 3431 | PyUnicode_DecodeUTF8Stateful. |
| 3432 | |
| 3433 | If it sets has_errors to 1, it means the value of unicode_size and max_char |
| 3434 | will be bogus and you should not rely on useful information in them. |
| 3435 | */ |
| 3436 | static Py_UCS4 |
| 3437 | utf8_max_char_size_and_has_errors(const char *s, Py_ssize_t string_size, |
| 3438 | Py_ssize_t *unicode_size, Py_ssize_t* consumed, |
| 3439 | int *has_errors) |
| 3440 | { |
| 3441 | Py_ssize_t n; |
| 3442 | Py_ssize_t char_count = 0; |
| 3443 | Py_UCS4 max_char = 127, new_max; |
| 3444 | Py_UCS4 upper_bound; |
| 3445 | const unsigned char *p = (const unsigned char *)s; |
| 3446 | const unsigned char *end = p + string_size; |
| 3447 | const unsigned char *aligned_end = (const unsigned char *) ((size_t) end & ~LONG_PTR_MASK); |
| 3448 | int err = 0; |
| 3449 | |
| 3450 | for (; p < end && !err; ++p, ++char_count) { |
| 3451 | /* Only check value if it's not a ASCII char... */ |
| 3452 | if (*p < 0x80) { |
| 3453 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 3454 | an explanation. */ |
| 3455 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 3456 | /* Help register allocation */ |
| 3457 | register const unsigned char *_p = p; |
| 3458 | while (_p < aligned_end) { |
| 3459 | unsigned long value = *(unsigned long *) _p; |
| 3460 | if (value & ASCII_CHAR_MASK) |
| 3461 | break; |
| 3462 | _p += SIZEOF_LONG; |
| 3463 | char_count += SIZEOF_LONG; |
| 3464 | } |
| 3465 | p = _p; |
| 3466 | if (p == end) |
| 3467 | break; |
| 3468 | } |
| 3469 | } |
| 3470 | if (*p >= 0x80) { |
| 3471 | n = utf8_code_length[*p]; |
| 3472 | new_max = max_char; |
| 3473 | switch (n) { |
| 3474 | /* invalid start byte */ |
| 3475 | case 0: |
| 3476 | err = 1; |
| 3477 | break; |
| 3478 | case 2: |
| 3479 | /* Code points between 0x00FF and 0x07FF inclusive. |
| 3480 | Approximate the upper bound of the code point, |
| 3481 | if this flips over 255 we can be sure it will be more |
| 3482 | than 255 and the string will need 2 bytes per code coint, |
| 3483 | if it stays under or equal to 255, we can be sure 1 byte |
| 3484 | is enough. |
| 3485 | ((*p & 0b00011111) << 6) | 0b00111111 */ |
| 3486 | upper_bound = ((*p & 0x1F) << 6) | 0x3F; |
| 3487 | if (max_char < upper_bound) |
| 3488 | new_max = upper_bound; |
| 3489 | /* Ensure we track at least that we left ASCII space. */ |
| 3490 | if (new_max < 128) |
| 3491 | new_max = 128; |
| 3492 | break; |
| 3493 | case 3: |
| 3494 | /* Between 0x0FFF and 0xFFFF inclusive, so values are |
| 3495 | always > 255 and <= 65535 and will always need 2 bytes. */ |
| 3496 | if (max_char < 65535) |
| 3497 | new_max = 65535; |
| 3498 | break; |
| 3499 | case 4: |
| 3500 | /* Code point will be above 0xFFFF for sure in this case. */ |
| 3501 | new_max = 65537; |
| 3502 | break; |
| 3503 | /* Internal error, this should be caught by the first if */ |
| 3504 | case 1: |
| 3505 | default: |
| 3506 | assert(0 && "Impossible case in utf8_max_char_and_size"); |
| 3507 | err = 1; |
| 3508 | } |
| 3509 | /* Instead of number of overall bytes for this code point, |
| 3510 | n containts the number of following bytes: */ |
| 3511 | --n; |
| 3512 | /* Check if the follow up chars are all valid continuation bytes */ |
| 3513 | if (n >= 1) { |
| 3514 | const unsigned char *cont; |
| 3515 | if ((p + n) >= end) { |
| 3516 | if (consumed == 0) |
| 3517 | /* incomplete data, non-incremental decoding */ |
| 3518 | err = 1; |
| 3519 | break; |
| 3520 | } |
| 3521 | for (cont = p + 1; cont < (p + n); ++cont) { |
| 3522 | if ((*cont & 0xc0) != 0x80) { |
| 3523 | err = 1; |
| 3524 | break; |
| 3525 | } |
| 3526 | } |
| 3527 | p += n; |
| 3528 | } |
| 3529 | else |
| 3530 | err = 1; |
| 3531 | max_char = new_max; |
| 3532 | } |
| 3533 | } |
| 3534 | |
| 3535 | if (unicode_size) |
| 3536 | *unicode_size = char_count; |
| 3537 | if (has_errors) |
| 3538 | *has_errors = err; |
| 3539 | return max_char; |
| 3540 | } |
| 3541 | |
| 3542 | /* Similar to PyUnicode_WRITE but can also write into wstr field |
| 3543 | of the legacy unicode representation */ |
| 3544 | #define WRITE_FLEXIBLE_OR_WSTR(kind, buf, index, value) \ |
| 3545 | do { \ |
| 3546 | const int k_ = (kind); \ |
| 3547 | if (k_ == PyUnicode_WCHAR_KIND) \ |
| 3548 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \ |
| 3549 | else if (k_ == PyUnicode_1BYTE_KIND) \ |
| 3550 | ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \ |
| 3551 | else if (k_ == PyUnicode_2BYTE_KIND) \ |
| 3552 | ((Py_UCS2 *)(buf))[(index)] = (Py_UCS2)(value); \ |
| 3553 | else \ |
| 3554 | ((Py_UCS4 *)(buf))[(index)] = (Py_UCS4)(value); \ |
| 3555 | } while (0) |
| 3556 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3557 | PyObject * |
| 3558 | PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3559 | Py_ssize_t size, |
| 3560 | const char *errors, |
| 3561 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3562 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3563 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3564 | int n; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3565 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3566 | Py_ssize_t startinpos; |
| 3567 | Py_ssize_t endinpos; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3568 | const char *e, *aligned_end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3569 | PyUnicodeObject *unicode; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 3570 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3571 | PyObject *errorHandler = NULL; |
| 3572 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3573 | Py_UCS4 maxchar = 0; |
| 3574 | Py_ssize_t unicode_size; |
| 3575 | Py_ssize_t i; |
| 3576 | int kind; |
| 3577 | void *data; |
| 3578 | int has_errors; |
| 3579 | Py_UNICODE *error_outptr; |
| 3580 | #if SIZEOF_WCHAR_T == 2 |
| 3581 | Py_ssize_t wchar_offset = 0; |
| 3582 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3583 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3584 | if (size == 0) { |
| 3585 | if (consumed) |
| 3586 | *consumed = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3587 | return (PyObject *)PyUnicode_New(0, 0); |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3588 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3589 | maxchar = utf8_max_char_size_and_has_errors(s, size, &unicode_size, |
| 3590 | consumed, &has_errors); |
| 3591 | if (has_errors) { |
| 3592 | unicode = _PyUnicode_New(size); |
| 3593 | if (!unicode) |
| 3594 | return NULL; |
| 3595 | kind = PyUnicode_WCHAR_KIND; |
| 3596 | data = PyUnicode_AS_UNICODE(unicode); |
| 3597 | assert(data != NULL); |
| 3598 | } |
| 3599 | else { |
| 3600 | unicode = (PyUnicodeObject *)PyUnicode_New(unicode_size, maxchar); |
| 3601 | if (!unicode) |
| 3602 | return NULL; |
| 3603 | /* When the string is ASCII only, just use memcpy and return. |
| 3604 | unicode_size may be != size if there is an incomplete UTF-8 |
| 3605 | sequence at the end of the ASCII block. */ |
| 3606 | if (maxchar < 128 && size == unicode_size) { |
| 3607 | Py_MEMCPY(PyUnicode_1BYTE_DATA(unicode), s, unicode_size); |
| 3608 | return (PyObject *)unicode; |
| 3609 | } |
| 3610 | kind = PyUnicode_KIND(unicode); |
| 3611 | data = PyUnicode_DATA(unicode); |
| 3612 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3613 | /* Unpack UTF-8 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3614 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3615 | e = s + size; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3616 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3617 | |
| 3618 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 3619 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3620 | |
| 3621 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3622 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 3623 | input will consist of an overwhelming majority of ASCII |
| 3624 | characters, we try to optimize for this case by checking |
| 3625 | as many characters as a C 'long' can contain. |
| 3626 | First, check if we can do an aligned read, as most CPUs have |
| 3627 | a penalty for unaligned reads. |
| 3628 | */ |
| 3629 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 3630 | /* Help register allocation */ |
| 3631 | register const char *_s = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3632 | register Py_ssize_t _i = i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3633 | while (_s < aligned_end) { |
| 3634 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 3635 | and do a fast unrolled copy if it only contains ASCII |
| 3636 | characters. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3637 | unsigned long value = *(unsigned long *) _s; |
| 3638 | if (value & ASCII_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3639 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3640 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+0, _s[0]); |
| 3641 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+1, _s[1]); |
| 3642 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+2, _s[2]); |
| 3643 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+3, _s[3]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3644 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3645 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+4, _s[4]); |
| 3646 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+5, _s[5]); |
| 3647 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+6, _s[6]); |
| 3648 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+7, _s[7]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3649 | #endif |
| 3650 | _s += SIZEOF_LONG; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3651 | _i += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3652 | } |
| 3653 | s = _s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3654 | i = _i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3655 | if (s == e) |
| 3656 | break; |
| 3657 | ch = (unsigned char)*s; |
| 3658 | } |
| 3659 | } |
| 3660 | |
| 3661 | if (ch < 0x80) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3662 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3663 | s++; |
| 3664 | continue; |
| 3665 | } |
| 3666 | |
| 3667 | n = utf8_code_length[ch]; |
| 3668 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 3669 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3670 | if (consumed) |
| 3671 | break; |
| 3672 | else { |
| 3673 | errmsg = "unexpected end of data"; |
| 3674 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3675 | endinpos = startinpos+1; |
| 3676 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 3677 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3678 | goto utf8Error; |
| 3679 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3680 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3681 | |
| 3682 | switch (n) { |
| 3683 | |
| 3684 | case 0: |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3685 | errmsg = "invalid start byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3686 | startinpos = s-starts; |
| 3687 | endinpos = startinpos+1; |
| 3688 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3689 | |
| 3690 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 3691 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3692 | startinpos = s-starts; |
| 3693 | endinpos = startinpos+1; |
| 3694 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3695 | |
| 3696 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 3697 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3698 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3699 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3700 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3701 | goto utf8Error; |
| 3702 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3703 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3704 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3705 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3706 | break; |
| 3707 | |
| 3708 | case 3: |
Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 3709 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 3710 | will result in surrogates in range d800-dfff. Surrogates are |
| 3711 | not valid UTF-8 so they are rejected. |
| 3712 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 3713 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3714 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3715 | (s[2] & 0xc0) != 0x80 || |
| 3716 | ((unsigned char)s[0] == 0xE0 && |
| 3717 | (unsigned char)s[1] < 0xA0) || |
| 3718 | ((unsigned char)s[0] == 0xED && |
| 3719 | (unsigned char)s[1] > 0x9F)) { |
| 3720 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3721 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3722 | endinpos = startinpos + 1; |
| 3723 | |
| 3724 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 3725 | continuation byte is s[2], so increment endinpos by 1, |
| 3726 | if not, s[1] is invalid and endinpos doesn't need to |
| 3727 | be incremented. */ |
| 3728 | if ((s[1] & 0xC0) == 0x80) |
| 3729 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3730 | goto utf8Error; |
| 3731 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3732 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3733 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3734 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 3735 | break; |
| 3736 | |
| 3737 | case 4: |
| 3738 | if ((s[1] & 0xc0) != 0x80 || |
| 3739 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3740 | (s[3] & 0xc0) != 0x80 || |
| 3741 | ((unsigned char)s[0] == 0xF0 && |
| 3742 | (unsigned char)s[1] < 0x90) || |
| 3743 | ((unsigned char)s[0] == 0xF4 && |
| 3744 | (unsigned char)s[1] > 0x8F)) { |
| 3745 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3746 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3747 | endinpos = startinpos + 1; |
| 3748 | if ((s[1] & 0xC0) == 0x80) { |
| 3749 | endinpos++; |
| 3750 | if ((s[2] & 0xC0) == 0x80) |
| 3751 | endinpos++; |
| 3752 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3753 | goto utf8Error; |
| 3754 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 3755 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3756 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 3757 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 3758 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3759 | /* If the string is flexible or we have native UCS-4, write |
| 3760 | directly.. */ |
| 3761 | if (sizeof(Py_UNICODE) > 2 || kind != PyUnicode_WCHAR_KIND) |
| 3762 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3763 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3764 | else { |
| 3765 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3766 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3767 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 3768 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3769 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3770 | /* high surrogate = top 10 bits added to D800 */ |
| 3771 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, |
| 3772 | (Py_UNICODE)(0xD800 + (ch >> 10))); |
| 3773 | |
| 3774 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 3775 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, |
| 3776 | (Py_UNICODE)(0xDC00 + (ch & 0x03FF))); |
| 3777 | } |
| 3778 | #if SIZEOF_WCHAR_T == 2 |
| 3779 | wchar_offset++; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3780 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3781 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3782 | } |
| 3783 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3784 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3785 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3786 | utf8Error: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3787 | /* If this is not yet a resizable string, make it one.. */ |
| 3788 | if (kind != PyUnicode_WCHAR_KIND) { |
| 3789 | const Py_UNICODE *u; |
| 3790 | PyUnicodeObject *new_unicode = _PyUnicode_New(size); |
| 3791 | if (!new_unicode) |
| 3792 | goto onError; |
| 3793 | u = PyUnicode_AsUnicode((PyObject *)unicode); |
| 3794 | if (!u) |
| 3795 | goto onError; |
| 3796 | #if SIZEOF_WCHAR_T == 2 |
| 3797 | i += wchar_offset; |
| 3798 | #endif |
| 3799 | Py_UNICODE_COPY(PyUnicode_AS_UNICODE(new_unicode), u, i); |
| 3800 | Py_DECREF(unicode); |
| 3801 | unicode = new_unicode; |
| 3802 | kind = 0; |
| 3803 | data = PyUnicode_AS_UNICODE(new_unicode); |
| 3804 | assert(data != NULL); |
| 3805 | } |
| 3806 | error_outptr = PyUnicode_AS_UNICODE(unicode) + i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3807 | if (unicode_decode_call_errorhandler( |
| 3808 | errors, &errorHandler, |
| 3809 | "utf8", errmsg, |
| 3810 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3811 | &unicode, &i, &error_outptr)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3812 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3813 | /* Update data because unicode_decode_call_errorhandler might have |
| 3814 | re-created or resized the unicode object. */ |
| 3815 | data = PyUnicode_AS_UNICODE(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3816 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3817 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3818 | /* Ensure the unicode_size calculation above was correct: */ |
| 3819 | assert(kind == PyUnicode_WCHAR_KIND || i == unicode_size); |
| 3820 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3821 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3822 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3823 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3824 | /* Adjust length and ready string when it contained errors and |
| 3825 | is of the old resizable kind. */ |
| 3826 | if (kind == PyUnicode_WCHAR_KIND) { |
| 3827 | if (_PyUnicode_Resize(&unicode, i) < 0 || |
| 3828 | PyUnicode_READY(unicode) == -1) |
| 3829 | goto onError; |
| 3830 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3831 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3832 | Py_XDECREF(errorHandler); |
| 3833 | Py_XDECREF(exc); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3834 | if (PyUnicode_READY(unicode) == -1) { |
| 3835 | Py_DECREF(unicode); |
| 3836 | return NULL; |
| 3837 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3838 | return (PyObject *)unicode; |
| 3839 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3840 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3841 | Py_XDECREF(errorHandler); |
| 3842 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3843 | Py_DECREF(unicode); |
| 3844 | return NULL; |
| 3845 | } |
| 3846 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3847 | #undef WRITE_FLEXIBLE_OR_WSTR |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3848 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 3849 | #ifdef __APPLE__ |
| 3850 | |
| 3851 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
| 3852 | used to decode the command line arguments on Mac OS X. */ |
| 3853 | |
| 3854 | wchar_t* |
| 3855 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 3856 | { |
| 3857 | int n; |
| 3858 | const char *e; |
| 3859 | wchar_t *unicode, *p; |
| 3860 | |
| 3861 | /* Note: size will always be longer than the resulting Unicode |
| 3862 | character count */ |
| 3863 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) { |
| 3864 | PyErr_NoMemory(); |
| 3865 | return NULL; |
| 3866 | } |
| 3867 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 3868 | if (!unicode) |
| 3869 | return NULL; |
| 3870 | |
| 3871 | /* Unpack UTF-8 encoded data */ |
| 3872 | p = unicode; |
| 3873 | e = s + size; |
| 3874 | while (s < e) { |
| 3875 | Py_UCS4 ch = (unsigned char)*s; |
| 3876 | |
| 3877 | if (ch < 0x80) { |
| 3878 | *p++ = (wchar_t)ch; |
| 3879 | s++; |
| 3880 | continue; |
| 3881 | } |
| 3882 | |
| 3883 | n = utf8_code_length[ch]; |
| 3884 | if (s + n > e) { |
| 3885 | goto surrogateescape; |
| 3886 | } |
| 3887 | |
| 3888 | switch (n) { |
| 3889 | case 0: |
| 3890 | case 1: |
| 3891 | goto surrogateescape; |
| 3892 | |
| 3893 | case 2: |
| 3894 | if ((s[1] & 0xc0) != 0x80) |
| 3895 | goto surrogateescape; |
| 3896 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
| 3897 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 3898 | *p++ = (wchar_t)ch; |
| 3899 | break; |
| 3900 | |
| 3901 | case 3: |
| 3902 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 3903 | will result in surrogates in range d800-dfff. Surrogates are |
| 3904 | not valid UTF-8 so they are rejected. |
| 3905 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 3906 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
| 3907 | if ((s[1] & 0xc0) != 0x80 || |
| 3908 | (s[2] & 0xc0) != 0x80 || |
| 3909 | ((unsigned char)s[0] == 0xE0 && |
| 3910 | (unsigned char)s[1] < 0xA0) || |
| 3911 | ((unsigned char)s[0] == 0xED && |
| 3912 | (unsigned char)s[1] > 0x9F)) { |
| 3913 | |
| 3914 | goto surrogateescape; |
| 3915 | } |
| 3916 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
| 3917 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3918 | *p++ = (wchar_t)ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 3919 | break; |
| 3920 | |
| 3921 | case 4: |
| 3922 | if ((s[1] & 0xc0) != 0x80 || |
| 3923 | (s[2] & 0xc0) != 0x80 || |
| 3924 | (s[3] & 0xc0) != 0x80 || |
| 3925 | ((unsigned char)s[0] == 0xF0 && |
| 3926 | (unsigned char)s[1] < 0x90) || |
| 3927 | ((unsigned char)s[0] == 0xF4 && |
| 3928 | (unsigned char)s[1] > 0x8F)) { |
| 3929 | goto surrogateescape; |
| 3930 | } |
| 3931 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 3932 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 3933 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 3934 | |
| 3935 | #if SIZEOF_WCHAR_T == 4 |
| 3936 | *p++ = (wchar_t)ch; |
| 3937 | #else |
| 3938 | /* compute and append the two surrogates: */ |
| 3939 | |
| 3940 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 3941 | ch -= 0x10000; |
| 3942 | |
| 3943 | /* high surrogate = top 10 bits added to D800 */ |
| 3944 | *p++ = (wchar_t)(0xD800 + (ch >> 10)); |
| 3945 | |
| 3946 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 3947 | *p++ = (wchar_t)(0xDC00 + (ch & 0x03FF)); |
| 3948 | #endif |
| 3949 | break; |
| 3950 | } |
| 3951 | s += n; |
| 3952 | continue; |
| 3953 | |
| 3954 | surrogateescape: |
| 3955 | *p++ = 0xDC00 + ch; |
| 3956 | s++; |
| 3957 | } |
| 3958 | *p = L'\0'; |
| 3959 | return unicode; |
| 3960 | } |
| 3961 | |
| 3962 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3963 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3964 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 3965 | |
| 3966 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 3967 | and allocate exactly as much space needed at the end. Else allocate the |
| 3968 | maximum possible needed (4 result bytes per Unicode character), and return |
| 3969 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 3970 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 3971 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3972 | _PyUnicode_AsUTF8String(PyObject *obj, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3973 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 3974 | #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] | 3975 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3976 | Py_ssize_t i; /* index into s of next input byte */ |
| 3977 | PyObject *result; /* result string object */ |
| 3978 | char *p; /* next free byte in output buffer */ |
| 3979 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 3980 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 3981 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 3982 | PyObject *errorHandler = NULL; |
| 3983 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3984 | int kind; |
| 3985 | void *data; |
| 3986 | Py_ssize_t size; |
| 3987 | PyUnicodeObject *unicode = (PyUnicodeObject *)obj; |
| 3988 | #if SIZEOF_WCHAR_T == 2 |
| 3989 | Py_ssize_t wchar_offset = 0; |
| 3990 | #endif |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 3991 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3992 | if (!PyUnicode_Check(unicode)) { |
| 3993 | PyErr_BadArgument(); |
| 3994 | return NULL; |
| 3995 | } |
| 3996 | |
| 3997 | if (PyUnicode_READY(unicode) == -1) |
| 3998 | return NULL; |
| 3999 | |
| 4000 | if (_PyUnicode_UTF8(unicode)) |
| 4001 | return PyBytes_FromStringAndSize(_PyUnicode_UTF8(unicode), |
| 4002 | _PyUnicode_UTF8_LENGTH(unicode)); |
| 4003 | |
| 4004 | kind = PyUnicode_KIND(unicode); |
| 4005 | data = PyUnicode_DATA(unicode); |
| 4006 | size = PyUnicode_GET_LENGTH(unicode); |
| 4007 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4008 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4009 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4010 | if (size <= MAX_SHORT_UNICHARS) { |
| 4011 | /* Write into the stack buffer; nallocated can't overflow. |
| 4012 | * At the end, we'll allocate exactly as much heap space as it |
| 4013 | * turns out we need. |
| 4014 | */ |
| 4015 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4016 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4017 | p = stackbuf; |
| 4018 | } |
| 4019 | else { |
| 4020 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 4021 | nallocated = size * 4; |
| 4022 | if (nallocated / 4 != size) /* overflow! */ |
| 4023 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4024 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4025 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4026 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4027 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4028 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4029 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4030 | for (i = 0; i < size;) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4031 | Py_UCS4 ch = PyUnicode_READ(kind, data, i++); |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4032 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4033 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4034 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4035 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4036 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4037 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4038 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 4039 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 4040 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4041 | } else if (0xD800 <= ch && ch <= 0xDFFF) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4042 | Py_ssize_t newpos; |
| 4043 | PyObject *rep; |
| 4044 | Py_ssize_t repsize, k, startpos; |
| 4045 | startpos = i-1; |
| 4046 | #if SIZEOF_WCHAR_T == 2 |
| 4047 | startpos += wchar_offset; |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 4048 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4049 | rep = unicode_encode_call_errorhandler( |
| 4050 | errors, &errorHandler, "utf-8", "surrogates not allowed", |
| 4051 | PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), |
| 4052 | &exc, startpos, startpos+1, &newpos); |
| 4053 | if (!rep) |
| 4054 | goto error; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4055 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4056 | if (PyBytes_Check(rep)) |
| 4057 | repsize = PyBytes_GET_SIZE(rep); |
| 4058 | else |
| 4059 | repsize = PyUnicode_GET_SIZE(rep); |
| 4060 | |
| 4061 | if (repsize > 4) { |
| 4062 | Py_ssize_t offset; |
| 4063 | |
| 4064 | if (result == NULL) |
| 4065 | offset = p - stackbuf; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4066 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4067 | offset = p - PyBytes_AS_STRING(result); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4068 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4069 | if (nallocated > PY_SSIZE_T_MAX - repsize + 4) { |
| 4070 | /* integer overflow */ |
| 4071 | PyErr_NoMemory(); |
| 4072 | goto error; |
| 4073 | } |
| 4074 | nallocated += repsize - 4; |
| 4075 | if (result != NULL) { |
| 4076 | if (_PyBytes_Resize(&result, nallocated) < 0) |
| 4077 | goto error; |
| 4078 | } else { |
| 4079 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4080 | if (result == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4081 | goto error; |
| 4082 | Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset); |
| 4083 | } |
| 4084 | p = PyBytes_AS_STRING(result) + offset; |
| 4085 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4086 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4087 | if (PyBytes_Check(rep)) { |
| 4088 | char *prep = PyBytes_AS_STRING(rep); |
| 4089 | for(k = repsize; k > 0; k--) |
| 4090 | *p++ = *prep++; |
| 4091 | } else /* rep is unicode */ { |
| 4092 | const Py_UNICODE *prep = PyUnicode_AS_UNICODE(rep); |
| 4093 | Py_UNICODE c; |
| 4094 | |
| 4095 | for(k=0; k<repsize; k++) { |
| 4096 | c = prep[k]; |
| 4097 | if (0x80 <= c) { |
| 4098 | raise_encode_exception(&exc, "utf-8", |
| 4099 | PyUnicode_AS_UNICODE(unicode), |
| 4100 | size, i-1, i, |
| 4101 | "surrogates not allowed"); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4102 | goto error; |
| 4103 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4104 | *p++ = (char)prep[k]; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4105 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4106 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4107 | Py_DECREF(rep); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4108 | } else if (ch < 0x10000) { |
| 4109 | *p++ = (char)(0xe0 | (ch >> 12)); |
| 4110 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4111 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 4112 | } else /* ch >= 0x10000 */ { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4113 | /* Encode UCS4 Unicode ordinals */ |
| 4114 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 4115 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 4116 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4117 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4118 | #if SIZEOF_WCHAR_T == 2 |
| 4119 | wchar_offset++; |
| 4120 | #endif |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4121 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4122 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 4123 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4124 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4125 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4126 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4127 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4128 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4129 | } |
| 4130 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 4131 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4132 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4133 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4134 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4135 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4136 | |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4137 | Py_XDECREF(errorHandler); |
| 4138 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4139 | return result; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4140 | error: |
| 4141 | Py_XDECREF(errorHandler); |
| 4142 | Py_XDECREF(exc); |
| 4143 | Py_XDECREF(result); |
| 4144 | return NULL; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4145 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4146 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4147 | } |
| 4148 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4149 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4150 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 4151 | Py_ssize_t size, |
| 4152 | const char *errors) |
| 4153 | { |
| 4154 | PyObject *v, *unicode; |
| 4155 | |
| 4156 | unicode = PyUnicode_FromUnicode(s, size); |
| 4157 | if (unicode == NULL) |
| 4158 | return NULL; |
| 4159 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 4160 | Py_DECREF(unicode); |
| 4161 | return v; |
| 4162 | } |
| 4163 | |
| 4164 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4165 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4166 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4167 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4168 | } |
| 4169 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4170 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 4171 | |
| 4172 | PyObject * |
| 4173 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4174 | Py_ssize_t size, |
| 4175 | const char *errors, |
| 4176 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4177 | { |
| 4178 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 4179 | } |
| 4180 | |
| 4181 | PyObject * |
| 4182 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4183 | Py_ssize_t size, |
| 4184 | const char *errors, |
| 4185 | int *byteorder, |
| 4186 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4187 | { |
| 4188 | const char *starts = s; |
| 4189 | Py_ssize_t startinpos; |
| 4190 | Py_ssize_t endinpos; |
| 4191 | Py_ssize_t outpos; |
| 4192 | PyUnicodeObject *unicode; |
| 4193 | Py_UNICODE *p; |
| 4194 | #ifndef Py_UNICODE_WIDE |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4195 | int pairs = 0; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4196 | const unsigned char *qq; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4197 | #else |
| 4198 | const int pairs = 0; |
| 4199 | #endif |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4200 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4201 | int bo = 0; /* assume native ordering by default */ |
| 4202 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4203 | /* Offsets from q for retrieving bytes in the right order. */ |
| 4204 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4205 | int iorder[] = {0, 1, 2, 3}; |
| 4206 | #else |
| 4207 | int iorder[] = {3, 2, 1, 0}; |
| 4208 | #endif |
| 4209 | PyObject *errorHandler = NULL; |
| 4210 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4211 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4212 | q = (unsigned char *)s; |
| 4213 | e = q + size; |
| 4214 | |
| 4215 | if (byteorder) |
| 4216 | bo = *byteorder; |
| 4217 | |
| 4218 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4219 | byte order setting accordingly. In native mode, the leading BOM |
| 4220 | mark is skipped, in all other modes, it is copied to the output |
| 4221 | stream as-is (giving a ZWNBSP character). */ |
| 4222 | if (bo == 0) { |
| 4223 | if (size >= 4) { |
| 4224 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4225 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4226 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4227 | if (bom == 0x0000FEFF) { |
| 4228 | q += 4; |
| 4229 | bo = -1; |
| 4230 | } |
| 4231 | else if (bom == 0xFFFE0000) { |
| 4232 | q += 4; |
| 4233 | bo = 1; |
| 4234 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4235 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4236 | if (bom == 0x0000FEFF) { |
| 4237 | q += 4; |
| 4238 | bo = 1; |
| 4239 | } |
| 4240 | else if (bom == 0xFFFE0000) { |
| 4241 | q += 4; |
| 4242 | bo = -1; |
| 4243 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4244 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4245 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4246 | } |
| 4247 | |
| 4248 | if (bo == -1) { |
| 4249 | /* force LE */ |
| 4250 | iorder[0] = 0; |
| 4251 | iorder[1] = 1; |
| 4252 | iorder[2] = 2; |
| 4253 | iorder[3] = 3; |
| 4254 | } |
| 4255 | else if (bo == 1) { |
| 4256 | /* force BE */ |
| 4257 | iorder[0] = 3; |
| 4258 | iorder[1] = 2; |
| 4259 | iorder[2] = 1; |
| 4260 | iorder[3] = 0; |
| 4261 | } |
| 4262 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4263 | /* On narrow builds we split characters outside the BMP into two |
| 4264 | codepoints => count how much extra space we need. */ |
| 4265 | #ifndef Py_UNICODE_WIDE |
| 4266 | for (qq = q; qq < e; qq += 4) |
| 4267 | if (qq[iorder[2]] != 0 || qq[iorder[3]] != 0) |
| 4268 | pairs++; |
| 4269 | #endif |
| 4270 | |
| 4271 | /* This might be one to much, because of a BOM */ |
| 4272 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 4273 | if (!unicode) |
| 4274 | return NULL; |
| 4275 | if (size == 0) |
| 4276 | return (PyObject *)unicode; |
| 4277 | |
| 4278 | /* Unpack UTF-32 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4279 | p = PyUnicode_AS_UNICODE(unicode); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4280 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4281 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4282 | Py_UCS4 ch; |
| 4283 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 4284 | if (e-q<4) { |
| 4285 | if (consumed) |
| 4286 | break; |
| 4287 | errmsg = "truncated data"; |
| 4288 | startinpos = ((const char *)q)-starts; |
| 4289 | endinpos = ((const char *)e)-starts; |
| 4290 | goto utf32Error; |
| 4291 | /* The remaining input chars are ignored if the callback |
| 4292 | chooses to skip the input */ |
| 4293 | } |
| 4294 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 4295 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4296 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4297 | if (ch >= 0x110000) |
| 4298 | { |
| 4299 | errmsg = "codepoint not in range(0x110000)"; |
| 4300 | startinpos = ((const char *)q)-starts; |
| 4301 | endinpos = startinpos+4; |
| 4302 | goto utf32Error; |
| 4303 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4304 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4305 | if (ch >= 0x10000) |
| 4306 | { |
| 4307 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 4308 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 4309 | } |
| 4310 | else |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4311 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4312 | *p++ = ch; |
| 4313 | q += 4; |
| 4314 | continue; |
| 4315 | utf32Error: |
| 4316 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 4317 | if (unicode_decode_call_errorhandler( |
| 4318 | errors, &errorHandler, |
| 4319 | "utf32", errmsg, |
| 4320 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
| 4321 | &unicode, &outpos, &p)) |
| 4322 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4323 | } |
| 4324 | |
| 4325 | if (byteorder) |
| 4326 | *byteorder = bo; |
| 4327 | |
| 4328 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4329 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4330 | |
| 4331 | /* Adjust length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4332 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4333 | goto onError; |
| 4334 | |
| 4335 | Py_XDECREF(errorHandler); |
| 4336 | Py_XDECREF(exc); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4337 | if (PyUnicode_READY(unicode) == -1) { |
| 4338 | Py_DECREF(unicode); |
| 4339 | return NULL; |
| 4340 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4341 | return (PyObject *)unicode; |
| 4342 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4343 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4344 | Py_DECREF(unicode); |
| 4345 | Py_XDECREF(errorHandler); |
| 4346 | Py_XDECREF(exc); |
| 4347 | return NULL; |
| 4348 | } |
| 4349 | |
| 4350 | PyObject * |
| 4351 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4352 | Py_ssize_t size, |
| 4353 | const char *errors, |
| 4354 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4355 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4356 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4357 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4358 | Py_ssize_t nsize, bytesize; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4359 | #ifndef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4360 | Py_ssize_t i, pairs; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4361 | #else |
| 4362 | const int pairs = 0; |
| 4363 | #endif |
| 4364 | /* Offsets from p for storing byte pairs in the right order. */ |
| 4365 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4366 | int iorder[] = {0, 1, 2, 3}; |
| 4367 | #else |
| 4368 | int iorder[] = {3, 2, 1, 0}; |
| 4369 | #endif |
| 4370 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4371 | #define STORECHAR(CH) \ |
| 4372 | do { \ |
| 4373 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 4374 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 4375 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 4376 | p[iorder[0]] = (CH) & 0xff; \ |
| 4377 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4378 | } while(0) |
| 4379 | |
| 4380 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 4381 | so we need less space. */ |
| 4382 | #ifndef Py_UNICODE_WIDE |
| 4383 | for (i = pairs = 0; i < size-1; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4384 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 4385 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 4386 | pairs++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4387 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4388 | nsize = (size - pairs + (byteorder == 0)); |
| 4389 | bytesize = nsize * 4; |
| 4390 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4391 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4392 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4393 | if (v == NULL) |
| 4394 | return NULL; |
| 4395 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4396 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4397 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4398 | STORECHAR(0xFEFF); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4399 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4400 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4401 | |
| 4402 | if (byteorder == -1) { |
| 4403 | /* force LE */ |
| 4404 | iorder[0] = 0; |
| 4405 | iorder[1] = 1; |
| 4406 | iorder[2] = 2; |
| 4407 | iorder[3] = 3; |
| 4408 | } |
| 4409 | else if (byteorder == 1) { |
| 4410 | /* force BE */ |
| 4411 | iorder[0] = 3; |
| 4412 | iorder[1] = 2; |
| 4413 | iorder[2] = 1; |
| 4414 | iorder[3] = 0; |
| 4415 | } |
| 4416 | |
| 4417 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4418 | Py_UCS4 ch = *s++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4419 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4420 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 4421 | Py_UCS4 ch2 = *s; |
| 4422 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 4423 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 4424 | s++; |
| 4425 | size--; |
| 4426 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4427 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4428 | #endif |
| 4429 | STORECHAR(ch); |
| 4430 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4431 | |
| 4432 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4433 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4434 | #undef STORECHAR |
| 4435 | } |
| 4436 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4437 | PyObject * |
| 4438 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4439 | { |
| 4440 | if (!PyUnicode_Check(unicode)) { |
| 4441 | PyErr_BadArgument(); |
| 4442 | return NULL; |
| 4443 | } |
| 4444 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4445 | PyUnicode_GET_SIZE(unicode), |
| 4446 | NULL, |
| 4447 | 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4448 | } |
| 4449 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4450 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 4451 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4452 | PyObject * |
| 4453 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4454 | Py_ssize_t size, |
| 4455 | const char *errors, |
| 4456 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4457 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4458 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 4459 | } |
| 4460 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4461 | /* Two masks for fast checking of whether a C 'long' may contain |
| 4462 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 4463 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 4464 | rare in most input. |
| 4465 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 4466 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4467 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4468 | #if (SIZEOF_LONG == 8) |
| 4469 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 4470 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 4471 | #elif (SIZEOF_LONG == 4) |
| 4472 | # define FAST_CHAR_MASK 0x80008000L |
| 4473 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 4474 | #else |
| 4475 | # error C 'long' size should be either 4 or 8! |
| 4476 | #endif |
| 4477 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4478 | PyObject * |
| 4479 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4480 | Py_ssize_t size, |
| 4481 | const char *errors, |
| 4482 | int *byteorder, |
| 4483 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4484 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4485 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4486 | Py_ssize_t startinpos; |
| 4487 | Py_ssize_t endinpos; |
| 4488 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4489 | PyUnicodeObject *unicode; |
| 4490 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4491 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4492 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4493 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4494 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4495 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 4496 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4497 | int ihi = 1, ilo = 0; |
| 4498 | #else |
| 4499 | int ihi = 0, ilo = 1; |
| 4500 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4501 | PyObject *errorHandler = NULL; |
| 4502 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4503 | |
| 4504 | /* Note: size will always be longer than the resulting Unicode |
| 4505 | character count */ |
| 4506 | unicode = _PyUnicode_New(size); |
| 4507 | if (!unicode) |
| 4508 | return NULL; |
| 4509 | if (size == 0) |
| 4510 | return (PyObject *)unicode; |
| 4511 | |
| 4512 | /* Unpack UTF-16 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4513 | p = PyUnicode_AS_UNICODE(unicode); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4514 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4515 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4516 | |
| 4517 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4518 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4519 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 4520 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4521 | byte order setting accordingly. In native mode, the leading BOM |
| 4522 | mark is skipped, in all other modes, it is copied to the output |
| 4523 | stream as-is (giving a ZWNBSP character). */ |
| 4524 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4525 | if (size >= 2) { |
| 4526 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 4527 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4528 | if (bom == 0xFEFF) { |
| 4529 | q += 2; |
| 4530 | bo = -1; |
| 4531 | } |
| 4532 | else if (bom == 0xFFFE) { |
| 4533 | q += 2; |
| 4534 | bo = 1; |
| 4535 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4536 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4537 | if (bom == 0xFEFF) { |
| 4538 | q += 2; |
| 4539 | bo = 1; |
| 4540 | } |
| 4541 | else if (bom == 0xFFFE) { |
| 4542 | q += 2; |
| 4543 | bo = -1; |
| 4544 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 4545 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4546 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 4547 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4548 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4549 | if (bo == -1) { |
| 4550 | /* force LE */ |
| 4551 | ihi = 1; |
| 4552 | ilo = 0; |
| 4553 | } |
| 4554 | else if (bo == 1) { |
| 4555 | /* force BE */ |
| 4556 | ihi = 0; |
| 4557 | ilo = 1; |
| 4558 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4559 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4560 | native_ordering = ilo < ihi; |
| 4561 | #else |
| 4562 | native_ordering = ilo > ihi; |
| 4563 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4564 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4565 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4566 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4567 | Py_UNICODE ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4568 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 4569 | reads are more expensive, better to defer to another iteration. */ |
| 4570 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 4571 | /* Fast path for runs of non-surrogate chars. */ |
| 4572 | register const unsigned char *_q = q; |
| 4573 | Py_UNICODE *_p = p; |
| 4574 | if (native_ordering) { |
| 4575 | /* Native ordering is simple: as long as the input cannot |
| 4576 | possibly contain a surrogate char, do an unrolled copy |
| 4577 | of several 16-bit code points to the target object. |
| 4578 | The non-surrogate check is done on several input bytes |
| 4579 | at a time (as many as a C 'long' can contain). */ |
| 4580 | while (_q < aligned_end) { |
| 4581 | unsigned long data = * (unsigned long *) _q; |
| 4582 | if (data & FAST_CHAR_MASK) |
| 4583 | break; |
| 4584 | _p[0] = ((unsigned short *) _q)[0]; |
| 4585 | _p[1] = ((unsigned short *) _q)[1]; |
| 4586 | #if (SIZEOF_LONG == 8) |
| 4587 | _p[2] = ((unsigned short *) _q)[2]; |
| 4588 | _p[3] = ((unsigned short *) _q)[3]; |
| 4589 | #endif |
| 4590 | _q += SIZEOF_LONG; |
| 4591 | _p += SIZEOF_LONG / 2; |
| 4592 | } |
| 4593 | } |
| 4594 | else { |
| 4595 | /* Byteswapped ordering is similar, but we must decompose |
| 4596 | the copy bytewise, and take care of zero'ing out the |
| 4597 | upper bytes if the target object is in 32-bit units |
| 4598 | (that is, in UCS-4 builds). */ |
| 4599 | while (_q < aligned_end) { |
| 4600 | unsigned long data = * (unsigned long *) _q; |
| 4601 | if (data & SWAPPED_FAST_CHAR_MASK) |
| 4602 | break; |
| 4603 | /* Zero upper bytes in UCS-4 builds */ |
| 4604 | #if (Py_UNICODE_SIZE > 2) |
| 4605 | _p[0] = 0; |
| 4606 | _p[1] = 0; |
| 4607 | #if (SIZEOF_LONG == 8) |
| 4608 | _p[2] = 0; |
| 4609 | _p[3] = 0; |
| 4610 | #endif |
| 4611 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 4612 | /* Issue #4916; UCS-4 builds on big endian machines must |
| 4613 | fill the two last bytes of each 4-byte unit. */ |
| 4614 | #if (!defined(BYTEORDER_IS_LITTLE_ENDIAN) && Py_UNICODE_SIZE > 2) |
| 4615 | # define OFF 2 |
| 4616 | #else |
| 4617 | # define OFF 0 |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4618 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 4619 | ((unsigned char *) _p)[OFF + 1] = _q[0]; |
| 4620 | ((unsigned char *) _p)[OFF + 0] = _q[1]; |
| 4621 | ((unsigned char *) _p)[OFF + 1 + Py_UNICODE_SIZE] = _q[2]; |
| 4622 | ((unsigned char *) _p)[OFF + 0 + Py_UNICODE_SIZE] = _q[3]; |
| 4623 | #if (SIZEOF_LONG == 8) |
| 4624 | ((unsigned char *) _p)[OFF + 1 + 2 * Py_UNICODE_SIZE] = _q[4]; |
| 4625 | ((unsigned char *) _p)[OFF + 0 + 2 * Py_UNICODE_SIZE] = _q[5]; |
| 4626 | ((unsigned char *) _p)[OFF + 1 + 3 * Py_UNICODE_SIZE] = _q[6]; |
| 4627 | ((unsigned char *) _p)[OFF + 0 + 3 * Py_UNICODE_SIZE] = _q[7]; |
| 4628 | #endif |
| 4629 | #undef OFF |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4630 | _q += SIZEOF_LONG; |
| 4631 | _p += SIZEOF_LONG / 2; |
| 4632 | } |
| 4633 | } |
| 4634 | p = _p; |
| 4635 | q = _q; |
| 4636 | if (q >= e) |
| 4637 | break; |
| 4638 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4639 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4640 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4641 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4642 | |
| 4643 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 4644 | *p++ = ch; |
| 4645 | continue; |
| 4646 | } |
| 4647 | |
| 4648 | /* UTF-16 code pair: */ |
| 4649 | if (q > e) { |
| 4650 | errmsg = "unexpected end of data"; |
| 4651 | startinpos = (((const char *)q) - 2) - starts; |
| 4652 | endinpos = ((const char *)e) + 1 - starts; |
| 4653 | goto utf16Error; |
| 4654 | } |
| 4655 | if (0xD800 <= ch && ch <= 0xDBFF) { |
| 4656 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 4657 | q += 2; |
| 4658 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 4659 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4660 | *p++ = ch; |
| 4661 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4662 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4663 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4664 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4665 | continue; |
| 4666 | } |
| 4667 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4668 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4669 | startinpos = (((const char *)q)-4)-starts; |
| 4670 | endinpos = startinpos+2; |
| 4671 | goto utf16Error; |
| 4672 | } |
| 4673 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4674 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4675 | errmsg = "illegal encoding"; |
| 4676 | startinpos = (((const char *)q)-2)-starts; |
| 4677 | endinpos = startinpos+2; |
| 4678 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4679 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4680 | utf16Error: |
| 4681 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 4682 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4683 | errors, |
| 4684 | &errorHandler, |
| 4685 | "utf16", errmsg, |
| 4686 | &starts, |
| 4687 | (const char **)&e, |
| 4688 | &startinpos, |
| 4689 | &endinpos, |
| 4690 | &exc, |
| 4691 | (const char **)&q, |
| 4692 | &unicode, |
| 4693 | &outpos, |
| 4694 | &p)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4695 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4696 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4697 | /* remaining byte at the end? (size should be even) */ |
| 4698 | if (e == q) { |
| 4699 | if (!consumed) { |
| 4700 | errmsg = "truncated data"; |
| 4701 | startinpos = ((const char *)q) - starts; |
| 4702 | endinpos = ((const char *)e) + 1 - starts; |
| 4703 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 4704 | if (unicode_decode_call_errorhandler( |
| 4705 | errors, |
| 4706 | &errorHandler, |
| 4707 | "utf16", errmsg, |
| 4708 | &starts, |
| 4709 | (const char **)&e, |
| 4710 | &startinpos, |
| 4711 | &endinpos, |
| 4712 | &exc, |
| 4713 | (const char **)&q, |
| 4714 | &unicode, |
| 4715 | &outpos, |
| 4716 | &p)) |
| 4717 | goto onError; |
| 4718 | /* The remaining input chars are ignored if the callback |
| 4719 | chooses to skip the input */ |
| 4720 | } |
| 4721 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4722 | |
| 4723 | if (byteorder) |
| 4724 | *byteorder = bo; |
| 4725 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4726 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4727 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4728 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4729 | /* Adjust length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4730 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4731 | goto onError; |
| 4732 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4733 | Py_XDECREF(errorHandler); |
| 4734 | Py_XDECREF(exc); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4735 | if (PyUnicode_READY(unicode) == -1) { |
| 4736 | Py_DECREF(unicode); |
| 4737 | return NULL; |
| 4738 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4739 | return (PyObject *)unicode; |
| 4740 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4741 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4742 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4743 | Py_XDECREF(errorHandler); |
| 4744 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4745 | return NULL; |
| 4746 | } |
| 4747 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4748 | #undef FAST_CHAR_MASK |
| 4749 | #undef SWAPPED_FAST_CHAR_MASK |
| 4750 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4751 | PyObject * |
| 4752 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4753 | Py_ssize_t size, |
| 4754 | const char *errors, |
| 4755 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4756 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4757 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4758 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4759 | Py_ssize_t nsize, bytesize; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4760 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4761 | Py_ssize_t i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4762 | #else |
| 4763 | const int pairs = 0; |
| 4764 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4765 | /* Offsets from p for storing byte pairs in the right order. */ |
| 4766 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4767 | int ihi = 1, ilo = 0; |
| 4768 | #else |
| 4769 | int ihi = 0, ilo = 1; |
| 4770 | #endif |
| 4771 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4772 | #define STORECHAR(CH) \ |
| 4773 | do { \ |
| 4774 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 4775 | p[ilo] = (CH) & 0xff; \ |
| 4776 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4777 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4778 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4779 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4780 | for (i = pairs = 0; i < size; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4781 | if (s[i] >= 0x10000) |
| 4782 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4783 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4784 | /* 2 * (size + pairs + (byteorder == 0)) */ |
| 4785 | if (size > PY_SSIZE_T_MAX || |
| 4786 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4787 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4788 | nsize = size + pairs + (byteorder == 0); |
| 4789 | bytesize = nsize * 2; |
| 4790 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4791 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4792 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4793 | if (v == NULL) |
| 4794 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4795 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4796 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4797 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4798 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 4799 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4800 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4801 | |
| 4802 | if (byteorder == -1) { |
| 4803 | /* force LE */ |
| 4804 | ihi = 1; |
| 4805 | ilo = 0; |
| 4806 | } |
| 4807 | else if (byteorder == 1) { |
| 4808 | /* force BE */ |
| 4809 | ihi = 0; |
| 4810 | ilo = 1; |
| 4811 | } |
| 4812 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4813 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4814 | Py_UNICODE ch = *s++; |
| 4815 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4816 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4817 | if (ch >= 0x10000) { |
| 4818 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 4819 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 4820 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4821 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4822 | STORECHAR(ch); |
| 4823 | if (ch2) |
| 4824 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4825 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4826 | |
| 4827 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4828 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4829 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4830 | } |
| 4831 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4832 | PyObject * |
| 4833 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4834 | { |
| 4835 | if (!PyUnicode_Check(unicode)) { |
| 4836 | PyErr_BadArgument(); |
| 4837 | return NULL; |
| 4838 | } |
| 4839 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4840 | PyUnicode_GET_SIZE(unicode), |
| 4841 | NULL, |
| 4842 | 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4843 | } |
| 4844 | |
| 4845 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 4846 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4847 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 4848 | if all the escapes in the string make it still a valid ASCII string. |
| 4849 | Returns -1 if any escapes were found which cause the string to |
| 4850 | pop out of ASCII range. Otherwise returns the length of the |
| 4851 | required buffer to hold the string. |
| 4852 | */ |
| 4853 | Py_ssize_t |
| 4854 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 4855 | { |
| 4856 | const unsigned char *p = (const unsigned char *)s; |
| 4857 | const unsigned char *end = p + size; |
| 4858 | Py_ssize_t length = 0; |
| 4859 | |
| 4860 | if (size < 0) |
| 4861 | return -1; |
| 4862 | |
| 4863 | for (; p < end; ++p) { |
| 4864 | if (*p > 127) { |
| 4865 | /* Non-ASCII */ |
| 4866 | return -1; |
| 4867 | } |
| 4868 | else if (*p != '\\') { |
| 4869 | /* Normal character */ |
| 4870 | ++length; |
| 4871 | } |
| 4872 | else { |
| 4873 | /* Backslash-escape, check next char */ |
| 4874 | ++p; |
| 4875 | /* Escape sequence reaches till end of string or |
| 4876 | non-ASCII follow-up. */ |
| 4877 | if (p >= end || *p > 127) |
| 4878 | return -1; |
| 4879 | switch (*p) { |
| 4880 | case '\n': |
| 4881 | /* backslash + \n result in zero characters */ |
| 4882 | break; |
| 4883 | case '\\': case '\'': case '\"': |
| 4884 | case 'b': case 'f': case 't': |
| 4885 | case 'n': case 'r': case 'v': case 'a': |
| 4886 | ++length; |
| 4887 | break; |
| 4888 | case '0': case '1': case '2': case '3': |
| 4889 | case '4': case '5': case '6': case '7': |
| 4890 | case 'x': case 'u': case 'U': case 'N': |
| 4891 | /* these do not guarantee ASCII characters */ |
| 4892 | return -1; |
| 4893 | default: |
| 4894 | /* count the backslash + the other character */ |
| 4895 | length += 2; |
| 4896 | } |
| 4897 | } |
| 4898 | } |
| 4899 | return length; |
| 4900 | } |
| 4901 | |
| 4902 | /* Similar to PyUnicode_WRITE but either write into wstr field |
| 4903 | or treat string as ASCII. */ |
| 4904 | #define WRITE_ASCII_OR_WSTR(kind, buf, index, value) \ |
| 4905 | do { \ |
| 4906 | if ((kind) != PyUnicode_WCHAR_KIND) \ |
| 4907 | ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \ |
| 4908 | else \ |
| 4909 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \ |
| 4910 | } while (0) |
| 4911 | |
| 4912 | #define WRITE_WSTR(buf, index, value) \ |
| 4913 | assert(kind == PyUnicode_WCHAR_KIND), \ |
| 4914 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value) |
| 4915 | |
| 4916 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 4917 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 4918 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4919 | PyObject * |
| 4920 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4921 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 4922 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4923 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4924 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4925 | Py_ssize_t startinpos; |
| 4926 | Py_ssize_t endinpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4927 | int j; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4928 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4929 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4930 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 4931 | char* message; |
| 4932 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4933 | PyObject *errorHandler = NULL; |
| 4934 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4935 | Py_ssize_t ascii_length; |
| 4936 | Py_ssize_t i; |
| 4937 | int kind; |
| 4938 | void *data; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 4939 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4940 | ascii_length = length_of_escaped_ascii_string(s, size); |
| 4941 | |
| 4942 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 4943 | either the string is pure ASCII with named escapes like \n, etc. |
| 4944 | and we determined it's exact size (common case) |
| 4945 | or it contains \x, \u, ... escape sequences. then we create a |
| 4946 | legacy wchar string and resize it at the end of this function. */ |
| 4947 | if (ascii_length >= 0) { |
| 4948 | v = (PyUnicodeObject *)PyUnicode_New(ascii_length, 127); |
| 4949 | if (!v) |
| 4950 | goto onError; |
| 4951 | assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND); |
| 4952 | kind = PyUnicode_1BYTE_KIND; |
| 4953 | data = PyUnicode_DATA(v); |
| 4954 | } |
| 4955 | else { |
| 4956 | /* Escaped strings will always be longer than the resulting |
| 4957 | Unicode string, so we start with size here and then reduce the |
| 4958 | length after conversion to the true value. |
| 4959 | (but if the error callback returns a long replacement string |
| 4960 | we'll have to allocate more space) */ |
| 4961 | v = _PyUnicode_New(size); |
| 4962 | if (!v) |
| 4963 | goto onError; |
| 4964 | kind = PyUnicode_WCHAR_KIND; |
| 4965 | data = PyUnicode_AS_UNICODE(v); |
| 4966 | } |
| 4967 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4968 | if (size == 0) |
| 4969 | return (PyObject *)v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4970 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4971 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 4972 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4973 | while (s < end) { |
| 4974 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 4975 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4976 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4977 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4978 | if (kind == PyUnicode_WCHAR_KIND) { |
| 4979 | assert(i < _PyUnicode_WSTR_LENGTH(v)); |
| 4980 | } |
| 4981 | else { |
| 4982 | /* The only case in which i == ascii_length is a backslash |
| 4983 | followed by a newline. */ |
| 4984 | assert(i <= ascii_length); |
| 4985 | } |
| 4986 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4987 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 4988 | if (*s != '\\') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4989 | WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char) *s++); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4990 | continue; |
| 4991 | } |
| 4992 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4993 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4994 | /* \ - Escapes */ |
| 4995 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 4996 | c = *s++; |
| 4997 | if (s > end) |
| 4998 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4999 | |
| 5000 | if (kind == PyUnicode_WCHAR_KIND) { |
| 5001 | assert(i < _PyUnicode_WSTR_LENGTH(v)); |
| 5002 | } |
| 5003 | else { |
| 5004 | /* The only case in which i == ascii_length is a backslash |
| 5005 | followed by a newline. */ |
| 5006 | assert(i < ascii_length || (i == ascii_length && c == '\n')); |
| 5007 | } |
| 5008 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5009 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5010 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5011 | /* \x escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5012 | case '\n': break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5013 | case '\\': WRITE_ASCII_OR_WSTR(kind, data, i++, '\\'); break; |
| 5014 | case '\'': WRITE_ASCII_OR_WSTR(kind, data, i++, '\''); break; |
| 5015 | case '\"': WRITE_ASCII_OR_WSTR(kind, data, i++, '\"'); break; |
| 5016 | case 'b': WRITE_ASCII_OR_WSTR(kind, data, i++, '\b'); break; |
| 5017 | /* FF */ |
| 5018 | case 'f': WRITE_ASCII_OR_WSTR(kind, data, i++, '\014'); break; |
| 5019 | case 't': WRITE_ASCII_OR_WSTR(kind, data, i++, '\t'); break; |
| 5020 | case 'n': WRITE_ASCII_OR_WSTR(kind, data, i++, '\n'); break; |
| 5021 | case 'r': WRITE_ASCII_OR_WSTR(kind, data, i++, '\r'); break; |
| 5022 | /* VT */ |
| 5023 | case 'v': WRITE_ASCII_OR_WSTR(kind, data, i++, '\013'); break; |
| 5024 | /* BEL, not classic C */ |
| 5025 | case 'a': WRITE_ASCII_OR_WSTR(kind, data, i++, '\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5026 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5027 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5028 | case '0': case '1': case '2': case '3': |
| 5029 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5030 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5031 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5032 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5033 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5034 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5035 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5036 | WRITE_WSTR(data, i++, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5037 | break; |
| 5038 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5039 | /* hex escapes */ |
| 5040 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5041 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5042 | digits = 2; |
| 5043 | message = "truncated \\xXX escape"; |
| 5044 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5045 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5046 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5047 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5048 | digits = 4; |
| 5049 | message = "truncated \\uXXXX escape"; |
| 5050 | goto hexescape; |
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 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5053 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5054 | digits = 8; |
| 5055 | message = "truncated \\UXXXXXXXX escape"; |
| 5056 | hexescape: |
| 5057 | chr = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5058 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5059 | if (s+digits>end) { |
| 5060 | endinpos = size; |
| 5061 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5062 | errors, &errorHandler, |
| 5063 | "unicodeescape", "end of string in escape sequence", |
| 5064 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5065 | &v, &i, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5066 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5067 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5068 | goto nextByte; |
| 5069 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5070 | for (j = 0; j < digits; ++j) { |
| 5071 | c = (unsigned char) s[j]; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5072 | if (!Py_ISXDIGIT(c)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5073 | endinpos = (s+j+1)-starts; |
| 5074 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5075 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5076 | errors, &errorHandler, |
| 5077 | "unicodeescape", message, |
| 5078 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5079 | &v, &i, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5080 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5081 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5082 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5083 | } |
| 5084 | chr = (chr<<4) & ~0xF; |
| 5085 | if (c >= '0' && c <= '9') |
| 5086 | chr += c - '0'; |
| 5087 | else if (c >= 'a' && c <= 'f') |
| 5088 | chr += 10 + c - 'a'; |
| 5089 | else |
| 5090 | chr += 10 + c - 'A'; |
| 5091 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5092 | s += j; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5093 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5094 | /* _decoding_error will have already written into the |
| 5095 | target buffer. */ |
| 5096 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5097 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5098 | /* when we get here, chr is a 32-bit unicode character */ |
| 5099 | if (chr <= 0xffff) |
| 5100 | /* UCS-2 character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5101 | WRITE_WSTR(data, i++, chr); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5102 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5103 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5104 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 5105 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5106 | WRITE_WSTR(data, i++, chr); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5107 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5108 | chr -= 0x10000L; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5109 | WRITE_WSTR(data, i++, 0xD800 + (Py_UNICODE) (chr >> 10)); |
| 5110 | WRITE_WSTR(data, i++, 0xDC00 + (Py_UNICODE) (chr & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5111 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5112 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5113 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5114 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5115 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5116 | errors, &errorHandler, |
| 5117 | "unicodeescape", "illegal Unicode character", |
| 5118 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5119 | &v, &i, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5120 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5121 | data = PyUnicode_AS_UNICODE(v); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5122 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5123 | break; |
| 5124 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5125 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5126 | case 'N': |
| 5127 | message = "malformed \\N character escape"; |
| 5128 | if (ucnhash_CAPI == NULL) { |
| 5129 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5130 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5131 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5132 | if (ucnhash_CAPI == NULL) |
| 5133 | goto ucnhashError; |
| 5134 | } |
| 5135 | if (*s == '{') { |
| 5136 | const char *start = s+1; |
| 5137 | /* look for the closing brace */ |
| 5138 | while (*s != '}' && s < end) |
| 5139 | s++; |
| 5140 | if (s > start && s < end && *s == '}') { |
| 5141 | /* found a name. look it up in the unicode database */ |
| 5142 | message = "unknown Unicode character name"; |
| 5143 | s++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5144 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
| 5145 | &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5146 | goto store; |
| 5147 | } |
| 5148 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5149 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5150 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5151 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5152 | errors, &errorHandler, |
| 5153 | "unicodeescape", message, |
| 5154 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5155 | &v, &i, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5156 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5157 | data = PyUnicode_AS_UNICODE(v); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5158 | break; |
| 5159 | |
| 5160 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5161 | if (s > end) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5162 | assert(kind == PyUnicode_WCHAR_KIND); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5163 | message = "\\ at end of string"; |
| 5164 | s--; |
| 5165 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5166 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5167 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5168 | errors, &errorHandler, |
| 5169 | "unicodeescape", message, |
| 5170 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5171 | &v, &i, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5172 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5173 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5174 | } |
| 5175 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5176 | WRITE_ASCII_OR_WSTR(kind, data, i++, '\\'); |
| 5177 | WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char)s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5178 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5179 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5180 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5181 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5182 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5183 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5184 | /* Ensure the length prediction worked in case of ASCII strings */ |
| 5185 | assert(kind == PyUnicode_WCHAR_KIND || i == ascii_length); |
| 5186 | |
| 5187 | if (kind == PyUnicode_WCHAR_KIND && (_PyUnicode_Resize(&v, i) < 0 || |
| 5188 | PyUnicode_READY(v) == -1)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5189 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5190 | Py_XDECREF(errorHandler); |
| 5191 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5192 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5193 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5194 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5195 | PyErr_SetString( |
| 5196 | PyExc_UnicodeError, |
| 5197 | "\\N escapes not supported (can't load unicodedata module)" |
| 5198 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5199 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5200 | Py_XDECREF(errorHandler); |
| 5201 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5202 | return NULL; |
| 5203 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5204 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5205 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5206 | Py_XDECREF(errorHandler); |
| 5207 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5208 | return NULL; |
| 5209 | } |
| 5210 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5211 | #undef WRITE_ASCII_OR_WSTR |
| 5212 | #undef WRITE_WSTR |
| 5213 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5214 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5215 | |
| 5216 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5217 | appropriate. |
| 5218 | |
| 5219 | */ |
| 5220 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5221 | static const char *hexdigits = "0123456789abcdef"; |
| 5222 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5223 | PyObject * |
| 5224 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5225 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5226 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5227 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5228 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5229 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5230 | #ifdef Py_UNICODE_WIDE |
| 5231 | const Py_ssize_t expandsize = 10; |
| 5232 | #else |
| 5233 | const Py_ssize_t expandsize = 6; |
| 5234 | #endif |
| 5235 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5236 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 5237 | better to choose a different scheme. Perhaps scan the |
| 5238 | first N-chars of the string and allocate based on that size. |
| 5239 | */ |
| 5240 | /* Initial allocation is based on the longest-possible unichr |
| 5241 | escape. |
| 5242 | |
| 5243 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 5244 | unichr, so in this case it's the longest unichr escape. In |
| 5245 | narrow (UTF-16) builds this is five chars per source unichr |
| 5246 | since there are two unichrs in the surrogate pair, so in narrow |
| 5247 | (UTF-16) builds it's not the longest unichr escape. |
| 5248 | |
| 5249 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 5250 | so in the narrow (UTF-16) build case it's the longest unichr |
| 5251 | escape. |
| 5252 | */ |
| 5253 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5254 | if (size == 0) |
| 5255 | return PyBytes_FromStringAndSize(NULL, 0); |
| 5256 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5257 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5258 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5259 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5260 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5261 | 2 |
| 5262 | + expandsize*size |
| 5263 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5264 | if (repr == NULL) |
| 5265 | return NULL; |
| 5266 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5267 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5268 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5269 | while (size-- > 0) { |
| 5270 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5271 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5272 | /* Escape backslashes */ |
| 5273 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5274 | *p++ = '\\'; |
| 5275 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5276 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5277 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5278 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 5279 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5280 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 5281 | else if (ch >= 0x10000) { |
| 5282 | *p++ = '\\'; |
| 5283 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5284 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 5285 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 5286 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 5287 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 5288 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 5289 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 5290 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 5291 | *p++ = hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5292 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5293 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5294 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5295 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 5296 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 5297 | Py_UNICODE ch2; |
| 5298 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5299 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5300 | ch2 = *s++; |
| 5301 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 5302 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5303 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 5304 | *p++ = '\\'; |
| 5305 | *p++ = 'U'; |
| 5306 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 5307 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 5308 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 5309 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 5310 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 5311 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 5312 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 5313 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 5314 | continue; |
| 5315 | } |
| 5316 | /* Fall through: isolated surrogates are copied as-is */ |
| 5317 | s--; |
| 5318 | size++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5319 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5320 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5321 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5322 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5323 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5324 | *p++ = '\\'; |
| 5325 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5326 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 5327 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 5328 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 5329 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5330 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5331 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5332 | /* Map special whitespace to '\t', \n', '\r' */ |
| 5333 | else if (ch == '\t') { |
| 5334 | *p++ = '\\'; |
| 5335 | *p++ = 't'; |
| 5336 | } |
| 5337 | else if (ch == '\n') { |
| 5338 | *p++ = '\\'; |
| 5339 | *p++ = 'n'; |
| 5340 | } |
| 5341 | else if (ch == '\r') { |
| 5342 | *p++ = '\\'; |
| 5343 | *p++ = 'r'; |
| 5344 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5345 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5346 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 5347 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5348 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5349 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5350 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 5351 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5352 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5353 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5354 | /* Copy everything else as-is */ |
| 5355 | else |
| 5356 | *p++ = (char) ch; |
| 5357 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5358 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5359 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 5360 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 5361 | return NULL; |
| 5362 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5363 | } |
| 5364 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5365 | PyObject * |
| 5366 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5367 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 5368 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5369 | if (!PyUnicode_Check(unicode)) { |
| 5370 | PyErr_BadArgument(); |
| 5371 | return NULL; |
| 5372 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5373 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 5374 | PyUnicode_GET_SIZE(unicode)); |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 5375 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5376 | } |
| 5377 | |
| 5378 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 5379 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5380 | PyObject * |
| 5381 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5382 | Py_ssize_t size, |
| 5383 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5384 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5385 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5386 | Py_ssize_t startinpos; |
| 5387 | Py_ssize_t endinpos; |
| 5388 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5389 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5390 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5391 | const char *end; |
| 5392 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5393 | PyObject *errorHandler = NULL; |
| 5394 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5395 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5396 | /* Escaped strings will always be longer than the resulting |
| 5397 | 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] | 5398 | length after conversion to the true value. (But decoding error |
| 5399 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5400 | v = _PyUnicode_New(size); |
| 5401 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5402 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5403 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5404 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5405 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5406 | end = s + size; |
| 5407 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5408 | unsigned char c; |
| 5409 | Py_UCS4 x; |
| 5410 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5411 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5412 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5413 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5414 | if (*s != '\\') { |
| 5415 | *p++ = (unsigned char)*s++; |
| 5416 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5417 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5418 | startinpos = s-starts; |
| 5419 | |
| 5420 | /* \u-escapes are only interpreted iff the number of leading |
| 5421 | backslashes if odd */ |
| 5422 | bs = s; |
| 5423 | for (;s < end;) { |
| 5424 | if (*s != '\\') |
| 5425 | break; |
| 5426 | *p++ = (unsigned char)*s++; |
| 5427 | } |
| 5428 | if (((s - bs) & 1) == 0 || |
| 5429 | s >= end || |
| 5430 | (*s != 'u' && *s != 'U')) { |
| 5431 | continue; |
| 5432 | } |
| 5433 | p--; |
| 5434 | count = *s=='u' ? 4 : 8; |
| 5435 | s++; |
| 5436 | |
| 5437 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
| 5438 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 5439 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 5440 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5441 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5442 | endinpos = s-starts; |
| 5443 | if (unicode_decode_call_errorhandler( |
| 5444 | errors, &errorHandler, |
| 5445 | "rawunicodeescape", "truncated \\uXXXX", |
| 5446 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 5447 | &v, &outpos, &p)) |
| 5448 | goto onError; |
| 5449 | goto nextByte; |
| 5450 | } |
| 5451 | x = (x<<4) & ~0xF; |
| 5452 | if (c >= '0' && c <= '9') |
| 5453 | x += c - '0'; |
| 5454 | else if (c >= 'a' && c <= 'f') |
| 5455 | x += 10 + c - 'a'; |
| 5456 | else |
| 5457 | x += 10 + c - 'A'; |
| 5458 | } |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5459 | if (x <= 0xffff) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5460 | /* UCS-2 character */ |
| 5461 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5462 | else if (x <= 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5463 | /* UCS-4 character. Either store directly, or as |
| 5464 | surrogate pair. */ |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5465 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5466 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5467 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5468 | x -= 0x10000L; |
| 5469 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 5470 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5471 | #endif |
| 5472 | } else { |
| 5473 | endinpos = s-starts; |
| 5474 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5475 | if (unicode_decode_call_errorhandler( |
| 5476 | errors, &errorHandler, |
| 5477 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5478 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 5479 | &v, &outpos, &p)) |
| 5480 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5481 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5482 | nextByte: |
| 5483 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5484 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5485 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5486 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5487 | Py_XDECREF(errorHandler); |
| 5488 | Py_XDECREF(exc); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5489 | if (PyUnicode_READY(v) == -1) { |
| 5490 | Py_DECREF(v); |
| 5491 | return NULL; |
| 5492 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5493 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5494 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5495 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5496 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5497 | Py_XDECREF(errorHandler); |
| 5498 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5499 | return NULL; |
| 5500 | } |
| 5501 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5502 | PyObject * |
| 5503 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5504 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5505 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5506 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5507 | char *p; |
| 5508 | char *q; |
| 5509 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5510 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5511 | const Py_ssize_t expandsize = 10; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5512 | #else |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5513 | const Py_ssize_t expandsize = 6; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5514 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5515 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5516 | if (size > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5517 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5518 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5519 | repr = PyBytes_FromStringAndSize(NULL, expandsize * size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5520 | if (repr == NULL) |
| 5521 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 5522 | if (size == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5523 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5524 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5525 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5526 | while (size-- > 0) { |
| 5527 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5528 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5529 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 5530 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5531 | *p++ = '\\'; |
| 5532 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 5533 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 5534 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 5535 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 5536 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 5537 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 5538 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 5539 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 5540 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5541 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5542 | else |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5543 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5544 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 5545 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 5546 | Py_UNICODE ch2; |
| 5547 | Py_UCS4 ucs; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5548 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5549 | ch2 = *s++; |
| 5550 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 5551 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5552 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 5553 | *p++ = '\\'; |
| 5554 | *p++ = 'U'; |
| 5555 | *p++ = hexdigits[(ucs >> 28) & 0xf]; |
| 5556 | *p++ = hexdigits[(ucs >> 24) & 0xf]; |
| 5557 | *p++ = hexdigits[(ucs >> 20) & 0xf]; |
| 5558 | *p++ = hexdigits[(ucs >> 16) & 0xf]; |
| 5559 | *p++ = hexdigits[(ucs >> 12) & 0xf]; |
| 5560 | *p++ = hexdigits[(ucs >> 8) & 0xf]; |
| 5561 | *p++ = hexdigits[(ucs >> 4) & 0xf]; |
| 5562 | *p++ = hexdigits[ucs & 0xf]; |
| 5563 | continue; |
| 5564 | } |
| 5565 | /* Fall through: isolated surrogates are copied as-is */ |
| 5566 | s--; |
| 5567 | size++; |
| 5568 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5569 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5570 | /* Map 16-bit characters to '\uxxxx' */ |
| 5571 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +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 >> 12) & 0xf]; |
| 5575 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 5576 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 5577 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5578 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5579 | /* Copy everything else as-is */ |
| 5580 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5581 | *p++ = (char) ch; |
| 5582 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5583 | size = p - q; |
| 5584 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5585 | assert(size > 0); |
| 5586 | if (_PyBytes_Resize(&repr, size) < 0) |
| 5587 | return NULL; |
| 5588 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5589 | } |
| 5590 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5591 | PyObject * |
| 5592 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5593 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 5594 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5595 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 5596 | PyErr_BadArgument(); |
| 5597 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5598 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 5599 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 5600 | PyUnicode_GET_SIZE(unicode)); |
| 5601 | |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 5602 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5603 | } |
| 5604 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5605 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 5606 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5607 | PyObject * |
| 5608 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5609 | Py_ssize_t size, |
| 5610 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5611 | { |
| 5612 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5613 | Py_ssize_t startinpos; |
| 5614 | Py_ssize_t endinpos; |
| 5615 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5616 | PyUnicodeObject *v; |
| 5617 | Py_UNICODE *p; |
| 5618 | const char *end; |
| 5619 | const char *reason; |
| 5620 | PyObject *errorHandler = NULL; |
| 5621 | PyObject *exc = NULL; |
| 5622 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 5623 | #ifdef Py_UNICODE_WIDE |
| 5624 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 5625 | #endif |
| 5626 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5627 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5628 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 5629 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5630 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5631 | /* Intentionally PyUnicode_GET_SIZE instead of PyUnicode_GET_LENGTH |
| 5632 | as string was created with the old API. */ |
| 5633 | if (PyUnicode_GET_SIZE(v) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5634 | return (PyObject *)v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5635 | p = PyUnicode_AS_UNICODE(v); |
| 5636 | end = s + size; |
| 5637 | |
| 5638 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5639 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5640 | /* We have to sanity check the raw data, otherwise doom looms for |
| 5641 | some malformed UCS-4 data. */ |
| 5642 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5643 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5644 | *p > unimax || *p < 0 || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5645 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5646 | end-s < Py_UNICODE_SIZE |
| 5647 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5648 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5649 | startinpos = s - starts; |
| 5650 | if (end-s < Py_UNICODE_SIZE) { |
| 5651 | endinpos = end-starts; |
| 5652 | reason = "truncated input"; |
| 5653 | } |
| 5654 | else { |
| 5655 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 5656 | reason = "illegal code point (> 0x10FFFF)"; |
| 5657 | } |
| 5658 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 5659 | if (unicode_decode_call_errorhandler( |
| 5660 | errors, &errorHandler, |
| 5661 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 5662 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 5663 | &v, &outpos, &p)) { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5664 | goto onError; |
| 5665 | } |
| 5666 | } |
| 5667 | else { |
| 5668 | p++; |
| 5669 | s += Py_UNICODE_SIZE; |
| 5670 | } |
| 5671 | } |
| 5672 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5673 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5674 | goto onError; |
| 5675 | Py_XDECREF(errorHandler); |
| 5676 | Py_XDECREF(exc); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5677 | if (PyUnicode_READY(v) == -1) { |
| 5678 | Py_DECREF(v); |
| 5679 | return NULL; |
| 5680 | } |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5681 | return (PyObject *)v; |
| 5682 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5683 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5684 | Py_XDECREF(v); |
| 5685 | Py_XDECREF(errorHandler); |
| 5686 | Py_XDECREF(exc); |
| 5687 | return NULL; |
| 5688 | } |
| 5689 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5690 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 5691 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5692 | PyObject * |
| 5693 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5694 | Py_ssize_t size, |
| 5695 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5696 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5697 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 5698 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5699 | } |
| 5700 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5701 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5702 | static void |
| 5703 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5704 | const char *encoding, |
| 5705 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5706 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5707 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5708 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5709 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5710 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 5711 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5712 | } |
| 5713 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5714 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 5715 | goto onError; |
| 5716 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 5717 | goto onError; |
| 5718 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 5719 | goto onError; |
| 5720 | return; |
| 5721 | onError: |
| 5722 | Py_DECREF(*exceptionObject); |
| 5723 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5724 | } |
| 5725 | } |
| 5726 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5727 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5728 | static void |
| 5729 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5730 | const char *encoding, |
| 5731 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5732 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5733 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5734 | { |
| 5735 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5736 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5737 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5738 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5739 | } |
| 5740 | |
| 5741 | /* error handling callback helper: |
| 5742 | build arguments, call the callback and check the arguments, |
| 5743 | put the result into newpos and return the replacement string, which |
| 5744 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5745 | static PyObject * |
| 5746 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5747 | PyObject **errorHandler, |
| 5748 | const char *encoding, const char *reason, |
| 5749 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 5750 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5751 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5752 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5753 | 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] | 5754 | |
| 5755 | PyObject *restuple; |
| 5756 | PyObject *resunicode; |
| 5757 | |
| 5758 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5759 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5760 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5761 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5762 | } |
| 5763 | |
| 5764 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5765 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5766 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5767 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5768 | |
| 5769 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5770 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5771 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5772 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5773 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5774 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5775 | Py_DECREF(restuple); |
| 5776 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5777 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5778 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5779 | &resunicode, newpos)) { |
| 5780 | Py_DECREF(restuple); |
| 5781 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5782 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5783 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 5784 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 5785 | Py_DECREF(restuple); |
| 5786 | return NULL; |
| 5787 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5788 | if (*newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5789 | *newpos = size+*newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5790 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5791 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 5792 | Py_DECREF(restuple); |
| 5793 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5794 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5795 | Py_INCREF(resunicode); |
| 5796 | Py_DECREF(restuple); |
| 5797 | return resunicode; |
| 5798 | } |
| 5799 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5800 | static PyObject * |
| 5801 | unicode_encode_ucs1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5802 | Py_ssize_t size, |
| 5803 | const char *errors, |
| 5804 | int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5805 | { |
| 5806 | /* output object */ |
| 5807 | PyObject *res; |
| 5808 | /* pointers to the beginning and end+1 of input */ |
| 5809 | const Py_UNICODE *startp = p; |
| 5810 | const Py_UNICODE *endp = p + size; |
| 5811 | /* pointer to the beginning of the unencodable characters */ |
| 5812 | /* const Py_UNICODE *badp = NULL; */ |
| 5813 | /* pointer into the output */ |
| 5814 | char *str; |
| 5815 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5816 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5817 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 5818 | 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] | 5819 | PyObject *errorHandler = NULL; |
| 5820 | PyObject *exc = NULL; |
| 5821 | /* the following variable is used for caching string comparisons |
| 5822 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 5823 | int known_errorHandler = -1; |
| 5824 | |
| 5825 | /* allocate enough for a simple encoding without |
| 5826 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5827 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5828 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5829 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5830 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5831 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5832 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5833 | ressize = size; |
| 5834 | |
| 5835 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5836 | Py_UNICODE c = *p; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5837 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5838 | /* can we encode this? */ |
| 5839 | if (c<limit) { |
| 5840 | /* no overflow check, because we know that the space is enough */ |
| 5841 | *str++ = (char)c; |
| 5842 | ++p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5843 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5844 | else { |
| 5845 | Py_ssize_t unicodepos = p-startp; |
| 5846 | Py_ssize_t requiredsize; |
| 5847 | PyObject *repunicode; |
| 5848 | Py_ssize_t repsize; |
| 5849 | Py_ssize_t newpos; |
| 5850 | Py_ssize_t respos; |
| 5851 | Py_UNICODE *uni2; |
| 5852 | /* startpos for collecting unencodable chars */ |
| 5853 | const Py_UNICODE *collstart = p; |
| 5854 | const Py_UNICODE *collend = p; |
| 5855 | /* find all unecodable characters */ |
| 5856 | while ((collend < endp) && ((*collend)>=limit)) |
| 5857 | ++collend; |
| 5858 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 5859 | if (known_errorHandler==-1) { |
| 5860 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5861 | known_errorHandler = 1; |
| 5862 | else if (!strcmp(errors, "replace")) |
| 5863 | known_errorHandler = 2; |
| 5864 | else if (!strcmp(errors, "ignore")) |
| 5865 | known_errorHandler = 3; |
| 5866 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5867 | known_errorHandler = 4; |
| 5868 | else |
| 5869 | known_errorHandler = 0; |
| 5870 | } |
| 5871 | switch (known_errorHandler) { |
| 5872 | case 1: /* strict */ |
| 5873 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 5874 | goto onError; |
| 5875 | case 2: /* replace */ |
| 5876 | while (collstart++<collend) |
| 5877 | *str++ = '?'; /* fall through */ |
| 5878 | case 3: /* ignore */ |
| 5879 | p = collend; |
| 5880 | break; |
| 5881 | case 4: /* xmlcharrefreplace */ |
| 5882 | respos = str - PyBytes_AS_STRING(res); |
| 5883 | /* determine replacement size (temporarily (mis)uses p) */ |
| 5884 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 5885 | if (*p<10) |
| 5886 | repsize += 2+1+1; |
| 5887 | else if (*p<100) |
| 5888 | repsize += 2+2+1; |
| 5889 | else if (*p<1000) |
| 5890 | repsize += 2+3+1; |
| 5891 | else if (*p<10000) |
| 5892 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 5893 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5894 | else |
| 5895 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 5896 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5897 | else if (*p<100000) |
| 5898 | repsize += 2+5+1; |
| 5899 | else if (*p<1000000) |
| 5900 | repsize += 2+6+1; |
| 5901 | else |
| 5902 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5903 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5904 | } |
| 5905 | requiredsize = respos+repsize+(endp-collend); |
| 5906 | if (requiredsize > ressize) { |
| 5907 | if (requiredsize<2*ressize) |
| 5908 | requiredsize = 2*ressize; |
| 5909 | if (_PyBytes_Resize(&res, requiredsize)) |
| 5910 | goto onError; |
| 5911 | str = PyBytes_AS_STRING(res) + respos; |
| 5912 | ressize = requiredsize; |
| 5913 | } |
| 5914 | /* generate replacement (temporarily (mis)uses p) */ |
| 5915 | for (p = collstart; p < collend; ++p) { |
| 5916 | str += sprintf(str, "&#%d;", (int)*p); |
| 5917 | } |
| 5918 | p = collend; |
| 5919 | break; |
| 5920 | default: |
| 5921 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 5922 | encoding, reason, startp, size, &exc, |
| 5923 | collstart-startp, collend-startp, &newpos); |
| 5924 | if (repunicode == NULL) |
| 5925 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5926 | if (PyBytes_Check(repunicode)) { |
| 5927 | /* Directly copy bytes result to output. */ |
| 5928 | repsize = PyBytes_Size(repunicode); |
| 5929 | if (repsize > 1) { |
| 5930 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 5931 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5932 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 5933 | Py_DECREF(repunicode); |
| 5934 | goto onError; |
| 5935 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 5936 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5937 | ressize += repsize-1; |
| 5938 | } |
| 5939 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 5940 | str += repsize; |
| 5941 | p = startp + newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5942 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5943 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5944 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5945 | /* need more space? (at least enough for what we |
| 5946 | have+the replacement+the rest of the string, so |
| 5947 | we won't have to check space for encodable characters) */ |
| 5948 | respos = str - PyBytes_AS_STRING(res); |
| 5949 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5950 | requiredsize = respos+repsize+(endp-collend); |
| 5951 | if (requiredsize > ressize) { |
| 5952 | if (requiredsize<2*ressize) |
| 5953 | requiredsize = 2*ressize; |
| 5954 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 5955 | Py_DECREF(repunicode); |
| 5956 | goto onError; |
| 5957 | } |
| 5958 | str = PyBytes_AS_STRING(res) + respos; |
| 5959 | ressize = requiredsize; |
| 5960 | } |
| 5961 | /* check if there is anything unencodable in the replacement |
| 5962 | and copy it to the output */ |
| 5963 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 5964 | c = *uni2; |
| 5965 | if (c >= limit) { |
| 5966 | raise_encode_exception(&exc, encoding, startp, size, |
| 5967 | unicodepos, unicodepos+1, reason); |
| 5968 | Py_DECREF(repunicode); |
| 5969 | goto onError; |
| 5970 | } |
| 5971 | *str = (char)c; |
| 5972 | } |
| 5973 | p = startp + newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5974 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5975 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5976 | } |
| 5977 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5978 | /* Resize if we allocated to much */ |
| 5979 | size = str - PyBytes_AS_STRING(res); |
| 5980 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 5981 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5982 | if (_PyBytes_Resize(&res, size) < 0) |
| 5983 | goto onError; |
| 5984 | } |
| 5985 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5986 | Py_XDECREF(errorHandler); |
| 5987 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5988 | return res; |
| 5989 | |
| 5990 | onError: |
| 5991 | Py_XDECREF(res); |
| 5992 | Py_XDECREF(errorHandler); |
| 5993 | Py_XDECREF(exc); |
| 5994 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5995 | } |
| 5996 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5997 | PyObject * |
| 5998 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5999 | Py_ssize_t size, |
| 6000 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6001 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6002 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6003 | } |
| 6004 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6005 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6006 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6007 | { |
| 6008 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6009 | PyErr_BadArgument(); |
| 6010 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6011 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6012 | if (PyUnicode_READY(unicode) == -1) |
| 6013 | return NULL; |
| 6014 | /* Fast path: if it is a one-byte string, construct |
| 6015 | bytes object directly. */ |
| 6016 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6017 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6018 | PyUnicode_GET_LENGTH(unicode)); |
| 6019 | /* Non-Latin-1 characters present. Defer to above function to |
| 6020 | raise the exception. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6021 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6022 | PyUnicode_GET_SIZE(unicode), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6023 | errors); |
| 6024 | } |
| 6025 | |
| 6026 | PyObject* |
| 6027 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6028 | { |
| 6029 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6030 | } |
| 6031 | |
| 6032 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6033 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6034 | PyObject * |
| 6035 | PyUnicode_DecodeASCII(const char *s, |
| 6036 | Py_ssize_t size, |
| 6037 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6038 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6039 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6040 | PyUnicodeObject *v; |
| 6041 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6042 | Py_ssize_t startinpos; |
| 6043 | Py_ssize_t endinpos; |
| 6044 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6045 | const char *e; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6046 | unsigned char* d; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6047 | PyObject *errorHandler = NULL; |
| 6048 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6049 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6050 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6051 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6052 | if (size == 1 && *(unsigned char*)s < 128) |
| 6053 | return PyUnicode_FromOrdinal(*(unsigned char*)s); |
| 6054 | |
| 6055 | /* Fast path. Assume the input actually *is* ASCII, and allocate |
| 6056 | a single-block Unicode object with that assumption. If there is |
| 6057 | an error, drop the object and start over. */ |
| 6058 | v = (PyUnicodeObject*)PyUnicode_New(size, 127); |
| 6059 | if (v == NULL) |
| 6060 | goto onError; |
| 6061 | d = PyUnicode_1BYTE_DATA(v); |
| 6062 | for (i = 0; i < size; i++) { |
| 6063 | unsigned char ch = ((unsigned char*)s)[i]; |
| 6064 | if (ch < 128) |
| 6065 | d[i] = ch; |
| 6066 | else |
| 6067 | break; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6068 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6069 | if (i == size) |
| 6070 | return (PyObject*)v; |
| 6071 | Py_DECREF(v); /* start over */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6072 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6073 | v = _PyUnicode_New(size); |
| 6074 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6075 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6076 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6077 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6078 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6079 | e = s + size; |
| 6080 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6081 | register unsigned char c = (unsigned char)*s; |
| 6082 | if (c < 128) { |
| 6083 | *p++ = c; |
| 6084 | ++s; |
| 6085 | } |
| 6086 | else { |
| 6087 | startinpos = s-starts; |
| 6088 | endinpos = startinpos + 1; |
| 6089 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
| 6090 | if (unicode_decode_call_errorhandler( |
| 6091 | errors, &errorHandler, |
| 6092 | "ascii", "ordinal not in range(128)", |
| 6093 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 6094 | &v, &outpos, &p)) |
| 6095 | goto onError; |
| 6096 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6097 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6098 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6099 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 6100 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6101 | Py_XDECREF(errorHandler); |
| 6102 | Py_XDECREF(exc); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6103 | if (PyUnicode_READY(v) == -1) { |
| 6104 | Py_DECREF(v); |
| 6105 | return NULL; |
| 6106 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6107 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6108 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6109 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6110 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6111 | Py_XDECREF(errorHandler); |
| 6112 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6113 | return NULL; |
| 6114 | } |
| 6115 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6116 | PyObject * |
| 6117 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6118 | Py_ssize_t size, |
| 6119 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6120 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6121 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6122 | } |
| 6123 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6124 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6125 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6126 | { |
| 6127 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6128 | PyErr_BadArgument(); |
| 6129 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6130 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6131 | if (PyUnicode_READY(unicode) == -1) |
| 6132 | return NULL; |
| 6133 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6134 | directly. Else defer to above function to raise the exception. */ |
| 6135 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 6136 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6137 | PyUnicode_GET_LENGTH(unicode)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6138 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6139 | PyUnicode_GET_SIZE(unicode), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6140 | errors); |
| 6141 | } |
| 6142 | |
| 6143 | PyObject * |
| 6144 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6145 | { |
| 6146 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6147 | } |
| 6148 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6149 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6150 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6151 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6152 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6153 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6154 | #define NEED_RETRY |
| 6155 | #endif |
| 6156 | |
| 6157 | /* XXX This code is limited to "true" double-byte encodings, as |
| 6158 | a) it assumes an incomplete character consists of a single byte, and |
| 6159 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6160 | encodings, see IsDBCSLeadByteEx documentation. */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6161 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6162 | static int |
| 6163 | is_dbcs_lead_byte(const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6164 | { |
| 6165 | const char *curr = s + offset; |
| 6166 | |
| 6167 | if (IsDBCSLeadByte(*curr)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6168 | const char *prev = CharPrev(s, curr); |
| 6169 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6170 | } |
| 6171 | return 0; |
| 6172 | } |
| 6173 | |
| 6174 | /* |
| 6175 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 6176 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 6177 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6178 | static int |
| 6179 | decode_mbcs(PyUnicodeObject **v, |
| 6180 | const char *s, /* MBCS string */ |
| 6181 | int size, /* sizeof MBCS string */ |
| 6182 | int final, |
| 6183 | const char *errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6184 | { |
| 6185 | Py_UNICODE *p; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6186 | Py_ssize_t n; |
| 6187 | DWORD usize; |
| 6188 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6189 | |
| 6190 | assert(size >= 0); |
| 6191 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6192 | /* check and handle 'errors' arg */ |
| 6193 | if (errors==NULL || strcmp(errors, "strict")==0) |
| 6194 | flags = MB_ERR_INVALID_CHARS; |
| 6195 | else if (strcmp(errors, "ignore")==0) |
| 6196 | flags = 0; |
| 6197 | else { |
| 6198 | PyErr_Format(PyExc_ValueError, |
| 6199 | "mbcs encoding does not support errors='%s'", |
| 6200 | errors); |
| 6201 | return -1; |
| 6202 | } |
| 6203 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6204 | /* Skip trailing lead-byte unless 'final' is set */ |
| 6205 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6206 | --size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6207 | |
| 6208 | /* First get the size of the result */ |
| 6209 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6210 | usize = MultiByteToWideChar(CP_ACP, flags, s, size, NULL, 0); |
| 6211 | if (usize==0) |
| 6212 | goto mbcs_decode_error; |
| 6213 | } else |
| 6214 | usize = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6215 | |
| 6216 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6217 | /* Create unicode object */ |
| 6218 | *v = _PyUnicode_New(usize); |
| 6219 | if (*v == NULL) |
| 6220 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6221 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6222 | } |
| 6223 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6224 | /* Extend unicode object */ |
| 6225 | n = PyUnicode_GET_SIZE(*v); |
| 6226 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 6227 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6228 | } |
| 6229 | |
| 6230 | /* Do the conversion */ |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6231 | if (usize > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6232 | p = PyUnicode_AS_UNICODE(*v) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6233 | if (0 == MultiByteToWideChar(CP_ACP, flags, s, size, p, usize)) { |
| 6234 | goto mbcs_decode_error; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6235 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6236 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6237 | return size; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6238 | |
| 6239 | mbcs_decode_error: |
| 6240 | /* If the last error was ERROR_NO_UNICODE_TRANSLATION, then |
| 6241 | we raise a UnicodeDecodeError - else it is a 'generic' |
| 6242 | windows error |
| 6243 | */ |
| 6244 | if (GetLastError()==ERROR_NO_UNICODE_TRANSLATION) { |
| 6245 | /* Ideally, we should get reason from FormatMessage - this |
| 6246 | is the Windows 2000 English version of the message |
| 6247 | */ |
| 6248 | PyObject *exc = NULL; |
| 6249 | const char *reason = "No mapping for the Unicode character exists " |
| 6250 | "in the target multi-byte code page."; |
| 6251 | make_decode_exception(&exc, "mbcs", s, size, 0, 0, reason); |
| 6252 | if (exc != NULL) { |
| 6253 | PyCodec_StrictErrors(exc); |
| 6254 | Py_DECREF(exc); |
| 6255 | } |
| 6256 | } else { |
| 6257 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 6258 | } |
| 6259 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6260 | } |
| 6261 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6262 | PyObject * |
| 6263 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 6264 | Py_ssize_t size, |
| 6265 | const char *errors, |
| 6266 | Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6267 | { |
| 6268 | PyUnicodeObject *v = NULL; |
| 6269 | int done; |
| 6270 | |
| 6271 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6272 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6273 | |
| 6274 | #ifdef NEED_RETRY |
| 6275 | retry: |
| 6276 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6277 | done = decode_mbcs(&v, s, INT_MAX, 0, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6278 | else |
| 6279 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6280 | done = decode_mbcs(&v, s, (int)size, !consumed, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6281 | |
| 6282 | if (done < 0) { |
| 6283 | Py_XDECREF(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6284 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6285 | } |
| 6286 | |
| 6287 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6288 | *consumed += done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6289 | |
| 6290 | #ifdef NEED_RETRY |
| 6291 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6292 | s += done; |
| 6293 | size -= done; |
| 6294 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6295 | } |
| 6296 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6297 | if (PyUnicode_READY(v) == -1) { |
| 6298 | Py_DECREF(v); |
| 6299 | return NULL; |
| 6300 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6301 | return (PyObject *)v; |
| 6302 | } |
| 6303 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6304 | PyObject * |
| 6305 | PyUnicode_DecodeMBCS(const char *s, |
| 6306 | Py_ssize_t size, |
| 6307 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6308 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6309 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 6310 | } |
| 6311 | |
| 6312 | /* |
| 6313 | * Convert unicode into string object (MBCS). |
| 6314 | * Returns 0 if succeed, -1 otherwise. |
| 6315 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6316 | static int |
| 6317 | encode_mbcs(PyObject **repr, |
| 6318 | const Py_UNICODE *p, /* unicode */ |
| 6319 | int size, /* size of unicode */ |
| 6320 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6321 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6322 | BOOL usedDefaultChar = FALSE; |
| 6323 | BOOL *pusedDefaultChar; |
| 6324 | int mbcssize; |
| 6325 | Py_ssize_t n; |
| 6326 | PyObject *exc = NULL; |
| 6327 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6328 | |
| 6329 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6330 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6331 | /* check and handle 'errors' arg */ |
| 6332 | if (errors==NULL || strcmp(errors, "strict")==0) { |
| 6333 | flags = WC_NO_BEST_FIT_CHARS; |
| 6334 | pusedDefaultChar = &usedDefaultChar; |
| 6335 | } else if (strcmp(errors, "replace")==0) { |
| 6336 | flags = 0; |
| 6337 | pusedDefaultChar = NULL; |
| 6338 | } else { |
| 6339 | PyErr_Format(PyExc_ValueError, |
| 6340 | "mbcs encoding does not support errors='%s'", |
| 6341 | errors); |
| 6342 | return -1; |
| 6343 | } |
| 6344 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6345 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6346 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6347 | mbcssize = WideCharToMultiByte(CP_ACP, flags, p, size, NULL, 0, |
| 6348 | NULL, pusedDefaultChar); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6349 | if (mbcssize == 0) { |
| 6350 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 6351 | return -1; |
| 6352 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6353 | /* If we used a default char, then we failed! */ |
| 6354 | if (pusedDefaultChar && *pusedDefaultChar) |
| 6355 | goto mbcs_encode_error; |
| 6356 | } else { |
| 6357 | mbcssize = 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6358 | } |
| 6359 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6360 | if (*repr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6361 | /* Create string object */ |
| 6362 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); |
| 6363 | if (*repr == NULL) |
| 6364 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6365 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6366 | } |
| 6367 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6368 | /* Extend string object */ |
| 6369 | n = PyBytes_Size(*repr); |
| 6370 | if (_PyBytes_Resize(repr, n + mbcssize) < 0) |
| 6371 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6372 | } |
| 6373 | |
| 6374 | /* Do the conversion */ |
| 6375 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6376 | char *s = PyBytes_AS_STRING(*repr) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6377 | if (0 == WideCharToMultiByte(CP_ACP, flags, p, size, s, mbcssize, |
| 6378 | NULL, pusedDefaultChar)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6379 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 6380 | return -1; |
| 6381 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6382 | if (pusedDefaultChar && *pusedDefaultChar) |
| 6383 | goto mbcs_encode_error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6384 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6385 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6386 | |
| 6387 | mbcs_encode_error: |
| 6388 | raise_encode_exception(&exc, "mbcs", p, size, 0, 0, "invalid character"); |
| 6389 | Py_XDECREF(exc); |
| 6390 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6391 | } |
| 6392 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6393 | PyObject * |
| 6394 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 6395 | Py_ssize_t size, |
| 6396 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6397 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6398 | PyObject *repr = NULL; |
| 6399 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 6400 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6401 | #ifdef NEED_RETRY |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6402 | retry: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6403 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6404 | ret = encode_mbcs(&repr, p, INT_MAX, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6405 | else |
| 6406 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6407 | ret = encode_mbcs(&repr, p, (int)size, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6408 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6409 | if (ret < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6410 | Py_XDECREF(repr); |
| 6411 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6412 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6413 | |
| 6414 | #ifdef NEED_RETRY |
| 6415 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6416 | p += INT_MAX; |
| 6417 | size -= INT_MAX; |
| 6418 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6419 | } |
| 6420 | #endif |
| 6421 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6422 | return repr; |
| 6423 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6424 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6425 | PyObject * |
| 6426 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 6427 | { |
| 6428 | if (!PyUnicode_Check(unicode)) { |
| 6429 | PyErr_BadArgument(); |
| 6430 | return NULL; |
| 6431 | } |
| 6432 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6433 | PyUnicode_GET_SIZE(unicode), |
| 6434 | NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 6435 | } |
| 6436 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6437 | #undef NEED_RETRY |
| 6438 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6439 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6440 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6441 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 6442 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6443 | PyObject * |
| 6444 | PyUnicode_DecodeCharmap(const char *s, |
| 6445 | Py_ssize_t size, |
| 6446 | PyObject *mapping, |
| 6447 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6448 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6449 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6450 | Py_ssize_t startinpos; |
| 6451 | Py_ssize_t endinpos; |
| 6452 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6453 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6454 | PyUnicodeObject *v; |
| 6455 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6456 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6457 | PyObject *errorHandler = NULL; |
| 6458 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 6459 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6460 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6461 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6462 | /* Default to Latin-1 */ |
| 6463 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6464 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6465 | |
| 6466 | v = _PyUnicode_New(size); |
| 6467 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6468 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6469 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6470 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6471 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6472 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 6473 | if (PyUnicode_CheckExact(mapping)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6474 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 6475 | maplen = PyUnicode_GET_SIZE(mapping); |
| 6476 | while (s < e) { |
| 6477 | unsigned char ch = *s; |
| 6478 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6479 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6480 | if (ch < maplen) |
| 6481 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6482 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6483 | if (x == 0xfffe) { |
| 6484 | /* undefined mapping */ |
| 6485 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 6486 | startinpos = s-starts; |
| 6487 | endinpos = startinpos+1; |
| 6488 | if (unicode_decode_call_errorhandler( |
| 6489 | errors, &errorHandler, |
| 6490 | "charmap", "character maps to <undefined>", |
| 6491 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 6492 | &v, &outpos, &p)) { |
| 6493 | goto onError; |
| 6494 | } |
| 6495 | continue; |
| 6496 | } |
| 6497 | *p++ = x; |
| 6498 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6499 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 6500 | } |
| 6501 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6502 | while (s < e) { |
| 6503 | unsigned char ch = *s; |
| 6504 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 6505 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6506 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 6507 | w = PyLong_FromLong((long)ch); |
| 6508 | if (w == NULL) |
| 6509 | goto onError; |
| 6510 | x = PyObject_GetItem(mapping, w); |
| 6511 | Py_DECREF(w); |
| 6512 | if (x == NULL) { |
| 6513 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 6514 | /* No mapping found means: mapping is undefined. */ |
| 6515 | PyErr_Clear(); |
| 6516 | x = Py_None; |
| 6517 | Py_INCREF(x); |
| 6518 | } else |
| 6519 | goto onError; |
| 6520 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6521 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6522 | /* Apply mapping */ |
| 6523 | if (PyLong_Check(x)) { |
| 6524 | long value = PyLong_AS_LONG(x); |
| 6525 | if (value < 0 || value > 65535) { |
| 6526 | PyErr_SetString(PyExc_TypeError, |
| 6527 | "character mapping must be in range(65536)"); |
| 6528 | Py_DECREF(x); |
| 6529 | goto onError; |
| 6530 | } |
| 6531 | *p++ = (Py_UNICODE)value; |
| 6532 | } |
| 6533 | else if (x == Py_None) { |
| 6534 | /* undefined mapping */ |
| 6535 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 6536 | startinpos = s-starts; |
| 6537 | endinpos = startinpos+1; |
| 6538 | if (unicode_decode_call_errorhandler( |
| 6539 | errors, &errorHandler, |
| 6540 | "charmap", "character maps to <undefined>", |
| 6541 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 6542 | &v, &outpos, &p)) { |
| 6543 | Py_DECREF(x); |
| 6544 | goto onError; |
| 6545 | } |
| 6546 | Py_DECREF(x); |
| 6547 | continue; |
| 6548 | } |
| 6549 | else if (PyUnicode_Check(x)) { |
| 6550 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6551 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6552 | if (targetsize == 1) |
| 6553 | /* 1-1 mapping */ |
| 6554 | *p++ = *PyUnicode_AS_UNICODE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6555 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6556 | else if (targetsize > 1) { |
| 6557 | /* 1-n mapping */ |
| 6558 | if (targetsize > extrachars) { |
| 6559 | /* resize first */ |
| 6560 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 6561 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 6562 | (targetsize << 2); |
| 6563 | extrachars += needed; |
| 6564 | /* XXX overflow detection missing */ |
| 6565 | if (_PyUnicode_Resize(&v, |
| 6566 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 6567 | Py_DECREF(x); |
| 6568 | goto onError; |
| 6569 | } |
| 6570 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 6571 | } |
| 6572 | Py_UNICODE_COPY(p, |
| 6573 | PyUnicode_AS_UNICODE(x), |
| 6574 | targetsize); |
| 6575 | p += targetsize; |
| 6576 | extrachars -= targetsize; |
| 6577 | } |
| 6578 | /* 1-0 mapping: skip the character */ |
| 6579 | } |
| 6580 | else { |
| 6581 | /* wrong return value */ |
| 6582 | PyErr_SetString(PyExc_TypeError, |
| 6583 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6584 | Py_DECREF(x); |
| 6585 | goto onError; |
| 6586 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6587 | Py_DECREF(x); |
| 6588 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6589 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6590 | } |
| 6591 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6592 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 6593 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6594 | Py_XDECREF(errorHandler); |
| 6595 | Py_XDECREF(exc); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6596 | if (PyUnicode_READY(v) == -1) { |
| 6597 | Py_DECREF(v); |
| 6598 | return NULL; |
| 6599 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6600 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6601 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6602 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6603 | Py_XDECREF(errorHandler); |
| 6604 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6605 | Py_XDECREF(v); |
| 6606 | return NULL; |
| 6607 | } |
| 6608 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6609 | /* Charmap encoding: the lookup table */ |
| 6610 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6611 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6612 | PyObject_HEAD |
| 6613 | unsigned char level1[32]; |
| 6614 | int count2, count3; |
| 6615 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6616 | }; |
| 6617 | |
| 6618 | static PyObject* |
| 6619 | encoding_map_size(PyObject *obj, PyObject* args) |
| 6620 | { |
| 6621 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6622 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6623 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6624 | } |
| 6625 | |
| 6626 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6627 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6628 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 6629 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6630 | }; |
| 6631 | |
| 6632 | static void |
| 6633 | encoding_map_dealloc(PyObject* o) |
| 6634 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6635 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6636 | } |
| 6637 | |
| 6638 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6639 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6640 | "EncodingMap", /*tp_name*/ |
| 6641 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 6642 | 0, /*tp_itemsize*/ |
| 6643 | /* methods */ |
| 6644 | encoding_map_dealloc, /*tp_dealloc*/ |
| 6645 | 0, /*tp_print*/ |
| 6646 | 0, /*tp_getattr*/ |
| 6647 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 6648 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6649 | 0, /*tp_repr*/ |
| 6650 | 0, /*tp_as_number*/ |
| 6651 | 0, /*tp_as_sequence*/ |
| 6652 | 0, /*tp_as_mapping*/ |
| 6653 | 0, /*tp_hash*/ |
| 6654 | 0, /*tp_call*/ |
| 6655 | 0, /*tp_str*/ |
| 6656 | 0, /*tp_getattro*/ |
| 6657 | 0, /*tp_setattro*/ |
| 6658 | 0, /*tp_as_buffer*/ |
| 6659 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 6660 | 0, /*tp_doc*/ |
| 6661 | 0, /*tp_traverse*/ |
| 6662 | 0, /*tp_clear*/ |
| 6663 | 0, /*tp_richcompare*/ |
| 6664 | 0, /*tp_weaklistoffset*/ |
| 6665 | 0, /*tp_iter*/ |
| 6666 | 0, /*tp_iternext*/ |
| 6667 | encoding_map_methods, /*tp_methods*/ |
| 6668 | 0, /*tp_members*/ |
| 6669 | 0, /*tp_getset*/ |
| 6670 | 0, /*tp_base*/ |
| 6671 | 0, /*tp_dict*/ |
| 6672 | 0, /*tp_descr_get*/ |
| 6673 | 0, /*tp_descr_set*/ |
| 6674 | 0, /*tp_dictoffset*/ |
| 6675 | 0, /*tp_init*/ |
| 6676 | 0, /*tp_alloc*/ |
| 6677 | 0, /*tp_new*/ |
| 6678 | 0, /*tp_free*/ |
| 6679 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6680 | }; |
| 6681 | |
| 6682 | PyObject* |
| 6683 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 6684 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6685 | PyObject *result; |
| 6686 | struct encoding_map *mresult; |
| 6687 | int i; |
| 6688 | int need_dict = 0; |
| 6689 | unsigned char level1[32]; |
| 6690 | unsigned char level2[512]; |
| 6691 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 6692 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6693 | int kind; |
| 6694 | void *data; |
| 6695 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6696 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6697 | if (!PyUnicode_Check(string) || PyUnicode_GET_LENGTH(string) != 256) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6698 | PyErr_BadArgument(); |
| 6699 | return NULL; |
| 6700 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6701 | kind = PyUnicode_KIND(string); |
| 6702 | data = PyUnicode_DATA(string); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6703 | memset(level1, 0xFF, sizeof level1); |
| 6704 | memset(level2, 0xFF, sizeof level2); |
| 6705 | |
| 6706 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 6707 | or if there are non-BMP characters, we need to use |
| 6708 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6709 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6710 | need_dict = 1; |
| 6711 | for (i = 1; i < 256; i++) { |
| 6712 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6713 | ch = PyUnicode_READ(kind, data, i); |
| 6714 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6715 | need_dict = 1; |
| 6716 | break; |
| 6717 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6718 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6719 | /* unmapped character */ |
| 6720 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6721 | l1 = ch >> 11; |
| 6722 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6723 | if (level1[l1] == 0xFF) |
| 6724 | level1[l1] = count2++; |
| 6725 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6726 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6727 | } |
| 6728 | |
| 6729 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 6730 | need_dict = 1; |
| 6731 | |
| 6732 | if (need_dict) { |
| 6733 | PyObject *result = PyDict_New(); |
| 6734 | PyObject *key, *value; |
| 6735 | if (!result) |
| 6736 | return NULL; |
| 6737 | for (i = 0; i < 256; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6738 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6739 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6740 | if (!key || !value) |
| 6741 | goto failed1; |
| 6742 | if (PyDict_SetItem(result, key, value) == -1) |
| 6743 | goto failed1; |
| 6744 | Py_DECREF(key); |
| 6745 | Py_DECREF(value); |
| 6746 | } |
| 6747 | return result; |
| 6748 | failed1: |
| 6749 | Py_XDECREF(key); |
| 6750 | Py_XDECREF(value); |
| 6751 | Py_DECREF(result); |
| 6752 | return NULL; |
| 6753 | } |
| 6754 | |
| 6755 | /* Create a three-level trie */ |
| 6756 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 6757 | 16*count2 + 128*count3 - 1); |
| 6758 | if (!result) |
| 6759 | return PyErr_NoMemory(); |
| 6760 | PyObject_Init(result, &EncodingMapType); |
| 6761 | mresult = (struct encoding_map*)result; |
| 6762 | mresult->count2 = count2; |
| 6763 | mresult->count3 = count3; |
| 6764 | mlevel1 = mresult->level1; |
| 6765 | mlevel2 = mresult->level23; |
| 6766 | mlevel3 = mresult->level23 + 16*count2; |
| 6767 | memcpy(mlevel1, level1, 32); |
| 6768 | memset(mlevel2, 0xFF, 16*count2); |
| 6769 | memset(mlevel3, 0, 128*count3); |
| 6770 | count3 = 0; |
| 6771 | for (i = 1; i < 256; i++) { |
| 6772 | int o1, o2, o3, i2, i3; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6773 | if (PyUnicode_READ(kind, data, i) == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6774 | /* unmapped character */ |
| 6775 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6776 | o1 = PyUnicode_READ(kind, data, i)>>11; |
| 6777 | o2 = (PyUnicode_READ(kind, data, i)>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6778 | i2 = 16*mlevel1[o1] + o2; |
| 6779 | if (mlevel2[i2] == 0xFF) |
| 6780 | mlevel2[i2] = count3++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6781 | o3 = PyUnicode_READ(kind, data, i) & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6782 | i3 = 128*mlevel2[i2] + o3; |
| 6783 | mlevel3[i3] = i; |
| 6784 | } |
| 6785 | return result; |
| 6786 | } |
| 6787 | |
| 6788 | static int |
| 6789 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 6790 | { |
| 6791 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 6792 | int l1 = c>>11; |
| 6793 | int l2 = (c>>7) & 0xF; |
| 6794 | int l3 = c & 0x7F; |
| 6795 | int i; |
| 6796 | |
| 6797 | #ifdef Py_UNICODE_WIDE |
| 6798 | if (c > 0xFFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6799 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6800 | } |
| 6801 | #endif |
| 6802 | if (c == 0) |
| 6803 | return 0; |
| 6804 | /* level 1*/ |
| 6805 | i = map->level1[l1]; |
| 6806 | if (i == 0xFF) { |
| 6807 | return -1; |
| 6808 | } |
| 6809 | /* level 2*/ |
| 6810 | i = map->level23[16*i+l2]; |
| 6811 | if (i == 0xFF) { |
| 6812 | return -1; |
| 6813 | } |
| 6814 | /* level 3 */ |
| 6815 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 6816 | if (i == 0) { |
| 6817 | return -1; |
| 6818 | } |
| 6819 | return i; |
| 6820 | } |
| 6821 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6822 | /* Lookup the character ch in the mapping. If the character |
| 6823 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 6824 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6825 | static PyObject * |
| 6826 | charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6827 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6828 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6829 | PyObject *x; |
| 6830 | |
| 6831 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6832 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6833 | x = PyObject_GetItem(mapping, w); |
| 6834 | Py_DECREF(w); |
| 6835 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6836 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 6837 | /* No mapping found means: mapping is undefined. */ |
| 6838 | PyErr_Clear(); |
| 6839 | x = Py_None; |
| 6840 | Py_INCREF(x); |
| 6841 | return x; |
| 6842 | } else |
| 6843 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6844 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 6845 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6846 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6847 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6848 | long value = PyLong_AS_LONG(x); |
| 6849 | if (value < 0 || value > 255) { |
| 6850 | PyErr_SetString(PyExc_TypeError, |
| 6851 | "character mapping must be in range(256)"); |
| 6852 | Py_DECREF(x); |
| 6853 | return NULL; |
| 6854 | } |
| 6855 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6856 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6857 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6858 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6859 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6860 | /* wrong return value */ |
| 6861 | PyErr_Format(PyExc_TypeError, |
| 6862 | "character mapping must return integer, bytes or None, not %.400s", |
| 6863 | x->ob_type->tp_name); |
| 6864 | Py_DECREF(x); |
| 6865 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6866 | } |
| 6867 | } |
| 6868 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6869 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6870 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6871 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6872 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 6873 | /* exponentially overallocate to minimize reallocations */ |
| 6874 | if (requiredsize < 2*outsize) |
| 6875 | requiredsize = 2*outsize; |
| 6876 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 6877 | return -1; |
| 6878 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6879 | } |
| 6880 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6881 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6882 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6883 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6884 | /* 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] | 6885 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6886 | space is available. Return a new reference to the object that |
| 6887 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 6888 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 6889 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6890 | static charmapencode_result |
| 6891 | charmapencode_output(Py_UNICODE c, PyObject *mapping, |
| 6892 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6893 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6894 | PyObject *rep; |
| 6895 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6896 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6897 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 6898 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6899 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6900 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6901 | if (res == -1) |
| 6902 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6903 | if (outsize<requiredsize) |
| 6904 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 6905 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6906 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6907 | outstart[(*outpos)++] = (char)res; |
| 6908 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6909 | } |
| 6910 | |
| 6911 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6912 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6913 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6914 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6915 | Py_DECREF(rep); |
| 6916 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6917 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6918 | if (PyLong_Check(rep)) { |
| 6919 | Py_ssize_t requiredsize = *outpos+1; |
| 6920 | if (outsize<requiredsize) |
| 6921 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 6922 | Py_DECREF(rep); |
| 6923 | return enc_EXCEPTION; |
| 6924 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6925 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6926 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6927 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6928 | else { |
| 6929 | const char *repchars = PyBytes_AS_STRING(rep); |
| 6930 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 6931 | Py_ssize_t requiredsize = *outpos+repsize; |
| 6932 | if (outsize<requiredsize) |
| 6933 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 6934 | Py_DECREF(rep); |
| 6935 | return enc_EXCEPTION; |
| 6936 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6937 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6938 | memcpy(outstart + *outpos, repchars, repsize); |
| 6939 | *outpos += repsize; |
| 6940 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6941 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6942 | Py_DECREF(rep); |
| 6943 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6944 | } |
| 6945 | |
| 6946 | /* handle an error in PyUnicode_EncodeCharmap |
| 6947 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6948 | static int |
| 6949 | charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6950 | 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] | 6951 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 6952 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6953 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6954 | { |
| 6955 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6956 | Py_ssize_t repsize; |
| 6957 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6958 | Py_UNICODE *uni2; |
| 6959 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6960 | Py_ssize_t collstartpos = *inpos; |
| 6961 | Py_ssize_t collendpos = *inpos+1; |
| 6962 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6963 | char *encoding = "charmap"; |
| 6964 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6965 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6966 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6967 | /* find all unencodable characters */ |
| 6968 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6969 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 6970 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6971 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 6972 | if (res != -1) |
| 6973 | break; |
| 6974 | ++collendpos; |
| 6975 | continue; |
| 6976 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6977 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6978 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 6979 | if (rep==NULL) |
| 6980 | return -1; |
| 6981 | else if (rep!=Py_None) { |
| 6982 | Py_DECREF(rep); |
| 6983 | break; |
| 6984 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6985 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6986 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6987 | } |
| 6988 | /* cache callback name lookup |
| 6989 | * (if not done yet, i.e. it's the first error) */ |
| 6990 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6991 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6992 | *known_errorHandler = 1; |
| 6993 | else if (!strcmp(errors, "replace")) |
| 6994 | *known_errorHandler = 2; |
| 6995 | else if (!strcmp(errors, "ignore")) |
| 6996 | *known_errorHandler = 3; |
| 6997 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6998 | *known_errorHandler = 4; |
| 6999 | else |
| 7000 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7001 | } |
| 7002 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7003 | case 1: /* strict */ |
| 7004 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7005 | return -1; |
| 7006 | case 2: /* replace */ |
| 7007 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7008 | x = charmapencode_output('?', mapping, res, respos); |
| 7009 | if (x==enc_EXCEPTION) { |
| 7010 | return -1; |
| 7011 | } |
| 7012 | else if (x==enc_FAILED) { |
| 7013 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7014 | return -1; |
| 7015 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7016 | } |
| 7017 | /* fall through */ |
| 7018 | case 3: /* ignore */ |
| 7019 | *inpos = collendpos; |
| 7020 | break; |
| 7021 | case 4: /* xmlcharrefreplace */ |
| 7022 | /* generate replacement (temporarily (mis)uses p) */ |
| 7023 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7024 | char buffer[2+29+1+1]; |
| 7025 | char *cp; |
| 7026 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 7027 | for (cp = buffer; *cp; ++cp) { |
| 7028 | x = charmapencode_output(*cp, mapping, res, respos); |
| 7029 | if (x==enc_EXCEPTION) |
| 7030 | return -1; |
| 7031 | else if (x==enc_FAILED) { |
| 7032 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7033 | return -1; |
| 7034 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7035 | } |
| 7036 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7037 | *inpos = collendpos; |
| 7038 | break; |
| 7039 | default: |
| 7040 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7041 | encoding, reason, p, size, exceptionObject, |
| 7042 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7043 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7044 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7045 | if (PyBytes_Check(repunicode)) { |
| 7046 | /* Directly copy bytes result to output. */ |
| 7047 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 7048 | Py_ssize_t requiredsize; |
| 7049 | repsize = PyBytes_Size(repunicode); |
| 7050 | requiredsize = *respos + repsize; |
| 7051 | if (requiredsize > outsize) |
| 7052 | /* Make room for all additional bytes. */ |
| 7053 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 7054 | Py_DECREF(repunicode); |
| 7055 | return -1; |
| 7056 | } |
| 7057 | memcpy(PyBytes_AsString(*res) + *respos, |
| 7058 | PyBytes_AsString(repunicode), repsize); |
| 7059 | *respos += repsize; |
| 7060 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7061 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7062 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7063 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7064 | /* generate replacement */ |
| 7065 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 7066 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7067 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 7068 | if (x==enc_EXCEPTION) { |
| 7069 | return -1; |
| 7070 | } |
| 7071 | else if (x==enc_FAILED) { |
| 7072 | Py_DECREF(repunicode); |
| 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 | *inpos = newpos; |
| 7078 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7079 | } |
| 7080 | return 0; |
| 7081 | } |
| 7082 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7083 | PyObject * |
| 7084 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 7085 | Py_ssize_t size, |
| 7086 | PyObject *mapping, |
| 7087 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7088 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7089 | /* output object */ |
| 7090 | PyObject *res = NULL; |
| 7091 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7092 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7093 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7094 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7095 | PyObject *errorHandler = NULL; |
| 7096 | PyObject *exc = NULL; |
| 7097 | /* the following variable is used for caching string comparisons |
| 7098 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 7099 | * 3=ignore, 4=xmlcharrefreplace */ |
| 7100 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7101 | |
| 7102 | /* Default to Latin-1 */ |
| 7103 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7104 | return PyUnicode_EncodeLatin1(p, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7105 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7106 | /* allocate enough for a simple encoding without |
| 7107 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7108 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7109 | if (res == NULL) |
| 7110 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 7111 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7112 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7113 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7114 | while (inpos<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7115 | /* try to encode it */ |
| 7116 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 7117 | if (x==enc_EXCEPTION) /* error */ |
| 7118 | goto onError; |
| 7119 | if (x==enc_FAILED) { /* unencodable character */ |
| 7120 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 7121 | &exc, |
| 7122 | &known_errorHandler, &errorHandler, errors, |
| 7123 | &res, &respos)) { |
| 7124 | goto onError; |
| 7125 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7126 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7127 | else |
| 7128 | /* done with this character => adjust input position */ |
| 7129 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7130 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7131 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7132 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7133 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 7134 | if (_PyBytes_Resize(&res, respos) < 0) |
| 7135 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7136 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7137 | Py_XDECREF(exc); |
| 7138 | Py_XDECREF(errorHandler); |
| 7139 | return res; |
| 7140 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7141 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7142 | Py_XDECREF(res); |
| 7143 | Py_XDECREF(exc); |
| 7144 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7145 | return NULL; |
| 7146 | } |
| 7147 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7148 | PyObject * |
| 7149 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 7150 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7151 | { |
| 7152 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7153 | PyErr_BadArgument(); |
| 7154 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7155 | } |
| 7156 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7157 | PyUnicode_GET_SIZE(unicode), |
| 7158 | mapping, |
| 7159 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7160 | } |
| 7161 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7162 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7163 | static void |
| 7164 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7165 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7166 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7167 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7168 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7169 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7170 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 7171 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7172 | } |
| 7173 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7174 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 7175 | goto onError; |
| 7176 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 7177 | goto onError; |
| 7178 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 7179 | goto onError; |
| 7180 | return; |
| 7181 | onError: |
| 7182 | Py_DECREF(*exceptionObject); |
| 7183 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7184 | } |
| 7185 | } |
| 7186 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7187 | /* raises a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7188 | static void |
| 7189 | raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7190 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7191 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7192 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7193 | { |
| 7194 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7195 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7196 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7197 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7198 | } |
| 7199 | |
| 7200 | /* error handling callback helper: |
| 7201 | build arguments, call the callback and check the arguments, |
| 7202 | put the result into newpos and return the replacement string, which |
| 7203 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7204 | static PyObject * |
| 7205 | unicode_translate_call_errorhandler(const char *errors, |
| 7206 | PyObject **errorHandler, |
| 7207 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7208 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7209 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7210 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7211 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7212 | 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] | 7213 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7214 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7215 | PyObject *restuple; |
| 7216 | PyObject *resunicode; |
| 7217 | |
| 7218 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7219 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7220 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7221 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7222 | } |
| 7223 | |
| 7224 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7225 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7226 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7227 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7228 | |
| 7229 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7230 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7231 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7232 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7233 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 7234 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7235 | Py_DECREF(restuple); |
| 7236 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7237 | } |
| 7238 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7239 | &resunicode, &i_newpos)) { |
| 7240 | Py_DECREF(restuple); |
| 7241 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7242 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7243 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7244 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7245 | else |
| 7246 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7247 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7248 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 7249 | Py_DECREF(restuple); |
| 7250 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 7251 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7252 | Py_INCREF(resunicode); |
| 7253 | Py_DECREF(restuple); |
| 7254 | return resunicode; |
| 7255 | } |
| 7256 | |
| 7257 | /* Lookup the character ch in the mapping and put the result in result, |
| 7258 | which must be decrefed by the caller. |
| 7259 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7260 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7261 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7262 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7263 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7264 | PyObject *x; |
| 7265 | |
| 7266 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7267 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7268 | x = PyObject_GetItem(mapping, w); |
| 7269 | Py_DECREF(w); |
| 7270 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7271 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7272 | /* No mapping found means: use 1:1 mapping. */ |
| 7273 | PyErr_Clear(); |
| 7274 | *result = NULL; |
| 7275 | return 0; |
| 7276 | } else |
| 7277 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7278 | } |
| 7279 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7280 | *result = x; |
| 7281 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7282 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7283 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7284 | long value = PyLong_AS_LONG(x); |
| 7285 | long max = PyUnicode_GetMax(); |
| 7286 | if (value < 0 || value > max) { |
| 7287 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 7288 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7289 | Py_DECREF(x); |
| 7290 | return -1; |
| 7291 | } |
| 7292 | *result = x; |
| 7293 | return 0; |
| 7294 | } |
| 7295 | else if (PyUnicode_Check(x)) { |
| 7296 | *result = x; |
| 7297 | return 0; |
| 7298 | } |
| 7299 | else { |
| 7300 | /* wrong return value */ |
| 7301 | PyErr_SetString(PyExc_TypeError, |
| 7302 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7303 | Py_DECREF(x); |
| 7304 | return -1; |
| 7305 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7306 | } |
| 7307 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7308 | if not reallocate and adjust various state variables. |
| 7309 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7310 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7311 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7312 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7313 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7314 | Py_ssize_t oldsize = *psize; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 7315 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7316 | /* exponentially overallocate to minimize reallocations */ |
| 7317 | if (requiredsize < 2 * oldsize) |
| 7318 | requiredsize = 2 * oldsize; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7319 | *outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 7320 | if (*outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7321 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7322 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7323 | } |
| 7324 | return 0; |
| 7325 | } |
| 7326 | /* lookup the character, put the result in the output string and adjust |
| 7327 | various state variables. Return a new reference to the object that |
| 7328 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 7329 | undefined (in which case no character was written). |
| 7330 | The called must decref result. |
| 7331 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7332 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7333 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 7334 | PyObject *mapping, Py_UCS4 **output, |
| 7335 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7336 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7337 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7338 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 7339 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7340 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7341 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7342 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7343 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7344 | } |
| 7345 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7346 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7347 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7348 | /* 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] | 7349 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7350 | } |
| 7351 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7352 | Py_ssize_t repsize; |
| 7353 | if (PyUnicode_READY(*res) == -1) |
| 7354 | return -1; |
| 7355 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7356 | if (repsize==1) { |
| 7357 | /* 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] | 7358 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7359 | } |
| 7360 | else if (repsize!=0) { |
| 7361 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7362 | Py_ssize_t requiredsize = *opos + |
| 7363 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7364 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7365 | Py_ssize_t i; |
| 7366 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7367 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7368 | for(i = 0; i < repsize; i++) |
| 7369 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7370 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7371 | } |
| 7372 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7373 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7374 | return 0; |
| 7375 | } |
| 7376 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7377 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7378 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 7379 | PyObject *mapping, |
| 7380 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7381 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7382 | /* input object */ |
| 7383 | char *idata; |
| 7384 | Py_ssize_t size, i; |
| 7385 | int kind; |
| 7386 | /* output buffer */ |
| 7387 | Py_UCS4 *output = NULL; |
| 7388 | Py_ssize_t osize; |
| 7389 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7390 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7391 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7392 | char *reason = "character maps to <undefined>"; |
| 7393 | PyObject *errorHandler = NULL; |
| 7394 | PyObject *exc = NULL; |
| 7395 | /* the following variable is used for caching string comparisons |
| 7396 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 7397 | * 3=ignore, 4=xmlcharrefreplace */ |
| 7398 | int known_errorHandler = -1; |
| 7399 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7400 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7401 | PyErr_BadArgument(); |
| 7402 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7403 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7404 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7405 | if (PyUnicode_READY(input) == -1) |
| 7406 | return NULL; |
| 7407 | idata = (char*)PyUnicode_DATA(input); |
| 7408 | kind = PyUnicode_KIND(input); |
| 7409 | size = PyUnicode_GET_LENGTH(input); |
| 7410 | i = 0; |
| 7411 | |
| 7412 | if (size == 0) { |
| 7413 | Py_INCREF(input); |
| 7414 | return input; |
| 7415 | } |
| 7416 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7417 | /* allocate enough for a simple 1:1 translation without |
| 7418 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7419 | osize = size; |
| 7420 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 7421 | opos = 0; |
| 7422 | if (output == NULL) { |
| 7423 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7424 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7425 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7426 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7427 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7428 | /* try to encode it */ |
| 7429 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7430 | if (charmaptranslate_output(input, i, mapping, |
| 7431 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7432 | Py_XDECREF(x); |
| 7433 | goto onError; |
| 7434 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7435 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7436 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7437 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7438 | else { /* untranslatable character */ |
| 7439 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 7440 | Py_ssize_t repsize; |
| 7441 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7442 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7443 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7444 | Py_ssize_t collstart = i; |
| 7445 | Py_ssize_t collend = i+1; |
| 7446 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7447 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7448 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7449 | while (collend < size) { |
| 7450 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7451 | goto onError; |
| 7452 | Py_XDECREF(x); |
| 7453 | if (x!=Py_None) |
| 7454 | break; |
| 7455 | ++collend; |
| 7456 | } |
| 7457 | /* cache callback name lookup |
| 7458 | * (if not done yet, i.e. it's the first error) */ |
| 7459 | if (known_errorHandler==-1) { |
| 7460 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 7461 | known_errorHandler = 1; |
| 7462 | else if (!strcmp(errors, "replace")) |
| 7463 | known_errorHandler = 2; |
| 7464 | else if (!strcmp(errors, "ignore")) |
| 7465 | known_errorHandler = 3; |
| 7466 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 7467 | known_errorHandler = 4; |
| 7468 | else |
| 7469 | known_errorHandler = 0; |
| 7470 | } |
| 7471 | switch (known_errorHandler) { |
| 7472 | case 1: /* strict */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7473 | raise_translate_exception(&exc, input, collstart, |
| 7474 | collend, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7475 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7476 | case 2: /* replace */ |
| 7477 | /* 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] | 7478 | for (coll = collstart; coll<collend; coll++) |
| 7479 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7480 | /* fall through */ |
| 7481 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7482 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7483 | break; |
| 7484 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7485 | /* generate replacement (temporarily (mis)uses i) */ |
| 7486 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7487 | char buffer[2+29+1+1]; |
| 7488 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7489 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 7490 | if (charmaptranslate_makespace(&output, &osize, |
| 7491 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7492 | goto onError; |
| 7493 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7494 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7495 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7496 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7497 | break; |
| 7498 | default: |
| 7499 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7500 | reason, input, &exc, |
| 7501 | collstart, collend, &newpos); |
| 7502 | if (repunicode == NULL || PyUnicode_READY(repunicode) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7503 | goto onError; |
| 7504 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7505 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 7506 | if (charmaptranslate_makespace(&output, &osize, |
| 7507 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7508 | Py_DECREF(repunicode); |
| 7509 | goto onError; |
| 7510 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7511 | for (uni2 = 0; repsize-->0; ++uni2) |
| 7512 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 7513 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7514 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7515 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7516 | } |
| 7517 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7518 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 7519 | if (!res) |
| 7520 | goto onError; |
| 7521 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7522 | Py_XDECREF(exc); |
| 7523 | Py_XDECREF(errorHandler); |
| 7524 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7525 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7526 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7527 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7528 | Py_XDECREF(exc); |
| 7529 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7530 | return NULL; |
| 7531 | } |
| 7532 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7533 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 7534 | PyObject * |
| 7535 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 7536 | Py_ssize_t size, |
| 7537 | PyObject *mapping, |
| 7538 | const char *errors) |
| 7539 | { |
| 7540 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 7541 | if (!unicode) |
| 7542 | return NULL; |
| 7543 | return _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 7544 | } |
| 7545 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7546 | PyObject * |
| 7547 | PyUnicode_Translate(PyObject *str, |
| 7548 | PyObject *mapping, |
| 7549 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7550 | { |
| 7551 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7552 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7553 | str = PyUnicode_FromObject(str); |
| 7554 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7555 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7556 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7557 | Py_DECREF(str); |
| 7558 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7559 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7560 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7561 | Py_XDECREF(str); |
| 7562 | return NULL; |
| 7563 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7564 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7565 | static Py_UCS4 |
| 7566 | fix_decimal_and_space_to_ascii(PyUnicodeObject *self) |
| 7567 | { |
| 7568 | /* No need to call PyUnicode_READY(self) because this function is only |
| 7569 | called as a callback from fixup() which does it already. */ |
| 7570 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 7571 | const int kind = PyUnicode_KIND(self); |
| 7572 | void *data = PyUnicode_DATA(self); |
| 7573 | Py_UCS4 maxchar = 0, ch, fixed; |
| 7574 | Py_ssize_t i; |
| 7575 | |
| 7576 | for (i = 0; i < len; ++i) { |
| 7577 | ch = PyUnicode_READ(kind, data, i); |
| 7578 | fixed = 0; |
| 7579 | if (ch > 127) { |
| 7580 | if (Py_UNICODE_ISSPACE(ch)) |
| 7581 | fixed = ' '; |
| 7582 | else { |
| 7583 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 7584 | if (decimal >= 0) |
| 7585 | fixed = '0' + decimal; |
| 7586 | } |
| 7587 | if (fixed != 0) { |
| 7588 | if (fixed > maxchar) |
| 7589 | maxchar = fixed; |
| 7590 | PyUnicode_WRITE(kind, data, i, fixed); |
| 7591 | } |
| 7592 | else if (ch > maxchar) |
| 7593 | maxchar = ch; |
| 7594 | } |
| 7595 | else if (ch > maxchar) |
| 7596 | maxchar = ch; |
| 7597 | } |
| 7598 | |
| 7599 | return maxchar; |
| 7600 | } |
| 7601 | |
| 7602 | PyObject * |
| 7603 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 7604 | { |
| 7605 | if (!PyUnicode_Check(unicode)) { |
| 7606 | PyErr_BadInternalCall(); |
| 7607 | return NULL; |
| 7608 | } |
| 7609 | if (PyUnicode_READY(unicode) == -1) |
| 7610 | return NULL; |
| 7611 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 7612 | /* If the string is already ASCII, just return the same string */ |
| 7613 | Py_INCREF(unicode); |
| 7614 | return unicode; |
| 7615 | } |
| 7616 | return fixup((PyUnicodeObject *)unicode, fix_decimal_and_space_to_ascii); |
| 7617 | } |
| 7618 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 7619 | PyObject * |
| 7620 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 7621 | Py_ssize_t length) |
| 7622 | { |
| 7623 | PyObject *result; |
| 7624 | Py_UNICODE *p; /* write pointer into result */ |
| 7625 | Py_ssize_t i; |
| 7626 | /* Copy to a new string */ |
| 7627 | result = (PyObject *)_PyUnicode_New(length); |
| 7628 | Py_UNICODE_COPY(PyUnicode_AS_UNICODE(result), s, length); |
| 7629 | if (result == NULL) |
| 7630 | return result; |
| 7631 | p = PyUnicode_AS_UNICODE(result); |
| 7632 | /* Iterate over code points */ |
| 7633 | for (i = 0; i < length; i++) { |
| 7634 | Py_UNICODE ch =s[i]; |
| 7635 | if (ch > 127) { |
| 7636 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 7637 | if (decimal >= 0) |
| 7638 | p[i] = '0' + decimal; |
| 7639 | } |
| 7640 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7641 | if (PyUnicode_READY((PyUnicodeObject*)result) == -1) { |
| 7642 | Py_DECREF(result); |
| 7643 | return NULL; |
| 7644 | } |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 7645 | return result; |
| 7646 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 7647 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 7648 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7649 | int |
| 7650 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 7651 | Py_ssize_t length, |
| 7652 | char *output, |
| 7653 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 7654 | { |
| 7655 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7656 | PyObject *errorHandler = NULL; |
| 7657 | PyObject *exc = NULL; |
| 7658 | const char *encoding = "decimal"; |
| 7659 | const char *reason = "invalid decimal Unicode string"; |
| 7660 | /* the following variable is used for caching string comparisons |
| 7661 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 7662 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 7663 | |
| 7664 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7665 | PyErr_BadArgument(); |
| 7666 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 7667 | } |
| 7668 | |
| 7669 | p = s; |
| 7670 | end = s + length; |
| 7671 | while (p < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7672 | register Py_UNICODE ch = *p; |
| 7673 | int decimal; |
| 7674 | PyObject *repunicode; |
| 7675 | Py_ssize_t repsize; |
| 7676 | Py_ssize_t newpos; |
| 7677 | Py_UNICODE *uni2; |
| 7678 | Py_UNICODE *collstart; |
| 7679 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7680 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7681 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7682 | *output++ = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7683 | ++p; |
| 7684 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7685 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7686 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 7687 | if (decimal >= 0) { |
| 7688 | *output++ = '0' + decimal; |
| 7689 | ++p; |
| 7690 | continue; |
| 7691 | } |
| 7692 | if (0 < ch && ch < 256) { |
| 7693 | *output++ = (char)ch; |
| 7694 | ++p; |
| 7695 | continue; |
| 7696 | } |
| 7697 | /* All other characters are considered unencodable */ |
| 7698 | collstart = p; |
| 7699 | collend = p+1; |
| 7700 | while (collend < end) { |
| 7701 | if ((0 < *collend && *collend < 256) || |
| 7702 | !Py_UNICODE_ISSPACE(*collend) || |
| 7703 | Py_UNICODE_TODECIMAL(*collend)) |
| 7704 | break; |
| 7705 | } |
| 7706 | /* cache callback name lookup |
| 7707 | * (if not done yet, i.e. it's the first error) */ |
| 7708 | if (known_errorHandler==-1) { |
| 7709 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 7710 | known_errorHandler = 1; |
| 7711 | else if (!strcmp(errors, "replace")) |
| 7712 | known_errorHandler = 2; |
| 7713 | else if (!strcmp(errors, "ignore")) |
| 7714 | known_errorHandler = 3; |
| 7715 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 7716 | known_errorHandler = 4; |
| 7717 | else |
| 7718 | known_errorHandler = 0; |
| 7719 | } |
| 7720 | switch (known_errorHandler) { |
| 7721 | case 1: /* strict */ |
| 7722 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 7723 | goto onError; |
| 7724 | case 2: /* replace */ |
| 7725 | for (p = collstart; p < collend; ++p) |
| 7726 | *output++ = '?'; |
| 7727 | /* fall through */ |
| 7728 | case 3: /* ignore */ |
| 7729 | p = collend; |
| 7730 | break; |
| 7731 | case 4: /* xmlcharrefreplace */ |
| 7732 | /* generate replacement (temporarily (mis)uses p) */ |
| 7733 | for (p = collstart; p < collend; ++p) |
| 7734 | output += sprintf(output, "&#%d;", (int)*p); |
| 7735 | p = collend; |
| 7736 | break; |
| 7737 | default: |
| 7738 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 7739 | encoding, reason, s, length, &exc, |
| 7740 | collstart-s, collend-s, &newpos); |
| 7741 | if (repunicode == NULL) |
| 7742 | goto onError; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7743 | if (!PyUnicode_Check(repunicode)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7744 | /* Byte results not supported, since they have no decimal property. */ |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7745 | PyErr_SetString(PyExc_TypeError, "error handler should return unicode"); |
| 7746 | Py_DECREF(repunicode); |
| 7747 | goto onError; |
| 7748 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7749 | /* generate replacement */ |
| 7750 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 7751 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 7752 | Py_UNICODE ch = *uni2; |
| 7753 | if (Py_UNICODE_ISSPACE(ch)) |
| 7754 | *output++ = ' '; |
| 7755 | else { |
| 7756 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 7757 | if (decimal >= 0) |
| 7758 | *output++ = '0' + decimal; |
| 7759 | else if (0 < ch && ch < 256) |
| 7760 | *output++ = (char)ch; |
| 7761 | else { |
| 7762 | Py_DECREF(repunicode); |
| 7763 | raise_encode_exception(&exc, encoding, |
| 7764 | s, length, collstart-s, collend-s, reason); |
| 7765 | goto onError; |
| 7766 | } |
| 7767 | } |
| 7768 | } |
| 7769 | p = s + newpos; |
| 7770 | Py_DECREF(repunicode); |
| 7771 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 7772 | } |
| 7773 | /* 0-terminate the output string */ |
| 7774 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7775 | Py_XDECREF(exc); |
| 7776 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 7777 | return 0; |
| 7778 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7779 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7780 | Py_XDECREF(exc); |
| 7781 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 7782 | return -1; |
| 7783 | } |
| 7784 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7785 | /* --- Helpers ------------------------------------------------------------ */ |
| 7786 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7787 | #include "stringlib/ucs1lib.h" |
| 7788 | #include "stringlib/fastsearch.h" |
| 7789 | #include "stringlib/partition.h" |
| 7790 | #include "stringlib/split.h" |
| 7791 | #include "stringlib/count.h" |
| 7792 | #include "stringlib/find.h" |
| 7793 | #include "stringlib/localeutil.h" |
| 7794 | #include "stringlib/undef.h" |
| 7795 | |
| 7796 | #include "stringlib/ucs2lib.h" |
| 7797 | #include "stringlib/fastsearch.h" |
| 7798 | #include "stringlib/partition.h" |
| 7799 | #include "stringlib/split.h" |
| 7800 | #include "stringlib/count.h" |
| 7801 | #include "stringlib/find.h" |
| 7802 | #include "stringlib/localeutil.h" |
| 7803 | #include "stringlib/undef.h" |
| 7804 | |
| 7805 | #include "stringlib/ucs4lib.h" |
| 7806 | #include "stringlib/fastsearch.h" |
| 7807 | #include "stringlib/partition.h" |
| 7808 | #include "stringlib/split.h" |
| 7809 | #include "stringlib/count.h" |
| 7810 | #include "stringlib/find.h" |
| 7811 | #include "stringlib/localeutil.h" |
| 7812 | #include "stringlib/undef.h" |
| 7813 | |
| 7814 | static Py_ssize_t |
| 7815 | any_find_slice(Py_ssize_t Py_LOCAL_CALLBACK(ucs1)(const Py_UCS1*, Py_ssize_t, |
| 7816 | const Py_UCS1*, Py_ssize_t, |
| 7817 | Py_ssize_t, Py_ssize_t), |
| 7818 | Py_ssize_t Py_LOCAL_CALLBACK(ucs2)(const Py_UCS2*, Py_ssize_t, |
| 7819 | const Py_UCS2*, Py_ssize_t, |
| 7820 | Py_ssize_t, Py_ssize_t), |
| 7821 | Py_ssize_t Py_LOCAL_CALLBACK(ucs4)(const Py_UCS4*, Py_ssize_t, |
| 7822 | const Py_UCS4*, Py_ssize_t, |
| 7823 | Py_ssize_t, Py_ssize_t), |
| 7824 | PyObject* s1, PyObject* s2, |
| 7825 | Py_ssize_t start, |
| 7826 | Py_ssize_t end) |
| 7827 | { |
| 7828 | int kind1, kind2, kind; |
| 7829 | void *buf1, *buf2; |
| 7830 | Py_ssize_t len1, len2, result; |
| 7831 | |
| 7832 | kind1 = PyUnicode_KIND(s1); |
| 7833 | kind2 = PyUnicode_KIND(s2); |
| 7834 | kind = kind1 > kind2 ? kind1 : kind2; |
| 7835 | buf1 = PyUnicode_DATA(s1); |
| 7836 | buf2 = PyUnicode_DATA(s2); |
| 7837 | if (kind1 != kind) |
| 7838 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 7839 | if (!buf1) |
| 7840 | return -2; |
| 7841 | if (kind2 != kind) |
| 7842 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 7843 | if (!buf2) { |
| 7844 | if (kind1 != kind) PyMem_Free(buf1); |
| 7845 | return -2; |
| 7846 | } |
| 7847 | len1 = PyUnicode_GET_LENGTH(s1); |
| 7848 | len2 = PyUnicode_GET_LENGTH(s2); |
| 7849 | |
| 7850 | switch(kind) { |
| 7851 | case PyUnicode_1BYTE_KIND: |
| 7852 | result = ucs1(buf1, len1, buf2, len2, start, end); |
| 7853 | break; |
| 7854 | case PyUnicode_2BYTE_KIND: |
| 7855 | result = ucs2(buf1, len1, buf2, len2, start, end); |
| 7856 | break; |
| 7857 | case PyUnicode_4BYTE_KIND: |
| 7858 | result = ucs4(buf1, len1, buf2, len2, start, end); |
| 7859 | break; |
| 7860 | default: |
| 7861 | assert(0); result = -2; |
| 7862 | } |
| 7863 | |
| 7864 | if (kind1 != kind) |
| 7865 | PyMem_Free(buf1); |
| 7866 | if (kind2 != kind) |
| 7867 | PyMem_Free(buf2); |
| 7868 | |
| 7869 | return result; |
| 7870 | } |
| 7871 | |
| 7872 | Py_ssize_t |
| 7873 | _PyUnicode_InsertThousandsGrouping(int kind, void *data, |
| 7874 | Py_ssize_t n_buffer, |
| 7875 | void *digits, Py_ssize_t n_digits, |
| 7876 | Py_ssize_t min_width, |
| 7877 | const char *grouping, |
| 7878 | const char *thousands_sep) |
| 7879 | { |
| 7880 | switch(kind) { |
| 7881 | case PyUnicode_1BYTE_KIND: |
| 7882 | return _PyUnicode_ucs1_InsertThousandsGrouping( |
| 7883 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 7884 | min_width, grouping, thousands_sep); |
| 7885 | case PyUnicode_2BYTE_KIND: |
| 7886 | return _PyUnicode_ucs2_InsertThousandsGrouping( |
| 7887 | (Py_UCS2*)data, n_buffer, (Py_UCS2*)digits, n_digits, |
| 7888 | min_width, grouping, thousands_sep); |
| 7889 | case PyUnicode_4BYTE_KIND: |
| 7890 | return _PyUnicode_ucs4_InsertThousandsGrouping( |
| 7891 | (Py_UCS4*)data, n_buffer, (Py_UCS4*)digits, n_digits, |
| 7892 | min_width, grouping, thousands_sep); |
| 7893 | } |
| 7894 | assert(0); |
| 7895 | return -1; |
| 7896 | } |
| 7897 | |
| 7898 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 7899 | #include "stringlib/unicodedefs.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7900 | #include "stringlib/fastsearch.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 7901 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7902 | #include "stringlib/count.h" |
| 7903 | #include "stringlib/find.h" |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 7904 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7905 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 7906 | #define ADJUST_INDICES(start, end, len) \ |
| 7907 | if (end > len) \ |
| 7908 | end = len; \ |
| 7909 | else if (end < 0) { \ |
| 7910 | end += len; \ |
| 7911 | if (end < 0) \ |
| 7912 | end = 0; \ |
| 7913 | } \ |
| 7914 | if (start < 0) { \ |
| 7915 | start += len; \ |
| 7916 | if (start < 0) \ |
| 7917 | start = 0; \ |
| 7918 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7919 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7920 | Py_ssize_t |
| 7921 | PyUnicode_Count(PyObject *str, |
| 7922 | PyObject *substr, |
| 7923 | Py_ssize_t start, |
| 7924 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7925 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7926 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7927 | PyUnicodeObject* str_obj; |
| 7928 | PyUnicodeObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7929 | int kind1, kind2, kind; |
| 7930 | void *buf1 = NULL, *buf2 = NULL; |
| 7931 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7932 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7933 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7934 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7935 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7936 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 7937 | if (!sub_obj || PyUnicode_READY(sub_obj) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7938 | Py_DECREF(str_obj); |
| 7939 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7940 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7941 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7942 | kind1 = PyUnicode_KIND(str_obj); |
| 7943 | kind2 = PyUnicode_KIND(sub_obj); |
| 7944 | kind = kind1 > kind2 ? kind1 : kind2; |
| 7945 | buf1 = PyUnicode_DATA(str_obj); |
| 7946 | if (kind1 != kind) |
| 7947 | buf1 = _PyUnicode_AsKind((PyObject*)str_obj, kind); |
| 7948 | if (!buf1) |
| 7949 | goto onError; |
| 7950 | buf2 = PyUnicode_DATA(sub_obj); |
| 7951 | if (kind2 != kind) |
| 7952 | buf2 = _PyUnicode_AsKind((PyObject*)sub_obj, kind); |
| 7953 | if (!buf2) |
| 7954 | goto onError; |
| 7955 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 7956 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 7957 | |
| 7958 | ADJUST_INDICES(start, end, len1); |
| 7959 | switch(kind) { |
| 7960 | case PyUnicode_1BYTE_KIND: |
| 7961 | result = ucs1lib_count( |
| 7962 | ((Py_UCS1*)buf1) + start, end - start, |
| 7963 | buf2, len2, PY_SSIZE_T_MAX |
| 7964 | ); |
| 7965 | break; |
| 7966 | case PyUnicode_2BYTE_KIND: |
| 7967 | result = ucs2lib_count( |
| 7968 | ((Py_UCS2*)buf1) + start, end - start, |
| 7969 | buf2, len2, PY_SSIZE_T_MAX |
| 7970 | ); |
| 7971 | break; |
| 7972 | case PyUnicode_4BYTE_KIND: |
| 7973 | result = ucs4lib_count( |
| 7974 | ((Py_UCS4*)buf1) + start, end - start, |
| 7975 | buf2, len2, PY_SSIZE_T_MAX |
| 7976 | ); |
| 7977 | break; |
| 7978 | default: |
| 7979 | assert(0); result = 0; |
| 7980 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7981 | |
| 7982 | Py_DECREF(sub_obj); |
| 7983 | Py_DECREF(str_obj); |
| 7984 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7985 | if (kind1 != kind) |
| 7986 | PyMem_Free(buf1); |
| 7987 | if (kind2 != kind) |
| 7988 | PyMem_Free(buf2); |
| 7989 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7990 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7991 | onError: |
| 7992 | Py_DECREF(sub_obj); |
| 7993 | Py_DECREF(str_obj); |
| 7994 | if (kind1 != kind && buf1) |
| 7995 | PyMem_Free(buf1); |
| 7996 | if (kind2 != kind && buf2) |
| 7997 | PyMem_Free(buf2); |
| 7998 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7999 | } |
| 8000 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8001 | Py_ssize_t |
| 8002 | PyUnicode_Find(PyObject *str, |
| 8003 | PyObject *sub, |
| 8004 | Py_ssize_t start, |
| 8005 | Py_ssize_t end, |
| 8006 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8007 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8008 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8009 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8010 | str = PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8011 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8012 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8013 | sub = PyUnicode_FromObject(sub); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8014 | if (!sub || PyUnicode_READY(sub) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8015 | Py_DECREF(str); |
| 8016 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8017 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8018 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8019 | if (direction > 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8020 | result = any_find_slice( |
| 8021 | ucs1lib_find_slice, ucs2lib_find_slice, ucs4lib_find_slice, |
| 8022 | str, sub, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8023 | ); |
| 8024 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8025 | result = any_find_slice( |
| 8026 | ucs1lib_rfind_slice, ucs2lib_rfind_slice, ucs4lib_rfind_slice, |
| 8027 | str, sub, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8028 | ); |
| 8029 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8030 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8031 | Py_DECREF(sub); |
| 8032 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8033 | return result; |
| 8034 | } |
| 8035 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8036 | Py_ssize_t |
| 8037 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 8038 | Py_ssize_t start, Py_ssize_t end, |
| 8039 | int direction) |
| 8040 | { |
| 8041 | char *result; |
| 8042 | int kind; |
| 8043 | if (PyUnicode_READY(str) == -1) |
| 8044 | return -2; |
| 8045 | if (end > PyUnicode_GET_LENGTH(str)) |
| 8046 | end = PyUnicode_GET_LENGTH(str); |
| 8047 | kind = PyUnicode_KIND(str); |
| 8048 | result = findchar(PyUnicode_1BYTE_DATA(str) |
| 8049 | + PyUnicode_KIND_SIZE(kind, start), |
| 8050 | kind, |
| 8051 | end-start, ch, direction); |
| 8052 | if (!result) |
| 8053 | return -1; |
| 8054 | return (result-(char*)PyUnicode_DATA(str)) >> (kind-1); |
| 8055 | } |
| 8056 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8057 | static int |
| 8058 | tailmatch(PyUnicodeObject *self, |
| 8059 | PyUnicodeObject *substring, |
| 8060 | Py_ssize_t start, |
| 8061 | Py_ssize_t end, |
| 8062 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8063 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8064 | int kind_self; |
| 8065 | int kind_sub; |
| 8066 | void *data_self; |
| 8067 | void *data_sub; |
| 8068 | Py_ssize_t offset; |
| 8069 | Py_ssize_t i; |
| 8070 | Py_ssize_t end_sub; |
| 8071 | |
| 8072 | if (PyUnicode_READY(self) == -1 || |
| 8073 | PyUnicode_READY(substring) == -1) |
| 8074 | return 0; |
| 8075 | |
| 8076 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8077 | return 1; |
| 8078 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8079 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 8080 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8081 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8082 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8083 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8084 | kind_self = PyUnicode_KIND(self); |
| 8085 | data_self = PyUnicode_DATA(self); |
| 8086 | kind_sub = PyUnicode_KIND(substring); |
| 8087 | data_sub = PyUnicode_DATA(substring); |
| 8088 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 8089 | |
| 8090 | if (direction > 0) |
| 8091 | offset = end; |
| 8092 | else |
| 8093 | offset = start; |
| 8094 | |
| 8095 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 8096 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 8097 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 8098 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 8099 | /* If both are of the same kind, memcmp is sufficient */ |
| 8100 | if (kind_self == kind_sub) { |
| 8101 | return ! memcmp((char *)data_self + |
| 8102 | (offset * PyUnicode_CHARACTER_SIZE(substring)), |
| 8103 | data_sub, |
| 8104 | PyUnicode_GET_LENGTH(substring) * |
| 8105 | PyUnicode_CHARACTER_SIZE(substring)); |
| 8106 | } |
| 8107 | /* otherwise we have to compare each character by first accesing it */ |
| 8108 | else { |
| 8109 | /* We do not need to compare 0 and len(substring)-1 because |
| 8110 | the if statement above ensured already that they are equal |
| 8111 | when we end up here. */ |
| 8112 | // TODO: honor direction and do a forward or backwards search |
| 8113 | for (i = 1; i < end_sub; ++i) { |
| 8114 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 8115 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 8116 | return 0; |
| 8117 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8118 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8119 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8120 | } |
| 8121 | |
| 8122 | return 0; |
| 8123 | } |
| 8124 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8125 | Py_ssize_t |
| 8126 | PyUnicode_Tailmatch(PyObject *str, |
| 8127 | PyObject *substr, |
| 8128 | Py_ssize_t start, |
| 8129 | Py_ssize_t end, |
| 8130 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8131 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8132 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8133 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8134 | str = PyUnicode_FromObject(str); |
| 8135 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8136 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8137 | substr = PyUnicode_FromObject(substr); |
| 8138 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8139 | Py_DECREF(str); |
| 8140 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8141 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8142 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8143 | result = tailmatch((PyUnicodeObject *)str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8144 | (PyUnicodeObject *)substr, |
| 8145 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8146 | Py_DECREF(str); |
| 8147 | Py_DECREF(substr); |
| 8148 | return result; |
| 8149 | } |
| 8150 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8151 | /* Apply fixfct filter to the Unicode object self and return a |
| 8152 | reference to the modified object */ |
| 8153 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8154 | static PyObject * |
| 8155 | fixup(PyUnicodeObject *self, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8156 | Py_UCS4 (*fixfct)(PyUnicodeObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8157 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8158 | PyObject *u; |
| 8159 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8160 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8161 | if (PyUnicode_READY(self) == -1) |
| 8162 | return NULL; |
| 8163 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(self); |
| 8164 | u = PyUnicode_New(PyUnicode_GET_LENGTH(self), |
| 8165 | maxchar_old); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8166 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8167 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8168 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8169 | Py_MEMCPY(PyUnicode_1BYTE_DATA(u), PyUnicode_1BYTE_DATA(self), |
| 8170 | PyUnicode_GET_LENGTH(u) * PyUnicode_CHARACTER_SIZE(u)); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8171 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8172 | /* fix functions return the new maximum character in a string, |
| 8173 | if the kind of the resulting unicode object does not change, |
| 8174 | everything is fine. Otherwise we need to change the string kind |
| 8175 | and re-run the fix function. */ |
| 8176 | maxchar_new = fixfct((PyUnicodeObject*)u); |
| 8177 | if (maxchar_new == 0) |
| 8178 | /* do nothing, keep maxchar_new at 0 which means no changes. */; |
| 8179 | else if (maxchar_new <= 127) |
| 8180 | maxchar_new = 127; |
| 8181 | else if (maxchar_new <= 255) |
| 8182 | maxchar_new = 255; |
| 8183 | else if (maxchar_new <= 65535) |
| 8184 | maxchar_new = 65535; |
| 8185 | else |
| 8186 | maxchar_new = 1114111; /* 0x10ffff */ |
| 8187 | |
| 8188 | if (!maxchar_new && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8189 | /* fixfct should return TRUE if it modified the buffer. If |
| 8190 | FALSE, return a reference to the original buffer instead |
| 8191 | (to save space, not time) */ |
| 8192 | Py_INCREF(self); |
| 8193 | Py_DECREF(u); |
| 8194 | return (PyObject*) self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8195 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8196 | else if (maxchar_new == maxchar_old) { |
| 8197 | return u; |
| 8198 | } |
| 8199 | else { |
| 8200 | /* In case the maximum character changed, we need to |
| 8201 | convert the string to the new category. */ |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8202 | PyObject *v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8203 | if (v == NULL) { |
| 8204 | Py_DECREF(u); |
| 8205 | return NULL; |
| 8206 | } |
| 8207 | if (maxchar_new > maxchar_old) { |
| 8208 | /* If the maxchar increased so that the kind changed, not all |
| 8209 | characters are representable anymore and we need to fix the |
| 8210 | string again. This only happens in very few cases. */ |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 8211 | if (PyUnicode_CopyCharacters(v, 0, |
| 8212 | (PyObject*)self, 0, |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8213 | PyUnicode_GET_LENGTH(self)) < 0) |
| 8214 | { |
| 8215 | Py_DECREF(u); |
| 8216 | return NULL; |
| 8217 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8218 | maxchar_old = fixfct((PyUnicodeObject*)v); |
| 8219 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
| 8220 | } |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8221 | else { |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 8222 | if (PyUnicode_CopyCharacters(v, 0, |
| 8223 | u, 0, |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8224 | PyUnicode_GET_LENGTH(self)) < 0) |
| 8225 | { |
| 8226 | Py_DECREF(u); |
| 8227 | return NULL; |
| 8228 | } |
| 8229 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8230 | |
| 8231 | Py_DECREF(u); |
| 8232 | return v; |
| 8233 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8234 | } |
| 8235 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8236 | static Py_UCS4 |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8237 | fixupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8238 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8239 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8240 | called as a callback from fixup() which does it already. */ |
| 8241 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8242 | const int kind = PyUnicode_KIND(self); |
| 8243 | void *data = PyUnicode_DATA(self); |
| 8244 | int touched = 0; |
| 8245 | Py_UCS4 maxchar = 0; |
| 8246 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8247 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8248 | for (i = 0; i < len; ++i) { |
| 8249 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8250 | const Py_UCS4 up = Py_UNICODE_TOUPPER(ch); |
| 8251 | if (up != ch) { |
| 8252 | if (up > maxchar) |
| 8253 | maxchar = up; |
| 8254 | PyUnicode_WRITE(kind, data, i, up); |
| 8255 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8256 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8257 | else if (ch > maxchar) |
| 8258 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8259 | } |
| 8260 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8261 | if (touched) |
| 8262 | return maxchar; |
| 8263 | else |
| 8264 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8265 | } |
| 8266 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8267 | static Py_UCS4 |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8268 | fixlower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8269 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8270 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8271 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8272 | const int kind = PyUnicode_KIND(self); |
| 8273 | void *data = PyUnicode_DATA(self); |
| 8274 | int touched = 0; |
| 8275 | Py_UCS4 maxchar = 0; |
| 8276 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8277 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8278 | for(i = 0; i < len; ++i) { |
| 8279 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8280 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 8281 | if (lo != ch) { |
| 8282 | if (lo > maxchar) |
| 8283 | maxchar = lo; |
| 8284 | PyUnicode_WRITE(kind, data, i, lo); |
| 8285 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8286 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8287 | else if (ch > maxchar) |
| 8288 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8289 | } |
| 8290 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8291 | if (touched) |
| 8292 | return maxchar; |
| 8293 | else |
| 8294 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8295 | } |
| 8296 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8297 | static Py_UCS4 |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8298 | fixswapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8299 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8300 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8301 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8302 | const int kind = PyUnicode_KIND(self); |
| 8303 | void *data = PyUnicode_DATA(self); |
| 8304 | int touched = 0; |
| 8305 | Py_UCS4 maxchar = 0; |
| 8306 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8307 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8308 | for(i = 0; i < len; ++i) { |
| 8309 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8310 | Py_UCS4 nu = 0; |
| 8311 | |
| 8312 | if (Py_UNICODE_ISUPPER(ch)) |
| 8313 | nu = Py_UNICODE_TOLOWER(ch); |
| 8314 | else if (Py_UNICODE_ISLOWER(ch)) |
| 8315 | nu = Py_UNICODE_TOUPPER(ch); |
| 8316 | |
| 8317 | if (nu != 0) { |
| 8318 | if (nu > maxchar) |
| 8319 | maxchar = nu; |
| 8320 | PyUnicode_WRITE(kind, data, i, nu); |
| 8321 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8322 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8323 | else if (ch > maxchar) |
| 8324 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8325 | } |
| 8326 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8327 | if (touched) |
| 8328 | return maxchar; |
| 8329 | else |
| 8330 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8331 | } |
| 8332 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8333 | static Py_UCS4 |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8334 | fixcapitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8335 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8336 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8337 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8338 | const int kind = PyUnicode_KIND(self); |
| 8339 | void *data = PyUnicode_DATA(self); |
| 8340 | int touched = 0; |
| 8341 | Py_UCS4 maxchar = 0; |
| 8342 | Py_ssize_t i = 0; |
| 8343 | Py_UCS4 ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8344 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 8345 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8346 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8347 | |
| 8348 | ch = PyUnicode_READ(kind, data, i); |
| 8349 | if (!Py_UNICODE_ISUPPER(ch)) { |
| 8350 | maxchar = Py_UNICODE_TOUPPER(ch); |
| 8351 | PyUnicode_WRITE(kind, data, i, maxchar); |
| 8352 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8353 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8354 | ++i; |
| 8355 | for(; i < len; ++i) { |
| 8356 | ch = PyUnicode_READ(kind, data, i); |
| 8357 | if (!Py_UNICODE_ISLOWER(ch)) { |
| 8358 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 8359 | if (lo > maxchar) |
| 8360 | maxchar = lo; |
| 8361 | PyUnicode_WRITE(kind, data, i, lo); |
| 8362 | touched = 1; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 8363 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8364 | else if (ch > maxchar) |
| 8365 | maxchar = ch; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 8366 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8367 | |
| 8368 | if (touched) |
| 8369 | return maxchar; |
| 8370 | else |
| 8371 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8372 | } |
| 8373 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8374 | static Py_UCS4 |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8375 | fixtitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8376 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8377 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8378 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8379 | const int kind = PyUnicode_KIND(self); |
| 8380 | void *data = PyUnicode_DATA(self); |
| 8381 | Py_UCS4 maxchar = 0; |
| 8382 | Py_ssize_t i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8383 | int previous_is_cased; |
| 8384 | |
| 8385 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8386 | if (len == 1) { |
| 8387 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8388 | const Py_UCS4 ti = Py_UNICODE_TOTITLE(ch); |
| 8389 | if (ti != ch) { |
| 8390 | PyUnicode_WRITE(kind, data, i, ti); |
| 8391 | return ti; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8392 | } |
| 8393 | else |
| 8394 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8395 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8396 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8397 | for(; i < len; ++i) { |
| 8398 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8399 | Py_UCS4 nu; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8400 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8401 | if (previous_is_cased) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8402 | nu = Py_UNICODE_TOLOWER(ch); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8403 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8404 | nu = Py_UNICODE_TOTITLE(ch); |
| 8405 | |
| 8406 | if (nu > maxchar) |
| 8407 | maxchar = nu; |
| 8408 | PyUnicode_WRITE(kind, data, i, nu); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8409 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8410 | if (Py_UNICODE_ISLOWER(ch) || |
| 8411 | Py_UNICODE_ISUPPER(ch) || |
| 8412 | Py_UNICODE_ISTITLE(ch)) |
| 8413 | previous_is_cased = 1; |
| 8414 | else |
| 8415 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8416 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8417 | return maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8418 | } |
| 8419 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8420 | PyObject * |
| 8421 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8422 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8423 | PyObject *sep = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8424 | Py_ssize_t seplen = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8425 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8426 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8427 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 8428 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8429 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8430 | Py_ssize_t sz, i, res_offset; |
| 8431 | Py_UCS4 maxchar = 0; |
| 8432 | Py_UCS4 item_maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8433 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8434 | fseq = PySequence_Fast(seq, ""); |
| 8435 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8436 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8437 | } |
| 8438 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8439 | /* NOTE: the following code can't call back into Python code, |
| 8440 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 8441 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8442 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8443 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 8444 | /* If empty sequence, return u"". */ |
| 8445 | if (seqlen == 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8446 | res = PyUnicode_New(0, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8447 | goto Done; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8448 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8449 | items = PySequence_Fast_ITEMS(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8450 | /* If singleton sequence with an exact Unicode, return that. */ |
| 8451 | if (seqlen == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8452 | item = items[0]; |
| 8453 | if (PyUnicode_CheckExact(item)) { |
| 8454 | Py_INCREF(item); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8455 | res = item; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8456 | goto Done; |
| 8457 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8458 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8459 | else { |
| 8460 | /* Set up sep and seplen */ |
| 8461 | if (separator == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8462 | /* fall back to a blank space separator */ |
| 8463 | sep = PyUnicode_FromOrdinal(' '); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 8464 | if (!sep) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8465 | goto onError; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8466 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8467 | else { |
| 8468 | if (!PyUnicode_Check(separator)) { |
| 8469 | PyErr_Format(PyExc_TypeError, |
| 8470 | "separator: expected str instance," |
| 8471 | " %.80s found", |
| 8472 | Py_TYPE(separator)->tp_name); |
| 8473 | goto onError; |
| 8474 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8475 | if (PyUnicode_READY(separator) == -1) |
| 8476 | goto onError; |
| 8477 | sep = separator; |
| 8478 | seplen = PyUnicode_GET_LENGTH(separator); |
| 8479 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 8480 | /* inc refcount to keep this code path symetric with the |
| 8481 | above case of a blank separator */ |
| 8482 | Py_INCREF(sep); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8483 | } |
| 8484 | } |
| 8485 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8486 | /* There are at least two things to join, or else we have a subclass |
| 8487 | * of str in the sequence. |
| 8488 | * Do a pre-pass to figure out the total amount of space we'll |
| 8489 | * need (sz), and see whether all argument are strings. |
| 8490 | */ |
| 8491 | sz = 0; |
| 8492 | for (i = 0; i < seqlen; i++) { |
| 8493 | const Py_ssize_t old_sz = sz; |
| 8494 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8495 | if (!PyUnicode_Check(item)) { |
| 8496 | PyErr_Format(PyExc_TypeError, |
| 8497 | "sequence item %zd: expected str instance," |
| 8498 | " %.80s found", |
| 8499 | i, Py_TYPE(item)->tp_name); |
| 8500 | goto onError; |
| 8501 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8502 | if (PyUnicode_READY(item) == -1) |
| 8503 | goto onError; |
| 8504 | sz += PyUnicode_GET_LENGTH(item); |
| 8505 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
| 8506 | if (item_maxchar > maxchar) |
| 8507 | maxchar = item_maxchar; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8508 | if (i != 0) |
| 8509 | sz += seplen; |
| 8510 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 8511 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8512 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8513 | goto onError; |
| 8514 | } |
| 8515 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8516 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8517 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8518 | if (res == NULL) |
| 8519 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 8520 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8521 | /* Catenate everything. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8522 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8523 | Py_ssize_t itemlen; |
| 8524 | item = items[i]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8525 | itemlen = PyUnicode_GET_LENGTH(item); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8526 | /* Copy item, and maybe the separator. */ |
| 8527 | if (i) { |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8528 | if (PyUnicode_CopyCharacters(res, res_offset, |
| 8529 | sep, 0, seplen) < 0) |
| 8530 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8531 | res_offset += seplen; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8532 | } |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8533 | if (PyUnicode_CopyCharacters(res, res_offset, |
| 8534 | item, 0, itemlen) < 0) |
| 8535 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8536 | res_offset += itemlen; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8537 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8538 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8539 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8540 | Done: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8541 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8542 | Py_XDECREF(sep); |
| 8543 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8544 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8545 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8546 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8547 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8548 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8549 | return NULL; |
| 8550 | } |
| 8551 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8552 | #define FILL(kind, data, value, start, length) \ |
| 8553 | do { \ |
| 8554 | Py_ssize_t i_ = 0; \ |
| 8555 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 8556 | switch ((kind)) { \ |
| 8557 | case PyUnicode_1BYTE_KIND: { \ |
| 8558 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
| 8559 | memset(to_, (unsigned char)value, length); \ |
| 8560 | break; \ |
| 8561 | } \ |
| 8562 | case PyUnicode_2BYTE_KIND: { \ |
| 8563 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 8564 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 8565 | break; \ |
| 8566 | } \ |
| 8567 | default: { \ |
| 8568 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 8569 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 8570 | break; \ |
| 8571 | } \ |
| 8572 | } \ |
| 8573 | } while (0) |
| 8574 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8575 | static PyUnicodeObject * |
| 8576 | pad(PyUnicodeObject *self, |
| 8577 | Py_ssize_t left, |
| 8578 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8579 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8580 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8581 | PyObject *u; |
| 8582 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8583 | int kind; |
| 8584 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8585 | |
| 8586 | if (left < 0) |
| 8587 | left = 0; |
| 8588 | if (right < 0) |
| 8589 | right = 0; |
| 8590 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 8591 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8592 | Py_INCREF(self); |
| 8593 | return self; |
| 8594 | } |
| 8595 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8596 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 8597 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 8598 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 8599 | return NULL; |
| 8600 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8601 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 8602 | if (fill > maxchar) |
| 8603 | maxchar = fill; |
| 8604 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8605 | if (!u) |
| 8606 | return NULL; |
| 8607 | |
| 8608 | kind = PyUnicode_KIND(u); |
| 8609 | data = PyUnicode_DATA(u); |
| 8610 | if (left) |
| 8611 | FILL(kind, data, fill, 0, left); |
| 8612 | if (right) |
| 8613 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 8614 | if (PyUnicode_CopyCharacters(u, left, |
| 8615 | (PyObject*)self, 0, |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8616 | _PyUnicode_LENGTH(self)) < 0) |
| 8617 | { |
| 8618 | Py_DECREF(u); |
| 8619 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8620 | } |
| 8621 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8622 | return (PyUnicodeObject*)u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8623 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8624 | #undef FILL |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8625 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8626 | PyObject * |
| 8627 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8628 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8629 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8630 | |
| 8631 | string = PyUnicode_FromObject(string); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8632 | if (string == NULL || PyUnicode_READY(string) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8633 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8634 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8635 | switch(PyUnicode_KIND(string)) { |
| 8636 | case PyUnicode_1BYTE_KIND: |
| 8637 | list = ucs1lib_splitlines( |
| 8638 | (PyObject*) string, PyUnicode_1BYTE_DATA(string), |
| 8639 | PyUnicode_GET_LENGTH(string), keepends); |
| 8640 | break; |
| 8641 | case PyUnicode_2BYTE_KIND: |
| 8642 | list = ucs2lib_splitlines( |
| 8643 | (PyObject*) string, PyUnicode_2BYTE_DATA(string), |
| 8644 | PyUnicode_GET_LENGTH(string), keepends); |
| 8645 | break; |
| 8646 | case PyUnicode_4BYTE_KIND: |
| 8647 | list = ucs4lib_splitlines( |
| 8648 | (PyObject*) string, PyUnicode_4BYTE_DATA(string), |
| 8649 | PyUnicode_GET_LENGTH(string), keepends); |
| 8650 | break; |
| 8651 | default: |
| 8652 | assert(0); |
| 8653 | list = 0; |
| 8654 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8655 | Py_DECREF(string); |
| 8656 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8657 | } |
| 8658 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8659 | static PyObject * |
| 8660 | split(PyUnicodeObject *self, |
| 8661 | PyUnicodeObject *substring, |
| 8662 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8663 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8664 | int kind1, kind2, kind; |
| 8665 | void *buf1, *buf2; |
| 8666 | Py_ssize_t len1, len2; |
| 8667 | PyObject* out; |
| 8668 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8669 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8670 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8671 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8672 | if (PyUnicode_READY(self) == -1) |
| 8673 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8674 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8675 | if (substring == NULL) |
| 8676 | switch(PyUnicode_KIND(self)) { |
| 8677 | case PyUnicode_1BYTE_KIND: |
| 8678 | return ucs1lib_split_whitespace( |
| 8679 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 8680 | PyUnicode_GET_LENGTH(self), maxcount |
| 8681 | ); |
| 8682 | case PyUnicode_2BYTE_KIND: |
| 8683 | return ucs2lib_split_whitespace( |
| 8684 | (PyObject*) self, PyUnicode_2BYTE_DATA(self), |
| 8685 | PyUnicode_GET_LENGTH(self), maxcount |
| 8686 | ); |
| 8687 | case PyUnicode_4BYTE_KIND: |
| 8688 | return ucs4lib_split_whitespace( |
| 8689 | (PyObject*) self, PyUnicode_4BYTE_DATA(self), |
| 8690 | PyUnicode_GET_LENGTH(self), maxcount |
| 8691 | ); |
| 8692 | default: |
| 8693 | assert(0); |
| 8694 | return NULL; |
| 8695 | } |
| 8696 | |
| 8697 | if (PyUnicode_READY(substring) == -1) |
| 8698 | return NULL; |
| 8699 | |
| 8700 | kind1 = PyUnicode_KIND(self); |
| 8701 | kind2 = PyUnicode_KIND(substring); |
| 8702 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8703 | buf1 = PyUnicode_DATA(self); |
| 8704 | buf2 = PyUnicode_DATA(substring); |
| 8705 | if (kind1 != kind) |
| 8706 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 8707 | if (!buf1) |
| 8708 | return NULL; |
| 8709 | if (kind2 != kind) |
| 8710 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 8711 | if (!buf2) { |
| 8712 | if (kind1 != kind) PyMem_Free(buf1); |
| 8713 | return NULL; |
| 8714 | } |
| 8715 | len1 = PyUnicode_GET_LENGTH(self); |
| 8716 | len2 = PyUnicode_GET_LENGTH(substring); |
| 8717 | |
| 8718 | switch(kind) { |
| 8719 | case PyUnicode_1BYTE_KIND: |
| 8720 | out = ucs1lib_split( |
| 8721 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 8722 | break; |
| 8723 | case PyUnicode_2BYTE_KIND: |
| 8724 | out = ucs2lib_split( |
| 8725 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 8726 | break; |
| 8727 | case PyUnicode_4BYTE_KIND: |
| 8728 | out = ucs4lib_split( |
| 8729 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 8730 | break; |
| 8731 | default: |
| 8732 | out = NULL; |
| 8733 | } |
| 8734 | if (kind1 != kind) |
| 8735 | PyMem_Free(buf1); |
| 8736 | if (kind2 != kind) |
| 8737 | PyMem_Free(buf2); |
| 8738 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8739 | } |
| 8740 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8741 | static PyObject * |
| 8742 | rsplit(PyUnicodeObject *self, |
| 8743 | PyUnicodeObject *substring, |
| 8744 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8745 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8746 | int kind1, kind2, kind; |
| 8747 | void *buf1, *buf2; |
| 8748 | Py_ssize_t len1, len2; |
| 8749 | PyObject* out; |
| 8750 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8751 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8752 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8753 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8754 | if (PyUnicode_READY(self) == -1) |
| 8755 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8756 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8757 | if (substring == NULL) |
| 8758 | switch(PyUnicode_KIND(self)) { |
| 8759 | case PyUnicode_1BYTE_KIND: |
| 8760 | return ucs1lib_rsplit_whitespace( |
| 8761 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 8762 | PyUnicode_GET_LENGTH(self), maxcount |
| 8763 | ); |
| 8764 | case PyUnicode_2BYTE_KIND: |
| 8765 | return ucs2lib_rsplit_whitespace( |
| 8766 | (PyObject*) self, PyUnicode_2BYTE_DATA(self), |
| 8767 | PyUnicode_GET_LENGTH(self), maxcount |
| 8768 | ); |
| 8769 | case PyUnicode_4BYTE_KIND: |
| 8770 | return ucs4lib_rsplit_whitespace( |
| 8771 | (PyObject*) self, PyUnicode_4BYTE_DATA(self), |
| 8772 | PyUnicode_GET_LENGTH(self), maxcount |
| 8773 | ); |
| 8774 | default: |
| 8775 | assert(0); |
| 8776 | return NULL; |
| 8777 | } |
| 8778 | |
| 8779 | if (PyUnicode_READY(substring) == -1) |
| 8780 | return NULL; |
| 8781 | |
| 8782 | kind1 = PyUnicode_KIND(self); |
| 8783 | kind2 = PyUnicode_KIND(substring); |
| 8784 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8785 | buf1 = PyUnicode_DATA(self); |
| 8786 | buf2 = PyUnicode_DATA(substring); |
| 8787 | if (kind1 != kind) |
| 8788 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 8789 | if (!buf1) |
| 8790 | return NULL; |
| 8791 | if (kind2 != kind) |
| 8792 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 8793 | if (!buf2) { |
| 8794 | if (kind1 != kind) PyMem_Free(buf1); |
| 8795 | return NULL; |
| 8796 | } |
| 8797 | len1 = PyUnicode_GET_LENGTH(self); |
| 8798 | len2 = PyUnicode_GET_LENGTH(substring); |
| 8799 | |
| 8800 | switch(kind) { |
| 8801 | case PyUnicode_1BYTE_KIND: |
| 8802 | out = ucs1lib_rsplit( |
| 8803 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 8804 | break; |
| 8805 | case PyUnicode_2BYTE_KIND: |
| 8806 | out = ucs2lib_rsplit( |
| 8807 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 8808 | break; |
| 8809 | case PyUnicode_4BYTE_KIND: |
| 8810 | out = ucs4lib_rsplit( |
| 8811 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 8812 | break; |
| 8813 | default: |
| 8814 | out = NULL; |
| 8815 | } |
| 8816 | if (kind1 != kind) |
| 8817 | PyMem_Free(buf1); |
| 8818 | if (kind2 != kind) |
| 8819 | PyMem_Free(buf2); |
| 8820 | return out; |
| 8821 | } |
| 8822 | |
| 8823 | static Py_ssize_t |
| 8824 | anylib_find(int kind, void *buf1, Py_ssize_t len1, |
| 8825 | void *buf2, Py_ssize_t len2, Py_ssize_t offset) |
| 8826 | { |
| 8827 | switch(kind) { |
| 8828 | case PyUnicode_1BYTE_KIND: |
| 8829 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
| 8830 | case PyUnicode_2BYTE_KIND: |
| 8831 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 8832 | case PyUnicode_4BYTE_KIND: |
| 8833 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 8834 | } |
| 8835 | assert(0); |
| 8836 | return -1; |
| 8837 | } |
| 8838 | |
| 8839 | static Py_ssize_t |
| 8840 | anylib_count(int kind, void* sbuf, Py_ssize_t slen, |
| 8841 | void *buf1, Py_ssize_t len1, Py_ssize_t maxcount) |
| 8842 | { |
| 8843 | switch(kind) { |
| 8844 | case PyUnicode_1BYTE_KIND: |
| 8845 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
| 8846 | case PyUnicode_2BYTE_KIND: |
| 8847 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 8848 | case PyUnicode_4BYTE_KIND: |
| 8849 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 8850 | } |
| 8851 | assert(0); |
| 8852 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8853 | } |
| 8854 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8855 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8856 | replace(PyObject *self, PyObject *str1, |
| 8857 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8858 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8859 | PyObject *u; |
| 8860 | char *sbuf = PyUnicode_DATA(self); |
| 8861 | char *buf1 = PyUnicode_DATA(str1); |
| 8862 | char *buf2 = PyUnicode_DATA(str2); |
| 8863 | int srelease = 0, release1 = 0, release2 = 0; |
| 8864 | int skind = PyUnicode_KIND(self); |
| 8865 | int kind1 = PyUnicode_KIND(str1); |
| 8866 | int kind2 = PyUnicode_KIND(str2); |
| 8867 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 8868 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 8869 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8870 | |
| 8871 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8872 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8873 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8874 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8875 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8876 | if (skind < kind1) |
| 8877 | /* substring too wide to be present */ |
| 8878 | goto nothing; |
| 8879 | |
| 8880 | if (len1 == len2) { |
Antoine Pitrou | cbfdee3 | 2010-01-13 08:58:08 +0000 | [diff] [blame] | 8881 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8882 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8883 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8884 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8885 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8886 | /* replace characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8887 | Py_UCS4 u1, u2, maxchar; |
| 8888 | int mayshrink, rkind; |
| 8889 | u1 = PyUnicode_READ_CHAR(str1, 0); |
| 8890 | if (!findchar(sbuf, PyUnicode_KIND(self), |
| 8891 | slen, u1, 1)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8892 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8893 | u2 = PyUnicode_READ_CHAR(str2, 0); |
| 8894 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 8895 | /* Replacing u1 with u2 may cause a maxchar reduction in the |
| 8896 | result string. */ |
| 8897 | mayshrink = maxchar > 127; |
| 8898 | if (u2 > maxchar) { |
| 8899 | maxchar = u2; |
| 8900 | mayshrink = 0; |
| 8901 | } |
| 8902 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8903 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8904 | goto error; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8905 | if (PyUnicode_CopyCharacters(u, 0, |
| 8906 | (PyObject*)self, 0, slen) < 0) |
| 8907 | { |
| 8908 | Py_DECREF(u); |
| 8909 | return NULL; |
| 8910 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8911 | rkind = PyUnicode_KIND(u); |
| 8912 | for (i = 0; i < PyUnicode_GET_LENGTH(u); i++) |
| 8913 | if (PyUnicode_READ(rkind, PyUnicode_DATA(u), i) == u1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8914 | if (--maxcount < 0) |
| 8915 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8916 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), i, u2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8917 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8918 | if (mayshrink) { |
| 8919 | PyObject *tmp = u; |
| 8920 | u = PyUnicode_FromKindAndData(rkind, PyUnicode_DATA(tmp), |
| 8921 | PyUnicode_GET_LENGTH(tmp)); |
| 8922 | Py_DECREF(tmp); |
| 8923 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8924 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8925 | int rkind = skind; |
| 8926 | char *res; |
| 8927 | if (kind1 < rkind) { |
| 8928 | /* widen substring */ |
| 8929 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 8930 | if (!buf1) goto error; |
| 8931 | release1 = 1; |
| 8932 | } |
| 8933 | i = anylib_find(rkind, sbuf, slen, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8934 | if (i < 0) |
| 8935 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8936 | if (rkind > kind2) { |
| 8937 | /* widen replacement */ |
| 8938 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 8939 | if (!buf2) goto error; |
| 8940 | release2 = 1; |
| 8941 | } |
| 8942 | else if (rkind < kind2) { |
| 8943 | /* widen self and buf1 */ |
| 8944 | rkind = kind2; |
| 8945 | if (release1) PyMem_Free(buf1); |
| 8946 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 8947 | if (!sbuf) goto error; |
| 8948 | srelease = 1; |
| 8949 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 8950 | if (!buf1) goto error; |
| 8951 | release1 = 1; |
| 8952 | } |
| 8953 | res = PyMem_Malloc(PyUnicode_KIND_SIZE(rkind, slen)); |
| 8954 | if (!res) { |
| 8955 | PyErr_NoMemory(); |
| 8956 | goto error; |
| 8957 | } |
| 8958 | memcpy(res, sbuf, PyUnicode_KIND_SIZE(rkind, slen)); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8959 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8960 | memcpy(res + PyUnicode_KIND_SIZE(rkind, i), |
| 8961 | buf2, |
| 8962 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 8963 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8964 | |
| 8965 | while ( --maxcount > 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8966 | i = anylib_find(rkind, sbuf+PyUnicode_KIND_SIZE(rkind, i), |
| 8967 | slen-i, |
| 8968 | buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8969 | if (i == -1) |
| 8970 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8971 | memcpy(res + PyUnicode_KIND_SIZE(rkind, i), |
| 8972 | buf2, |
| 8973 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 8974 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8975 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8976 | |
| 8977 | u = PyUnicode_FromKindAndData(rkind, res, slen); |
| 8978 | PyMem_Free(res); |
| 8979 | if (!u) goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8980 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8981 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8982 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8983 | Py_ssize_t n, i, j, ires; |
| 8984 | Py_ssize_t product, new_size; |
| 8985 | int rkind = skind; |
| 8986 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8987 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8988 | if (kind1 < rkind) { |
| 8989 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 8990 | if (!buf1) goto error; |
| 8991 | release1 = 1; |
| 8992 | } |
| 8993 | n = anylib_count(rkind, sbuf, slen, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8994 | if (n == 0) |
| 8995 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8996 | if (kind2 < rkind) { |
| 8997 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 8998 | if (!buf2) goto error; |
| 8999 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9000 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9001 | else if (kind2 > rkind) { |
| 9002 | rkind = kind2; |
| 9003 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 9004 | if (!sbuf) goto error; |
| 9005 | srelease = 1; |
| 9006 | if (release1) PyMem_Free(buf1); |
| 9007 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9008 | if (!buf1) goto error; |
| 9009 | release1 = 1; |
| 9010 | } |
| 9011 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 9012 | PyUnicode_GET_LENGTH(str1))); */ |
| 9013 | product = n * (len2-len1); |
| 9014 | if ((product / (len2-len1)) != n) { |
| 9015 | PyErr_SetString(PyExc_OverflowError, |
| 9016 | "replace string is too long"); |
| 9017 | goto error; |
| 9018 | } |
| 9019 | new_size = slen + product; |
| 9020 | if (new_size < 0 || new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
| 9021 | PyErr_SetString(PyExc_OverflowError, |
| 9022 | "replace string is too long"); |
| 9023 | goto error; |
| 9024 | } |
| 9025 | res = PyMem_Malloc(PyUnicode_KIND_SIZE(rkind, new_size)); |
| 9026 | if (!res) |
| 9027 | goto error; |
| 9028 | ires = i = 0; |
| 9029 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9030 | while (n-- > 0) { |
| 9031 | /* look for next match */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9032 | j = anylib_find(rkind, |
| 9033 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9034 | slen-i, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9035 | if (j == -1) |
| 9036 | break; |
| 9037 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9038 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9039 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9040 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9041 | PyUnicode_KIND_SIZE(rkind, j-i)); |
| 9042 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9043 | } |
| 9044 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9045 | if (len2 > 0) { |
| 9046 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9047 | buf2, |
| 9048 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 9049 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9050 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9051 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9052 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9053 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9054 | /* copy tail [i:] */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9055 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9056 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9057 | PyUnicode_KIND_SIZE(rkind, slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9058 | } else { |
| 9059 | /* interleave */ |
| 9060 | while (n > 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9061 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9062 | buf2, |
| 9063 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 9064 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9065 | if (--n <= 0) |
| 9066 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9067 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9068 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9069 | PyUnicode_KIND_SIZE(rkind, 1)); |
| 9070 | ires++; |
| 9071 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9072 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9073 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9074 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9075 | PyUnicode_KIND_SIZE(rkind, slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9076 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9077 | u = PyUnicode_FromKindAndData(rkind, res, new_size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9078 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9079 | if (srelease) |
| 9080 | PyMem_FREE(sbuf); |
| 9081 | if (release1) |
| 9082 | PyMem_FREE(buf1); |
| 9083 | if (release2) |
| 9084 | PyMem_FREE(buf2); |
| 9085 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9086 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9087 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9088 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9089 | if (srelease) |
| 9090 | PyMem_FREE(sbuf); |
| 9091 | if (release1) |
| 9092 | PyMem_FREE(buf1); |
| 9093 | if (release2) |
| 9094 | PyMem_FREE(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9095 | if (PyUnicode_CheckExact(self)) { |
| 9096 | Py_INCREF(self); |
| 9097 | return (PyObject *) self; |
| 9098 | } |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 9099 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9100 | error: |
| 9101 | if (srelease && sbuf) |
| 9102 | PyMem_FREE(sbuf); |
| 9103 | if (release1 && buf1) |
| 9104 | PyMem_FREE(buf1); |
| 9105 | if (release2 && buf2) |
| 9106 | PyMem_FREE(buf2); |
| 9107 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9108 | } |
| 9109 | |
| 9110 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 9111 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9112 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9113 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9114 | \n\ |
| 9115 | 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] | 9116 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9117 | |
| 9118 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9119 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9120 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9121 | return fixup(self, fixtitle); |
| 9122 | } |
| 9123 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9124 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9125 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9126 | \n\ |
| 9127 | 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] | 9128 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9129 | |
| 9130 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9131 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9132 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9133 | return fixup(self, fixcapitalize); |
| 9134 | } |
| 9135 | |
| 9136 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9137 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9138 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9139 | \n\ |
| 9140 | 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] | 9141 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9142 | |
| 9143 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9144 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9145 | { |
| 9146 | PyObject *list; |
| 9147 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9148 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9149 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9150 | /* Split into words */ |
| 9151 | list = split(self, NULL, -1); |
| 9152 | if (!list) |
| 9153 | return NULL; |
| 9154 | |
| 9155 | /* Capitalize each word */ |
| 9156 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 9157 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9158 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9159 | if (item == NULL) |
| 9160 | goto onError; |
| 9161 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 9162 | PyList_SET_ITEM(list, i, item); |
| 9163 | } |
| 9164 | |
| 9165 | /* Join the words to form a new string */ |
| 9166 | item = PyUnicode_Join(NULL, list); |
| 9167 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9168 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9169 | Py_DECREF(list); |
| 9170 | return (PyObject *)item; |
| 9171 | } |
| 9172 | #endif |
| 9173 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9174 | /* Argument converter. Coerces to a single unicode character */ |
| 9175 | |
| 9176 | static int |
| 9177 | convert_uc(PyObject *obj, void *addr) |
| 9178 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9179 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9180 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9181 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9182 | uniobj = PyUnicode_FromObject(obj); |
| 9183 | if (uniobj == NULL) { |
| 9184 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9185 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9186 | return 0; |
| 9187 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9188 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9189 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9190 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9191 | Py_DECREF(uniobj); |
| 9192 | return 0; |
| 9193 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9194 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9195 | Py_DECREF(uniobj); |
| 9196 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9197 | } |
| 9198 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9199 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9200 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9201 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 9202 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9203 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9204 | |
| 9205 | static PyObject * |
| 9206 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 9207 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9208 | Py_ssize_t marg, left; |
| 9209 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9210 | Py_UCS4 fillchar = ' '; |
| 9211 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 9212 | 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] | 9213 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9214 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 9215 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9216 | return NULL; |
| 9217 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9218 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9219 | Py_INCREF(self); |
| 9220 | return (PyObject*) self; |
| 9221 | } |
| 9222 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9223 | marg = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9224 | left = marg / 2 + (marg & width & 1); |
| 9225 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9226 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9227 | } |
| 9228 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9229 | #if 0 |
| 9230 | |
| 9231 | /* This code should go into some future Unicode collation support |
| 9232 | module. The basic comparison should compare ordinals on a naive |
Georg Brandl | c6c3178 | 2009-06-08 13:41:29 +0000 | [diff] [blame] | 9233 | basis (this is what Java does and thus Jython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9234 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9235 | /* speedy UTF-16 code point order comparison */ |
| 9236 | /* gleaned from: */ |
| 9237 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 9238 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 9239 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9240 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9241 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9242 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 9243 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 9244 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9245 | }; |
| 9246 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9247 | static int |
| 9248 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 9249 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9250 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9251 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9252 | Py_UNICODE *s1 = str1->str; |
| 9253 | Py_UNICODE *s2 = str2->str; |
| 9254 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9255 | len1 = str1->_base._base.length; |
| 9256 | len2 = str2->_base._base.length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9257 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9258 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9259 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9260 | |
| 9261 | c1 = *s1++; |
| 9262 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 9263 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9264 | if (c1 > (1<<11) * 26) |
| 9265 | c1 += utf16Fixup[c1>>11]; |
| 9266 | if (c2 > (1<<11) * 26) |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9267 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9268 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 9269 | |
| 9270 | if (c1 != c2) |
| 9271 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9272 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9273 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9274 | } |
| 9275 | |
| 9276 | return (len1 < len2) ? -1 : (len1 != len2); |
| 9277 | } |
| 9278 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9279 | #else |
| 9280 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9281 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 9282 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9283 | static int |
| 9284 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 9285 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9286 | int kind1, kind2; |
| 9287 | void *data1, *data2; |
| 9288 | Py_ssize_t len1, len2, i; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9289 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9290 | kind1 = PyUnicode_KIND(str1); |
| 9291 | kind2 = PyUnicode_KIND(str2); |
| 9292 | data1 = PyUnicode_DATA(str1); |
| 9293 | data2 = PyUnicode_DATA(str2); |
| 9294 | len1 = PyUnicode_GET_LENGTH(str1); |
| 9295 | len2 = PyUnicode_GET_LENGTH(str2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9296 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9297 | for (i = 0; i < len1 && i < len2; ++i) { |
| 9298 | Py_UCS4 c1, c2; |
| 9299 | c1 = PyUnicode_READ(kind1, data1, i); |
| 9300 | c2 = PyUnicode_READ(kind2, data2, i); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 9301 | |
| 9302 | if (c1 != c2) |
| 9303 | return (c1 < c2) ? -1 : 1; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9304 | } |
| 9305 | |
| 9306 | return (len1 < len2) ? -1 : (len1 != len2); |
| 9307 | } |
| 9308 | |
| 9309 | #endif |
| 9310 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9311 | int |
| 9312 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9313 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9314 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 9315 | if (PyUnicode_READY(left) == -1 || |
| 9316 | PyUnicode_READY(right) == -1) |
| 9317 | return -1; |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 9318 | return unicode_compare((PyUnicodeObject *)left, |
| 9319 | (PyUnicodeObject *)right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9320 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 9321 | PyErr_Format(PyExc_TypeError, |
| 9322 | "Can't compare %.100s and %.100s", |
| 9323 | left->ob_type->tp_name, |
| 9324 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9325 | return -1; |
| 9326 | } |
| 9327 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9328 | int |
| 9329 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 9330 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9331 | Py_ssize_t i; |
| 9332 | int kind; |
| 9333 | void *data; |
| 9334 | Py_UCS4 chr; |
| 9335 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9336 | assert(PyUnicode_Check(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9337 | if (PyUnicode_READY(uni) == -1) |
| 9338 | return -1; |
| 9339 | kind = PyUnicode_KIND(uni); |
| 9340 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9341 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9342 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 9343 | if (chr != str[i]) |
| 9344 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 9345 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 9346 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9347 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9348 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9349 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9350 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9351 | return 0; |
| 9352 | } |
| 9353 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9354 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9355 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9356 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9357 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9358 | PyObject * |
| 9359 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9360 | { |
| 9361 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9362 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9363 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 9364 | PyObject *v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9365 | if (PyUnicode_READY(left) == -1 || |
| 9366 | PyUnicode_READY(right) == -1) |
| 9367 | return NULL; |
| 9368 | if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) || |
| 9369 | PyUnicode_KIND(left) != PyUnicode_KIND(right)) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9370 | if (op == Py_EQ) { |
| 9371 | Py_INCREF(Py_False); |
| 9372 | return Py_False; |
| 9373 | } |
| 9374 | if (op == Py_NE) { |
| 9375 | Py_INCREF(Py_True); |
| 9376 | return Py_True; |
| 9377 | } |
| 9378 | } |
| 9379 | if (left == right) |
| 9380 | result = 0; |
| 9381 | else |
| 9382 | result = unicode_compare((PyUnicodeObject *)left, |
| 9383 | (PyUnicodeObject *)right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9384 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9385 | /* Convert the return value to a Boolean */ |
| 9386 | switch (op) { |
| 9387 | case Py_EQ: |
| 9388 | v = TEST_COND(result == 0); |
| 9389 | break; |
| 9390 | case Py_NE: |
| 9391 | v = TEST_COND(result != 0); |
| 9392 | break; |
| 9393 | case Py_LE: |
| 9394 | v = TEST_COND(result <= 0); |
| 9395 | break; |
| 9396 | case Py_GE: |
| 9397 | v = TEST_COND(result >= 0); |
| 9398 | break; |
| 9399 | case Py_LT: |
| 9400 | v = TEST_COND(result == -1); |
| 9401 | break; |
| 9402 | case Py_GT: |
| 9403 | v = TEST_COND(result == 1); |
| 9404 | break; |
| 9405 | default: |
| 9406 | PyErr_BadArgument(); |
| 9407 | return NULL; |
| 9408 | } |
| 9409 | Py_INCREF(v); |
| 9410 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9411 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9412 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 9413 | Py_RETURN_NOTIMPLEMENTED; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9414 | } |
| 9415 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9416 | int |
| 9417 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9418 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9419 | PyObject *str, *sub; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9420 | int kind1, kind2, kind; |
| 9421 | void *buf1, *buf2; |
| 9422 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9423 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9424 | |
| 9425 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9426 | sub = PyUnicode_FromObject(element); |
| 9427 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9428 | PyErr_Format(PyExc_TypeError, |
| 9429 | "'in <string>' requires string as left operand, not %s", |
| 9430 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9431 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9432 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9433 | if (PyUnicode_READY(sub) == -1) |
| 9434 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9435 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9436 | str = PyUnicode_FromObject(container); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 9437 | if (!str || PyUnicode_READY(str) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9438 | Py_DECREF(sub); |
| 9439 | return -1; |
| 9440 | } |
| 9441 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9442 | kind1 = PyUnicode_KIND(str); |
| 9443 | kind2 = PyUnicode_KIND(sub); |
| 9444 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9445 | buf1 = PyUnicode_DATA(str); |
| 9446 | buf2 = PyUnicode_DATA(sub); |
| 9447 | if (kind1 != kind) |
| 9448 | buf1 = _PyUnicode_AsKind((PyObject*)str, kind); |
| 9449 | if (!buf1) { |
| 9450 | Py_DECREF(sub); |
| 9451 | return -1; |
| 9452 | } |
| 9453 | if (kind2 != kind) |
| 9454 | buf2 = _PyUnicode_AsKind((PyObject*)sub, kind); |
| 9455 | if (!buf2) { |
| 9456 | Py_DECREF(sub); |
| 9457 | if (kind1 != kind) PyMem_Free(buf1); |
| 9458 | return -1; |
| 9459 | } |
| 9460 | len1 = PyUnicode_GET_LENGTH(str); |
| 9461 | len2 = PyUnicode_GET_LENGTH(sub); |
| 9462 | |
| 9463 | switch(kind) { |
| 9464 | case PyUnicode_1BYTE_KIND: |
| 9465 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 9466 | break; |
| 9467 | case PyUnicode_2BYTE_KIND: |
| 9468 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 9469 | break; |
| 9470 | case PyUnicode_4BYTE_KIND: |
| 9471 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 9472 | break; |
| 9473 | default: |
| 9474 | result = -1; |
| 9475 | assert(0); |
| 9476 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9477 | |
| 9478 | Py_DECREF(str); |
| 9479 | Py_DECREF(sub); |
| 9480 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9481 | if (kind1 != kind) |
| 9482 | PyMem_Free(buf1); |
| 9483 | if (kind2 != kind) |
| 9484 | PyMem_Free(buf2); |
| 9485 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9486 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9487 | } |
| 9488 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9489 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 9490 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9491 | PyObject * |
| 9492 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9493 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9494 | PyObject *u = NULL, *v = NULL, *w; |
| 9495 | Py_UCS4 maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9496 | |
| 9497 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9498 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9499 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9500 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9501 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9502 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9503 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9504 | |
| 9505 | /* Shortcuts */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9506 | if (v == (PyObject*)unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9507 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9508 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9509 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9510 | if (u == (PyObject*)unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9511 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9512 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9513 | } |
| 9514 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9515 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | ff9e50f | 2011-09-28 22:17:19 +0200 | [diff] [blame] | 9516 | maxchar = Py_MAX(maxchar, PyUnicode_MAX_CHAR_VALUE(v)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9517 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9518 | /* Concat the two Unicode strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9519 | w = PyUnicode_New( |
| 9520 | PyUnicode_GET_LENGTH(u) + PyUnicode_GET_LENGTH(v), |
| 9521 | maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9522 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9523 | goto onError; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9524 | if (PyUnicode_CopyCharacters(w, 0, u, 0, PyUnicode_GET_LENGTH(u)) < 0) |
| 9525 | goto onError; |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 9526 | if (PyUnicode_CopyCharacters(w, PyUnicode_GET_LENGTH(u), |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9527 | v, 0, |
| 9528 | PyUnicode_GET_LENGTH(v)) < 0) |
| 9529 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9530 | Py_DECREF(u); |
| 9531 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9532 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9533 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9534 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9535 | Py_XDECREF(u); |
| 9536 | Py_XDECREF(v); |
| 9537 | return NULL; |
| 9538 | } |
| 9539 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 9540 | void |
| 9541 | PyUnicode_Append(PyObject **pleft, PyObject *right) |
| 9542 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9543 | PyObject *new; |
| 9544 | if (*pleft == NULL) |
| 9545 | return; |
| 9546 | if (right == NULL || !PyUnicode_Check(*pleft)) { |
| 9547 | Py_DECREF(*pleft); |
| 9548 | *pleft = NULL; |
| 9549 | return; |
| 9550 | } |
| 9551 | new = PyUnicode_Concat(*pleft, right); |
| 9552 | Py_DECREF(*pleft); |
| 9553 | *pleft = new; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 9554 | } |
| 9555 | |
| 9556 | void |
| 9557 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 9558 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9559 | PyUnicode_Append(pleft, right); |
| 9560 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 9561 | } |
| 9562 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9563 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9564 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9565 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9566 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 9567 | 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] | 9568 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9569 | |
| 9570 | static PyObject * |
| 9571 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 9572 | { |
| 9573 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9574 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9575 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9576 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9577 | int kind1, kind2, kind; |
| 9578 | void *buf1, *buf2; |
| 9579 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9580 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 9581 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 9582 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9583 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9584 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9585 | kind1 = PyUnicode_KIND(self); |
| 9586 | kind2 = PyUnicode_KIND(substring); |
| 9587 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9588 | buf1 = PyUnicode_DATA(self); |
| 9589 | buf2 = PyUnicode_DATA(substring); |
| 9590 | if (kind1 != kind) |
| 9591 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 9592 | if (!buf1) { |
| 9593 | Py_DECREF(substring); |
| 9594 | return NULL; |
| 9595 | } |
| 9596 | if (kind2 != kind) |
| 9597 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 9598 | if (!buf2) { |
| 9599 | Py_DECREF(substring); |
| 9600 | if (kind1 != kind) PyMem_Free(buf1); |
| 9601 | return NULL; |
| 9602 | } |
| 9603 | len1 = PyUnicode_GET_LENGTH(self); |
| 9604 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9605 | |
| 9606 | ADJUST_INDICES(start, end, len1); |
| 9607 | switch(kind) { |
| 9608 | case PyUnicode_1BYTE_KIND: |
| 9609 | iresult = ucs1lib_count( |
| 9610 | ((Py_UCS1*)buf1) + start, end - start, |
| 9611 | buf2, len2, PY_SSIZE_T_MAX |
| 9612 | ); |
| 9613 | break; |
| 9614 | case PyUnicode_2BYTE_KIND: |
| 9615 | iresult = ucs2lib_count( |
| 9616 | ((Py_UCS2*)buf1) + start, end - start, |
| 9617 | buf2, len2, PY_SSIZE_T_MAX |
| 9618 | ); |
| 9619 | break; |
| 9620 | case PyUnicode_4BYTE_KIND: |
| 9621 | iresult = ucs4lib_count( |
| 9622 | ((Py_UCS4*)buf1) + start, end - start, |
| 9623 | buf2, len2, PY_SSIZE_T_MAX |
| 9624 | ); |
| 9625 | break; |
| 9626 | default: |
| 9627 | assert(0); iresult = 0; |
| 9628 | } |
| 9629 | |
| 9630 | result = PyLong_FromSsize_t(iresult); |
| 9631 | |
| 9632 | if (kind1 != kind) |
| 9633 | PyMem_Free(buf1); |
| 9634 | if (kind2 != kind) |
| 9635 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9636 | |
| 9637 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9638 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9639 | return result; |
| 9640 | } |
| 9641 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9642 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 9643 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9644 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 9645 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 9646 | 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] | 9647 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 9648 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 9649 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 9650 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9651 | |
| 9652 | static PyObject * |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 9653 | unicode_encode(PyUnicodeObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9654 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 9655 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9656 | char *encoding = NULL; |
| 9657 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 9658 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 9659 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 9660 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9661 | return NULL; |
Georg Brandl | 3b9406b | 2010-12-03 07:54:09 +0000 | [diff] [blame] | 9662 | return PyUnicode_AsEncodedString((PyObject *)self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 9663 | } |
| 9664 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9665 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9666 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9667 | \n\ |
| 9668 | 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] | 9669 | 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] | 9670 | |
| 9671 | static PyObject* |
| 9672 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 9673 | { |
| 9674 | Py_UNICODE *e; |
| 9675 | Py_UNICODE *p; |
| 9676 | Py_UNICODE *q; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9677 | Py_UNICODE *qe; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9678 | Py_ssize_t i, j, incr, wstr_length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9679 | PyUnicodeObject *u; |
| 9680 | int tabsize = 8; |
| 9681 | |
| 9682 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9683 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9684 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9685 | if (PyUnicode_AsUnicodeAndSize((PyObject *)self, &wstr_length) == NULL) |
| 9686 | return NULL; |
| 9687 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 9688 | /* First pass: determine size of output string */ |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9689 | i = 0; /* chars up to and including most recent \n or \r */ |
| 9690 | 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] | 9691 | e = _PyUnicode_WSTR(self) + wstr_length; /* end of input */ |
| 9692 | for (p = _PyUnicode_WSTR(self); p < e; p++) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9693 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9694 | if (tabsize > 0) { |
| 9695 | incr = tabsize - (j % tabsize); /* cannot overflow */ |
| 9696 | if (j > PY_SSIZE_T_MAX - incr) |
| 9697 | goto overflow1; |
| 9698 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9699 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9700 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9701 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9702 | if (j > PY_SSIZE_T_MAX - 1) |
| 9703 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9704 | j++; |
| 9705 | if (*p == '\n' || *p == '\r') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9706 | if (i > PY_SSIZE_T_MAX - j) |
| 9707 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9708 | i += j; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9709 | j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9710 | } |
| 9711 | } |
| 9712 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9713 | if (i > PY_SSIZE_T_MAX - j) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9714 | goto overflow1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 9715 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9716 | /* Second pass: create output string and fill it */ |
| 9717 | u = _PyUnicode_New(i + j); |
| 9718 | if (!u) |
| 9719 | return NULL; |
| 9720 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9721 | j = 0; /* same as in first pass */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9722 | q = _PyUnicode_WSTR(u); /* next output char */ |
| 9723 | qe = _PyUnicode_WSTR(u) + PyUnicode_GET_SIZE(u); /* end of output */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9724 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9725 | for (p = _PyUnicode_WSTR(self); p < e; p++) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9726 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9727 | if (tabsize > 0) { |
| 9728 | i = tabsize - (j % tabsize); |
| 9729 | j += i; |
| 9730 | while (i--) { |
| 9731 | if (q >= qe) |
| 9732 | goto overflow2; |
| 9733 | *q++ = ' '; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9734 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9735 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9736 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9737 | else { |
| 9738 | if (q >= qe) |
| 9739 | goto overflow2; |
| 9740 | *q++ = *p; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9741 | j++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9742 | if (*p == '\n' || *p == '\r') |
| 9743 | j = 0; |
| 9744 | } |
| 9745 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9746 | if (PyUnicode_READY(u) == -1) { |
| 9747 | Py_DECREF(u); |
| 9748 | return NULL; |
| 9749 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9750 | return (PyObject*) u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9751 | |
| 9752 | overflow2: |
| 9753 | Py_DECREF(u); |
| 9754 | overflow1: |
| 9755 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 9756 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9757 | } |
| 9758 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9759 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9760 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9761 | \n\ |
| 9762 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 9763 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9764 | arguments start and end are interpreted as in slice notation.\n\ |
| 9765 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9766 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9767 | |
| 9768 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9769 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9770 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 9771 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 9772 | Py_ssize_t start; |
| 9773 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9774 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9775 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 9776 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 9777 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9778 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9779 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9780 | if (PyUnicode_READY(self) == -1) |
| 9781 | return NULL; |
| 9782 | if (PyUnicode_READY(substring) == -1) |
| 9783 | return NULL; |
| 9784 | |
| 9785 | result = any_find_slice( |
| 9786 | ucs1lib_find_slice, ucs2lib_find_slice, ucs4lib_find_slice, |
| 9787 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9788 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9789 | |
| 9790 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9791 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9792 | if (result == -2) |
| 9793 | return NULL; |
| 9794 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9795 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9796 | } |
| 9797 | |
| 9798 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9799 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9800 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9801 | Py_UCS4 ch; |
| 9802 | |
| 9803 | if (PyUnicode_READY(self) == -1) |
| 9804 | return NULL; |
| 9805 | if (index < 0 || index >= _PyUnicode_LENGTH(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9806 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9807 | return NULL; |
| 9808 | } |
| 9809 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9810 | ch = PyUnicode_READ(PyUnicode_KIND(self), PyUnicode_DATA(self), index); |
| 9811 | return PyUnicode_FromOrdinal(ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9812 | } |
| 9813 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 9814 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 9815 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 9816 | static Py_hash_t |
Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 9817 | unicode_hash(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9818 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 9819 | Py_ssize_t len; |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 9820 | Py_uhash_t x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 9821 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9822 | if (_PyUnicode_HASH(self) != -1) |
| 9823 | return _PyUnicode_HASH(self); |
| 9824 | if (PyUnicode_READY(self) == -1) |
| 9825 | return -1; |
| 9826 | len = PyUnicode_GET_LENGTH(self); |
| 9827 | |
| 9828 | /* The hash function as a macro, gets expanded three times below. */ |
| 9829 | #define HASH(P) \ |
| 9830 | x = (Py_uhash_t)*P << 7; \ |
| 9831 | while (--len >= 0) \ |
| 9832 | x = (1000003*x) ^ (Py_uhash_t)*P++; |
| 9833 | |
| 9834 | switch (PyUnicode_KIND(self)) { |
| 9835 | case PyUnicode_1BYTE_KIND: { |
| 9836 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 9837 | HASH(c); |
| 9838 | break; |
| 9839 | } |
| 9840 | case PyUnicode_2BYTE_KIND: { |
| 9841 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 9842 | HASH(s); |
| 9843 | break; |
| 9844 | } |
| 9845 | default: { |
| 9846 | Py_UCS4 *l; |
| 9847 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 9848 | "Impossible switch case in unicode_hash"); |
| 9849 | l = PyUnicode_4BYTE_DATA(self); |
| 9850 | HASH(l); |
| 9851 | break; |
| 9852 | } |
| 9853 | } |
| 9854 | x ^= (Py_uhash_t)PyUnicode_GET_LENGTH(self); |
| 9855 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 9856 | if (x == -1) |
| 9857 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9858 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 9859 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9860 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9861 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9862 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9863 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9864 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9865 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9866 | 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] | 9867 | |
| 9868 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9869 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9870 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9871 | Py_ssize_t result; |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 9872 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 9873 | Py_ssize_t start; |
| 9874 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9875 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 9876 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 9877 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9878 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9879 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9880 | if (PyUnicode_READY(self) == -1) |
| 9881 | return NULL; |
| 9882 | if (PyUnicode_READY(substring) == -1) |
| 9883 | return NULL; |
| 9884 | |
| 9885 | result = any_find_slice( |
| 9886 | ucs1lib_find_slice, ucs2lib_find_slice, ucs4lib_find_slice, |
| 9887 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9888 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9889 | |
| 9890 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9891 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9892 | if (result == -2) |
| 9893 | return NULL; |
| 9894 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9895 | if (result < 0) { |
| 9896 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 9897 | return NULL; |
| 9898 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9899 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9900 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9901 | } |
| 9902 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9903 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9904 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9905 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 9906 | 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] | 9907 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9908 | |
| 9909 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9910 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9911 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9912 | Py_ssize_t i, length; |
| 9913 | int kind; |
| 9914 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9915 | int cased; |
| 9916 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9917 | if (PyUnicode_READY(self) == -1) |
| 9918 | return NULL; |
| 9919 | length = PyUnicode_GET_LENGTH(self); |
| 9920 | kind = PyUnicode_KIND(self); |
| 9921 | data = PyUnicode_DATA(self); |
| 9922 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9923 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9924 | if (length == 1) |
| 9925 | return PyBool_FromLong( |
| 9926 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9927 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 9928 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9929 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9930 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 9931 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9932 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9933 | for (i = 0; i < length; i++) { |
| 9934 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9935 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9936 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 9937 | return PyBool_FromLong(0); |
| 9938 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 9939 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9940 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 9941 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9942 | } |
| 9943 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9944 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9945 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9946 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 9947 | 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] | 9948 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9949 | |
| 9950 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9951 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9952 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9953 | Py_ssize_t i, length; |
| 9954 | int kind; |
| 9955 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9956 | int cased; |
| 9957 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9958 | if (PyUnicode_READY(self) == -1) |
| 9959 | return NULL; |
| 9960 | length = PyUnicode_GET_LENGTH(self); |
| 9961 | kind = PyUnicode_KIND(self); |
| 9962 | data = PyUnicode_DATA(self); |
| 9963 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9964 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9965 | if (length == 1) |
| 9966 | return PyBool_FromLong( |
| 9967 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9968 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 9969 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9970 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9971 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 9972 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9973 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9974 | for (i = 0; i < length; i++) { |
| 9975 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9976 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9977 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 9978 | return PyBool_FromLong(0); |
| 9979 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 9980 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9981 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 9982 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9983 | } |
| 9984 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9985 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9986 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9987 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 9988 | Return True if S is a titlecased string and there is at least one\n\ |
| 9989 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 9990 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 9991 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9992 | |
| 9993 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9994 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9995 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9996 | Py_ssize_t i, length; |
| 9997 | int kind; |
| 9998 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9999 | int cased, previous_is_cased; |
| 10000 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10001 | if (PyUnicode_READY(self) == -1) |
| 10002 | return NULL; |
| 10003 | length = PyUnicode_GET_LENGTH(self); |
| 10004 | kind = PyUnicode_KIND(self); |
| 10005 | data = PyUnicode_DATA(self); |
| 10006 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10007 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10008 | if (length == 1) { |
| 10009 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 10010 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 10011 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 10012 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10013 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10014 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10015 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10016 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10017 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10018 | cased = 0; |
| 10019 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10020 | for (i = 0; i < length; i++) { |
| 10021 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10022 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10023 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 10024 | if (previous_is_cased) |
| 10025 | return PyBool_FromLong(0); |
| 10026 | previous_is_cased = 1; |
| 10027 | cased = 1; |
| 10028 | } |
| 10029 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 10030 | if (!previous_is_cased) |
| 10031 | return PyBool_FromLong(0); |
| 10032 | previous_is_cased = 1; |
| 10033 | cased = 1; |
| 10034 | } |
| 10035 | else |
| 10036 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10037 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10038 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10039 | } |
| 10040 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10041 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10042 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10043 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10044 | Return True if all characters in S are whitespace\n\ |
| 10045 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10046 | |
| 10047 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10048 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10049 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10050 | Py_ssize_t i, length; |
| 10051 | int kind; |
| 10052 | void *data; |
| 10053 | |
| 10054 | if (PyUnicode_READY(self) == -1) |
| 10055 | return NULL; |
| 10056 | length = PyUnicode_GET_LENGTH(self); |
| 10057 | kind = PyUnicode_KIND(self); |
| 10058 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10059 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10060 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10061 | if (length == 1) |
| 10062 | return PyBool_FromLong( |
| 10063 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10064 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10065 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10066 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10067 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10068 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10069 | for (i = 0; i < length; i++) { |
| 10070 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 10071 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10072 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10073 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10074 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10075 | } |
| 10076 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10077 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10078 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10079 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10080 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10081 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10082 | |
| 10083 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10084 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10085 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10086 | Py_ssize_t i, length; |
| 10087 | int kind; |
| 10088 | void *data; |
| 10089 | |
| 10090 | if (PyUnicode_READY(self) == -1) |
| 10091 | return NULL; |
| 10092 | length = PyUnicode_GET_LENGTH(self); |
| 10093 | kind = PyUnicode_KIND(self); |
| 10094 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10095 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10096 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10097 | if (length == 1) |
| 10098 | return PyBool_FromLong( |
| 10099 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10100 | |
| 10101 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10102 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10103 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10104 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10105 | for (i = 0; i < length; i++) { |
| 10106 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10107 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10108 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10109 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10110 | } |
| 10111 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10112 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10113 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10114 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10115 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10116 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10117 | |
| 10118 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10119 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10120 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10121 | int kind; |
| 10122 | void *data; |
| 10123 | Py_ssize_t len, i; |
| 10124 | |
| 10125 | if (PyUnicode_READY(self) == -1) |
| 10126 | return NULL; |
| 10127 | |
| 10128 | kind = PyUnicode_KIND(self); |
| 10129 | data = PyUnicode_DATA(self); |
| 10130 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10131 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10132 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10133 | if (len == 1) { |
| 10134 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 10135 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 10136 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10137 | |
| 10138 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10139 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10140 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10141 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10142 | for (i = 0; i < len; i++) { |
| 10143 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 10144 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10145 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10146 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10147 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10148 | } |
| 10149 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10150 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10151 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10152 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10153 | 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] | 10154 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10155 | |
| 10156 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10157 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10158 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10159 | Py_ssize_t i, length; |
| 10160 | int kind; |
| 10161 | void *data; |
| 10162 | |
| 10163 | if (PyUnicode_READY(self) == -1) |
| 10164 | return NULL; |
| 10165 | length = PyUnicode_GET_LENGTH(self); |
| 10166 | kind = PyUnicode_KIND(self); |
| 10167 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10168 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10169 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10170 | if (length == 1) |
| 10171 | return PyBool_FromLong( |
| 10172 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10173 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10174 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10175 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10176 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10177 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10178 | for (i = 0; i < length; i++) { |
| 10179 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10180 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10181 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10182 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10183 | } |
| 10184 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10185 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10186 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10187 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10188 | Return True if all characters in S are digits\n\ |
| 10189 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10190 | |
| 10191 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10192 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10193 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10194 | Py_ssize_t i, length; |
| 10195 | int kind; |
| 10196 | void *data; |
| 10197 | |
| 10198 | if (PyUnicode_READY(self) == -1) |
| 10199 | return NULL; |
| 10200 | length = PyUnicode_GET_LENGTH(self); |
| 10201 | kind = PyUnicode_KIND(self); |
| 10202 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10203 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10204 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10205 | if (length == 1) { |
| 10206 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 10207 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 10208 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10209 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10210 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10211 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10212 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10213 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10214 | for (i = 0; i < length; i++) { |
| 10215 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10216 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10217 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10218 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10219 | } |
| 10220 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10221 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10222 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10223 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10224 | 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] | 10225 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10226 | |
| 10227 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10228 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10229 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10230 | Py_ssize_t i, length; |
| 10231 | int kind; |
| 10232 | void *data; |
| 10233 | |
| 10234 | if (PyUnicode_READY(self) == -1) |
| 10235 | return NULL; |
| 10236 | length = PyUnicode_GET_LENGTH(self); |
| 10237 | kind = PyUnicode_KIND(self); |
| 10238 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10239 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10240 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10241 | if (length == 1) |
| 10242 | return PyBool_FromLong( |
| 10243 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10244 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10245 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10246 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10247 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10248 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10249 | for (i = 0; i < length; i++) { |
| 10250 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10251 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10252 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10253 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10254 | } |
| 10255 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10256 | int |
| 10257 | PyUnicode_IsIdentifier(PyObject *self) |
| 10258 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10259 | int kind; |
| 10260 | void *data; |
| 10261 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 10262 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10263 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10264 | if (PyUnicode_READY(self) == -1) { |
| 10265 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10266 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10267 | } |
| 10268 | |
| 10269 | /* Special case for empty strings */ |
| 10270 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 10271 | return 0; |
| 10272 | kind = PyUnicode_KIND(self); |
| 10273 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10274 | |
| 10275 | /* PEP 3131 says that the first character must be in |
| 10276 | XID_Start and subsequent characters in XID_Continue, |
| 10277 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10278 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10279 | letters, digits, underscore). However, given the current |
| 10280 | definition of XID_Start and XID_Continue, it is sufficient |
| 10281 | to check just for these, except that _ must be allowed |
| 10282 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10283 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 10284 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10285 | return 0; |
| 10286 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 10287 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10288 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10289 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10290 | return 1; |
| 10291 | } |
| 10292 | |
| 10293 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10294 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10295 | \n\ |
| 10296 | Return True if S is a valid identifier according\n\ |
| 10297 | to the language definition."); |
| 10298 | |
| 10299 | static PyObject* |
| 10300 | unicode_isidentifier(PyObject *self) |
| 10301 | { |
| 10302 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 10303 | } |
| 10304 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10305 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10306 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10307 | \n\ |
| 10308 | Return True if all characters in S are considered\n\ |
| 10309 | printable in repr() or S is empty, False otherwise."); |
| 10310 | |
| 10311 | static PyObject* |
| 10312 | unicode_isprintable(PyObject *self) |
| 10313 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10314 | Py_ssize_t i, length; |
| 10315 | int kind; |
| 10316 | void *data; |
| 10317 | |
| 10318 | if (PyUnicode_READY(self) == -1) |
| 10319 | return NULL; |
| 10320 | length = PyUnicode_GET_LENGTH(self); |
| 10321 | kind = PyUnicode_KIND(self); |
| 10322 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10323 | |
| 10324 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10325 | if (length == 1) |
| 10326 | return PyBool_FromLong( |
| 10327 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10328 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10329 | for (i = 0; i < length; i++) { |
| 10330 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10331 | Py_RETURN_FALSE; |
| 10332 | } |
| 10333 | } |
| 10334 | Py_RETURN_TRUE; |
| 10335 | } |
| 10336 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10337 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 10338 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10339 | \n\ |
| 10340 | 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] | 10341 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10342 | |
| 10343 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10344 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10345 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10346 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10347 | } |
| 10348 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10349 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10350 | unicode_length(PyUnicodeObject *self) |
| 10351 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10352 | if (PyUnicode_READY(self) == -1) |
| 10353 | return -1; |
| 10354 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10355 | } |
| 10356 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10357 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10358 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10359 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 10360 | 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] | 10361 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10362 | |
| 10363 | static PyObject * |
| 10364 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 10365 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10366 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10367 | Py_UCS4 fillchar = ' '; |
| 10368 | |
| 10369 | if (PyUnicode_READY(self) == -1) |
| 10370 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10371 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10372 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10373 | return NULL; |
| 10374 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10375 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10376 | Py_INCREF(self); |
| 10377 | return (PyObject*) self; |
| 10378 | } |
| 10379 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10380 | return (PyObject*) pad(self, 0, width - _PyUnicode_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10381 | } |
| 10382 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10383 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10384 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10385 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10386 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10387 | |
| 10388 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10389 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10390 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10391 | return fixup(self, fixlower); |
| 10392 | } |
| 10393 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10394 | #define LEFTSTRIP 0 |
| 10395 | #define RIGHTSTRIP 1 |
| 10396 | #define BOTHSTRIP 2 |
| 10397 | |
| 10398 | /* Arrays indexed by above */ |
| 10399 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 10400 | |
| 10401 | #define STRIPNAME(i) (stripformat[i]+3) |
| 10402 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10403 | /* externally visible for str.strip(unicode) */ |
| 10404 | PyObject * |
| 10405 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 10406 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10407 | void *data; |
| 10408 | int kind; |
| 10409 | Py_ssize_t i, j, len; |
| 10410 | BLOOM_MASK sepmask; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10411 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10412 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 10413 | return NULL; |
| 10414 | |
| 10415 | kind = PyUnicode_KIND(self); |
| 10416 | data = PyUnicode_DATA(self); |
| 10417 | len = PyUnicode_GET_LENGTH(self); |
| 10418 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 10419 | PyUnicode_DATA(sepobj), |
| 10420 | PyUnicode_GET_LENGTH(sepobj)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10421 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10422 | i = 0; |
| 10423 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10424 | while (i < len && |
| 10425 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10426 | i++; |
| 10427 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10428 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10429 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10430 | j = len; |
| 10431 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10432 | do { |
| 10433 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10434 | } while (j >= i && |
| 10435 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10436 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10437 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10438 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10439 | return PyUnicode_Substring((PyObject*)self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10440 | } |
| 10441 | |
| 10442 | PyObject* |
| 10443 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 10444 | { |
| 10445 | unsigned char *data; |
| 10446 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10447 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10448 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10449 | if (start == 0 && end == PyUnicode_GET_LENGTH(self)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10450 | { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10451 | if (PyUnicode_CheckExact(self)) { |
| 10452 | Py_INCREF(self); |
| 10453 | return self; |
| 10454 | } |
| 10455 | else |
| 10456 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10457 | } |
| 10458 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10459 | length = end - start; |
| 10460 | if (length == 1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10461 | return unicode_getitem((PyUnicodeObject*)self, start); |
| 10462 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10463 | if (start < 0 || end < 0 || end > PyUnicode_GET_LENGTH(self)) { |
| 10464 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 10465 | return NULL; |
| 10466 | } |
| 10467 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10468 | if (PyUnicode_READY(self) == -1) |
| 10469 | return NULL; |
| 10470 | kind = PyUnicode_KIND(self); |
| 10471 | data = PyUnicode_1BYTE_DATA(self); |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 10472 | return PyUnicode_FromKindAndData(kind, |
| 10473 | data + PyUnicode_KIND_SIZE(kind, start), |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10474 | length); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10475 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10476 | |
| 10477 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10478 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10479 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10480 | int kind; |
| 10481 | void *data; |
| 10482 | Py_ssize_t len, i, j; |
| 10483 | |
| 10484 | if (PyUnicode_READY(self) == -1) |
| 10485 | return NULL; |
| 10486 | |
| 10487 | kind = PyUnicode_KIND(self); |
| 10488 | data = PyUnicode_DATA(self); |
| 10489 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10490 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10491 | i = 0; |
| 10492 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10493 | while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10494 | i++; |
| 10495 | } |
| 10496 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10497 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10498 | j = len; |
| 10499 | if (striptype != LEFTSTRIP) { |
| 10500 | do { |
| 10501 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10502 | } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10503 | j++; |
| 10504 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10505 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10506 | return PyUnicode_Substring((PyObject*)self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10507 | } |
| 10508 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10509 | |
| 10510 | static PyObject * |
| 10511 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 10512 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10513 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10514 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10515 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 10516 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10517 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10518 | if (sep != NULL && sep != Py_None) { |
| 10519 | if (PyUnicode_Check(sep)) |
| 10520 | return _PyUnicode_XStrip(self, striptype, sep); |
| 10521 | else { |
| 10522 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10523 | "%s arg must be None or str", |
| 10524 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10525 | return NULL; |
| 10526 | } |
| 10527 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10528 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10529 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10530 | } |
| 10531 | |
| 10532 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10533 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10534 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10535 | \n\ |
| 10536 | Return a copy of the string S with leading and trailing\n\ |
| 10537 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10538 | 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] | 10539 | |
| 10540 | static PyObject * |
| 10541 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 10542 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10543 | if (PyTuple_GET_SIZE(args) == 0) |
| 10544 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 10545 | else |
| 10546 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10547 | } |
| 10548 | |
| 10549 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10550 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10551 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10552 | \n\ |
| 10553 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10554 | 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] | 10555 | |
| 10556 | static PyObject * |
| 10557 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 10558 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10559 | if (PyTuple_GET_SIZE(args) == 0) |
| 10560 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 10561 | else |
| 10562 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10563 | } |
| 10564 | |
| 10565 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10566 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10567 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10568 | \n\ |
| 10569 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10570 | 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] | 10571 | |
| 10572 | static PyObject * |
| 10573 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 10574 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10575 | if (PyTuple_GET_SIZE(args) == 0) |
| 10576 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 10577 | else |
| 10578 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10579 | } |
| 10580 | |
| 10581 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10582 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10583 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10584 | { |
| 10585 | PyUnicodeObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10586 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10587 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 10588 | if (len < 1) { |
| 10589 | Py_INCREF(unicode_empty); |
| 10590 | return (PyObject *)unicode_empty; |
| 10591 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10592 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 10593 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10594 | /* no repeat, return original string */ |
| 10595 | Py_INCREF(str); |
| 10596 | return (PyObject*) str; |
| 10597 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 10598 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10599 | if (PyUnicode_READY(str) == -1) |
| 10600 | return NULL; |
| 10601 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 10602 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 10603 | PyErr_SetString(PyExc_OverflowError, |
| 10604 | "repeated string is too long"); |
| 10605 | return NULL; |
| 10606 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10607 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 10608 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10609 | u = (PyUnicodeObject *)PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10610 | if (!u) |
| 10611 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 10612 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10613 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10614 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 10615 | const int kind = PyUnicode_KIND(str); |
| 10616 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
| 10617 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 10618 | if (kind == PyUnicode_1BYTE_KIND) |
| 10619 | memset(to, (unsigned char)fill_char, len); |
| 10620 | else { |
| 10621 | for (n = 0; n < len; ++n) |
| 10622 | PyUnicode_WRITE(kind, to, n, fill_char); |
| 10623 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10624 | } |
| 10625 | else { |
| 10626 | /* number of characters copied this far */ |
| 10627 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
| 10628 | const Py_ssize_t char_size = PyUnicode_CHARACTER_SIZE(str); |
| 10629 | char *to = (char *) PyUnicode_DATA(u); |
| 10630 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 10631 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10632 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10633 | n = (done <= nchars-done) ? done : nchars-done; |
| 10634 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10635 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10636 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10637 | } |
| 10638 | |
| 10639 | return (PyObject*) u; |
| 10640 | } |
| 10641 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10642 | PyObject * |
| 10643 | PyUnicode_Replace(PyObject *obj, |
| 10644 | PyObject *subobj, |
| 10645 | PyObject *replobj, |
| 10646 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10647 | { |
| 10648 | PyObject *self; |
| 10649 | PyObject *str1; |
| 10650 | PyObject *str2; |
| 10651 | PyObject *result; |
| 10652 | |
| 10653 | self = PyUnicode_FromObject(obj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10654 | if (self == NULL || PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10655 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10656 | str1 = PyUnicode_FromObject(subobj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10657 | if (str1 == NULL || PyUnicode_READY(str1) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10658 | Py_DECREF(self); |
| 10659 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10660 | } |
| 10661 | str2 = PyUnicode_FromObject(replobj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10662 | if (str2 == NULL || PyUnicode_READY(str2)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10663 | Py_DECREF(self); |
| 10664 | Py_DECREF(str1); |
| 10665 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10666 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10667 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10668 | Py_DECREF(self); |
| 10669 | Py_DECREF(str1); |
| 10670 | Py_DECREF(str2); |
| 10671 | return result; |
| 10672 | } |
| 10673 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10674 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 10675 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10676 | \n\ |
| 10677 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 10678 | old replaced by new. If the optional argument count is\n\ |
| 10679 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10680 | |
| 10681 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10682 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10683 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10684 | PyObject *str1; |
| 10685 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10686 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10687 | PyObject *result; |
| 10688 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10689 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10690 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10691 | if (!PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10692 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10693 | str1 = PyUnicode_FromObject(str1); |
| 10694 | if (str1 == NULL || PyUnicode_READY(str1) == -1) |
| 10695 | return NULL; |
| 10696 | str2 = PyUnicode_FromObject(str2); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10697 | if (str2 == NULL || PyUnicode_READY(str2) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10698 | Py_DECREF(str1); |
| 10699 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 10700 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10701 | |
| 10702 | result = replace(self, str1, str2, maxcount); |
| 10703 | |
| 10704 | Py_DECREF(str1); |
| 10705 | Py_DECREF(str2); |
| 10706 | return result; |
| 10707 | } |
| 10708 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10709 | static PyObject * |
| 10710 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10711 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10712 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10713 | Py_ssize_t isize; |
| 10714 | Py_ssize_t osize, squote, dquote, i, o; |
| 10715 | Py_UCS4 max, quote; |
| 10716 | int ikind, okind; |
| 10717 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10718 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10719 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10720 | return NULL; |
| 10721 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10722 | isize = PyUnicode_GET_LENGTH(unicode); |
| 10723 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10724 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10725 | /* Compute length of output, quote characters, and |
| 10726 | maximum character */ |
| 10727 | osize = 2; /* quotes */ |
| 10728 | max = 127; |
| 10729 | squote = dquote = 0; |
| 10730 | ikind = PyUnicode_KIND(unicode); |
| 10731 | for (i = 0; i < isize; i++) { |
| 10732 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 10733 | switch (ch) { |
| 10734 | case '\'': squote++; osize++; break; |
| 10735 | case '"': dquote++; osize++; break; |
| 10736 | case '\\': case '\t': case '\r': case '\n': |
| 10737 | osize += 2; break; |
| 10738 | default: |
| 10739 | /* Fast-path ASCII */ |
| 10740 | if (ch < ' ' || ch == 0x7f) |
| 10741 | osize += 4; /* \xHH */ |
| 10742 | else if (ch < 0x7f) |
| 10743 | osize++; |
| 10744 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 10745 | osize++; |
| 10746 | max = ch > max ? ch : max; |
| 10747 | } |
| 10748 | else if (ch < 0x100) |
| 10749 | osize += 4; /* \xHH */ |
| 10750 | else if (ch < 0x10000) |
| 10751 | osize += 6; /* \uHHHH */ |
| 10752 | else |
| 10753 | osize += 10; /* \uHHHHHHHH */ |
| 10754 | } |
| 10755 | } |
| 10756 | |
| 10757 | quote = '\''; |
| 10758 | if (squote) { |
| 10759 | if (dquote) |
| 10760 | /* Both squote and dquote present. Use squote, |
| 10761 | and escape them */ |
| 10762 | osize += squote; |
| 10763 | else |
| 10764 | quote = '"'; |
| 10765 | } |
| 10766 | |
| 10767 | repr = PyUnicode_New(osize, max); |
| 10768 | if (repr == NULL) |
| 10769 | return NULL; |
| 10770 | okind = PyUnicode_KIND(repr); |
| 10771 | odata = PyUnicode_DATA(repr); |
| 10772 | |
| 10773 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 10774 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
| 10775 | |
| 10776 | for (i = 0, o = 1; i < isize; i++) { |
| 10777 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10778 | |
| 10779 | /* Escape quotes and backslashes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10780 | if ((ch == quote) || (ch == '\\')) { |
| 10781 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 10782 | PyUnicode_WRITE(okind, odata, o++, ch); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10783 | continue; |
| 10784 | } |
| 10785 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10786 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10787 | if (ch == '\t') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10788 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 10789 | PyUnicode_WRITE(okind, odata, o++, 't'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10790 | } |
| 10791 | else if (ch == '\n') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10792 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 10793 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10794 | } |
| 10795 | else if (ch == '\r') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10796 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 10797 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10798 | } |
| 10799 | |
| 10800 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10801 | else if (ch < ' ' || ch == 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10802 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 10803 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 10804 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0x000F]); |
| 10805 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0x000F]); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10806 | } |
| 10807 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10808 | /* Copy ASCII characters as-is */ |
| 10809 | else if (ch < 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10810 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10811 | } |
| 10812 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10813 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10814 | else { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10815 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10816 | (categories Z* and C* except ASCII space) |
| 10817 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10818 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10819 | /* Map 8-bit characters to '\xhh' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10820 | if (ch <= 0xff) { |
| 10821 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 10822 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 10823 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0x000F]); |
| 10824 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0x000F]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10825 | } |
| 10826 | /* Map 21-bit characters to '\U00xxxxxx' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10827 | else if (ch >= 0x10000) { |
| 10828 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 10829 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
| 10830 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 28) & 0xF]); |
| 10831 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 24) & 0xF]); |
| 10832 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 20) & 0xF]); |
| 10833 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 16) & 0xF]); |
| 10834 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 12) & 0xF]); |
| 10835 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 8) & 0xF]); |
| 10836 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0xF]); |
| 10837 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10838 | } |
| 10839 | /* Map 16-bit characters to '\uxxxx' */ |
| 10840 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10841 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 10842 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
| 10843 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 12) & 0xF]); |
| 10844 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 8) & 0xF]); |
| 10845 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0xF]); |
| 10846 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10847 | } |
| 10848 | } |
| 10849 | /* Copy characters as-is */ |
| 10850 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10851 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10852 | } |
| 10853 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10854 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10855 | /* Closing quote already added at the beginning */ |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10856 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10857 | } |
| 10858 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10859 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10860 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10861 | \n\ |
| 10862 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 10863 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10864 | arguments start and end are interpreted as in slice notation.\n\ |
| 10865 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10866 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10867 | |
| 10868 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10869 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10870 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10871 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10872 | Py_ssize_t start; |
| 10873 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10874 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10875 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10876 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 10877 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10878 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10879 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10880 | if (PyUnicode_READY(self) == -1) |
| 10881 | return NULL; |
| 10882 | if (PyUnicode_READY(substring) == -1) |
| 10883 | return NULL; |
| 10884 | |
| 10885 | result = any_find_slice( |
| 10886 | ucs1lib_rfind_slice, ucs2lib_rfind_slice, ucs4lib_rfind_slice, |
| 10887 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10888 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10889 | |
| 10890 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10891 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10892 | if (result == -2) |
| 10893 | return NULL; |
| 10894 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10895 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10896 | } |
| 10897 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10898 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10899 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10900 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10901 | 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] | 10902 | |
| 10903 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10904 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10905 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10906 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10907 | Py_ssize_t start; |
| 10908 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10909 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10910 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10911 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 10912 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10913 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10914 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10915 | if (PyUnicode_READY(self) == -1) |
| 10916 | return NULL; |
| 10917 | if (PyUnicode_READY(substring) == -1) |
| 10918 | return NULL; |
| 10919 | |
| 10920 | result = any_find_slice( |
| 10921 | ucs1lib_rfind_slice, ucs2lib_rfind_slice, ucs4lib_rfind_slice, |
| 10922 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10923 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10924 | |
| 10925 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10926 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10927 | if (result == -2) |
| 10928 | return NULL; |
| 10929 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10930 | if (result < 0) { |
| 10931 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 10932 | return NULL; |
| 10933 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10934 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10935 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10936 | } |
| 10937 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10938 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10939 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10940 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 10941 | 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] | 10942 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10943 | |
| 10944 | static PyObject * |
| 10945 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 10946 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10947 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10948 | Py_UCS4 fillchar = ' '; |
| 10949 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10950 | 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] | 10951 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10952 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10953 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10954 | return NULL; |
| 10955 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10956 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10957 | Py_INCREF(self); |
| 10958 | return (PyObject*) self; |
| 10959 | } |
| 10960 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10961 | return (PyObject*) pad(self, width - _PyUnicode_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10962 | } |
| 10963 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10964 | PyObject * |
| 10965 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10966 | { |
| 10967 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10968 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10969 | s = PyUnicode_FromObject(s); |
| 10970 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10971 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10972 | if (sep != NULL) { |
| 10973 | sep = PyUnicode_FromObject(sep); |
| 10974 | if (sep == NULL) { |
| 10975 | Py_DECREF(s); |
| 10976 | return NULL; |
| 10977 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10978 | } |
| 10979 | |
| 10980 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 10981 | |
| 10982 | Py_DECREF(s); |
| 10983 | Py_XDECREF(sep); |
| 10984 | return result; |
| 10985 | } |
| 10986 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10987 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10988 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10989 | \n\ |
| 10990 | Return a list of the words in S, using sep as the\n\ |
| 10991 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 10992 | 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] | 10993 | whitespace string is a separator and empty strings are\n\ |
| 10994 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10995 | |
| 10996 | static PyObject* |
| 10997 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 10998 | { |
| 10999 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11000 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11001 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11002 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11003 | return NULL; |
| 11004 | |
| 11005 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11006 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11007 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11008 | return split(self, (PyUnicodeObject *)substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11009 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11010 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11011 | } |
| 11012 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11013 | PyObject * |
| 11014 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 11015 | { |
| 11016 | PyObject* str_obj; |
| 11017 | PyObject* sep_obj; |
| 11018 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11019 | int kind1, kind2, kind; |
| 11020 | void *buf1 = NULL, *buf2 = NULL; |
| 11021 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11022 | |
| 11023 | str_obj = PyUnicode_FromObject(str_in); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11024 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11025 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11026 | sep_obj = PyUnicode_FromObject(sep_in); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11027 | if (!sep_obj || PyUnicode_READY(sep_obj) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11028 | Py_DECREF(str_obj); |
| 11029 | return NULL; |
| 11030 | } |
| 11031 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11032 | kind1 = PyUnicode_KIND(str_in); |
| 11033 | kind2 = PyUnicode_KIND(sep_obj); |
| 11034 | kind = kind1 > kind2 ? kind1 : kind2; |
| 11035 | buf1 = PyUnicode_DATA(str_in); |
| 11036 | if (kind1 != kind) |
| 11037 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 11038 | if (!buf1) |
| 11039 | goto onError; |
| 11040 | buf2 = PyUnicode_DATA(sep_obj); |
| 11041 | if (kind2 != kind) |
| 11042 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 11043 | if (!buf2) |
| 11044 | goto onError; |
| 11045 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 11046 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 11047 | |
| 11048 | switch(PyUnicode_KIND(str_in)) { |
| 11049 | case PyUnicode_1BYTE_KIND: |
| 11050 | out = ucs1lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11051 | break; |
| 11052 | case PyUnicode_2BYTE_KIND: |
| 11053 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11054 | break; |
| 11055 | case PyUnicode_4BYTE_KIND: |
| 11056 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11057 | break; |
| 11058 | default: |
| 11059 | assert(0); |
| 11060 | out = 0; |
| 11061 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11062 | |
| 11063 | Py_DECREF(sep_obj); |
| 11064 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11065 | if (kind1 != kind) |
| 11066 | PyMem_Free(buf1); |
| 11067 | if (kind2 != kind) |
| 11068 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11069 | |
| 11070 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11071 | onError: |
| 11072 | Py_DECREF(sep_obj); |
| 11073 | Py_DECREF(str_obj); |
| 11074 | if (kind1 != kind && buf1) |
| 11075 | PyMem_Free(buf1); |
| 11076 | if (kind2 != kind && buf2) |
| 11077 | PyMem_Free(buf2); |
| 11078 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11079 | } |
| 11080 | |
| 11081 | |
| 11082 | PyObject * |
| 11083 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 11084 | { |
| 11085 | PyObject* str_obj; |
| 11086 | PyObject* sep_obj; |
| 11087 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11088 | int kind1, kind2, kind; |
| 11089 | void *buf1 = NULL, *buf2 = NULL; |
| 11090 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11091 | |
| 11092 | str_obj = PyUnicode_FromObject(str_in); |
| 11093 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11094 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11095 | sep_obj = PyUnicode_FromObject(sep_in); |
| 11096 | if (!sep_obj) { |
| 11097 | Py_DECREF(str_obj); |
| 11098 | return NULL; |
| 11099 | } |
| 11100 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11101 | kind1 = PyUnicode_KIND(str_in); |
| 11102 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 11103 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11104 | buf1 = PyUnicode_DATA(str_in); |
| 11105 | if (kind1 != kind) |
| 11106 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 11107 | if (!buf1) |
| 11108 | goto onError; |
| 11109 | buf2 = PyUnicode_DATA(sep_obj); |
| 11110 | if (kind2 != kind) |
| 11111 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 11112 | if (!buf2) |
| 11113 | goto onError; |
| 11114 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 11115 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 11116 | |
| 11117 | switch(PyUnicode_KIND(str_in)) { |
| 11118 | case PyUnicode_1BYTE_KIND: |
| 11119 | out = ucs1lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11120 | break; |
| 11121 | case PyUnicode_2BYTE_KIND: |
| 11122 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11123 | break; |
| 11124 | case PyUnicode_4BYTE_KIND: |
| 11125 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11126 | break; |
| 11127 | default: |
| 11128 | assert(0); |
| 11129 | out = 0; |
| 11130 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11131 | |
| 11132 | Py_DECREF(sep_obj); |
| 11133 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11134 | if (kind1 != kind) |
| 11135 | PyMem_Free(buf1); |
| 11136 | if (kind2 != kind) |
| 11137 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11138 | |
| 11139 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11140 | onError: |
| 11141 | Py_DECREF(sep_obj); |
| 11142 | Py_DECREF(str_obj); |
| 11143 | if (kind1 != kind && buf1) |
| 11144 | PyMem_Free(buf1); |
| 11145 | if (kind2 != kind && buf2) |
| 11146 | PyMem_Free(buf2); |
| 11147 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11148 | } |
| 11149 | |
| 11150 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11151 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11152 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 11153 | 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] | 11154 | 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] | 11155 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11156 | |
| 11157 | static PyObject* |
| 11158 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
| 11159 | { |
| 11160 | return PyUnicode_Partition((PyObject *)self, separator); |
| 11161 | } |
| 11162 | |
| 11163 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 11164 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11165 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 11166 | 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] | 11167 | 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] | 11168 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11169 | |
| 11170 | static PyObject* |
| 11171 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 11172 | { |
| 11173 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 11174 | } |
| 11175 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11176 | PyObject * |
| 11177 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11178 | { |
| 11179 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11180 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11181 | s = PyUnicode_FromObject(s); |
| 11182 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11183 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11184 | if (sep != NULL) { |
| 11185 | sep = PyUnicode_FromObject(sep); |
| 11186 | if (sep == NULL) { |
| 11187 | Py_DECREF(s); |
| 11188 | return NULL; |
| 11189 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11190 | } |
| 11191 | |
| 11192 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 11193 | |
| 11194 | Py_DECREF(s); |
| 11195 | Py_XDECREF(sep); |
| 11196 | return result; |
| 11197 | } |
| 11198 | |
| 11199 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11200 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11201 | \n\ |
| 11202 | Return a list of the words in S, using sep as the\n\ |
| 11203 | delimiter string, starting at the end of the string and\n\ |
| 11204 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 11205 | splits are done. If sep is not specified, any whitespace string\n\ |
| 11206 | is a separator."); |
| 11207 | |
| 11208 | static PyObject* |
| 11209 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 11210 | { |
| 11211 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11212 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11213 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11214 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11215 | return NULL; |
| 11216 | |
| 11217 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11218 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11219 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11220 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11221 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11222 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11223 | } |
| 11224 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11225 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11226 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11227 | \n\ |
| 11228 | 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] | 11229 | 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] | 11230 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11231 | |
| 11232 | static PyObject* |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 11233 | unicode_splitlines(PyUnicodeObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11234 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 11235 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 11236 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11237 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 11238 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 11239 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11240 | return NULL; |
| 11241 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 11242 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11243 | } |
| 11244 | |
| 11245 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 11246 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11247 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 11248 | if (PyUnicode_CheckExact(self)) { |
| 11249 | Py_INCREF(self); |
| 11250 | return self; |
| 11251 | } else |
| 11252 | /* Subtype -- return genuine unicode string with the same value. */ |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 11253 | return PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11254 | } |
| 11255 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11256 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11257 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11258 | \n\ |
| 11259 | 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] | 11260 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11261 | |
| 11262 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11263 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11264 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11265 | return fixup(self, fixswapcase); |
| 11266 | } |
| 11267 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11268 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11269 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11270 | \n\ |
| 11271 | Return a translation table usable for str.translate().\n\ |
| 11272 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 11273 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11274 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11275 | If there are two arguments, they must be strings of equal length, and\n\ |
| 11276 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 11277 | character at the same position in y. If there is a third argument, it\n\ |
| 11278 | must be a string, whose characters will be mapped to None in the result."); |
| 11279 | |
| 11280 | static PyObject* |
| 11281 | unicode_maketrans(PyUnicodeObject *null, PyObject *args) |
| 11282 | { |
| 11283 | PyObject *x, *y = NULL, *z = NULL; |
| 11284 | PyObject *new = NULL, *key, *value; |
| 11285 | Py_ssize_t i = 0; |
| 11286 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11287 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11288 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 11289 | return NULL; |
| 11290 | new = PyDict_New(); |
| 11291 | if (!new) |
| 11292 | return NULL; |
| 11293 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11294 | int x_kind, y_kind, z_kind; |
| 11295 | void *x_data, *y_data, *z_data; |
| 11296 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11297 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11298 | if (!PyUnicode_Check(x)) { |
| 11299 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 11300 | "be a string if there is a second argument"); |
| 11301 | goto err; |
| 11302 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11303 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11304 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 11305 | "arguments must have equal length"); |
| 11306 | goto err; |
| 11307 | } |
| 11308 | /* 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] | 11309 | x_kind = PyUnicode_KIND(x); |
| 11310 | y_kind = PyUnicode_KIND(y); |
| 11311 | x_data = PyUnicode_DATA(x); |
| 11312 | y_data = PyUnicode_DATA(y); |
| 11313 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 11314 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
| 11315 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11316 | if (!key || !value) |
| 11317 | goto err; |
| 11318 | res = PyDict_SetItem(new, key, value); |
| 11319 | Py_DECREF(key); |
| 11320 | Py_DECREF(value); |
| 11321 | if (res < 0) |
| 11322 | goto err; |
| 11323 | } |
| 11324 | /* create entries for deleting chars in z */ |
| 11325 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11326 | z_kind = PyUnicode_KIND(z); |
| 11327 | z_data = PyUnicode_DATA(z); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11328 | for (i = 0; i < PyUnicode_GET_SIZE(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11329 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11330 | if (!key) |
| 11331 | goto err; |
| 11332 | res = PyDict_SetItem(new, key, Py_None); |
| 11333 | Py_DECREF(key); |
| 11334 | if (res < 0) |
| 11335 | goto err; |
| 11336 | } |
| 11337 | } |
| 11338 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11339 | int kind; |
| 11340 | void *data; |
| 11341 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11342 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 11343 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11344 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 11345 | "to maketrans it must be a dict"); |
| 11346 | goto err; |
| 11347 | } |
| 11348 | /* copy entries into the new dict, converting string keys to int keys */ |
| 11349 | while (PyDict_Next(x, &i, &key, &value)) { |
| 11350 | if (PyUnicode_Check(key)) { |
| 11351 | /* convert string keys to integer keys */ |
| 11352 | PyObject *newkey; |
| 11353 | if (PyUnicode_GET_SIZE(key) != 1) { |
| 11354 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 11355 | "table must be of length 1"); |
| 11356 | goto err; |
| 11357 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11358 | kind = PyUnicode_KIND(key); |
| 11359 | data = PyUnicode_DATA(key); |
| 11360 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11361 | if (!newkey) |
| 11362 | goto err; |
| 11363 | res = PyDict_SetItem(new, newkey, value); |
| 11364 | Py_DECREF(newkey); |
| 11365 | if (res < 0) |
| 11366 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11367 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11368 | /* just keep integer keys */ |
| 11369 | if (PyDict_SetItem(new, key, value) < 0) |
| 11370 | goto err; |
| 11371 | } else { |
| 11372 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 11373 | "be strings or integers"); |
| 11374 | goto err; |
| 11375 | } |
| 11376 | } |
| 11377 | } |
| 11378 | return new; |
| 11379 | err: |
| 11380 | Py_DECREF(new); |
| 11381 | return NULL; |
| 11382 | } |
| 11383 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11384 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11385 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11386 | \n\ |
| 11387 | Return a copy of the string S, where all characters have been mapped\n\ |
| 11388 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11389 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 11390 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 11391 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11392 | |
| 11393 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11394 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11395 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11396 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11397 | } |
| 11398 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11399 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11400 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11401 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11402 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11403 | |
| 11404 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11405 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11406 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11407 | return fixup(self, fixupper); |
| 11408 | } |
| 11409 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11410 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11411 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11412 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 11413 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 11414 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11415 | |
| 11416 | static PyObject * |
| 11417 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 11418 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11419 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11420 | PyUnicodeObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11421 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11422 | int kind; |
| 11423 | void *data; |
| 11424 | Py_UCS4 chr; |
| 11425 | |
| 11426 | if (PyUnicode_READY(self) == -1) |
| 11427 | return NULL; |
| 11428 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11429 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11430 | return NULL; |
| 11431 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11432 | if (PyUnicode_GET_LENGTH(self) >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 11433 | if (PyUnicode_CheckExact(self)) { |
| 11434 | Py_INCREF(self); |
| 11435 | return (PyObject*) self; |
| 11436 | } |
| 11437 | else |
Victor Stinner | 2219e0a | 2011-10-01 01:16:59 +0200 | [diff] [blame] | 11438 | return PyUnicode_Copy((PyObject*)self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11439 | } |
| 11440 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11441 | fill = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11442 | |
| 11443 | u = pad(self, fill, 0, '0'); |
| 11444 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 11445 | if (u == NULL) |
| 11446 | return NULL; |
| 11447 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11448 | kind = PyUnicode_KIND(u); |
| 11449 | data = PyUnicode_DATA(u); |
| 11450 | chr = PyUnicode_READ(kind, data, fill); |
| 11451 | |
| 11452 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11453 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11454 | PyUnicode_WRITE(kind, data, 0, chr); |
| 11455 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11456 | } |
| 11457 | |
| 11458 | return (PyObject*) u; |
| 11459 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11460 | |
| 11461 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 11462 | static PyObject * |
| 11463 | unicode__decimal2ascii(PyObject *self) |
| 11464 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11465 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 11466 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11467 | #endif |
| 11468 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11469 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11470 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11471 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 11472 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 11473 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11474 | With optional end, stop comparing S at that position.\n\ |
| 11475 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11476 | |
| 11477 | static PyObject * |
| 11478 | unicode_startswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11479 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11480 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11481 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11482 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11483 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11484 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11485 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11486 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11487 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11488 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11489 | if (PyTuple_Check(subobj)) { |
| 11490 | Py_ssize_t i; |
| 11491 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 11492 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11493 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11494 | if (substring == NULL) |
| 11495 | return NULL; |
| 11496 | result = tailmatch(self, substring, start, end, -1); |
| 11497 | Py_DECREF(substring); |
| 11498 | if (result) { |
| 11499 | Py_RETURN_TRUE; |
| 11500 | } |
| 11501 | } |
| 11502 | /* nothing matched */ |
| 11503 | Py_RETURN_FALSE; |
| 11504 | } |
| 11505 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 11506 | if (substring == NULL) { |
| 11507 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 11508 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 11509 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11510 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 11511 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11512 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11513 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11514 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11515 | } |
| 11516 | |
| 11517 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11518 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11519 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11520 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 11521 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 11522 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11523 | With optional end, stop comparing S at that position.\n\ |
| 11524 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11525 | |
| 11526 | static PyObject * |
| 11527 | unicode_endswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11528 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11529 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11530 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11531 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11532 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11533 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11534 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11535 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11536 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11537 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11538 | if (PyTuple_Check(subobj)) { |
| 11539 | Py_ssize_t i; |
| 11540 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 11541 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11542 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11543 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11544 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11545 | result = tailmatch(self, substring, start, end, +1); |
| 11546 | Py_DECREF(substring); |
| 11547 | if (result) { |
| 11548 | Py_RETURN_TRUE; |
| 11549 | } |
| 11550 | } |
| 11551 | Py_RETURN_FALSE; |
| 11552 | } |
| 11553 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 11554 | if (substring == NULL) { |
| 11555 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 11556 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 11557 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11558 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 11559 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11560 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11561 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11562 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11563 | } |
| 11564 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11565 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 11566 | |
| 11567 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11568 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 11569 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 11570 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 11571 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 11572 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 11573 | PyDoc_STRVAR(format_map__doc__, |
| 11574 | "S.format_map(mapping) -> str\n\ |
| 11575 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 11576 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 11577 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 11578 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 11579 | static PyObject * |
| 11580 | unicode__format__(PyObject* self, PyObject* args) |
| 11581 | { |
| 11582 | PyObject *format_spec; |
| 11583 | |
| 11584 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 11585 | return NULL; |
| 11586 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11587 | return _PyUnicode_FormatAdvanced(self, format_spec, 0, |
| 11588 | PyUnicode_GET_LENGTH(format_spec)); |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 11589 | } |
| 11590 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 11591 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11592 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 11593 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 11594 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 11595 | |
| 11596 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 11597 | unicode__sizeof__(PyUnicodeObject *v) |
| 11598 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11599 | Py_ssize_t size; |
| 11600 | |
| 11601 | /* If it's a compact object, account for base structure + |
| 11602 | character data. */ |
| 11603 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 11604 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 11605 | else if (PyUnicode_IS_COMPACT(v)) |
| 11606 | size = sizeof(PyCompactUnicodeObject) + |
| 11607 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_CHARACTER_SIZE(v); |
| 11608 | else { |
| 11609 | /* If it is a two-block object, account for base object, and |
| 11610 | for character block if present. */ |
| 11611 | size = sizeof(PyUnicodeObject); |
| 11612 | if (v->data.any) |
| 11613 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
| 11614 | PyUnicode_CHARACTER_SIZE(v); |
| 11615 | } |
| 11616 | /* If the wstr pointer is present, account for it unless it is shared |
| 11617 | with the data pointer. Since PyUnicode_DATA will crash if the object |
| 11618 | is not ready, check whether it's either not ready (in which case the |
| 11619 | data is entirely in wstr) or if the data is not shared. */ |
| 11620 | if (_PyUnicode_WSTR(v) && |
| 11621 | (!PyUnicode_IS_READY(v) || |
| 11622 | (PyUnicode_DATA(v) != _PyUnicode_WSTR(v)))) |
| 11623 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
| 11624 | if (_PyUnicode_UTF8(v) && _PyUnicode_UTF8(v) != PyUnicode_DATA(v)) |
| 11625 | size += _PyUnicode_UTF8_LENGTH(v) + 1; |
| 11626 | |
| 11627 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 11628 | } |
| 11629 | |
| 11630 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11631 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 11632 | |
| 11633 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 11634 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 11635 | { |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 11636 | PyObject *copy = PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11637 | if (!copy) |
| 11638 | return NULL; |
| 11639 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 11640 | } |
| 11641 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11642 | static PyMethodDef unicode_methods[] = { |
| 11643 | |
| 11644 | /* Order is according to common usage: often used methods should |
| 11645 | appear first, since lookup is done sequentially. */ |
| 11646 | |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 11647 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11648 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 11649 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11650 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11651 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 11652 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 11653 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 11654 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 11655 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 11656 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 11657 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11658 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11659 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 11660 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 11661 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11662 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11663 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 11664 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 11665 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11666 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11667 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 11668 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11669 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11670 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 11671 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 11672 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 11673 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 11674 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 11675 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 11676 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 11677 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 11678 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 11679 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 11680 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 11681 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 11682 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 11683 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11684 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11685 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11686 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 11687 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 11688 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 11689 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11690 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 11691 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 11692 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 11693 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11694 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11695 | #endif |
| 11696 | |
| 11697 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 11698 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 11699 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11700 | #endif |
| 11701 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11702 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11703 | {NULL, NULL} |
| 11704 | }; |
| 11705 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 11706 | static PyObject * |
| 11707 | unicode_mod(PyObject *v, PyObject *w) |
| 11708 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 11709 | if (!PyUnicode_Check(v)) |
| 11710 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11711 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 11712 | } |
| 11713 | |
| 11714 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11715 | 0, /*nb_add*/ |
| 11716 | 0, /*nb_subtract*/ |
| 11717 | 0, /*nb_multiply*/ |
| 11718 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 11719 | }; |
| 11720 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11721 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11722 | (lenfunc) unicode_length, /* sq_length */ |
| 11723 | PyUnicode_Concat, /* sq_concat */ |
| 11724 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 11725 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 11726 | 0, /* sq_slice */ |
| 11727 | 0, /* sq_ass_item */ |
| 11728 | 0, /* sq_ass_slice */ |
| 11729 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11730 | }; |
| 11731 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11732 | static PyObject* |
| 11733 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 11734 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11735 | if (PyUnicode_READY(self) == -1) |
| 11736 | return NULL; |
| 11737 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 11738 | if (PyIndex_Check(item)) { |
| 11739 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11740 | if (i == -1 && PyErr_Occurred()) |
| 11741 | return NULL; |
| 11742 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11743 | i += PyUnicode_GET_LENGTH(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11744 | return unicode_getitem(self, i); |
| 11745 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11746 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11747 | const Py_UNICODE* source_buf; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11748 | Py_UNICODE* result_buf; |
| 11749 | PyObject* result; |
| 11750 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11751 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11752 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11753 | return NULL; |
| 11754 | } |
| 11755 | |
| 11756 | if (slicelength <= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11757 | return PyUnicode_New(0, 0); |
| 11758 | } else if (start == 0 && step == 1 && |
| 11759 | slicelength == PyUnicode_GET_LENGTH(self) && |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 11760 | PyUnicode_CheckExact(self)) { |
| 11761 | Py_INCREF(self); |
| 11762 | return (PyObject *)self; |
| 11763 | } else if (step == 1) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11764 | return PyUnicode_Substring((PyObject*)self, |
| 11765 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11766 | } else { |
| 11767 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 11768 | result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* |
| 11769 | sizeof(Py_UNICODE)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11770 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11771 | if (result_buf == NULL) |
| 11772 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11773 | |
| 11774 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 11775 | result_buf[i] = source_buf[cur]; |
| 11776 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11777 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11778 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 11779 | PyObject_FREE(result_buf); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11780 | return result; |
| 11781 | } |
| 11782 | } else { |
| 11783 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 11784 | return NULL; |
| 11785 | } |
| 11786 | } |
| 11787 | |
| 11788 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11789 | (lenfunc)unicode_length, /* mp_length */ |
| 11790 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 11791 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11792 | }; |
| 11793 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11794 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11795 | /* Helpers for PyUnicode_Format() */ |
| 11796 | |
| 11797 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11798 | 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] | 11799 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11800 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11801 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11802 | (*p_argidx)++; |
| 11803 | if (arglen < 0) |
| 11804 | return args; |
| 11805 | else |
| 11806 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11807 | } |
| 11808 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11809 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11810 | return NULL; |
| 11811 | } |
| 11812 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 11813 | /* 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] | 11814 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 11815 | static PyObject * |
| 11816 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11817 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 11818 | char *p; |
| 11819 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11820 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11821 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11822 | x = PyFloat_AsDouble(v); |
| 11823 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 11824 | return NULL; |
| 11825 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11826 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11827 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 11828 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 11829 | p = PyOS_double_to_string(x, type, prec, |
| 11830 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 11831 | if (p == NULL) |
| 11832 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11833 | result = PyUnicode_DecodeASCII(p, strlen(p), NULL); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 11834 | PyMem_Free(p); |
| 11835 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11836 | } |
| 11837 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 11838 | static PyObject* |
| 11839 | formatlong(PyObject *val, int flags, int prec, int type) |
| 11840 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11841 | char *buf; |
| 11842 | int len; |
| 11843 | PyObject *str; /* temporary string object. */ |
| 11844 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 11845 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11846 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 11847 | if (!str) |
| 11848 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11849 | result = PyUnicode_DecodeASCII(buf, len, NULL); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11850 | Py_DECREF(str); |
| 11851 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 11852 | } |
| 11853 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11854 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11855 | formatchar(Py_UCS4 *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 11856 | size_t buflen, |
| 11857 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11858 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 11859 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 11860 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11861 | if (PyUnicode_GET_LENGTH(v) == 1) { |
| 11862 | buf[0] = PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11863 | buf[1] = '\0'; |
| 11864 | return 1; |
| 11865 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11866 | goto onError; |
| 11867 | } |
| 11868 | else { |
| 11869 | /* Integer input truncated to a character */ |
| 11870 | long x; |
| 11871 | x = PyLong_AsLong(v); |
| 11872 | if (x == -1 && PyErr_Occurred()) |
| 11873 | goto onError; |
| 11874 | |
| 11875 | if (x < 0 || x > 0x10ffff) { |
| 11876 | PyErr_SetString(PyExc_OverflowError, |
| 11877 | "%c arg not in range(0x110000)"); |
| 11878 | return -1; |
| 11879 | } |
| 11880 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11881 | buf[0] = (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11882 | buf[1] = '\0'; |
| 11883 | return 1; |
| 11884 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 11885 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11886 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 11887 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11888 | "%c requires int or char"); |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 11889 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11890 | } |
| 11891 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 11892 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 11893 | 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] | 11894 | */ |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 11895 | #define FORMATBUFLEN (size_t)10 |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 11896 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11897 | PyObject * |
| 11898 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11899 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11900 | void *fmt; |
| 11901 | int fmtkind; |
| 11902 | PyObject *result; |
| 11903 | Py_UCS4 *res, *res0; |
| 11904 | Py_UCS4 max; |
| 11905 | int kind; |
| 11906 | Py_ssize_t fmtcnt, fmtpos, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11907 | int args_owned = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11908 | PyObject *dict = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11909 | PyUnicodeObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11910 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11911 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11912 | PyErr_BadInternalCall(); |
| 11913 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11914 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11915 | uformat = (PyUnicodeObject*)PyUnicode_FromObject(format); |
| 11916 | if (uformat == NULL || PyUnicode_READY(uformat) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11917 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11918 | fmt = PyUnicode_DATA(uformat); |
| 11919 | fmtkind = PyUnicode_KIND(uformat); |
| 11920 | fmtcnt = PyUnicode_GET_LENGTH(uformat); |
| 11921 | fmtpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11922 | |
| 11923 | reslen = rescnt = fmtcnt + 100; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11924 | res = res0 = PyMem_Malloc(reslen * sizeof(Py_UCS4)); |
| 11925 | if (res0 == NULL) { |
| 11926 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11927 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11928 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11929 | |
| 11930 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11931 | arglen = PyTuple_Size(args); |
| 11932 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11933 | } |
| 11934 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11935 | arglen = -1; |
| 11936 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11937 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 11938 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 11939 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11940 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11941 | |
| 11942 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11943 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11944 | if (--rescnt < 0) { |
| 11945 | rescnt = fmtcnt + 100; |
| 11946 | reslen += rescnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11947 | res0 = PyMem_Realloc(res0, reslen*sizeof(Py_UCS4)); |
| 11948 | if (res0 == NULL){ |
| 11949 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11950 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11951 | } |
| 11952 | res = res0 + reslen - rescnt; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11953 | --rescnt; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11954 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11955 | *res++ = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11956 | } |
| 11957 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11958 | /* Got a format specifier */ |
| 11959 | int flags = 0; |
| 11960 | Py_ssize_t width = -1; |
| 11961 | int prec = -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11962 | Py_UCS4 c = '\0'; |
| 11963 | Py_UCS4 fill; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11964 | int isnumok; |
| 11965 | PyObject *v = NULL; |
| 11966 | PyObject *temp = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11967 | void *pbuf; |
| 11968 | Py_ssize_t pindex; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11969 | Py_UNICODE sign; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11970 | Py_ssize_t len, len1; |
| 11971 | Py_UCS4 formatbuf[FORMATBUFLEN]; /* For formatchar() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11972 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11973 | fmtpos++; |
| 11974 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') { |
| 11975 | Py_ssize_t keystart; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11976 | Py_ssize_t keylen; |
| 11977 | PyObject *key; |
| 11978 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 11979 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11980 | if (dict == NULL) { |
| 11981 | PyErr_SetString(PyExc_TypeError, |
| 11982 | "format requires a mapping"); |
| 11983 | goto onError; |
| 11984 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11985 | ++fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11986 | --fmtcnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11987 | keystart = fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11988 | /* Skip over balanced parentheses */ |
| 11989 | while (pcount > 0 && --fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11990 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == ')') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11991 | --pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11992 | else if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11993 | ++pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11994 | fmtpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11995 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11996 | keylen = fmtpos - keystart - 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11997 | if (fmtcnt < 0 || pcount > 0) { |
| 11998 | PyErr_SetString(PyExc_ValueError, |
| 11999 | "incomplete format key"); |
| 12000 | goto onError; |
| 12001 | } |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12002 | key = PyUnicode_Substring((PyObject*)uformat, |
| 12003 | keystart, keystart + keylen); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12004 | if (key == NULL) |
| 12005 | goto onError; |
| 12006 | if (args_owned) { |
| 12007 | Py_DECREF(args); |
| 12008 | args_owned = 0; |
| 12009 | } |
| 12010 | args = PyObject_GetItem(dict, key); |
| 12011 | Py_DECREF(key); |
| 12012 | if (args == NULL) { |
| 12013 | goto onError; |
| 12014 | } |
| 12015 | args_owned = 1; |
| 12016 | arglen = -1; |
| 12017 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12018 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12019 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12020 | switch (c = PyUnicode_READ(fmtkind, fmt, fmtpos++)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12021 | case '-': flags |= F_LJUST; continue; |
| 12022 | case '+': flags |= F_SIGN; continue; |
| 12023 | case ' ': flags |= F_BLANK; continue; |
| 12024 | case '#': flags |= F_ALT; continue; |
| 12025 | case '0': flags |= F_ZERO; continue; |
| 12026 | } |
| 12027 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12028 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12029 | if (c == '*') { |
| 12030 | v = getnextarg(args, arglen, &argidx); |
| 12031 | if (v == NULL) |
| 12032 | goto onError; |
| 12033 | if (!PyLong_Check(v)) { |
| 12034 | PyErr_SetString(PyExc_TypeError, |
| 12035 | "* wants int"); |
| 12036 | goto onError; |
| 12037 | } |
| 12038 | width = PyLong_AsLong(v); |
| 12039 | if (width == -1 && PyErr_Occurred()) |
| 12040 | goto onError; |
| 12041 | if (width < 0) { |
| 12042 | flags |= F_LJUST; |
| 12043 | width = -width; |
| 12044 | } |
| 12045 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12046 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12047 | } |
| 12048 | else if (c >= '0' && c <= '9') { |
| 12049 | width = c - '0'; |
| 12050 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12051 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12052 | if (c < '0' || c > '9') |
| 12053 | break; |
| 12054 | if ((width*10) / 10 != width) { |
| 12055 | PyErr_SetString(PyExc_ValueError, |
| 12056 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12057 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12058 | } |
| 12059 | width = width*10 + (c - '0'); |
| 12060 | } |
| 12061 | } |
| 12062 | if (c == '.') { |
| 12063 | prec = 0; |
| 12064 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12065 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12066 | if (c == '*') { |
| 12067 | v = getnextarg(args, arglen, &argidx); |
| 12068 | if (v == NULL) |
| 12069 | goto onError; |
| 12070 | if (!PyLong_Check(v)) { |
| 12071 | PyErr_SetString(PyExc_TypeError, |
| 12072 | "* wants int"); |
| 12073 | goto onError; |
| 12074 | } |
| 12075 | prec = PyLong_AsLong(v); |
| 12076 | if (prec == -1 && PyErr_Occurred()) |
| 12077 | goto onError; |
| 12078 | if (prec < 0) |
| 12079 | prec = 0; |
| 12080 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12081 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12082 | } |
| 12083 | else if (c >= '0' && c <= '9') { |
| 12084 | prec = c - '0'; |
| 12085 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12086 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12087 | if (c < '0' || c > '9') |
| 12088 | break; |
| 12089 | if ((prec*10) / 10 != prec) { |
| 12090 | PyErr_SetString(PyExc_ValueError, |
| 12091 | "prec too big"); |
| 12092 | goto onError; |
| 12093 | } |
| 12094 | prec = prec*10 + (c - '0'); |
| 12095 | } |
| 12096 | } |
| 12097 | } /* prec */ |
| 12098 | if (fmtcnt >= 0) { |
| 12099 | if (c == 'h' || c == 'l' || c == 'L') { |
| 12100 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12101 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12102 | } |
| 12103 | } |
| 12104 | if (fmtcnt < 0) { |
| 12105 | PyErr_SetString(PyExc_ValueError, |
| 12106 | "incomplete format"); |
| 12107 | goto onError; |
| 12108 | } |
| 12109 | if (c != '%') { |
| 12110 | v = getnextarg(args, arglen, &argidx); |
| 12111 | if (v == NULL) |
| 12112 | goto onError; |
| 12113 | } |
| 12114 | sign = 0; |
| 12115 | fill = ' '; |
| 12116 | switch (c) { |
| 12117 | |
| 12118 | case '%': |
| 12119 | pbuf = formatbuf; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12120 | kind = PyUnicode_4BYTE_KIND; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12121 | /* presume that buffer length is at least 1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12122 | PyUnicode_WRITE(kind, pbuf, 0, '%'); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12123 | len = 1; |
| 12124 | break; |
| 12125 | |
| 12126 | case 's': |
| 12127 | case 'r': |
| 12128 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 12129 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12130 | temp = v; |
| 12131 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12132 | } |
| 12133 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12134 | if (c == 's') |
| 12135 | temp = PyObject_Str(v); |
| 12136 | else if (c == 'r') |
| 12137 | temp = PyObject_Repr(v); |
| 12138 | else |
| 12139 | temp = PyObject_ASCII(v); |
| 12140 | if (temp == NULL) |
| 12141 | goto onError; |
| 12142 | if (PyUnicode_Check(temp)) |
| 12143 | /* nothing to do */; |
| 12144 | else { |
| 12145 | Py_DECREF(temp); |
| 12146 | PyErr_SetString(PyExc_TypeError, |
| 12147 | "%s argument has non-string str()"); |
| 12148 | goto onError; |
| 12149 | } |
| 12150 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12151 | if (PyUnicode_READY(temp) == -1) { |
| 12152 | Py_CLEAR(temp); |
| 12153 | goto onError; |
| 12154 | } |
| 12155 | pbuf = PyUnicode_DATA(temp); |
| 12156 | kind = PyUnicode_KIND(temp); |
| 12157 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12158 | if (prec >= 0 && len > prec) |
| 12159 | len = prec; |
| 12160 | break; |
| 12161 | |
| 12162 | case 'i': |
| 12163 | case 'd': |
| 12164 | case 'u': |
| 12165 | case 'o': |
| 12166 | case 'x': |
| 12167 | case 'X': |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12168 | isnumok = 0; |
| 12169 | if (PyNumber_Check(v)) { |
| 12170 | PyObject *iobj=NULL; |
| 12171 | |
| 12172 | if (PyLong_Check(v)) { |
| 12173 | iobj = v; |
| 12174 | Py_INCREF(iobj); |
| 12175 | } |
| 12176 | else { |
| 12177 | iobj = PyNumber_Long(v); |
| 12178 | } |
| 12179 | if (iobj!=NULL) { |
| 12180 | if (PyLong_Check(iobj)) { |
| 12181 | isnumok = 1; |
Senthil Kumaran | 9ebe08d | 2011-07-03 21:03:16 -0700 | [diff] [blame] | 12182 | temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12183 | Py_DECREF(iobj); |
| 12184 | if (!temp) |
| 12185 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12186 | if (PyUnicode_READY(temp) == -1) { |
| 12187 | Py_CLEAR(temp); |
| 12188 | goto onError; |
| 12189 | } |
| 12190 | pbuf = PyUnicode_DATA(temp); |
| 12191 | kind = PyUnicode_KIND(temp); |
| 12192 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12193 | sign = 1; |
| 12194 | } |
| 12195 | else { |
| 12196 | Py_DECREF(iobj); |
| 12197 | } |
| 12198 | } |
| 12199 | } |
| 12200 | if (!isnumok) { |
| 12201 | PyErr_Format(PyExc_TypeError, |
| 12202 | "%%%c format: a number is required, " |
| 12203 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 12204 | goto onError; |
| 12205 | } |
| 12206 | if (flags & F_ZERO) |
| 12207 | fill = '0'; |
| 12208 | break; |
| 12209 | |
| 12210 | case 'e': |
| 12211 | case 'E': |
| 12212 | case 'f': |
| 12213 | case 'F': |
| 12214 | case 'g': |
| 12215 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12216 | temp = formatfloat(v, flags, prec, c); |
| 12217 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12218 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12219 | if (PyUnicode_READY(temp) == -1) { |
| 12220 | Py_CLEAR(temp); |
| 12221 | goto onError; |
| 12222 | } |
| 12223 | pbuf = PyUnicode_DATA(temp); |
| 12224 | kind = PyUnicode_KIND(temp); |
| 12225 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12226 | sign = 1; |
| 12227 | if (flags & F_ZERO) |
| 12228 | fill = '0'; |
| 12229 | break; |
| 12230 | |
| 12231 | case 'c': |
| 12232 | pbuf = formatbuf; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12233 | kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | b9dcffb | 2011-09-29 00:39:24 +0200 | [diff] [blame] | 12234 | len = formatchar(pbuf, Py_ARRAY_LENGTH(formatbuf), v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12235 | if (len < 0) |
| 12236 | goto onError; |
| 12237 | break; |
| 12238 | |
| 12239 | default: |
| 12240 | PyErr_Format(PyExc_ValueError, |
| 12241 | "unsupported format character '%c' (0x%x) " |
| 12242 | "at index %zd", |
| 12243 | (31<=c && c<=126) ? (char)c : '?', |
| 12244 | (int)c, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12245 | fmtpos - 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12246 | goto onError; |
| 12247 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12248 | /* pbuf is initialized here. */ |
| 12249 | pindex = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12250 | if (sign) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12251 | if (PyUnicode_READ(kind, pbuf, pindex) == '-' || |
| 12252 | PyUnicode_READ(kind, pbuf, pindex) == '+') { |
| 12253 | sign = PyUnicode_READ(kind, pbuf, pindex++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12254 | len--; |
| 12255 | } |
| 12256 | else if (flags & F_SIGN) |
| 12257 | sign = '+'; |
| 12258 | else if (flags & F_BLANK) |
| 12259 | sign = ' '; |
| 12260 | else |
| 12261 | sign = 0; |
| 12262 | } |
| 12263 | if (width < len) |
| 12264 | width = len; |
| 12265 | if (rescnt - (sign != 0) < width) { |
| 12266 | reslen -= rescnt; |
| 12267 | rescnt = width + fmtcnt + 100; |
| 12268 | reslen += rescnt; |
| 12269 | if (reslen < 0) { |
| 12270 | Py_XDECREF(temp); |
| 12271 | PyErr_NoMemory(); |
| 12272 | goto onError; |
| 12273 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12274 | res0 = PyMem_Realloc(res0, reslen*sizeof(Py_UCS4)); |
| 12275 | if (res0 == 0) { |
| 12276 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12277 | Py_XDECREF(temp); |
| 12278 | goto onError; |
| 12279 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12280 | res = res0 + reslen - rescnt; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12281 | } |
| 12282 | if (sign) { |
| 12283 | if (fill != ' ') |
| 12284 | *res++ = sign; |
| 12285 | rescnt--; |
| 12286 | if (width > len) |
| 12287 | width--; |
| 12288 | } |
| 12289 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12290 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 12291 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12292 | if (fill != ' ') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12293 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
| 12294 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12295 | } |
| 12296 | rescnt -= 2; |
| 12297 | width -= 2; |
| 12298 | if (width < 0) |
| 12299 | width = 0; |
| 12300 | len -= 2; |
| 12301 | } |
| 12302 | if (width > len && !(flags & F_LJUST)) { |
| 12303 | do { |
| 12304 | --rescnt; |
| 12305 | *res++ = fill; |
| 12306 | } while (--width > len); |
| 12307 | } |
| 12308 | if (fill == ' ') { |
| 12309 | if (sign) |
| 12310 | *res++ = sign; |
| 12311 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12312 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 12313 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
| 12314 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
| 12315 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12316 | } |
| 12317 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12318 | /* Copy all characters, preserving len */ |
| 12319 | len1 = len; |
| 12320 | while (len1--) { |
| 12321 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
| 12322 | rescnt--; |
| 12323 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12324 | while (--width >= len) { |
| 12325 | --rescnt; |
| 12326 | *res++ = ' '; |
| 12327 | } |
| 12328 | if (dict && (argidx < arglen) && c != '%') { |
| 12329 | PyErr_SetString(PyExc_TypeError, |
| 12330 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 12331 | Py_XDECREF(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12332 | goto onError; |
| 12333 | } |
| 12334 | Py_XDECREF(temp); |
| 12335 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12336 | } /* until end */ |
| 12337 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12338 | PyErr_SetString(PyExc_TypeError, |
| 12339 | "not all arguments converted during string formatting"); |
| 12340 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12341 | } |
| 12342 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12343 | |
| 12344 | for (max=0, res = res0; res < res0+reslen-rescnt; res++) |
| 12345 | if (*res > max) |
| 12346 | max = *res; |
| 12347 | result = PyUnicode_New(reslen - rescnt, max); |
| 12348 | if (!result) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12349 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12350 | kind = PyUnicode_KIND(result); |
| 12351 | for (res = res0; res < res0+reslen-rescnt; res++) |
| 12352 | PyUnicode_WRITE(kind, PyUnicode_DATA(result), res-res0, *res); |
| 12353 | PyMem_Free(res0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12354 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12355 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12356 | } |
| 12357 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12358 | return (PyObject *)result; |
| 12359 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12360 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12361 | PyMem_Free(res0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12362 | Py_DECREF(uformat); |
| 12363 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12364 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12365 | } |
| 12366 | return NULL; |
| 12367 | } |
| 12368 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 12369 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 12370 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 12371 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12372 | static PyObject * |
| 12373 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 12374 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12375 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12376 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 12377 | char *encoding = NULL; |
| 12378 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12379 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12380 | if (type != &PyUnicode_Type) |
| 12381 | return unicode_subtype_new(type, args, kwds); |
| 12382 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12383 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12384 | return NULL; |
| 12385 | if (x == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12386 | return (PyObject *)PyUnicode_New(0, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12387 | if (encoding == NULL && errors == NULL) |
| 12388 | return PyObject_Str(x); |
| 12389 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12390 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12391 | } |
| 12392 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 12393 | static PyObject * |
| 12394 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 12395 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12396 | PyUnicodeObject *tmp, *pnew; |
| 12397 | Py_ssize_t n; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12398 | PyObject *err = NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 12399 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12400 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 12401 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 12402 | if (tmp == NULL) |
| 12403 | return NULL; |
| 12404 | assert(PyUnicode_Check(tmp)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12405 | // TODO: Verify the PyUnicode_GET_SIZE does the right thing. |
| 12406 | // it seems kind of strange that tp_alloc gets passed the size |
| 12407 | // of the unicode string because there will follow another |
| 12408 | // malloc. |
| 12409 | pnew = (PyUnicodeObject *) type->tp_alloc(type, |
| 12410 | n = PyUnicode_GET_SIZE(tmp)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12411 | if (pnew == NULL) { |
| 12412 | Py_DECREF(tmp); |
| 12413 | return NULL; |
| 12414 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12415 | _PyUnicode_WSTR(pnew) = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1)); |
| 12416 | if (_PyUnicode_WSTR(pnew) == NULL) { |
| 12417 | err = PyErr_NoMemory(); |
| 12418 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12419 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12420 | Py_UNICODE_COPY(_PyUnicode_WSTR(pnew), PyUnicode_AS_UNICODE(tmp), n+1); |
| 12421 | _PyUnicode_WSTR_LENGTH(pnew) = n; |
| 12422 | _PyUnicode_HASH(pnew) = _PyUnicode_HASH(tmp); |
| 12423 | _PyUnicode_STATE(pnew).interned = 0; |
| 12424 | _PyUnicode_STATE(pnew).kind = 0; |
| 12425 | _PyUnicode_STATE(pnew).compact = 0; |
| 12426 | _PyUnicode_STATE(pnew).ready = 0; |
| 12427 | _PyUnicode_STATE(pnew).ascii = 0; |
| 12428 | pnew->data.any = NULL; |
| 12429 | _PyUnicode_LENGTH(pnew) = 0; |
| 12430 | pnew->_base.utf8 = NULL; |
| 12431 | pnew->_base.utf8_length = 0; |
| 12432 | |
| 12433 | if (PyUnicode_READY(pnew) == -1) { |
| 12434 | PyObject_FREE(_PyUnicode_WSTR(pnew)); |
| 12435 | goto onError; |
| 12436 | } |
| 12437 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12438 | Py_DECREF(tmp); |
| 12439 | return (PyObject *)pnew; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12440 | |
| 12441 | onError: |
| 12442 | _Py_ForgetReference((PyObject *)pnew); |
| 12443 | PyObject_Del(pnew); |
| 12444 | Py_DECREF(tmp); |
| 12445 | return err; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 12446 | } |
| 12447 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12448 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12449 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12450 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 12451 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 12452 | encoding defaults to the current default string encoding.\n\ |
| 12453 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12454 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12455 | static PyObject *unicode_iter(PyObject *seq); |
| 12456 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12457 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 12458 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12459 | "str", /* tp_name */ |
| 12460 | sizeof(PyUnicodeObject), /* tp_size */ |
| 12461 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12462 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12463 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 12464 | 0, /* tp_print */ |
| 12465 | 0, /* tp_getattr */ |
| 12466 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 12467 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12468 | unicode_repr, /* tp_repr */ |
| 12469 | &unicode_as_number, /* tp_as_number */ |
| 12470 | &unicode_as_sequence, /* tp_as_sequence */ |
| 12471 | &unicode_as_mapping, /* tp_as_mapping */ |
| 12472 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 12473 | 0, /* tp_call*/ |
| 12474 | (reprfunc) unicode_str, /* tp_str */ |
| 12475 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 12476 | 0, /* tp_setattro */ |
| 12477 | 0, /* tp_as_buffer */ |
| 12478 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12479 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12480 | unicode_doc, /* tp_doc */ |
| 12481 | 0, /* tp_traverse */ |
| 12482 | 0, /* tp_clear */ |
| 12483 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 12484 | 0, /* tp_weaklistoffset */ |
| 12485 | unicode_iter, /* tp_iter */ |
| 12486 | 0, /* tp_iternext */ |
| 12487 | unicode_methods, /* tp_methods */ |
| 12488 | 0, /* tp_members */ |
| 12489 | 0, /* tp_getset */ |
| 12490 | &PyBaseObject_Type, /* tp_base */ |
| 12491 | 0, /* tp_dict */ |
| 12492 | 0, /* tp_descr_get */ |
| 12493 | 0, /* tp_descr_set */ |
| 12494 | 0, /* tp_dictoffset */ |
| 12495 | 0, /* tp_init */ |
| 12496 | 0, /* tp_alloc */ |
| 12497 | unicode_new, /* tp_new */ |
| 12498 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12499 | }; |
| 12500 | |
| 12501 | /* Initialize the Unicode implementation */ |
| 12502 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 12503 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12504 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 12505 | int i; |
| 12506 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12507 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12508 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12509 | 0x000A, /* LINE FEED */ |
| 12510 | 0x000D, /* CARRIAGE RETURN */ |
| 12511 | 0x001C, /* FILE SEPARATOR */ |
| 12512 | 0x001D, /* GROUP SEPARATOR */ |
| 12513 | 0x001E, /* RECORD SEPARATOR */ |
| 12514 | 0x0085, /* NEXT LINE */ |
| 12515 | 0x2028, /* LINE SEPARATOR */ |
| 12516 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 12517 | }; |
| 12518 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 12519 | /* Init the implementation */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12520 | unicode_empty = (PyUnicodeObject *) PyUnicode_New(0, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12521 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12522 | Py_FatalError("Can't create empty string"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12523 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 12524 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12525 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 12526 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12527 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12528 | |
| 12529 | /* initialize the linebreak bloom filter */ |
| 12530 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12531 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 12532 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12533 | |
| 12534 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12535 | } |
| 12536 | |
| 12537 | /* Finalize the Unicode implementation */ |
| 12538 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 12539 | int |
| 12540 | PyUnicode_ClearFreeList(void) |
| 12541 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12542 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 12543 | } |
| 12544 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12545 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 12546 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12547 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 12548 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12549 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 12550 | Py_XDECREF(unicode_empty); |
| 12551 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 12552 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 12553 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12554 | if (unicode_latin1[i]) { |
| 12555 | Py_DECREF(unicode_latin1[i]); |
| 12556 | unicode_latin1[i] = NULL; |
| 12557 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 12558 | } |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 12559 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12560 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 12561 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12562 | void |
| 12563 | PyUnicode_InternInPlace(PyObject **p) |
| 12564 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12565 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 12566 | PyObject *t; |
| 12567 | if (s == NULL || !PyUnicode_Check(s)) |
| 12568 | Py_FatalError( |
| 12569 | "PyUnicode_InternInPlace: unicode strings only please!"); |
| 12570 | /* If it's a subclass, we don't really know what putting |
| 12571 | it in the interned dict might do. */ |
| 12572 | if (!PyUnicode_CheckExact(s)) |
| 12573 | return; |
| 12574 | if (PyUnicode_CHECK_INTERNED(s)) |
| 12575 | return; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12576 | if (PyUnicode_READY(s) == -1) { |
| 12577 | assert(0 && "ready fail in intern..."); |
| 12578 | return; |
| 12579 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12580 | if (interned == NULL) { |
| 12581 | interned = PyDict_New(); |
| 12582 | if (interned == NULL) { |
| 12583 | PyErr_Clear(); /* Don't leave an exception */ |
| 12584 | return; |
| 12585 | } |
| 12586 | } |
| 12587 | /* It might be that the GetItem call fails even |
| 12588 | though the key is present in the dictionary, |
| 12589 | namely when this happens during a stack overflow. */ |
| 12590 | Py_ALLOW_RECURSION |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12591 | t = PyDict_GetItem(interned, (PyObject *)s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12592 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 12593 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12594 | if (t) { |
| 12595 | Py_INCREF(t); |
| 12596 | Py_DECREF(*p); |
| 12597 | *p = t; |
| 12598 | return; |
| 12599 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12600 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12601 | PyThreadState_GET()->recursion_critical = 1; |
| 12602 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 12603 | PyErr_Clear(); |
| 12604 | PyThreadState_GET()->recursion_critical = 0; |
| 12605 | return; |
| 12606 | } |
| 12607 | PyThreadState_GET()->recursion_critical = 0; |
| 12608 | /* The two references in interned are not counted by refcnt. |
| 12609 | The deallocator will take care of this */ |
| 12610 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12611 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12612 | } |
| 12613 | |
| 12614 | void |
| 12615 | PyUnicode_InternImmortal(PyObject **p) |
| 12616 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12617 | PyUnicodeObject *u = (PyUnicodeObject *)*p; |
| 12618 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12619 | PyUnicode_InternInPlace(p); |
| 12620 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12621 | _PyUnicode_STATE(u).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12622 | Py_INCREF(*p); |
| 12623 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12624 | } |
| 12625 | |
| 12626 | PyObject * |
| 12627 | PyUnicode_InternFromString(const char *cp) |
| 12628 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12629 | PyObject *s = PyUnicode_FromString(cp); |
| 12630 | if (s == NULL) |
| 12631 | return NULL; |
| 12632 | PyUnicode_InternInPlace(&s); |
| 12633 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12634 | } |
| 12635 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12636 | void |
| 12637 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12638 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12639 | PyObject *keys; |
| 12640 | PyUnicodeObject *s; |
| 12641 | Py_ssize_t i, n; |
| 12642 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12643 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12644 | if (interned == NULL || !PyDict_Check(interned)) |
| 12645 | return; |
| 12646 | keys = PyDict_Keys(interned); |
| 12647 | if (keys == NULL || !PyList_Check(keys)) { |
| 12648 | PyErr_Clear(); |
| 12649 | return; |
| 12650 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12651 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12652 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 12653 | detector, interned unicode strings are not forcibly deallocated; |
| 12654 | rather, we give them their stolen references back, and then clear |
| 12655 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12656 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12657 | n = PyList_GET_SIZE(keys); |
| 12658 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12659 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12660 | for (i = 0; i < n; i++) { |
| 12661 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12662 | if (PyUnicode_READY(s) == -1) |
| 12663 | fprintf(stderr, "could not ready string\n"); |
| 12664 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12665 | case SSTATE_NOT_INTERNED: |
| 12666 | /* XXX Shouldn't happen */ |
| 12667 | break; |
| 12668 | case SSTATE_INTERNED_IMMORTAL: |
| 12669 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12670 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12671 | break; |
| 12672 | case SSTATE_INTERNED_MORTAL: |
| 12673 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12674 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12675 | break; |
| 12676 | default: |
| 12677 | Py_FatalError("Inconsistent interned string state."); |
| 12678 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12679 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12680 | } |
| 12681 | fprintf(stderr, "total size of all interned strings: " |
| 12682 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 12683 | "mortal/immortal\n", mortal_size, immortal_size); |
| 12684 | Py_DECREF(keys); |
| 12685 | PyDict_Clear(interned); |
| 12686 | Py_DECREF(interned); |
| 12687 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12688 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12689 | |
| 12690 | |
| 12691 | /********************* Unicode Iterator **************************/ |
| 12692 | |
| 12693 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12694 | PyObject_HEAD |
| 12695 | Py_ssize_t it_index; |
| 12696 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12697 | } unicodeiterobject; |
| 12698 | |
| 12699 | static void |
| 12700 | unicodeiter_dealloc(unicodeiterobject *it) |
| 12701 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12702 | _PyObject_GC_UNTRACK(it); |
| 12703 | Py_XDECREF(it->it_seq); |
| 12704 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12705 | } |
| 12706 | |
| 12707 | static int |
| 12708 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 12709 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12710 | Py_VISIT(it->it_seq); |
| 12711 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12712 | } |
| 12713 | |
| 12714 | static PyObject * |
| 12715 | unicodeiter_next(unicodeiterobject *it) |
| 12716 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12717 | PyUnicodeObject *seq; |
| 12718 | PyObject *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12719 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12720 | assert(it != NULL); |
| 12721 | seq = it->it_seq; |
| 12722 | if (seq == NULL) |
| 12723 | return NULL; |
| 12724 | assert(PyUnicode_Check(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12725 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12726 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 12727 | int kind = PyUnicode_KIND(seq); |
| 12728 | void *data = PyUnicode_DATA(seq); |
| 12729 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 12730 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12731 | if (item != NULL) |
| 12732 | ++it->it_index; |
| 12733 | return item; |
| 12734 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12735 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12736 | Py_DECREF(seq); |
| 12737 | it->it_seq = NULL; |
| 12738 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12739 | } |
| 12740 | |
| 12741 | static PyObject * |
| 12742 | unicodeiter_len(unicodeiterobject *it) |
| 12743 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12744 | Py_ssize_t len = 0; |
| 12745 | if (it->it_seq) |
| 12746 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
| 12747 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12748 | } |
| 12749 | |
| 12750 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 12751 | |
| 12752 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12753 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12754 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12755 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12756 | }; |
| 12757 | |
| 12758 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12759 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 12760 | "str_iterator", /* tp_name */ |
| 12761 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 12762 | 0, /* tp_itemsize */ |
| 12763 | /* methods */ |
| 12764 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 12765 | 0, /* tp_print */ |
| 12766 | 0, /* tp_getattr */ |
| 12767 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 12768 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12769 | 0, /* tp_repr */ |
| 12770 | 0, /* tp_as_number */ |
| 12771 | 0, /* tp_as_sequence */ |
| 12772 | 0, /* tp_as_mapping */ |
| 12773 | 0, /* tp_hash */ |
| 12774 | 0, /* tp_call */ |
| 12775 | 0, /* tp_str */ |
| 12776 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 12777 | 0, /* tp_setattro */ |
| 12778 | 0, /* tp_as_buffer */ |
| 12779 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 12780 | 0, /* tp_doc */ |
| 12781 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 12782 | 0, /* tp_clear */ |
| 12783 | 0, /* tp_richcompare */ |
| 12784 | 0, /* tp_weaklistoffset */ |
| 12785 | PyObject_SelfIter, /* tp_iter */ |
| 12786 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 12787 | unicodeiter_methods, /* tp_methods */ |
| 12788 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12789 | }; |
| 12790 | |
| 12791 | static PyObject * |
| 12792 | unicode_iter(PyObject *seq) |
| 12793 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12794 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12795 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12796 | if (!PyUnicode_Check(seq)) { |
| 12797 | PyErr_BadInternalCall(); |
| 12798 | return NULL; |
| 12799 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12800 | if (PyUnicode_READY(seq) == -1) |
| 12801 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12802 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 12803 | if (it == NULL) |
| 12804 | return NULL; |
| 12805 | it->it_index = 0; |
| 12806 | Py_INCREF(seq); |
| 12807 | it->it_seq = (PyUnicodeObject *)seq; |
| 12808 | _PyObject_GC_TRACK(it); |
| 12809 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12810 | } |
| 12811 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12812 | #define UNIOP(x) Py_UNICODE_##x |
| 12813 | #define UNIOP_t Py_UNICODE |
| 12814 | #include "uniops.h" |
| 12815 | #undef UNIOP |
| 12816 | #undef UNIOP_t |
| 12817 | #define UNIOP(x) Py_UCS4_##x |
| 12818 | #define UNIOP_t Py_UCS4 |
| 12819 | #include "uniops.h" |
| 12820 | #undef UNIOP |
| 12821 | #undef UNIOP_t |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 12822 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 12823 | Py_UNICODE* |
Victor Stinner | 4640860 | 2010-09-03 16:18:00 +0000 | [diff] [blame] | 12824 | PyUnicode_AsUnicodeCopy(PyObject *object) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 12825 | { |
| 12826 | PyUnicodeObject *unicode = (PyUnicodeObject *)object; |
| 12827 | Py_UNICODE *copy; |
| 12828 | Py_ssize_t size; |
| 12829 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12830 | if (!PyUnicode_Check(unicode)) { |
| 12831 | PyErr_BadArgument(); |
| 12832 | return NULL; |
| 12833 | } |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 12834 | /* Ensure we won't overflow the size. */ |
| 12835 | if (PyUnicode_GET_SIZE(unicode) > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 12836 | PyErr_NoMemory(); |
| 12837 | return NULL; |
| 12838 | } |
| 12839 | size = PyUnicode_GET_SIZE(unicode) + 1; /* copy the nul character */ |
| 12840 | size *= sizeof(Py_UNICODE); |
| 12841 | copy = PyMem_Malloc(size); |
| 12842 | if (copy == NULL) { |
| 12843 | PyErr_NoMemory(); |
| 12844 | return NULL; |
| 12845 | } |
| 12846 | memcpy(copy, PyUnicode_AS_UNICODE(unicode), size); |
| 12847 | return copy; |
| 12848 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 12849 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 12850 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 12851 | to the string.Formatter class implemented in Python. */ |
| 12852 | |
| 12853 | static PyMethodDef _string_methods[] = { |
| 12854 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 12855 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 12856 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 12857 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 12858 | {NULL, NULL} |
| 12859 | }; |
| 12860 | |
| 12861 | static struct PyModuleDef _string_module = { |
| 12862 | PyModuleDef_HEAD_INIT, |
| 12863 | "_string", |
| 12864 | PyDoc_STR("string helper module"), |
| 12865 | 0, |
| 12866 | _string_methods, |
| 12867 | NULL, |
| 12868 | NULL, |
| 12869 | NULL, |
| 12870 | NULL |
| 12871 | }; |
| 12872 | |
| 12873 | PyMODINIT_FUNC |
| 12874 | PyInit__string(void) |
| 12875 | { |
| 12876 | return PyModule_Create(&_string_module); |
| 12877 | } |
| 12878 | |
| 12879 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12880 | #ifdef __cplusplus |
| 12881 | } |
| 12882 | #endif |