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, |
Fred Drake | 785d14f | 2000-05-09 19:54:43 +0000 | [diff] [blame] | 4 | modified by Marc-Andre Lemburg <mal@lemburg.com> according to the |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5 | Unicode Integration Proposal (see file Misc/unicode.txt). |
| 6 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7 | Major speed upgrades to the method implementations at the Reykjavik |
| 8 | NeedForSpeed sprint, by Fredrik Lundh and Andrew Dalke. |
| 9 | |
Guido van Rossum | 16b1ad9 | 2000-08-03 16:24:25 +0000 | [diff] [blame] | 10 | Copyright (c) Corporation for National Research Initiatives. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 12 | -------------------------------------------------------------------- |
| 13 | The original string type implementation is: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 15 | Copyright (c) 1999 by Secret Labs AB |
| 16 | Copyright (c) 1999 by Fredrik Lundh |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 17 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 18 | By obtaining, using, and/or copying this software and/or its |
| 19 | associated documentation, you agree that you have read, understood, |
| 20 | and will comply with the following terms and conditions: |
| 21 | |
| 22 | Permission to use, copy, modify, and distribute this software and its |
| 23 | associated documentation for any purpose and without fee is hereby |
| 24 | granted, provided that the above copyright notice appears in all |
| 25 | copies, and that both that copyright notice and this permission notice |
| 26 | appear in supporting documentation, and that the name of Secret Labs |
| 27 | AB or the author not be used in advertising or publicity pertaining to |
| 28 | distribution of the software without specific, written prior |
| 29 | permission. |
| 30 | |
| 31 | SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 32 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 33 | FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR |
| 34 | ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 35 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 36 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 37 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 38 | -------------------------------------------------------------------- |
| 39 | |
| 40 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 41 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 42 | #define PY_SSIZE_T_CLEAN |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 43 | #include "Python.h" |
Marc-André Lemburg | d49e5b4 | 2000-06-30 14:58:20 +0000 | [diff] [blame] | 44 | #include "ucnhash.h" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 45 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 46 | #ifdef MS_WINDOWS |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 47 | #include <windows.h> |
| 48 | #endif |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 49 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 50 | /* Limit for the Unicode object free list */ |
| 51 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 52 | #define PyUnicode_MAXFREELIST 1024 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 53 | |
| 54 | /* Limit for the Unicode object free list stay alive optimization. |
| 55 | |
| 56 | The implementation will keep allocated Unicode memory intact for |
| 57 | all objects on the free list having a size less than this |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 58 | limit. This reduces malloc() overhead for small Unicode objects. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 59 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 60 | At worst this will result in PyUnicode_MAXFREELIST * |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 61 | (sizeof(PyUnicodeObject) + KEEPALIVE_SIZE_LIMIT + |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 62 | malloc()-overhead) bytes of unused garbage. |
| 63 | |
| 64 | Setting the limit to 0 effectively turns the feature off. |
| 65 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 66 | Note: This is an experimental feature ! If you get core dumps when |
| 67 | using Unicode objects, turn this feature off. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 68 | |
| 69 | */ |
| 70 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 71 | #define KEEPALIVE_SIZE_LIMIT 9 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 72 | |
| 73 | /* Endianness switches; defaults to little endian */ |
| 74 | |
| 75 | #ifdef WORDS_BIGENDIAN |
| 76 | # define BYTEORDER_IS_BIG_ENDIAN |
| 77 | #else |
| 78 | # define BYTEORDER_IS_LITTLE_ENDIAN |
| 79 | #endif |
| 80 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 81 | /* --- Globals ------------------------------------------------------------ |
| 82 | |
| 83 | The globals are initialized by the _PyUnicode_Init() API and should |
| 84 | not be used before calling that API. |
| 85 | |
| 86 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 87 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 88 | |
| 89 | #ifdef __cplusplus |
| 90 | extern "C" { |
| 91 | #endif |
| 92 | |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 93 | /* Generic helper macro to convert characters of different types. |
| 94 | from_type and to_type have to be valid type names, begin and end |
| 95 | are pointers to the source characters which should be of type |
| 96 | "from_type *". to is a pointer of type "to_type *" and points to the |
| 97 | buffer where the result characters are written to. */ |
| 98 | #define _PyUnicode_CONVERT_BYTES(from_type, to_type, begin, end, to) \ |
| 99 | do { \ |
| 100 | const from_type *iter_; to_type *to_; \ |
| 101 | for (iter_ = (begin), to_ = (to_type *)(to); \ |
| 102 | iter_ < (end); \ |
| 103 | ++iter_, ++to_) { \ |
| 104 | *to_ = (to_type)*iter_; \ |
| 105 | } \ |
| 106 | } while (0) |
| 107 | |
Victor Stinner | bc8b81b | 2011-09-29 19:31:34 +0200 | [diff] [blame] | 108 | #define _PyUnicode_UTF8(op) \ |
| 109 | (PyUnicode_IS_COMPACT_ASCII(op) ? \ |
| 110 | ((char*)((PyASCIIObject*)(op) + 1)) : \ |
| 111 | ((PyCompactUnicodeObject*)(op))->utf8) |
| 112 | #define _PyUnicode_UTF8_LENGTH(op) \ |
| 113 | (PyUnicode_IS_COMPACT_ASCII(op) ? \ |
| 114 | ((PyASCIIObject*)(op))->length : \ |
| 115 | ((PyCompactUnicodeObject*)(op))->utf8_length) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 116 | #define _PyUnicode_WSTR(op) (((PyASCIIObject*)(op))->wstr) |
| 117 | #define _PyUnicode_WSTR_LENGTH(op) (((PyCompactUnicodeObject*)(op))->wstr_length) |
| 118 | #define _PyUnicode_LENGTH(op) (((PyASCIIObject *)(op))->length) |
| 119 | #define _PyUnicode_STATE(op) (((PyASCIIObject *)(op))->state) |
| 120 | #define _PyUnicode_HASH(op) (((PyASCIIObject *)(op))->hash) |
| 121 | #define _PyUnicode_KIND(op) \ |
| 122 | (assert(PyUnicode_Check(op)), \ |
| 123 | ((PyASCIIObject *)(op))->state.kind) |
| 124 | #define _PyUnicode_GET_LENGTH(op) \ |
| 125 | (assert(PyUnicode_Check(op)), \ |
| 126 | ((PyASCIIObject *)(op))->length) |
| 127 | |
Victor Stinner | b15d4d8 | 2011-09-28 23:59:20 +0200 | [diff] [blame] | 128 | /* The Unicode string has been modified: reset the hash */ |
| 129 | #define _PyUnicode_DIRTY(op) do { _PyUnicode_HASH(op) = -1; } while (0) |
| 130 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 131 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 132 | /* This dictionary holds all interned unicode strings. Note that references |
| 133 | to strings in this dictionary are *not* counted in the string's ob_refcnt. |
| 134 | When the interned string reaches a refcnt of 0 the string deallocation |
| 135 | function will delete the reference from this dictionary. |
| 136 | |
| 137 | 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] | 138 | 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] | 139 | */ |
| 140 | static PyObject *interned; |
| 141 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 142 | /* The empty Unicode object is shared to improve performance. */ |
| 143 | static PyUnicodeObject *unicode_empty; |
| 144 | |
| 145 | /* Single character Unicode strings in the Latin-1 range are being |
| 146 | shared as well. */ |
| 147 | static PyUnicodeObject *unicode_latin1[256]; |
| 148 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 149 | /* Fast detection of the most frequent whitespace characters */ |
| 150 | const unsigned char _Py_ascii_whitespace[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 151 | 0, 0, 0, 0, 0, 0, 0, 0, |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 152 | /* case 0x0009: * CHARACTER TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 153 | /* case 0x000A: * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 154 | /* case 0x000B: * LINE TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 155 | /* case 0x000C: * FORM FEED */ |
| 156 | /* case 0x000D: * CARRIAGE RETURN */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 157 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 158 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 159 | /* case 0x001C: * FILE SEPARATOR */ |
| 160 | /* case 0x001D: * GROUP SEPARATOR */ |
| 161 | /* case 0x001E: * RECORD SEPARATOR */ |
| 162 | /* case 0x001F: * UNIT SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 163 | 0, 0, 0, 0, 1, 1, 1, 1, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 164 | /* case 0x0020: * SPACE */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 165 | 1, 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, |
| 168 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 169 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 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, |
| 177 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 178 | }; |
| 179 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 180 | static PyObject * |
| 181 | unicode_encode_call_errorhandler(const char *errors, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 182 | PyObject **errorHandler,const char *encoding, const char *reason, |
| 183 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 184 | Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos); |
| 185 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 186 | static void |
| 187 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 188 | const char *encoding, |
| 189 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 190 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 191 | const char *reason); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 192 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 193 | /* Same for linebreaks */ |
| 194 | static unsigned char ascii_linebreak[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 195 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 196 | /* 0x000A, * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 197 | /* 0x000B, * LINE TABULATION */ |
| 198 | /* 0x000C, * FORM FEED */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 199 | /* 0x000D, * CARRIAGE RETURN */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 200 | 0, 0, 1, 1, 1, 1, 0, 0, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 201 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 202 | /* 0x001C, * FILE SEPARATOR */ |
| 203 | /* 0x001D, * GROUP SEPARATOR */ |
| 204 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 205 | 0, 0, 0, 0, 1, 1, 1, 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, |
| 209 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 210 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 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, |
| 218 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 219 | }; |
| 220 | |
Ezio Melotti | 48a2f8f | 2011-09-29 00:18:19 +0300 | [diff] [blame] | 221 | /* The max unicode value is always 0x10FFFF while using the PEP-393 API. |
| 222 | 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] | 223 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 224 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 225 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 226 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 227 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 228 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 229 | /* This is actually an illegal character, so it should |
| 230 | not be passed to unichr. */ |
| 231 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 232 | #endif |
| 233 | } |
| 234 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 235 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 236 | |
| 237 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 238 | to keep things simple, we use a single bitmask, using the least 5 |
| 239 | bits from each unicode characters as the bit index. */ |
| 240 | |
| 241 | /* the linebreak mask is set up by Unicode_Init below */ |
| 242 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 243 | #if LONG_BIT >= 128 |
| 244 | #define BLOOM_WIDTH 128 |
| 245 | #elif LONG_BIT >= 64 |
| 246 | #define BLOOM_WIDTH 64 |
| 247 | #elif LONG_BIT >= 32 |
| 248 | #define BLOOM_WIDTH 32 |
| 249 | #else |
| 250 | #error "LONG_BIT is smaller than 32" |
| 251 | #endif |
| 252 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 253 | #define BLOOM_MASK unsigned long |
| 254 | |
| 255 | static BLOOM_MASK bloom_linebreak; |
| 256 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 257 | #define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
| 258 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 259 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 260 | #define BLOOM_LINEBREAK(ch) \ |
| 261 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 262 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 263 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 264 | Py_LOCAL_INLINE(BLOOM_MASK) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 265 | make_bloom_mask(int kind, void* ptr, Py_ssize_t len) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 266 | { |
| 267 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 268 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 269 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 270 | Py_ssize_t i; |
| 271 | |
| 272 | mask = 0; |
| 273 | for (i = 0; i < len; i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 274 | BLOOM_ADD(mask, PyUnicode_READ(kind, ptr, i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 275 | |
| 276 | return mask; |
| 277 | } |
| 278 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 279 | #define BLOOM_MEMBER(mask, chr, str) \ |
| 280 | (BLOOM(mask, chr) \ |
| 281 | && (PyUnicode_FindChar(str, chr, 0, PyUnicode_GET_LENGTH(str), 1) >= 0)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 282 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 283 | /* --- Unicode Object ----------------------------------------------------- */ |
| 284 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 285 | static PyObject * |
| 286 | substring(PyUnicodeObject *self, Py_ssize_t start, Py_ssize_t len); |
| 287 | |
| 288 | static PyObject * |
| 289 | fixup(PyUnicodeObject *self, Py_UCS4 (*fixfct)(PyUnicodeObject *s)); |
| 290 | |
| 291 | Py_LOCAL_INLINE(char *) findchar(void *s, int kind, |
| 292 | Py_ssize_t size, Py_UCS4 ch, |
| 293 | int direction) |
| 294 | { |
| 295 | /* like wcschr, but doesn't stop at NULL characters */ |
| 296 | Py_ssize_t i; |
| 297 | if (direction == 1) { |
| 298 | for(i = 0; i < size; i++) |
| 299 | if (PyUnicode_READ(kind, s, i) == ch) |
| 300 | return (char*)s + PyUnicode_KIND_SIZE(kind, i); |
| 301 | } |
| 302 | else { |
| 303 | for(i = size-1; i >= 0; i--) |
| 304 | if (PyUnicode_READ(kind, s, i) == ch) |
| 305 | return (char*)s + PyUnicode_KIND_SIZE(kind, i); |
| 306 | } |
| 307 | return NULL; |
| 308 | } |
| 309 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 310 | static int |
| 311 | unicode_resize(register PyUnicodeObject *unicode, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 312 | Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 313 | { |
| 314 | void *oldstr; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 315 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 316 | /* Resizing is only supported for old unicode objects. */ |
| 317 | assert(!PyUnicode_IS_COMPACT(unicode)); |
| 318 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 319 | |
| 320 | /* ... and only if they have not been readied yet, because |
| 321 | callees usually rely on the wstr representation when resizing. */ |
| 322 | assert(unicode->data.any == NULL); |
| 323 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 324 | /* Shortcut if there's nothing much to do. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 325 | if (_PyUnicode_WSTR_LENGTH(unicode) == length) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 326 | goto reset; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 327 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 328 | /* Resizing shared object (unicode_empty or single character |
| 329 | objects) in-place is not allowed. Use PyUnicode_Resize() |
| 330 | instead ! */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 331 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 332 | if (unicode == unicode_empty || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 333 | (_PyUnicode_WSTR_LENGTH(unicode) == 1 && |
| 334 | _PyUnicode_WSTR(unicode)[0] < 256U && |
| 335 | unicode_latin1[_PyUnicode_WSTR(unicode)[0]] == unicode)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 336 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 337 | "can't resize shared str objects"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 338 | return -1; |
| 339 | } |
| 340 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 341 | /* We allocate one more byte to make sure the string is Ux0000 terminated. |
| 342 | The overallocation is also used by fastsearch, which assumes that it's |
| 343 | safe to look at str[length] (without making any assumptions about what |
| 344 | it contains). */ |
| 345 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 346 | oldstr = _PyUnicode_WSTR(unicode); |
| 347 | _PyUnicode_WSTR(unicode) = PyObject_REALLOC(_PyUnicode_WSTR(unicode), |
| 348 | sizeof(Py_UNICODE) * (length + 1)); |
| 349 | if (!_PyUnicode_WSTR(unicode)) { |
| 350 | _PyUnicode_WSTR(unicode) = (Py_UNICODE *)oldstr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 351 | PyErr_NoMemory(); |
| 352 | return -1; |
| 353 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 354 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 355 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 356 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 357 | reset: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 358 | if (unicode->data.any != NULL) { |
| 359 | PyObject_FREE(unicode->data.any); |
| 360 | if (unicode->_base.utf8 && unicode->_base.utf8 != unicode->data.any) { |
| 361 | PyObject_FREE(unicode->_base.utf8); |
| 362 | } |
| 363 | unicode->_base.utf8 = NULL; |
| 364 | unicode->_base.utf8_length = 0; |
| 365 | unicode->data.any = NULL; |
| 366 | _PyUnicode_LENGTH(unicode) = 0; |
| 367 | _PyUnicode_STATE(unicode).interned = _PyUnicode_STATE(unicode).interned; |
| 368 | _PyUnicode_STATE(unicode).kind = PyUnicode_WCHAR_KIND; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 369 | } |
Victor Stinner | b15d4d8 | 2011-09-28 23:59:20 +0200 | [diff] [blame] | 370 | _PyUnicode_DIRTY(unicode); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 371 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | /* 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] | 376 | Ux0000 terminated; some code (e.g. new_identifier) |
| 377 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 378 | |
| 379 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 380 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 381 | |
| 382 | */ |
| 383 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 384 | #ifdef Py_DEBUG |
| 385 | int unicode_old_new_calls = 0; |
| 386 | #endif |
| 387 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 388 | static PyUnicodeObject * |
| 389 | _PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 390 | { |
| 391 | register PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 392 | size_t new_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 393 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 394 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 395 | if (length == 0 && unicode_empty != NULL) { |
| 396 | Py_INCREF(unicode_empty); |
| 397 | return unicode_empty; |
| 398 | } |
| 399 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 400 | /* Ensure we won't overflow the size. */ |
| 401 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 402 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 403 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 404 | if (length < 0) { |
| 405 | PyErr_SetString(PyExc_SystemError, |
| 406 | "Negative size passed to _PyUnicode_New"); |
| 407 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 410 | #ifdef Py_DEBUG |
| 411 | ++unicode_old_new_calls; |
| 412 | #endif |
| 413 | |
| 414 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
| 415 | if (unicode == NULL) |
| 416 | return NULL; |
| 417 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 418 | _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); |
| 419 | if (!_PyUnicode_WSTR(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 420 | PyErr_NoMemory(); |
| 421 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 422 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 423 | |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 424 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 425 | * the caller fails before initializing str -- unicode_resize() |
| 426 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 427 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 428 | * We don't want unicode_resize to read uninitialized memory in |
| 429 | * that case. |
| 430 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 431 | _PyUnicode_WSTR(unicode)[0] = 0; |
| 432 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 433 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 434 | _PyUnicode_HASH(unicode) = -1; |
| 435 | _PyUnicode_STATE(unicode).interned = 0; |
| 436 | _PyUnicode_STATE(unicode).kind = 0; |
| 437 | _PyUnicode_STATE(unicode).compact = 0; |
| 438 | _PyUnicode_STATE(unicode).ready = 0; |
| 439 | _PyUnicode_STATE(unicode).ascii = 0; |
| 440 | unicode->data.any = NULL; |
| 441 | _PyUnicode_LENGTH(unicode) = 0; |
| 442 | unicode->_base.utf8 = NULL; |
| 443 | unicode->_base.utf8_length = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 444 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 445 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 446 | onError: |
Amaury Forgeot d'Arc | 7888d08 | 2008-08-01 01:06:32 +0000 | [diff] [blame] | 447 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
| 448 | _Py_DEC_REFTOTAL; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 449 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 450 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 451 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 454 | #ifdef Py_DEBUG |
| 455 | int unicode_new_new_calls = 0; |
| 456 | |
| 457 | /* Functions wrapping macros for use in debugger */ |
| 458 | char *_PyUnicode_utf8(void *unicode){ |
| 459 | return _PyUnicode_UTF8(unicode); |
| 460 | } |
| 461 | |
| 462 | void *_PyUnicode_compact_data(void *unicode) { |
| 463 | return _PyUnicode_COMPACT_DATA(unicode); |
| 464 | } |
| 465 | void *_PyUnicode_data(void *unicode){ |
| 466 | printf("obj %p\n", unicode); |
| 467 | printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); |
| 468 | printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); |
| 469 | printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); |
| 470 | printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); |
| 471 | printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); |
| 472 | return PyUnicode_DATA(unicode); |
| 473 | } |
| 474 | #endif |
| 475 | |
| 476 | PyObject * |
| 477 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) |
| 478 | { |
| 479 | PyObject *obj; |
| 480 | PyCompactUnicodeObject *unicode; |
| 481 | void *data; |
| 482 | int kind_state; |
| 483 | int is_sharing = 0, is_ascii = 0; |
| 484 | Py_ssize_t char_size; |
| 485 | Py_ssize_t struct_size; |
| 486 | |
| 487 | /* Optimization for empty strings */ |
| 488 | if (size == 0 && unicode_empty != NULL) { |
| 489 | Py_INCREF(unicode_empty); |
| 490 | return (PyObject *)unicode_empty; |
| 491 | } |
| 492 | |
| 493 | #ifdef Py_DEBUG |
| 494 | ++unicode_new_new_calls; |
| 495 | #endif |
| 496 | |
| 497 | struct_size = sizeof(PyCompactUnicodeObject); |
| 498 | if (maxchar < 128) { |
| 499 | kind_state = PyUnicode_1BYTE_KIND; |
| 500 | char_size = 1; |
| 501 | is_ascii = 1; |
| 502 | struct_size = sizeof(PyASCIIObject); |
| 503 | } |
| 504 | else if (maxchar < 256) { |
| 505 | kind_state = PyUnicode_1BYTE_KIND; |
| 506 | char_size = 1; |
| 507 | } |
| 508 | else if (maxchar < 65536) { |
| 509 | kind_state = PyUnicode_2BYTE_KIND; |
| 510 | char_size = 2; |
| 511 | if (sizeof(wchar_t) == 2) |
| 512 | is_sharing = 1; |
| 513 | } |
| 514 | else { |
| 515 | kind_state = PyUnicode_4BYTE_KIND; |
| 516 | char_size = 4; |
| 517 | if (sizeof(wchar_t) == 4) |
| 518 | is_sharing = 1; |
| 519 | } |
| 520 | |
| 521 | /* Ensure we won't overflow the size. */ |
| 522 | if (size < 0) { |
| 523 | PyErr_SetString(PyExc_SystemError, |
| 524 | "Negative size passed to PyUnicode_New"); |
| 525 | return NULL; |
| 526 | } |
| 527 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) |
| 528 | return PyErr_NoMemory(); |
| 529 | |
| 530 | /* Duplicated allocation code from _PyObject_New() instead of a call to |
| 531 | * PyObject_New() so we are able to allocate space for the object and |
| 532 | * it's data buffer. |
| 533 | */ |
| 534 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); |
| 535 | if (obj == NULL) |
| 536 | return PyErr_NoMemory(); |
| 537 | obj = PyObject_INIT(obj, &PyUnicode_Type); |
| 538 | if (obj == NULL) |
| 539 | return NULL; |
| 540 | |
| 541 | unicode = (PyCompactUnicodeObject *)obj; |
| 542 | if (is_ascii) |
| 543 | data = ((PyASCIIObject*)obj) + 1; |
| 544 | else |
| 545 | data = unicode + 1; |
| 546 | _PyUnicode_LENGTH(unicode) = size; |
| 547 | _PyUnicode_HASH(unicode) = -1; |
| 548 | _PyUnicode_STATE(unicode).interned = 0; |
| 549 | _PyUnicode_STATE(unicode).kind = kind_state; |
| 550 | _PyUnicode_STATE(unicode).compact = 1; |
| 551 | _PyUnicode_STATE(unicode).ready = 1; |
| 552 | _PyUnicode_STATE(unicode).ascii = is_ascii; |
| 553 | if (is_ascii) { |
| 554 | ((char*)data)[size] = 0; |
| 555 | _PyUnicode_WSTR(unicode) = NULL; |
| 556 | } |
| 557 | else if (kind_state == PyUnicode_1BYTE_KIND) { |
| 558 | ((char*)data)[size] = 0; |
| 559 | _PyUnicode_WSTR(unicode) = NULL; |
| 560 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 561 | unicode->utf8_length = 0; |
| 562 | unicode->utf8 = NULL; |
| 563 | } |
| 564 | else { |
| 565 | unicode->utf8 = NULL; |
| 566 | if (kind_state == PyUnicode_2BYTE_KIND) |
| 567 | ((Py_UCS2*)data)[size] = 0; |
| 568 | else /* kind_state == PyUnicode_4BYTE_KIND */ |
| 569 | ((Py_UCS4*)data)[size] = 0; |
| 570 | if (is_sharing) { |
| 571 | _PyUnicode_WSTR_LENGTH(unicode) = size; |
| 572 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; |
| 573 | } |
| 574 | else { |
| 575 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 576 | _PyUnicode_WSTR(unicode) = NULL; |
| 577 | } |
| 578 | } |
| 579 | return obj; |
| 580 | } |
| 581 | |
| 582 | #if SIZEOF_WCHAR_T == 2 |
| 583 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this |
| 584 | will decode surrogate pairs, the other conversions are implemented as macros |
| 585 | for efficency. |
| 586 | |
| 587 | This function assumes that unicode can hold one more code point than wstr |
| 588 | characters for a terminating null character. */ |
| 589 | static int |
| 590 | unicode_convert_wchar_to_ucs4(const wchar_t *begin, const wchar_t *end, |
| 591 | PyUnicodeObject *unicode) |
| 592 | { |
| 593 | const wchar_t *iter; |
| 594 | Py_UCS4 *ucs4_out; |
| 595 | |
| 596 | assert(unicode && PyUnicode_Check(unicode)); |
| 597 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 598 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); |
| 599 | |
| 600 | for (iter = begin; iter < end; ) { |
| 601 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + |
| 602 | _PyUnicode_GET_LENGTH(unicode))); |
| 603 | if (*iter >= 0xD800 && *iter <= 0xDBFF |
| 604 | && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) |
| 605 | { |
| 606 | *ucs4_out++ = (((iter[0] & 0x3FF)<<10) | (iter[1] & 0x3FF)) + 0x10000; |
| 607 | iter += 2; |
| 608 | } |
| 609 | else { |
| 610 | *ucs4_out++ = *iter; |
| 611 | iter++; |
| 612 | } |
| 613 | } |
| 614 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 615 | _PyUnicode_GET_LENGTH(unicode))); |
| 616 | |
| 617 | return 0; |
| 618 | } |
| 619 | #endif |
| 620 | |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 621 | Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 622 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 623 | PyObject *from, Py_ssize_t from_start, |
| 624 | Py_ssize_t how_many) |
| 625 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 626 | unsigned int from_kind, to_kind; |
| 627 | void *from_data, *to_data; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 628 | |
Victor Stinner | b153615 | 2011-09-30 02:26:10 +0200 | [diff] [blame] | 629 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 630 | PyErr_BadInternalCall(); |
| 631 | return -1; |
| 632 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 633 | |
| 634 | if (PyUnicode_READY(from)) |
| 635 | return -1; |
| 636 | if (PyUnicode_READY(to)) |
| 637 | return -1; |
| 638 | |
Victor Stinner | ff9e50f | 2011-09-28 22:17:19 +0200 | [diff] [blame] | 639 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 640 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
| 641 | PyErr_Format(PyExc_ValueError, |
| 642 | "Cannot write %zi characters at %zi " |
| 643 | "in a string of %zi characters", |
| 644 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 645 | return -1; |
| 646 | } |
Victor Stinner | f5ca1a2 | 2011-09-28 23:54:59 +0200 | [diff] [blame] | 647 | if (how_many == 0) |
| 648 | return 0; |
| 649 | |
| 650 | if (Py_REFCNT(to) != 1) { |
| 651 | PyErr_SetString(PyExc_ValueError, |
| 652 | "Cannot modify a string having more than 1 reference"); |
| 653 | return -1; |
| 654 | } |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 655 | _PyUnicode_DIRTY(to); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 656 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 657 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 658 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 659 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 660 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 661 | |
| 662 | if (from_kind == to_kind) { |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 663 | /* fast path */ |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 664 | Py_MEMCPY((char*)to_data |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 665 | + PyUnicode_KIND_SIZE(to_kind, to_start), |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 666 | (char*)from_data |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 667 | + PyUnicode_KIND_SIZE(from_kind, from_start), |
| 668 | PyUnicode_KIND_SIZE(to_kind, how_many)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 669 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 670 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 671 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 672 | { |
| 673 | _PyUnicode_CONVERT_BYTES( |
| 674 | Py_UCS1, Py_UCS2, |
| 675 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 676 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 677 | PyUnicode_2BYTE_DATA(to) + to_start |
| 678 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 679 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 680 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 681 | && to_kind == PyUnicode_4BYTE_KIND) |
| 682 | { |
| 683 | _PyUnicode_CONVERT_BYTES( |
| 684 | Py_UCS1, Py_UCS4, |
| 685 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 686 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 687 | PyUnicode_4BYTE_DATA(to) + to_start |
| 688 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 689 | } |
| 690 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 691 | && to_kind == PyUnicode_4BYTE_KIND) |
| 692 | { |
| 693 | _PyUnicode_CONVERT_BYTES( |
| 694 | Py_UCS2, Py_UCS4, |
| 695 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 696 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 697 | PyUnicode_4BYTE_DATA(to) + to_start |
| 698 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 699 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 700 | else { |
| 701 | int invalid_kinds; |
| 702 | if (from_kind > to_kind) { |
| 703 | /* slow path to check for character overflow */ |
| 704 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 705 | Py_UCS4 ch, maxchar; |
| 706 | Py_ssize_t i; |
| 707 | |
| 708 | maxchar = 0; |
| 709 | invalid_kinds = 0; |
| 710 | for (i=0; i < how_many; i++) { |
| 711 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 712 | if (ch > maxchar) { |
| 713 | maxchar = ch; |
| 714 | if (maxchar > to_maxchar) { |
| 715 | invalid_kinds = 1; |
| 716 | break; |
| 717 | } |
| 718 | } |
| 719 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 720 | } |
| 721 | } |
| 722 | else |
| 723 | invalid_kinds = 1; |
| 724 | if (invalid_kinds) { |
| 725 | PyErr_Format(PyExc_ValueError, |
| 726 | "Cannot copy UCS%u characters " |
| 727 | "into a string of UCS%u characters", |
| 728 | 1 << (from_kind - 1), |
| 729 | 1 << (to_kind -1)); |
| 730 | return -1; |
| 731 | } |
| 732 | } |
| 733 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 734 | } |
| 735 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 736 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 737 | correct string length can be computed before converting a string to UCS4. |
| 738 | This function counts single surrogates as a character and not as a pair. |
| 739 | |
| 740 | Return 0 on success, or -1 on error. */ |
| 741 | static int |
| 742 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 743 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 744 | { |
| 745 | const wchar_t *iter; |
| 746 | |
| 747 | if (num_surrogates == NULL || maxchar == NULL) { |
| 748 | PyErr_SetString(PyExc_SystemError, |
| 749 | "unexpected NULL arguments to " |
| 750 | "PyUnicode_FindMaxCharAndNumSurrogatePairs"); |
| 751 | return -1; |
| 752 | } |
| 753 | |
| 754 | *num_surrogates = 0; |
| 755 | *maxchar = 0; |
| 756 | |
| 757 | for (iter = begin; iter < end; ) { |
| 758 | if (*iter > *maxchar) |
| 759 | *maxchar = *iter; |
| 760 | #if SIZEOF_WCHAR_T == 2 |
| 761 | if (*iter >= 0xD800 && *iter <= 0xDBFF |
| 762 | && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) |
| 763 | { |
| 764 | Py_UCS4 surrogate_val; |
| 765 | surrogate_val = (((iter[0] & 0x3FF)<<10) |
| 766 | | (iter[1] & 0x3FF)) + 0x10000; |
| 767 | ++(*num_surrogates); |
| 768 | if (surrogate_val > *maxchar) |
| 769 | *maxchar = surrogate_val; |
| 770 | iter += 2; |
| 771 | } |
| 772 | else |
| 773 | iter++; |
| 774 | #else |
| 775 | iter++; |
| 776 | #endif |
| 777 | } |
| 778 | return 0; |
| 779 | } |
| 780 | |
| 781 | #ifdef Py_DEBUG |
| 782 | int unicode_ready_calls = 0; |
| 783 | #endif |
| 784 | |
| 785 | int |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 786 | _PyUnicode_Ready(PyObject *obj) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 787 | { |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 788 | PyUnicodeObject *unicode = (PyUnicodeObject *)obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 789 | wchar_t *end; |
| 790 | Py_UCS4 maxchar = 0; |
| 791 | Py_ssize_t num_surrogates; |
| 792 | #if SIZEOF_WCHAR_T == 2 |
| 793 | Py_ssize_t length_wo_surrogates; |
| 794 | #endif |
| 795 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 796 | /* _PyUnicode_Ready() is only intented for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 797 | strings were created using _PyObject_New() and where no canonical |
| 798 | representation (the str field) has been set yet aka strings |
| 799 | which are not yet ready. */ |
| 800 | assert(PyUnicode_Check(obj)); |
| 801 | assert(!PyUnicode_IS_READY(obj)); |
| 802 | assert(!PyUnicode_IS_COMPACT(obj)); |
| 803 | assert(_PyUnicode_KIND(obj) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 804 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 805 | assert(unicode->data.any == NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 806 | assert(unicode->_base.utf8 == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 807 | /* Actually, it should neither be interned nor be anything else: */ |
| 808 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 809 | |
| 810 | #ifdef Py_DEBUG |
| 811 | ++unicode_ready_calls; |
| 812 | #endif |
| 813 | |
| 814 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 815 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 816 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 817 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 818 | |
| 819 | if (maxchar < 256) { |
| 820 | unicode->data.any = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 821 | if (!unicode->data.any) { |
| 822 | PyErr_NoMemory(); |
| 823 | return -1; |
| 824 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 825 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 826 | _PyUnicode_WSTR(unicode), end, |
| 827 | PyUnicode_1BYTE_DATA(unicode)); |
| 828 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 829 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 830 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 831 | if (maxchar < 128) { |
| 832 | unicode->_base.utf8 = unicode->data.any; |
| 833 | unicode->_base.utf8_length = _PyUnicode_WSTR_LENGTH(unicode); |
| 834 | } |
| 835 | else { |
| 836 | unicode->_base.utf8 = NULL; |
| 837 | unicode->_base.utf8_length = 0; |
| 838 | } |
| 839 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 840 | _PyUnicode_WSTR(unicode) = NULL; |
| 841 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 842 | } |
| 843 | /* In this case we might have to convert down from 4-byte native |
| 844 | wchar_t to 2-byte unicode. */ |
| 845 | else if (maxchar < 65536) { |
| 846 | assert(num_surrogates == 0 && |
| 847 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 848 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 849 | #if SIZEOF_WCHAR_T == 2 |
| 850 | /* We can share representations and are done. */ |
| 851 | unicode->data.any = _PyUnicode_WSTR(unicode); |
| 852 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 853 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 854 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
| 855 | unicode->_base.utf8 = NULL; |
| 856 | unicode->_base.utf8_length = 0; |
| 857 | #else |
| 858 | /* sizeof(wchar_t) == 4 */ |
| 859 | unicode->data.any = PyObject_MALLOC( |
| 860 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
| 861 | if (!unicode->data.any) { |
| 862 | PyErr_NoMemory(); |
| 863 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 864 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 865 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 866 | _PyUnicode_WSTR(unicode), end, |
| 867 | PyUnicode_2BYTE_DATA(unicode)); |
| 868 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 869 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 870 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
| 871 | unicode->_base.utf8 = NULL; |
| 872 | unicode->_base.utf8_length = 0; |
| 873 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 874 | _PyUnicode_WSTR(unicode) = NULL; |
| 875 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 876 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 877 | } |
| 878 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 879 | else { |
| 880 | #if SIZEOF_WCHAR_T == 2 |
| 881 | /* in case the native representation is 2-bytes, we need to allocate a |
| 882 | new normalized 4-byte version. */ |
| 883 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
| 884 | unicode->data.any = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 885 | if (!unicode->data.any) { |
| 886 | PyErr_NoMemory(); |
| 887 | return -1; |
| 888 | } |
| 889 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 890 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 891 | unicode->_base.utf8 = NULL; |
| 892 | unicode->_base.utf8_length = 0; |
| 893 | if (unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, |
| 894 | unicode) < 0) { |
| 895 | assert(0 && "ConvertWideCharToUCS4 failed"); |
| 896 | return -1; |
| 897 | } |
| 898 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 899 | _PyUnicode_WSTR(unicode) = NULL; |
| 900 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 901 | #else |
| 902 | assert(num_surrogates == 0); |
| 903 | |
| 904 | unicode->data.any = _PyUnicode_WSTR(unicode); |
| 905 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 906 | unicode->_base.utf8 = NULL; |
| 907 | unicode->_base.utf8_length = 0; |
| 908 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 909 | #endif |
| 910 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 911 | } |
| 912 | _PyUnicode_STATE(unicode).ready = 1; |
| 913 | return 0; |
| 914 | } |
| 915 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 916 | static void |
| 917 | unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 918 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 919 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 920 | case SSTATE_NOT_INTERNED: |
| 921 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 922 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 923 | case SSTATE_INTERNED_MORTAL: |
| 924 | /* revive dead object temporarily for DelItem */ |
| 925 | Py_REFCNT(unicode) = 3; |
| 926 | if (PyDict_DelItem(interned, (PyObject *)unicode) != 0) |
| 927 | Py_FatalError( |
| 928 | "deletion of interned string failed"); |
| 929 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 930 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 931 | case SSTATE_INTERNED_IMMORTAL: |
| 932 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 933 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 934 | default: |
| 935 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 936 | } |
| 937 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 938 | if (_PyUnicode_WSTR(unicode) && |
| 939 | (!PyUnicode_IS_READY(unicode) || |
| 940 | _PyUnicode_WSTR(unicode) != PyUnicode_DATA(unicode))) |
| 941 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
| 942 | if (_PyUnicode_UTF8(unicode) && _PyUnicode_UTF8(unicode) != PyUnicode_DATA(unicode)) |
| 943 | PyObject_DEL(unicode->_base.utf8); |
| 944 | |
| 945 | if (PyUnicode_IS_COMPACT(unicode)) { |
| 946 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 947 | } |
| 948 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 949 | if (unicode->data.any) |
| 950 | PyObject_DEL(unicode->data.any); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 951 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 952 | } |
| 953 | } |
| 954 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 955 | static int |
| 956 | _PyUnicode_Resize(PyUnicodeObject **unicode, Py_ssize_t length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 957 | { |
| 958 | register PyUnicodeObject *v; |
| 959 | |
| 960 | /* Argument checks */ |
| 961 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 962 | PyErr_BadInternalCall(); |
| 963 | return -1; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 964 | } |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 965 | v = *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 966 | if (v == NULL || !PyUnicode_Check(v) || Py_REFCNT(v) != 1 || length < 0 || |
| 967 | PyUnicode_IS_COMPACT(v) || _PyUnicode_WSTR(v) == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 968 | PyErr_BadInternalCall(); |
| 969 | return -1; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | /* Resizing unicode_empty and single character objects is not |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 973 | possible since these are being shared. |
| 974 | The same goes for new-representation unicode objects or objects which |
| 975 | have already been readied. |
| 976 | For these, we simply return a fresh copy with the same Unicode content. |
| 977 | */ |
| 978 | if ((_PyUnicode_WSTR_LENGTH(v) != length && |
| 979 | (v == unicode_empty || _PyUnicode_WSTR_LENGTH(v) == 1)) || |
| 980 | PyUnicode_IS_COMPACT(v) || v->data.any) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 981 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 982 | if (w == NULL) |
| 983 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 984 | Py_UNICODE_COPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(v), |
| 985 | length < _PyUnicode_WSTR_LENGTH(v) ? length : _PyUnicode_WSTR_LENGTH(v)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 986 | Py_DECREF(*unicode); |
| 987 | *unicode = w; |
| 988 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 992 | objects, since we can modify them in-place. */ |
| 993 | return unicode_resize(v, length); |
| 994 | } |
| 995 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 996 | int |
| 997 | PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 998 | { |
| 999 | return _PyUnicode_Resize((PyUnicodeObject **)unicode, length); |
| 1000 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1001 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1002 | static PyObject* |
| 1003 | get_latin1_char(unsigned char ch) |
| 1004 | { |
| 1005 | PyUnicodeObject *unicode = unicode_latin1[ch]; |
| 1006 | if (!unicode) { |
| 1007 | unicode = (PyUnicodeObject *)PyUnicode_New(1, ch); |
| 1008 | if (!unicode) |
| 1009 | return NULL; |
| 1010 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
| 1011 | unicode_latin1[ch] = unicode; |
| 1012 | } |
| 1013 | Py_INCREF(unicode); |
| 1014 | return (PyObject *)unicode; |
| 1015 | } |
| 1016 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1017 | PyObject * |
| 1018 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1019 | { |
| 1020 | PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1021 | Py_UCS4 maxchar = 0; |
| 1022 | Py_ssize_t num_surrogates; |
| 1023 | |
| 1024 | if (u == NULL) |
| 1025 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1026 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1027 | /* If the Unicode data is known at construction time, we can apply |
| 1028 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1029 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1030 | /* Optimization for empty strings */ |
| 1031 | if (size == 0 && unicode_empty != NULL) { |
| 1032 | Py_INCREF(unicode_empty); |
| 1033 | return (PyObject *)unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1034 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1035 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1036 | /* Single character Unicode objects in the Latin-1 range are |
| 1037 | shared when using this constructor */ |
| 1038 | if (size == 1 && *u < 256) |
| 1039 | return get_latin1_char((unsigned char)*u); |
| 1040 | |
| 1041 | /* If not empty and not single character, copy the Unicode data |
| 1042 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1043 | if (find_maxchar_surrogates(u, u + size, |
| 1044 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1045 | return NULL; |
| 1046 | |
| 1047 | unicode = (PyUnicodeObject *) PyUnicode_New(size - num_surrogates, |
| 1048 | maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1049 | if (!unicode) |
| 1050 | return NULL; |
| 1051 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1052 | switch (PyUnicode_KIND(unicode)) { |
| 1053 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1054 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1055 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1056 | break; |
| 1057 | case PyUnicode_2BYTE_KIND: |
| 1058 | #if Py_UNICODE_SIZE == 2 |
| 1059 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1060 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1061 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1062 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1063 | #endif |
| 1064 | break; |
| 1065 | case PyUnicode_4BYTE_KIND: |
| 1066 | #if SIZEOF_WCHAR_T == 2 |
| 1067 | /* This is the only case which has to process surrogates, thus |
| 1068 | a simple copy loop is not enough and we need a function. */ |
| 1069 | if (unicode_convert_wchar_to_ucs4(u, u + size, unicode) < 0) { |
| 1070 | Py_DECREF(unicode); |
| 1071 | return NULL; |
| 1072 | } |
| 1073 | #else |
| 1074 | assert(num_surrogates == 0); |
| 1075 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1076 | #endif |
| 1077 | break; |
| 1078 | default: |
| 1079 | assert(0 && "Impossible state"); |
| 1080 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1081 | |
| 1082 | return (PyObject *)unicode; |
| 1083 | } |
| 1084 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1085 | PyObject * |
| 1086 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1087 | { |
| 1088 | PyUnicodeObject *unicode; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1089 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1090 | if (size < 0) { |
| 1091 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1092 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1093 | return NULL; |
| 1094 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1095 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1096 | /* 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] | 1097 | some optimizations which share commonly used objects. |
| 1098 | Also, this means the input must be UTF-8, so fall back to the |
| 1099 | UTF-8 decoder at the end. */ |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1100 | if (u != NULL) { |
| 1101 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1102 | /* Optimization for empty strings */ |
| 1103 | if (size == 0 && unicode_empty != NULL) { |
| 1104 | Py_INCREF(unicode_empty); |
| 1105 | return (PyObject *)unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1106 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1107 | |
| 1108 | /* Single characters are shared when using this constructor. |
| 1109 | Restrict to ASCII, since the input must be UTF-8. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1110 | if (size == 1 && Py_CHARMASK(*u) < 128) |
| 1111 | return get_latin1_char(Py_CHARMASK(*u)); |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 1112 | |
| 1113 | return PyUnicode_DecodeUTF8(u, size, NULL); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
Walter Dörwald | 5550731 | 2007-05-18 13:12:10 +0000 | [diff] [blame] | 1116 | unicode = _PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1117 | if (!unicode) |
| 1118 | return NULL; |
| 1119 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1120 | return (PyObject *)unicode; |
| 1121 | } |
| 1122 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1123 | PyObject * |
| 1124 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1125 | { |
| 1126 | size_t size = strlen(u); |
| 1127 | if (size > PY_SSIZE_T_MAX) { |
| 1128 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1129 | return NULL; |
| 1130 | } |
| 1131 | |
| 1132 | return PyUnicode_FromStringAndSize(u, size); |
| 1133 | } |
| 1134 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1135 | static PyObject* |
| 1136 | _PyUnicode_FromUCS1(const unsigned char* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1137 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1138 | PyObject *res; |
| 1139 | unsigned char max = 127; |
| 1140 | Py_ssize_t i; |
| 1141 | for (i = 0; i < size; i++) { |
| 1142 | if (u[i] & 0x80) { |
| 1143 | max = 255; |
| 1144 | break; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1145 | } |
| 1146 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1147 | res = PyUnicode_New(size, max); |
| 1148 | if (!res) |
| 1149 | return NULL; |
| 1150 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
| 1151 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1154 | static PyObject* |
| 1155 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1156 | { |
| 1157 | PyObject *res; |
| 1158 | Py_UCS2 max = 0; |
| 1159 | Py_ssize_t i; |
| 1160 | for (i = 0; i < size; i++) |
| 1161 | if (u[i] > max) |
| 1162 | max = u[i]; |
| 1163 | res = PyUnicode_New(size, max); |
| 1164 | if (!res) |
| 1165 | return NULL; |
| 1166 | if (max >= 256) |
| 1167 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
| 1168 | else |
| 1169 | for (i = 0; i < size; i++) |
| 1170 | PyUnicode_1BYTE_DATA(res)[i] = (Py_UCS1)u[i]; |
| 1171 | return res; |
| 1172 | } |
| 1173 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1174 | static PyObject* |
| 1175 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1176 | { |
| 1177 | PyObject *res; |
| 1178 | Py_UCS4 max = 0; |
| 1179 | Py_ssize_t i; |
| 1180 | for (i = 0; i < size; i++) |
| 1181 | if (u[i] > max) |
| 1182 | max = u[i]; |
| 1183 | res = PyUnicode_New(size, max); |
| 1184 | if (!res) |
| 1185 | return NULL; |
| 1186 | if (max >= 0x10000) |
| 1187 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
| 1188 | else { |
| 1189 | int kind = PyUnicode_KIND(res); |
| 1190 | void *data = PyUnicode_DATA(res); |
| 1191 | for (i = 0; i < size; i++) |
| 1192 | PyUnicode_WRITE(kind, data, i, u[i]); |
| 1193 | } |
| 1194 | return res; |
| 1195 | } |
| 1196 | |
| 1197 | PyObject* |
| 1198 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 1199 | { |
| 1200 | switch(kind) { |
| 1201 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1202 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1203 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1204 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1205 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1206 | return _PyUnicode_FromUCS4(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1207 | } |
| 1208 | assert(0); |
| 1209 | return NULL; |
| 1210 | } |
| 1211 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1212 | PyObject* |
| 1213 | PyUnicode_Copy(PyObject *unicode) |
| 1214 | { |
| 1215 | if (!PyUnicode_Check(unicode)) { |
| 1216 | PyErr_BadInternalCall(); |
| 1217 | return NULL; |
| 1218 | } |
| 1219 | if (PyUnicode_READY(unicode)) |
| 1220 | return NULL; |
| 1221 | return PyUnicode_FromKindAndData(PyUnicode_KIND(unicode), |
| 1222 | PyUnicode_DATA(unicode), |
| 1223 | PyUnicode_GET_LENGTH(unicode)); |
| 1224 | } |
| 1225 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1226 | |
| 1227 | /* Widen Unicode objects to larger buffers. |
| 1228 | Return NULL if the string is too wide already. */ |
| 1229 | |
| 1230 | void* |
| 1231 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 1232 | { |
| 1233 | Py_ssize_t i; |
| 1234 | Py_ssize_t len = PyUnicode_GET_LENGTH(s); |
| 1235 | void *d = PyUnicode_DATA(s); |
| 1236 | unsigned int skind = PyUnicode_KIND(s); |
| 1237 | if (PyUnicode_KIND(s) >= kind) { |
| 1238 | PyErr_SetString(PyExc_RuntimeError, "invalid widening attempt"); |
| 1239 | return NULL; |
| 1240 | } |
| 1241 | switch(kind) { |
| 1242 | case PyUnicode_2BYTE_KIND: { |
| 1243 | Py_UCS2 *result = PyMem_Malloc(PyUnicode_GET_LENGTH(s) * sizeof(Py_UCS2)); |
| 1244 | if (!result) { |
| 1245 | PyErr_NoMemory(); |
| 1246 | return 0; |
| 1247 | } |
| 1248 | for (i = 0; i < len; i++) |
| 1249 | result[i] = ((Py_UCS1*)d)[i]; |
| 1250 | return result; |
| 1251 | } |
| 1252 | case PyUnicode_4BYTE_KIND: { |
| 1253 | Py_UCS4 *result = PyMem_Malloc(PyUnicode_GET_LENGTH(s) * sizeof(Py_UCS4)); |
| 1254 | if (!result) { |
| 1255 | PyErr_NoMemory(); |
| 1256 | return 0; |
| 1257 | } |
| 1258 | for (i = 0; i < len; i++) |
| 1259 | result[i] = PyUnicode_READ(skind, d, i); |
| 1260 | return result; |
| 1261 | } |
| 1262 | } |
| 1263 | Py_FatalError("invalid kind"); |
| 1264 | return NULL; |
| 1265 | } |
| 1266 | |
| 1267 | static Py_UCS4* |
| 1268 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 1269 | int copy_null) |
| 1270 | { |
| 1271 | int kind; |
| 1272 | void *data; |
| 1273 | Py_ssize_t len, targetlen; |
| 1274 | if (PyUnicode_READY(string) == -1) |
| 1275 | return NULL; |
| 1276 | kind = PyUnicode_KIND(string); |
| 1277 | data = PyUnicode_DATA(string); |
| 1278 | len = PyUnicode_GET_LENGTH(string); |
| 1279 | targetlen = len; |
| 1280 | if (copy_null) |
| 1281 | targetlen++; |
| 1282 | if (!target) { |
| 1283 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 1284 | PyErr_NoMemory(); |
| 1285 | return NULL; |
| 1286 | } |
| 1287 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 1288 | if (!target) { |
| 1289 | PyErr_NoMemory(); |
| 1290 | return NULL; |
| 1291 | } |
| 1292 | } |
| 1293 | else { |
| 1294 | if (targetsize < targetlen) { |
| 1295 | PyErr_Format(PyExc_SystemError, |
| 1296 | "string is longer than the buffer"); |
| 1297 | if (copy_null && 0 < targetsize) |
| 1298 | target[0] = 0; |
| 1299 | return NULL; |
| 1300 | } |
| 1301 | } |
| 1302 | if (kind != PyUnicode_4BYTE_KIND) { |
| 1303 | Py_ssize_t i; |
| 1304 | for (i = 0; i < len; i++) |
| 1305 | target[i] = PyUnicode_READ(kind, data, i); |
| 1306 | } |
| 1307 | else |
| 1308 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
| 1309 | if (copy_null) |
| 1310 | target[len] = 0; |
| 1311 | return target; |
| 1312 | } |
| 1313 | |
| 1314 | Py_UCS4* |
| 1315 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 1316 | int copy_null) |
| 1317 | { |
| 1318 | if (target == NULL || targetsize < 1) { |
| 1319 | PyErr_BadInternalCall(); |
| 1320 | return NULL; |
| 1321 | } |
| 1322 | return as_ucs4(string, target, targetsize, copy_null); |
| 1323 | } |
| 1324 | |
| 1325 | Py_UCS4* |
| 1326 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 1327 | { |
| 1328 | return as_ucs4(string, NULL, 0, 1); |
| 1329 | } |
| 1330 | |
| 1331 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1332 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1333 | PyObject * |
| 1334 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1335 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1336 | if (w == NULL) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 1337 | if (size == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1338 | return PyUnicode_New(0, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1339 | PyErr_BadInternalCall(); |
| 1340 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1341 | } |
| 1342 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 1343 | if (size == -1) { |
| 1344 | size = wcslen(w); |
| 1345 | } |
| 1346 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1347 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1348 | } |
| 1349 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1350 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1351 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1352 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1353 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 1354 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1355 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1356 | *fmt++ = '%'; |
| 1357 | if (width) { |
| 1358 | if (zeropad) |
| 1359 | *fmt++ = '0'; |
| 1360 | fmt += sprintf(fmt, "%d", width); |
| 1361 | } |
| 1362 | if (precision) |
| 1363 | fmt += sprintf(fmt, ".%d", precision); |
| 1364 | if (longflag) |
| 1365 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1366 | else if (longlongflag) { |
| 1367 | /* longlongflag should only ever be nonzero on machines with |
| 1368 | HAVE_LONG_LONG defined */ |
| 1369 | #ifdef HAVE_LONG_LONG |
| 1370 | char *f = PY_FORMAT_LONG_LONG; |
| 1371 | while (*f) |
| 1372 | *fmt++ = *f++; |
| 1373 | #else |
| 1374 | /* we shouldn't ever get here */ |
| 1375 | assert(0); |
| 1376 | *fmt++ = 'l'; |
| 1377 | #endif |
| 1378 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1379 | else if (size_tflag) { |
| 1380 | char *f = PY_FORMAT_SIZE_T; |
| 1381 | while (*f) |
| 1382 | *fmt++ = *f++; |
| 1383 | } |
| 1384 | *fmt++ = c; |
| 1385 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1388 | /* helper for PyUnicode_FromFormatV() */ |
| 1389 | |
| 1390 | static const char* |
| 1391 | parse_format_flags(const char *f, |
| 1392 | int *p_width, int *p_precision, |
| 1393 | int *p_longflag, int *p_longlongflag, int *p_size_tflag) |
| 1394 | { |
| 1395 | int width, precision, longflag, longlongflag, size_tflag; |
| 1396 | |
| 1397 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ |
| 1398 | f++; |
| 1399 | width = 0; |
| 1400 | while (Py_ISDIGIT((unsigned)*f)) |
| 1401 | width = (width*10) + *f++ - '0'; |
| 1402 | precision = 0; |
| 1403 | if (*f == '.') { |
| 1404 | f++; |
| 1405 | while (Py_ISDIGIT((unsigned)*f)) |
| 1406 | precision = (precision*10) + *f++ - '0'; |
| 1407 | if (*f == '%') { |
| 1408 | /* "%.3%s" => f points to "3" */ |
| 1409 | f--; |
| 1410 | } |
| 1411 | } |
| 1412 | if (*f == '\0') { |
| 1413 | /* bogus format "%.1" => go backward, f points to "1" */ |
| 1414 | f--; |
| 1415 | } |
| 1416 | if (p_width != NULL) |
| 1417 | *p_width = width; |
| 1418 | if (p_precision != NULL) |
| 1419 | *p_precision = precision; |
| 1420 | |
| 1421 | /* Handle %ld, %lu, %lld and %llu. */ |
| 1422 | longflag = 0; |
| 1423 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 1424 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1425 | |
| 1426 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 1427 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1428 | longflag = 1; |
| 1429 | ++f; |
| 1430 | } |
| 1431 | #ifdef HAVE_LONG_LONG |
| 1432 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 1433 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1434 | longlongflag = 1; |
| 1435 | f += 2; |
| 1436 | } |
| 1437 | #endif |
| 1438 | } |
| 1439 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 1440 | 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] | 1441 | size_tflag = 1; |
| 1442 | ++f; |
| 1443 | } |
| 1444 | if (p_longflag != NULL) |
| 1445 | *p_longflag = longflag; |
| 1446 | if (p_longlongflag != NULL) |
| 1447 | *p_longlongflag = longlongflag; |
| 1448 | if (p_size_tflag != NULL) |
| 1449 | *p_size_tflag = size_tflag; |
| 1450 | return f; |
| 1451 | } |
| 1452 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1453 | /* maximum number of characters required for output of %ld. 21 characters |
| 1454 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 1455 | #define MAX_LONG_CHARS 21 |
| 1456 | /* maximum number of characters required for output of %lld. |
| 1457 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 1458 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 1459 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 1460 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1461 | PyObject * |
| 1462 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 1463 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1464 | va_list count; |
| 1465 | Py_ssize_t callcount = 0; |
| 1466 | PyObject **callresults = NULL; |
| 1467 | PyObject **callresult = NULL; |
| 1468 | Py_ssize_t n = 0; |
| 1469 | int width = 0; |
| 1470 | int precision = 0; |
| 1471 | int zeropad; |
| 1472 | const char* f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1473 | PyUnicodeObject *string; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1474 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1475 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1476 | Py_UCS4 maxchar = 127; /* result is ASCII by default */ |
| 1477 | Py_UCS4 argmaxchar; |
| 1478 | Py_ssize_t numbersize = 0; |
| 1479 | char *numberresults = NULL; |
| 1480 | char *numberresult = NULL; |
| 1481 | Py_ssize_t i; |
| 1482 | int kind; |
| 1483 | void *data; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1484 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 1485 | Py_VA_COPY(count, vargs); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1486 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 1487 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 1488 | * 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] | 1489 | * result in an array) |
| 1490 | * also esimate a upper bound for all the number formats in the string, |
| 1491 | * numbers will be formated in step 3 and be keept in a '\0'-separated |
| 1492 | * buffer before putting everything together. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1493 | for (f = format; *f; f++) { |
| 1494 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1495 | int longlongflag; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1496 | /* skip width or width.precision (eg. "1.2" of "%1.2f") */ |
| 1497 | f = parse_format_flags(f, &width, NULL, NULL, &longlongflag, NULL); |
| 1498 | if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V') |
| 1499 | ++callcount; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1500 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1501 | else if (*f == 'd' || *f=='u' || *f=='i' || *f=='x' || *f=='p') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1502 | #ifdef HAVE_LONG_LONG |
| 1503 | if (longlongflag) { |
| 1504 | if (width < MAX_LONG_LONG_CHARS) |
| 1505 | width = MAX_LONG_LONG_CHARS; |
| 1506 | } |
| 1507 | else |
| 1508 | #endif |
| 1509 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 1510 | including sign. Decimal takes the most space. This |
| 1511 | isn't enough for octal. If a width is specified we |
| 1512 | need more (which we allocate later). */ |
| 1513 | if (width < MAX_LONG_CHARS) |
| 1514 | width = MAX_LONG_CHARS; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1515 | |
| 1516 | /* account for the size + '\0' to separate numbers |
| 1517 | inside of the numberresults buffer */ |
| 1518 | numbersize += (width + 1); |
| 1519 | } |
| 1520 | } |
| 1521 | else if ((unsigned char)*f > 127) { |
| 1522 | PyErr_Format(PyExc_ValueError, |
| 1523 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 1524 | "string, got a non-ASCII byte: 0x%02x", |
| 1525 | (unsigned char)*f); |
| 1526 | return NULL; |
| 1527 | } |
| 1528 | } |
| 1529 | /* step 2: allocate memory for the results of |
| 1530 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
| 1531 | if (callcount) { |
| 1532 | callresults = PyObject_Malloc(sizeof(PyObject *) * callcount); |
| 1533 | if (!callresults) { |
| 1534 | PyErr_NoMemory(); |
| 1535 | return NULL; |
| 1536 | } |
| 1537 | callresult = callresults; |
| 1538 | } |
| 1539 | /* step 2.5: allocate memory for the results of formating numbers */ |
| 1540 | if (numbersize) { |
| 1541 | numberresults = PyObject_Malloc(numbersize); |
| 1542 | if (!numberresults) { |
| 1543 | PyErr_NoMemory(); |
| 1544 | goto fail; |
| 1545 | } |
| 1546 | numberresult = numberresults; |
| 1547 | } |
| 1548 | |
| 1549 | /* step 3: format numbers and figure out how large a buffer we need */ |
| 1550 | for (f = format; *f; f++) { |
| 1551 | if (*f == '%') { |
| 1552 | const char* p; |
| 1553 | int longflag; |
| 1554 | int longlongflag; |
| 1555 | int size_tflag; |
| 1556 | int numprinted; |
| 1557 | |
| 1558 | p = f; |
| 1559 | zeropad = (f[1] == '0'); |
| 1560 | f = parse_format_flags(f, &width, &precision, |
| 1561 | &longflag, &longlongflag, &size_tflag); |
| 1562 | switch (*f) { |
| 1563 | case 'c': |
| 1564 | { |
| 1565 | Py_UCS4 ordinal = va_arg(count, int); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 1566 | maxchar = Py_MAX(maxchar, ordinal); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1567 | n++; |
| 1568 | break; |
| 1569 | } |
| 1570 | case '%': |
| 1571 | n++; |
| 1572 | break; |
| 1573 | case 'i': |
| 1574 | case 'd': |
| 1575 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 1576 | width, precision, *f); |
| 1577 | if (longflag) |
| 1578 | numprinted = sprintf(numberresult, fmt, |
| 1579 | va_arg(count, long)); |
| 1580 | #ifdef HAVE_LONG_LONG |
| 1581 | else if (longlongflag) |
| 1582 | numprinted = sprintf(numberresult, fmt, |
| 1583 | va_arg(count, PY_LONG_LONG)); |
| 1584 | #endif |
| 1585 | else if (size_tflag) |
| 1586 | numprinted = sprintf(numberresult, fmt, |
| 1587 | va_arg(count, Py_ssize_t)); |
| 1588 | else |
| 1589 | numprinted = sprintf(numberresult, fmt, |
| 1590 | va_arg(count, int)); |
| 1591 | n += numprinted; |
| 1592 | /* advance by +1 to skip over the '\0' */ |
| 1593 | numberresult += (numprinted + 1); |
| 1594 | assert(*(numberresult - 1) == '\0'); |
| 1595 | assert(*(numberresult - 2) != '\0'); |
| 1596 | assert(numprinted >= 0); |
| 1597 | assert(numberresult <= numberresults + numbersize); |
| 1598 | break; |
| 1599 | case 'u': |
| 1600 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 1601 | width, precision, 'u'); |
| 1602 | if (longflag) |
| 1603 | numprinted = sprintf(numberresult, fmt, |
| 1604 | va_arg(count, unsigned long)); |
| 1605 | #ifdef HAVE_LONG_LONG |
| 1606 | else if (longlongflag) |
| 1607 | numprinted = sprintf(numberresult, fmt, |
| 1608 | va_arg(count, unsigned PY_LONG_LONG)); |
| 1609 | #endif |
| 1610 | else if (size_tflag) |
| 1611 | numprinted = sprintf(numberresult, fmt, |
| 1612 | va_arg(count, size_t)); |
| 1613 | else |
| 1614 | numprinted = sprintf(numberresult, fmt, |
| 1615 | va_arg(count, unsigned int)); |
| 1616 | n += numprinted; |
| 1617 | numberresult += (numprinted + 1); |
| 1618 | assert(*(numberresult - 1) == '\0'); |
| 1619 | assert(*(numberresult - 2) != '\0'); |
| 1620 | assert(numprinted >= 0); |
| 1621 | assert(numberresult <= numberresults + numbersize); |
| 1622 | break; |
| 1623 | case 'x': |
| 1624 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
| 1625 | numprinted = sprintf(numberresult, fmt, va_arg(count, int)); |
| 1626 | n += numprinted; |
| 1627 | numberresult += (numprinted + 1); |
| 1628 | assert(*(numberresult - 1) == '\0'); |
| 1629 | assert(*(numberresult - 2) != '\0'); |
| 1630 | assert(numprinted >= 0); |
| 1631 | assert(numberresult <= numberresults + numbersize); |
| 1632 | break; |
| 1633 | case 'p': |
| 1634 | numprinted = sprintf(numberresult, "%p", va_arg(count, void*)); |
| 1635 | /* %p is ill-defined: ensure leading 0x. */ |
| 1636 | if (numberresult[1] == 'X') |
| 1637 | numberresult[1] = 'x'; |
| 1638 | else if (numberresult[1] != 'x') { |
| 1639 | memmove(numberresult + 2, numberresult, |
| 1640 | strlen(numberresult) + 1); |
| 1641 | numberresult[0] = '0'; |
| 1642 | numberresult[1] = 'x'; |
| 1643 | numprinted += 2; |
| 1644 | } |
| 1645 | n += numprinted; |
| 1646 | numberresult += (numprinted + 1); |
| 1647 | assert(*(numberresult - 1) == '\0'); |
| 1648 | assert(*(numberresult - 2) != '\0'); |
| 1649 | assert(numprinted >= 0); |
| 1650 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1651 | break; |
| 1652 | case 's': |
| 1653 | { |
| 1654 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 1655 | const char *s = va_arg(count, const char*); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1656 | PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); |
| 1657 | if (!str) |
| 1658 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1659 | /* since PyUnicode_DecodeUTF8 returns already flexible |
| 1660 | unicode objects, there is no need to call ready on them */ |
| 1661 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 1662 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1663 | n += PyUnicode_GET_LENGTH(str); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1664 | /* Remember the str and switch to the next slot */ |
| 1665 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1666 | break; |
| 1667 | } |
| 1668 | case 'U': |
| 1669 | { |
| 1670 | PyObject *obj = va_arg(count, PyObject *); |
| 1671 | assert(obj && PyUnicode_Check(obj)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1672 | if (PyUnicode_READY(obj) == -1) |
| 1673 | goto fail; |
| 1674 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 1675 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1676 | n += PyUnicode_GET_LENGTH(obj); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1677 | break; |
| 1678 | } |
| 1679 | case 'V': |
| 1680 | { |
| 1681 | PyObject *obj = va_arg(count, PyObject *); |
| 1682 | const char *str = va_arg(count, const char *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1683 | PyObject *str_obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1684 | assert(obj || str); |
| 1685 | assert(!obj || PyUnicode_Check(obj)); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1686 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1687 | if (PyUnicode_READY(obj) == -1) |
| 1688 | goto fail; |
| 1689 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 1690 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1691 | n += PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1692 | *callresult++ = NULL; |
| 1693 | } |
| 1694 | else { |
| 1695 | str_obj = PyUnicode_DecodeUTF8(str, strlen(str), "replace"); |
| 1696 | if (!str_obj) |
| 1697 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1698 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 1699 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1700 | n += PyUnicode_GET_LENGTH(str_obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1701 | *callresult++ = str_obj; |
| 1702 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1703 | break; |
| 1704 | } |
| 1705 | case 'S': |
| 1706 | { |
| 1707 | PyObject *obj = va_arg(count, PyObject *); |
| 1708 | PyObject *str; |
| 1709 | assert(obj); |
| 1710 | str = PyObject_Str(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1711 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1712 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1713 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 1714 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1715 | n += PyUnicode_GET_LENGTH(str); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1716 | /* Remember the str and switch to the next slot */ |
| 1717 | *callresult++ = str; |
| 1718 | break; |
| 1719 | } |
| 1720 | case 'R': |
| 1721 | { |
| 1722 | PyObject *obj = va_arg(count, PyObject *); |
| 1723 | PyObject *repr; |
| 1724 | assert(obj); |
| 1725 | repr = PyObject_Repr(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1726 | if (!repr || PyUnicode_READY(repr) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1727 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1728 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(repr); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 1729 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1730 | n += PyUnicode_GET_LENGTH(repr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1731 | /* Remember the repr and switch to the next slot */ |
| 1732 | *callresult++ = repr; |
| 1733 | break; |
| 1734 | } |
| 1735 | case 'A': |
| 1736 | { |
| 1737 | PyObject *obj = va_arg(count, PyObject *); |
| 1738 | PyObject *ascii; |
| 1739 | assert(obj); |
| 1740 | ascii = PyObject_ASCII(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1741 | if (!ascii || PyUnicode_READY(ascii) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1742 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1743 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(ascii); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 1744 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1745 | n += PyUnicode_GET_LENGTH(ascii); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1746 | /* Remember the repr and switch to the next slot */ |
| 1747 | *callresult++ = ascii; |
| 1748 | break; |
| 1749 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1750 | default: |
| 1751 | /* if we stumble upon an unknown |
| 1752 | formatting code, copy the rest of |
| 1753 | the format string to the output |
| 1754 | string. (we cannot just skip the |
| 1755 | code, since there's no way to know |
| 1756 | what's in the argument list) */ |
| 1757 | n += strlen(p); |
| 1758 | goto expand; |
| 1759 | } |
| 1760 | } else |
| 1761 | n++; |
| 1762 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1763 | expand: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1764 | /* step 4: fill the buffer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1765 | /* Since we've analyzed how much space we need, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1766 | we don't have to resize the string. |
| 1767 | There can be no errors beyond this point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1768 | string = (PyUnicodeObject *)PyUnicode_New(n, maxchar); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1769 | if (!string) |
| 1770 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1771 | kind = PyUnicode_KIND(string); |
| 1772 | data = PyUnicode_DATA(string); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1773 | callresult = callresults; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1774 | numberresult = numberresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1775 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1776 | for (i = 0, f = format; *f; f++) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1777 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1778 | const char* p; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1779 | |
| 1780 | p = f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1781 | f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL); |
| 1782 | /* checking for == because the last argument could be a empty |
| 1783 | string, which causes i to point to end, the assert at the end of |
| 1784 | the loop */ |
| 1785 | assert(i <= PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1786 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1787 | switch (*f) { |
| 1788 | case 'c': |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 1789 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1790 | const int ordinal = va_arg(vargs, int); |
| 1791 | PyUnicode_WRITE(kind, data, i++, ordinal); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1792 | break; |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 1793 | } |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 1794 | case 'i': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1795 | case 'd': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1796 | case 'u': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1797 | case 'x': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1798 | case 'p': |
| 1799 | /* unused, since we already have the result */ |
| 1800 | if (*f == 'p') |
| 1801 | (void) va_arg(vargs, void *); |
| 1802 | else |
| 1803 | (void) va_arg(vargs, int); |
| 1804 | /* extract the result from numberresults and append. */ |
| 1805 | for (; *numberresult; ++i, ++numberresult) |
| 1806 | PyUnicode_WRITE(kind, data, i, *numberresult); |
| 1807 | /* skip over the separating '\0' */ |
| 1808 | assert(*numberresult == '\0'); |
| 1809 | numberresult++; |
| 1810 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1811 | break; |
| 1812 | case 's': |
| 1813 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1814 | /* unused, since we already have the result */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1815 | Py_ssize_t size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1816 | (void) va_arg(vargs, char *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1817 | size = PyUnicode_GET_LENGTH(*callresult); |
| 1818 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 1819 | if (PyUnicode_CopyCharacters((PyObject*)string, i, |
| 1820 | *callresult, 0, |
| 1821 | size) < 0) |
| 1822 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1823 | i += size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1824 | /* We're done with the unicode()/repr() => forget it */ |
| 1825 | Py_DECREF(*callresult); |
| 1826 | /* switch to next unicode()/repr() result */ |
| 1827 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1828 | break; |
| 1829 | } |
| 1830 | case 'U': |
| 1831 | { |
| 1832 | PyObject *obj = va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1833 | Py_ssize_t size; |
| 1834 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
| 1835 | size = PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 1836 | if (PyUnicode_CopyCharacters((PyObject*)string, i, |
| 1837 | obj, 0, |
| 1838 | size) < 0) |
| 1839 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1840 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1841 | break; |
| 1842 | } |
| 1843 | case 'V': |
| 1844 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1845 | Py_ssize_t size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1846 | PyObject *obj = va_arg(vargs, PyObject *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1847 | va_arg(vargs, const char *); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1848 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1849 | size = PyUnicode_GET_LENGTH(obj); |
| 1850 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 1851 | if (PyUnicode_CopyCharacters((PyObject*)string, i, |
| 1852 | obj, 0, |
| 1853 | size) < 0) |
| 1854 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1855 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1856 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1857 | size = PyUnicode_GET_LENGTH(*callresult); |
| 1858 | assert(PyUnicode_KIND(*callresult) <= |
| 1859 | PyUnicode_KIND(string)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 1860 | if (PyUnicode_CopyCharacters((PyObject*)string, i, |
| 1861 | *callresult, |
| 1862 | 0, size) < 0) |
| 1863 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1864 | i += size; |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1865 | Py_DECREF(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1866 | } |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1867 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1868 | break; |
| 1869 | } |
| 1870 | case 'S': |
| 1871 | case 'R': |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 1872 | case 'A': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1873 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1874 | /* unused, since we already have the result */ |
| 1875 | (void) va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1876 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 1877 | if (PyUnicode_CopyCharacters((PyObject*)string, i, |
| 1878 | *callresult, 0, |
| 1879 | PyUnicode_GET_LENGTH(*callresult)) < 0) |
| 1880 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1881 | i += PyUnicode_GET_LENGTH(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1882 | /* We're done with the unicode()/repr() => forget it */ |
| 1883 | Py_DECREF(*callresult); |
| 1884 | /* switch to next unicode()/repr() result */ |
| 1885 | ++callresult; |
| 1886 | break; |
| 1887 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1888 | case '%': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1889 | PyUnicode_WRITE(kind, data, i++, '%'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1890 | break; |
| 1891 | default: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1892 | for (; *p; ++p, ++i) |
| 1893 | PyUnicode_WRITE(kind, data, i, *p); |
| 1894 | assert(i == PyUnicode_GET_LENGTH(string)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1895 | goto end; |
| 1896 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 1897 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1898 | else { |
| 1899 | assert(i < PyUnicode_GET_LENGTH(string)); |
| 1900 | PyUnicode_WRITE(kind, data, i++, *f); |
| 1901 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1902 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1903 | assert(i == PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1904 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1905 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1906 | if (callresults) |
| 1907 | PyObject_Free(callresults); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1908 | if (numberresults) |
| 1909 | PyObject_Free(numberresults); |
| 1910 | return (PyObject *)string; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1911 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1912 | if (callresults) { |
| 1913 | PyObject **callresult2 = callresults; |
| 1914 | while (callresult2 < callresult) { |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1915 | Py_XDECREF(*callresult2); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1916 | ++callresult2; |
| 1917 | } |
| 1918 | PyObject_Free(callresults); |
| 1919 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1920 | if (numberresults) |
| 1921 | PyObject_Free(numberresults); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1922 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1923 | } |
| 1924 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1925 | PyObject * |
| 1926 | PyUnicode_FromFormat(const char *format, ...) |
| 1927 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1928 | PyObject* ret; |
| 1929 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1930 | |
| 1931 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1932 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1933 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1934 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1935 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1936 | ret = PyUnicode_FromFormatV(format, vargs); |
| 1937 | va_end(vargs); |
| 1938 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1939 | } |
| 1940 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1941 | #ifdef HAVE_WCHAR_H |
| 1942 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1943 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 1944 | convert a Unicode object to a wide character string. |
| 1945 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 1946 | - 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] | 1947 | character) required to convert the unicode object. Ignore size argument. |
| 1948 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 1949 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1950 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 1951 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1952 | static Py_ssize_t |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1953 | unicode_aswidechar(PyUnicodeObject *unicode, |
| 1954 | wchar_t *w, |
| 1955 | Py_ssize_t size) |
| 1956 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1957 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1958 | const wchar_t *wstr; |
| 1959 | |
| 1960 | wstr = PyUnicode_AsUnicodeAndSize((PyObject *)unicode, &res); |
| 1961 | if (wstr == NULL) |
| 1962 | return -1; |
| 1963 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1964 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1965 | if (size > res) |
| 1966 | size = res + 1; |
| 1967 | else |
| 1968 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1969 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1970 | return res; |
| 1971 | } |
| 1972 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1973 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1974 | } |
| 1975 | |
| 1976 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 1977 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1978 | wchar_t *w, |
| 1979 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1980 | { |
| 1981 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1982 | PyErr_BadInternalCall(); |
| 1983 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1984 | } |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 1985 | return unicode_aswidechar((PyUnicodeObject*)unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1986 | } |
| 1987 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1988 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 1989 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1990 | Py_ssize_t *size) |
| 1991 | { |
| 1992 | wchar_t* buffer; |
| 1993 | Py_ssize_t buflen; |
| 1994 | |
| 1995 | if (unicode == NULL) { |
| 1996 | PyErr_BadInternalCall(); |
| 1997 | return NULL; |
| 1998 | } |
| 1999 | |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2000 | buflen = unicode_aswidechar((PyUnicodeObject *)unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2001 | if (buflen == -1) |
| 2002 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2003 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2004 | PyErr_NoMemory(); |
| 2005 | return NULL; |
| 2006 | } |
| 2007 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2008 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2009 | if (buffer == NULL) { |
| 2010 | PyErr_NoMemory(); |
| 2011 | return NULL; |
| 2012 | } |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2013 | buflen = unicode_aswidechar((PyUnicodeObject *)unicode, buffer, buflen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2014 | if (buflen == -1) |
| 2015 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2016 | if (size != NULL) |
| 2017 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2018 | return buffer; |
| 2019 | } |
| 2020 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2021 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2022 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2023 | PyObject * |
| 2024 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2025 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2026 | PyObject *v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2027 | if (ordinal < 0 || ordinal > 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2028 | PyErr_SetString(PyExc_ValueError, |
| 2029 | "chr() arg not in range(0x110000)"); |
| 2030 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2031 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2032 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2033 | if (ordinal < 256) |
| 2034 | return get_latin1_char(ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2035 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2036 | v = PyUnicode_New(1, ordinal); |
| 2037 | if (v == NULL) |
| 2038 | return NULL; |
| 2039 | PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal); |
| 2040 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2041 | } |
| 2042 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2043 | PyObject * |
| 2044 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2045 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2046 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2047 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2048 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2049 | Py_INCREF(obj); |
| 2050 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2051 | } |
| 2052 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2053 | /* For a Unicode subtype that's not a Unicode object, |
| 2054 | return a true Unicode object with the same data. */ |
Victor Stinner | 2219e0a | 2011-10-01 01:16:59 +0200 | [diff] [blame^] | 2055 | return PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2056 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2057 | PyErr_Format(PyExc_TypeError, |
| 2058 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2059 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2060 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2061 | } |
| 2062 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2063 | PyObject * |
| 2064 | PyUnicode_FromEncodedObject(register PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2065 | const char *encoding, |
| 2066 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2067 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2068 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2069 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2070 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2071 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2072 | PyErr_BadInternalCall(); |
| 2073 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2074 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2075 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2076 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2077 | if (PyBytes_Check(obj)) { |
| 2078 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2079 | Py_INCREF(unicode_empty); |
| 2080 | v = (PyObject *) unicode_empty; |
| 2081 | } |
| 2082 | else { |
| 2083 | v = PyUnicode_Decode( |
| 2084 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 2085 | encoding, errors); |
| 2086 | } |
| 2087 | return v; |
| 2088 | } |
| 2089 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2090 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2091 | PyErr_SetString(PyExc_TypeError, |
| 2092 | "decoding str is not supported"); |
| 2093 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2094 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2095 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2096 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 2097 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 2098 | PyErr_Format(PyExc_TypeError, |
| 2099 | "coercing to str: need bytes, bytearray " |
| 2100 | "or buffer-like object, %.80s found", |
| 2101 | Py_TYPE(obj)->tp_name); |
| 2102 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2103 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2104 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2105 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2106 | Py_INCREF(unicode_empty); |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2107 | v = (PyObject *) unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2108 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2109 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2110 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2111 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2112 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2113 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2114 | } |
| 2115 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2116 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2117 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 2118 | 1 on success. */ |
| 2119 | static int |
| 2120 | normalize_encoding(const char *encoding, |
| 2121 | char *lower, |
| 2122 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2123 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2124 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2125 | char *l; |
| 2126 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2127 | |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2128 | e = encoding; |
| 2129 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2130 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2131 | while (*e) { |
| 2132 | if (l == l_end) |
| 2133 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2134 | if (Py_ISUPPER(*e)) { |
| 2135 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2136 | } |
| 2137 | else if (*e == '_') { |
| 2138 | *l++ = '-'; |
| 2139 | e++; |
| 2140 | } |
| 2141 | else { |
| 2142 | *l++ = *e++; |
| 2143 | } |
| 2144 | } |
| 2145 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2146 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2147 | } |
| 2148 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2149 | PyObject * |
| 2150 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2151 | Py_ssize_t size, |
| 2152 | const char *encoding, |
| 2153 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2154 | { |
| 2155 | PyObject *buffer = NULL, *unicode; |
| 2156 | Py_buffer info; |
| 2157 | char lower[11]; /* Enough for any encoding shortcut */ |
| 2158 | |
| 2159 | if (encoding == NULL) |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2160 | return PyUnicode_DecodeUTF8(s, size, errors); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2161 | |
| 2162 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2163 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2164 | if ((strcmp(lower, "utf-8") == 0) || |
| 2165 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2166 | return PyUnicode_DecodeUTF8(s, size, errors); |
| 2167 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2168 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2169 | (strcmp(lower, "iso-8859-1") == 0)) |
| 2170 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2171 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2172 | else if (strcmp(lower, "mbcs") == 0) |
| 2173 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2174 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2175 | else if (strcmp(lower, "ascii") == 0) |
| 2176 | return PyUnicode_DecodeASCII(s, size, errors); |
| 2177 | else if (strcmp(lower, "utf-16") == 0) |
| 2178 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 2179 | else if (strcmp(lower, "utf-32") == 0) |
| 2180 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 2181 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2182 | |
| 2183 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 2184 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 2185 | 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] | 2186 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 2187 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2188 | if (buffer == NULL) |
| 2189 | goto onError; |
| 2190 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 2191 | if (unicode == NULL) |
| 2192 | goto onError; |
| 2193 | if (!PyUnicode_Check(unicode)) { |
| 2194 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2195 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2196 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2197 | Py_DECREF(unicode); |
| 2198 | goto onError; |
| 2199 | } |
| 2200 | Py_DECREF(buffer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2201 | if (PyUnicode_READY(unicode)) { |
| 2202 | Py_DECREF(unicode); |
| 2203 | return NULL; |
| 2204 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2205 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2206 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2207 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2208 | Py_XDECREF(buffer); |
| 2209 | return NULL; |
| 2210 | } |
| 2211 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2212 | PyObject * |
| 2213 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2214 | const char *encoding, |
| 2215 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2216 | { |
| 2217 | PyObject *v; |
| 2218 | |
| 2219 | if (!PyUnicode_Check(unicode)) { |
| 2220 | PyErr_BadArgument(); |
| 2221 | goto onError; |
| 2222 | } |
| 2223 | |
| 2224 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2225 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2226 | |
| 2227 | /* Decode via the codec registry */ |
| 2228 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2229 | if (v == NULL) |
| 2230 | goto onError; |
| 2231 | return v; |
| 2232 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2233 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2234 | return NULL; |
| 2235 | } |
| 2236 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2237 | PyObject * |
| 2238 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2239 | const char *encoding, |
| 2240 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2241 | { |
| 2242 | PyObject *v; |
| 2243 | |
| 2244 | if (!PyUnicode_Check(unicode)) { |
| 2245 | PyErr_BadArgument(); |
| 2246 | goto onError; |
| 2247 | } |
| 2248 | |
| 2249 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2250 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2251 | |
| 2252 | /* Decode via the codec registry */ |
| 2253 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2254 | if (v == NULL) |
| 2255 | goto onError; |
| 2256 | if (!PyUnicode_Check(v)) { |
| 2257 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2258 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2259 | Py_TYPE(v)->tp_name); |
| 2260 | Py_DECREF(v); |
| 2261 | goto onError; |
| 2262 | } |
| 2263 | return v; |
| 2264 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2265 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2266 | return NULL; |
| 2267 | } |
| 2268 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2269 | PyObject * |
| 2270 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2271 | Py_ssize_t size, |
| 2272 | const char *encoding, |
| 2273 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2274 | { |
| 2275 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2276 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2277 | unicode = PyUnicode_FromUnicode(s, size); |
| 2278 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2279 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2280 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 2281 | Py_DECREF(unicode); |
| 2282 | return v; |
| 2283 | } |
| 2284 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2285 | PyObject * |
| 2286 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2287 | const char *encoding, |
| 2288 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2289 | { |
| 2290 | PyObject *v; |
| 2291 | |
| 2292 | if (!PyUnicode_Check(unicode)) { |
| 2293 | PyErr_BadArgument(); |
| 2294 | goto onError; |
| 2295 | } |
| 2296 | |
| 2297 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2298 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2299 | |
| 2300 | /* Encode via the codec registry */ |
| 2301 | v = PyCodec_Encode(unicode, encoding, errors); |
| 2302 | if (v == NULL) |
| 2303 | goto onError; |
| 2304 | return v; |
| 2305 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2306 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2307 | return NULL; |
| 2308 | } |
| 2309 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2310 | PyObject * |
| 2311 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2312 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2313 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2314 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 2315 | PyUnicode_GET_SIZE(unicode), |
| 2316 | NULL); |
| 2317 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2318 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2319 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 2320 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 2321 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 2322 | cannot use it to encode and decode filenames before it is loaded. Load |
| 2323 | the Python codec requires to encode at least its own filename. Use the C |
| 2324 | version of the locale codec until the codec registry is initialized and |
| 2325 | the Python codec is loaded. |
| 2326 | |
| 2327 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 2328 | cannot only rely on it: check also interp->fscodec_initialized for |
| 2329 | subinterpreters. */ |
| 2330 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2331 | return PyUnicode_AsEncodedString(unicode, |
| 2332 | Py_FileSystemDefaultEncoding, |
| 2333 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 2334 | } |
| 2335 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2336 | /* locale encoding with surrogateescape */ |
| 2337 | wchar_t *wchar; |
| 2338 | char *bytes; |
| 2339 | PyObject *bytes_obj; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 2340 | size_t error_pos; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2341 | |
| 2342 | wchar = PyUnicode_AsWideCharString(unicode, NULL); |
| 2343 | if (wchar == NULL) |
| 2344 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 2345 | bytes = _Py_wchar2char(wchar, &error_pos); |
| 2346 | if (bytes == NULL) { |
| 2347 | if (error_pos != (size_t)-1) { |
| 2348 | char *errmsg = strerror(errno); |
| 2349 | PyObject *exc = NULL; |
| 2350 | if (errmsg == NULL) |
| 2351 | errmsg = "Py_wchar2char() failed"; |
| 2352 | raise_encode_exception(&exc, |
| 2353 | "filesystemencoding", |
| 2354 | PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), |
| 2355 | error_pos, error_pos+1, |
| 2356 | errmsg); |
| 2357 | Py_XDECREF(exc); |
| 2358 | } |
| 2359 | else |
| 2360 | PyErr_NoMemory(); |
| 2361 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2362 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 2363 | } |
| 2364 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2365 | |
| 2366 | bytes_obj = PyBytes_FromString(bytes); |
| 2367 | PyMem_Free(bytes); |
| 2368 | return bytes_obj; |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 2369 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2370 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2371 | } |
| 2372 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2373 | PyObject * |
| 2374 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2375 | const char *encoding, |
| 2376 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2377 | { |
| 2378 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2379 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2380 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2381 | if (!PyUnicode_Check(unicode)) { |
| 2382 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2383 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2384 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2385 | |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2386 | if (encoding == NULL) { |
| 2387 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2388 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2389 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2390 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2391 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2392 | |
| 2393 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2394 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2395 | if ((strcmp(lower, "utf-8") == 0) || |
| 2396 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 2397 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2398 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2399 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2400 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2401 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 2402 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2403 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2404 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2405 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2406 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2407 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2408 | else if (strcmp(lower, "mbcs") == 0) |
| 2409 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 2410 | PyUnicode_GET_SIZE(unicode), |
| 2411 | errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2412 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2413 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2414 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2415 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2416 | |
| 2417 | /* Encode via the codec registry */ |
| 2418 | v = PyCodec_Encode(unicode, encoding, errors); |
| 2419 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2420 | return NULL; |
| 2421 | |
| 2422 | /* The normal path */ |
| 2423 | if (PyBytes_Check(v)) |
| 2424 | return v; |
| 2425 | |
| 2426 | /* 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] | 2427 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2428 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2429 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2430 | |
| 2431 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 2432 | "encoder %s returned bytearray instead of bytes", |
| 2433 | encoding); |
| 2434 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2435 | Py_DECREF(v); |
| 2436 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2437 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2438 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2439 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 2440 | Py_DECREF(v); |
| 2441 | return b; |
| 2442 | } |
| 2443 | |
| 2444 | PyErr_Format(PyExc_TypeError, |
| 2445 | "encoder did not return a bytes object (type=%.400s)", |
| 2446 | Py_TYPE(v)->tp_name); |
| 2447 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2448 | return NULL; |
| 2449 | } |
| 2450 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2451 | PyObject * |
| 2452 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2453 | const char *encoding, |
| 2454 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2455 | { |
| 2456 | PyObject *v; |
| 2457 | |
| 2458 | if (!PyUnicode_Check(unicode)) { |
| 2459 | PyErr_BadArgument(); |
| 2460 | goto onError; |
| 2461 | } |
| 2462 | |
| 2463 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2464 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2465 | |
| 2466 | /* Encode via the codec registry */ |
| 2467 | v = PyCodec_Encode(unicode, encoding, errors); |
| 2468 | if (v == NULL) |
| 2469 | goto onError; |
| 2470 | if (!PyUnicode_Check(v)) { |
| 2471 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2472 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2473 | Py_TYPE(v)->tp_name); |
| 2474 | Py_DECREF(v); |
| 2475 | goto onError; |
| 2476 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2477 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2478 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2479 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2480 | return NULL; |
| 2481 | } |
| 2482 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2483 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 2484 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2485 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 2486 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 2487 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2488 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 2489 | PyObject* |
| 2490 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 2491 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2492 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2493 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 2494 | #elif defined(__APPLE__) |
| 2495 | return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); |
| 2496 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 2497 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 2498 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 2499 | cannot use it to encode and decode filenames before it is loaded. Load |
| 2500 | the Python codec requires to encode at least its own filename. Use the C |
| 2501 | version of the locale codec until the codec registry is initialized and |
| 2502 | the Python codec is loaded. |
| 2503 | |
| 2504 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 2505 | cannot only rely on it: check also interp->fscodec_initialized for |
| 2506 | subinterpreters. */ |
| 2507 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2508 | return PyUnicode_Decode(s, size, |
| 2509 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 2510 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2511 | } |
| 2512 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2513 | /* locale encoding with surrogateescape */ |
| 2514 | wchar_t *wchar; |
| 2515 | PyObject *unicode; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 2516 | size_t len; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2517 | |
| 2518 | if (s[size] != '\0' || size != strlen(s)) { |
| 2519 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 2520 | return NULL; |
| 2521 | } |
| 2522 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 2523 | wchar = _Py_char2wchar(s, &len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2524 | if (wchar == NULL) |
Victor Stinner | d5af0a5 | 2010-11-08 23:34:29 +0000 | [diff] [blame] | 2525 | return PyErr_NoMemory(); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2526 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 2527 | unicode = PyUnicode_FromWideChar(wchar, len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2528 | PyMem_Free(wchar); |
| 2529 | return unicode; |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2530 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2531 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2532 | } |
| 2533 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2534 | |
| 2535 | int |
| 2536 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 2537 | { |
| 2538 | PyObject *output = NULL; |
| 2539 | Py_ssize_t size; |
| 2540 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 2541 | if (arg == NULL) { |
| 2542 | Py_DECREF(*(PyObject**)addr); |
| 2543 | return 1; |
| 2544 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2545 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2546 | output = arg; |
| 2547 | Py_INCREF(output); |
| 2548 | } |
| 2549 | else { |
| 2550 | arg = PyUnicode_FromObject(arg); |
| 2551 | if (!arg) |
| 2552 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2553 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2554 | Py_DECREF(arg); |
| 2555 | if (!output) |
| 2556 | return 0; |
| 2557 | if (!PyBytes_Check(output)) { |
| 2558 | Py_DECREF(output); |
| 2559 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 2560 | return 0; |
| 2561 | } |
| 2562 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 2563 | size = PyBytes_GET_SIZE(output); |
| 2564 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2565 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 2566 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2567 | Py_DECREF(output); |
| 2568 | return 0; |
| 2569 | } |
| 2570 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 2571 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2572 | } |
| 2573 | |
| 2574 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 2575 | int |
| 2576 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 2577 | { |
| 2578 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 2579 | if (arg == NULL) { |
| 2580 | Py_DECREF(*(PyObject**)addr); |
| 2581 | return 1; |
| 2582 | } |
| 2583 | if (PyUnicode_Check(arg)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2584 | if (PyUnicode_READY(arg)) |
| 2585 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 2586 | output = arg; |
| 2587 | Py_INCREF(output); |
| 2588 | } |
| 2589 | else { |
| 2590 | arg = PyBytes_FromObject(arg); |
| 2591 | if (!arg) |
| 2592 | return 0; |
| 2593 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 2594 | PyBytes_GET_SIZE(arg)); |
| 2595 | Py_DECREF(arg); |
| 2596 | if (!output) |
| 2597 | return 0; |
| 2598 | if (!PyUnicode_Check(output)) { |
| 2599 | Py_DECREF(output); |
| 2600 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 2601 | return 0; |
| 2602 | } |
| 2603 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2604 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
| 2605 | PyUnicode_GET_LENGTH(output), 0, 1)) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 2606 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 2607 | Py_DECREF(output); |
| 2608 | return 0; |
| 2609 | } |
| 2610 | *(PyObject**)addr = output; |
| 2611 | return Py_CLEANUP_SUPPORTED; |
| 2612 | } |
| 2613 | |
| 2614 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2615 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2616 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2617 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 2618 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2619 | PyUnicodeObject *u = (PyUnicodeObject *)unicode; |
| 2620 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 2621 | if (!PyUnicode_Check(unicode)) { |
| 2622 | PyErr_BadArgument(); |
| 2623 | return NULL; |
| 2624 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2625 | if (PyUnicode_READY(u) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2626 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2627 | |
| 2628 | if (_PyUnicode_UTF8(unicode) == NULL) { |
| 2629 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 2630 | if (bytes == NULL) |
| 2631 | return NULL; |
| 2632 | u->_base.utf8 = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 2633 | if (u->_base.utf8 == NULL) { |
| 2634 | Py_DECREF(bytes); |
| 2635 | return NULL; |
| 2636 | } |
| 2637 | u->_base.utf8_length = PyBytes_GET_SIZE(bytes); |
| 2638 | Py_MEMCPY(u->_base.utf8, PyBytes_AS_STRING(bytes), u->_base.utf8_length + 1); |
| 2639 | Py_DECREF(bytes); |
| 2640 | } |
| 2641 | |
| 2642 | if (psize) |
| 2643 | *psize = _PyUnicode_UTF8_LENGTH(unicode); |
| 2644 | return _PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 2645 | } |
| 2646 | |
| 2647 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2648 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 2649 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2650 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 2651 | } |
| 2652 | |
| 2653 | #ifdef Py_DEBUG |
| 2654 | int unicode_as_unicode_calls = 0; |
| 2655 | #endif |
| 2656 | |
| 2657 | |
| 2658 | Py_UNICODE * |
| 2659 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 2660 | { |
| 2661 | PyUnicodeObject *u; |
| 2662 | const unsigned char *one_byte; |
| 2663 | #if SIZEOF_WCHAR_T == 4 |
| 2664 | const Py_UCS2 *two_bytes; |
| 2665 | #else |
| 2666 | const Py_UCS4 *four_bytes; |
| 2667 | const Py_UCS4 *ucs4_end; |
| 2668 | Py_ssize_t num_surrogates; |
| 2669 | #endif |
| 2670 | wchar_t *w; |
| 2671 | wchar_t *wchar_end; |
| 2672 | |
| 2673 | if (!PyUnicode_Check(unicode)) { |
| 2674 | PyErr_BadArgument(); |
| 2675 | return NULL; |
| 2676 | } |
| 2677 | u = (PyUnicodeObject*)unicode; |
| 2678 | if (_PyUnicode_WSTR(u) == NULL) { |
| 2679 | /* Non-ASCII compact unicode object */ |
| 2680 | assert(_PyUnicode_KIND(u) != 0); |
| 2681 | assert(PyUnicode_IS_READY(u)); |
| 2682 | |
| 2683 | #ifdef Py_DEBUG |
| 2684 | ++unicode_as_unicode_calls; |
| 2685 | #endif |
| 2686 | |
| 2687 | if (PyUnicode_KIND(u) == PyUnicode_4BYTE_KIND) { |
| 2688 | #if SIZEOF_WCHAR_T == 2 |
| 2689 | four_bytes = PyUnicode_4BYTE_DATA(u); |
| 2690 | ucs4_end = four_bytes + _PyUnicode_LENGTH(u); |
| 2691 | num_surrogates = 0; |
| 2692 | |
| 2693 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 2694 | if (*four_bytes > 0xFFFF) |
| 2695 | ++num_surrogates; |
| 2696 | } |
| 2697 | |
| 2698 | _PyUnicode_WSTR(u) = (wchar_t *) PyObject_MALLOC( |
| 2699 | sizeof(wchar_t) * (_PyUnicode_LENGTH(u) + 1 + num_surrogates)); |
| 2700 | if (!_PyUnicode_WSTR(u)) { |
| 2701 | PyErr_NoMemory(); |
| 2702 | return NULL; |
| 2703 | } |
| 2704 | _PyUnicode_WSTR_LENGTH(u) = _PyUnicode_LENGTH(u) + num_surrogates; |
| 2705 | |
| 2706 | w = _PyUnicode_WSTR(u); |
| 2707 | wchar_end = w + _PyUnicode_WSTR_LENGTH(u); |
| 2708 | four_bytes = PyUnicode_4BYTE_DATA(u); |
| 2709 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 2710 | if (*four_bytes > 0xFFFF) { |
| 2711 | /* encode surrogate pair in this case */ |
| 2712 | *w++ = 0xD800 | ((*four_bytes - 0x10000) >> 10); |
| 2713 | *w = 0xDC00 | ((*four_bytes - 0x10000) & 0x3FF); |
| 2714 | } |
| 2715 | else |
| 2716 | *w = *four_bytes; |
| 2717 | |
| 2718 | if (w > wchar_end) { |
| 2719 | assert(0 && "Miscalculated string end"); |
| 2720 | } |
| 2721 | } |
| 2722 | *w = 0; |
| 2723 | #else |
| 2724 | /* sizeof(wchar_t) == 4 */ |
| 2725 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 2726 | "should share memory already."); |
| 2727 | return NULL; |
| 2728 | #endif |
| 2729 | } |
| 2730 | else { |
| 2731 | _PyUnicode_WSTR(u) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 2732 | (_PyUnicode_LENGTH(u) + 1)); |
| 2733 | if (!_PyUnicode_WSTR(u)) { |
| 2734 | PyErr_NoMemory(); |
| 2735 | return NULL; |
| 2736 | } |
| 2737 | if (!PyUnicode_IS_COMPACT_ASCII(u)) |
| 2738 | _PyUnicode_WSTR_LENGTH(u) = _PyUnicode_LENGTH(u); |
| 2739 | w = _PyUnicode_WSTR(u); |
| 2740 | wchar_end = w + _PyUnicode_LENGTH(u); |
| 2741 | |
| 2742 | if (PyUnicode_KIND(u) == PyUnicode_1BYTE_KIND) { |
| 2743 | one_byte = PyUnicode_1BYTE_DATA(u); |
| 2744 | for (; w < wchar_end; ++one_byte, ++w) |
| 2745 | *w = *one_byte; |
| 2746 | /* null-terminate the wstr */ |
| 2747 | *w = 0; |
| 2748 | } |
| 2749 | else if (PyUnicode_KIND(u) == PyUnicode_2BYTE_KIND) { |
| 2750 | #if SIZEOF_WCHAR_T == 4 |
| 2751 | two_bytes = PyUnicode_2BYTE_DATA(u); |
| 2752 | for (; w < wchar_end; ++two_bytes, ++w) |
| 2753 | *w = *two_bytes; |
| 2754 | /* null-terminate the wstr */ |
| 2755 | *w = 0; |
| 2756 | #else |
| 2757 | /* sizeof(wchar_t) == 2 */ |
| 2758 | PyObject_FREE(_PyUnicode_WSTR(u)); |
| 2759 | _PyUnicode_WSTR(u) = NULL; |
| 2760 | Py_FatalError("Impossible unicode object state, wstr " |
| 2761 | "and str should share memory already."); |
| 2762 | return NULL; |
| 2763 | #endif |
| 2764 | } |
| 2765 | else { |
| 2766 | assert(0 && "This should never happen."); |
| 2767 | } |
| 2768 | } |
| 2769 | } |
| 2770 | if (size != NULL) |
| 2771 | *size = PyUnicode_WSTR_LENGTH(u); |
| 2772 | return _PyUnicode_WSTR(u); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2773 | } |
| 2774 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2775 | Py_UNICODE * |
| 2776 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2777 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2778 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2779 | } |
| 2780 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2781 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2782 | Py_ssize_t |
| 2783 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2784 | { |
| 2785 | if (!PyUnicode_Check(unicode)) { |
| 2786 | PyErr_BadArgument(); |
| 2787 | goto onError; |
| 2788 | } |
| 2789 | return PyUnicode_GET_SIZE(unicode); |
| 2790 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2791 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2792 | return -1; |
| 2793 | } |
| 2794 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2795 | Py_ssize_t |
| 2796 | PyUnicode_GetLength(PyObject *unicode) |
| 2797 | { |
| 2798 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) != -1) { |
| 2799 | PyErr_BadArgument(); |
| 2800 | return -1; |
| 2801 | } |
| 2802 | |
| 2803 | return PyUnicode_GET_LENGTH(unicode); |
| 2804 | } |
| 2805 | |
| 2806 | Py_UCS4 |
| 2807 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 2808 | { |
| 2809 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) != -1) { |
| 2810 | return PyErr_BadArgument(); |
| 2811 | return (Py_UCS4)-1; |
| 2812 | } |
| 2813 | return PyUnicode_READ_CHAR(unicode, index); |
| 2814 | } |
| 2815 | |
| 2816 | int |
| 2817 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 2818 | { |
| 2819 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
| 2820 | return PyErr_BadArgument(); |
| 2821 | return -1; |
| 2822 | } |
| 2823 | |
| 2824 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 2825 | index, ch); |
| 2826 | return 0; |
| 2827 | } |
| 2828 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2829 | const char * |
| 2830 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2831 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 2832 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2833 | } |
| 2834 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 2835 | /* create or adjust a UnicodeDecodeError */ |
| 2836 | static void |
| 2837 | make_decode_exception(PyObject **exceptionObject, |
| 2838 | const char *encoding, |
| 2839 | const char *input, Py_ssize_t length, |
| 2840 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 2841 | const char *reason) |
| 2842 | { |
| 2843 | if (*exceptionObject == NULL) { |
| 2844 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 2845 | encoding, input, length, startpos, endpos, reason); |
| 2846 | } |
| 2847 | else { |
| 2848 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 2849 | goto onError; |
| 2850 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 2851 | goto onError; |
| 2852 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 2853 | goto onError; |
| 2854 | } |
| 2855 | return; |
| 2856 | |
| 2857 | onError: |
| 2858 | Py_DECREF(*exceptionObject); |
| 2859 | *exceptionObject = NULL; |
| 2860 | } |
| 2861 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2862 | /* error handling callback helper: |
| 2863 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 2864 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2865 | and adjust various state variables. |
| 2866 | return 0 on success, -1 on error |
| 2867 | */ |
| 2868 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2869 | static int |
| 2870 | unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2871 | const char *encoding, const char *reason, |
| 2872 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 2873 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 2874 | PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2875 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2876 | 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] | 2877 | |
| 2878 | PyObject *restuple = NULL; |
| 2879 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2880 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2881 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2882 | Py_ssize_t requiredsize; |
| 2883 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2884 | const Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2885 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2886 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2887 | int res = -1; |
| 2888 | |
| 2889 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2890 | *errorHandler = PyCodec_LookupError(errors); |
| 2891 | if (*errorHandler == NULL) |
| 2892 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2893 | } |
| 2894 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 2895 | make_decode_exception(exceptionObject, |
| 2896 | encoding, |
| 2897 | *input, *inend - *input, |
| 2898 | *startinpos, *endinpos, |
| 2899 | reason); |
| 2900 | if (*exceptionObject == NULL) |
| 2901 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2902 | |
| 2903 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 2904 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2905 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2906 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 2907 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2908 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2909 | } |
| 2910 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2911 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2912 | |
| 2913 | /* Copy back the bytes variables, which might have been modified by the |
| 2914 | callback */ |
| 2915 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 2916 | if (!inputobj) |
| 2917 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2918 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2919 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2920 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2921 | *input = PyBytes_AS_STRING(inputobj); |
| 2922 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2923 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 2924 | /* we can DECREF safely, as the exception has another reference, |
| 2925 | so the object won't go away. */ |
| 2926 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2927 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2928 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2929 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 2930 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2931 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 2932 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 2933 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2934 | |
| 2935 | /* need more space? (at least enough for what we |
| 2936 | have+the replacement+the rest of the string (starting |
| 2937 | at the new input position), so we won't have to check space |
| 2938 | when there are no errors in the rest of the string) */ |
| 2939 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 2940 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 2941 | requiredsize = *outpos + repsize + insize-newpos; |
| 2942 | if (requiredsize > outsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2943 | if (requiredsize<2*outsize) |
| 2944 | requiredsize = 2*outsize; |
| 2945 | if (_PyUnicode_Resize(output, requiredsize) < 0) |
| 2946 | goto onError; |
| 2947 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2948 | } |
| 2949 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2950 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2951 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 2952 | *outptr += repsize; |
| 2953 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2954 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2955 | /* we made it! */ |
| 2956 | res = 0; |
| 2957 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2958 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2959 | Py_XDECREF(restuple); |
| 2960 | return res; |
| 2961 | } |
| 2962 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2963 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 2964 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2965 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 2966 | |
| 2967 | /* Three simple macros defining base-64. */ |
| 2968 | |
| 2969 | /* Is c a base-64 character? */ |
| 2970 | |
| 2971 | #define IS_BASE64(c) \ |
| 2972 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 2973 | ((c) >= 'a' && (c) <= 'z') || \ |
| 2974 | ((c) >= '0' && (c) <= '9') || \ |
| 2975 | (c) == '+' || (c) == '/') |
| 2976 | |
| 2977 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 2978 | |
| 2979 | #define FROM_BASE64(c) \ |
| 2980 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 2981 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 2982 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 2983 | (c) == '+' ? 62 : 63) |
| 2984 | |
| 2985 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 2986 | |
| 2987 | #define TO_BASE64(n) \ |
| 2988 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 2989 | |
| 2990 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 2991 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 2992 | * byte not decoding to itself is the + which begins a base64 |
| 2993 | * string. */ |
| 2994 | |
| 2995 | #define DECODE_DIRECT(c) \ |
| 2996 | ((c) <= 127 && (c) != '+') |
| 2997 | |
| 2998 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 2999 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 3000 | * the above). See RFC2152. This array identifies these different |
| 3001 | * sets: |
| 3002 | * 0 : "Set D" |
| 3003 | * alphanumeric and '(),-./:? |
| 3004 | * 1 : "Set O" |
| 3005 | * !"#$%&*;<=>@[]^_`{|} |
| 3006 | * 2 : "whitespace" |
| 3007 | * ht nl cr sp |
| 3008 | * 3 : special (must be base64 encoded) |
| 3009 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 3010 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3011 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3012 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3013 | char utf7_category[128] = { |
| 3014 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 3015 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 3016 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 3017 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 3018 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 3019 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 3020 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 3021 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 3022 | /* @ A B C D E F G H I J K L M N O */ |
| 3023 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3024 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 3025 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 3026 | /* ` a b c d e f g h i j k l m n o */ |
| 3027 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3028 | /* p q r s t u v w x y z { | } ~ del */ |
| 3029 | 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] | 3030 | }; |
| 3031 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3032 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 3033 | * answer depends on whether we are encoding set O as itself, and also |
| 3034 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 3035 | * clear that the answers to these questions vary between |
| 3036 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 3037 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3038 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 3039 | ((c) < 128 && (c) > 0 && \ |
| 3040 | ((utf7_category[(c)] == 0) || \ |
| 3041 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 3042 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3043 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3044 | PyObject * |
| 3045 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3046 | Py_ssize_t size, |
| 3047 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3048 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3049 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 3050 | } |
| 3051 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3052 | /* The decoder. The only state we preserve is our read position, |
| 3053 | * i.e. how many characters we have consumed. So if we end in the |
| 3054 | * middle of a shift sequence we have to back off the read position |
| 3055 | * and the output to the beginning of the sequence, otherwise we lose |
| 3056 | * all the shift state (seen bits, number of bits seen, high |
| 3057 | * surrogate). */ |
| 3058 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3059 | PyObject * |
| 3060 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3061 | Py_ssize_t size, |
| 3062 | const char *errors, |
| 3063 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3064 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3065 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3066 | Py_ssize_t startinpos; |
| 3067 | Py_ssize_t endinpos; |
| 3068 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3069 | const char *e; |
| 3070 | PyUnicodeObject *unicode; |
| 3071 | Py_UNICODE *p; |
| 3072 | const char *errmsg = ""; |
| 3073 | int inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3074 | Py_UNICODE *shiftOutStart; |
| 3075 | unsigned int base64bits = 0; |
| 3076 | unsigned long base64buffer = 0; |
| 3077 | Py_UNICODE surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3078 | PyObject *errorHandler = NULL; |
| 3079 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3080 | |
| 3081 | unicode = _PyUnicode_New(size); |
| 3082 | if (!unicode) |
| 3083 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3084 | if (size == 0) { |
| 3085 | if (consumed) |
| 3086 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3087 | return (PyObject *)unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3088 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3089 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3090 | p = PyUnicode_AS_UNICODE(unicode); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3091 | shiftOutStart = p; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3092 | e = s + size; |
| 3093 | |
| 3094 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3095 | Py_UNICODE ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3096 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 3097 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3098 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3099 | if (inShift) { /* in a base-64 section */ |
| 3100 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 3101 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 3102 | base64bits += 6; |
| 3103 | s++; |
| 3104 | if (base64bits >= 16) { |
| 3105 | /* we have enough bits for a UTF-16 value */ |
| 3106 | Py_UNICODE outCh = (Py_UNICODE) |
| 3107 | (base64buffer >> (base64bits-16)); |
| 3108 | base64bits -= 16; |
| 3109 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 3110 | if (surrogate) { |
| 3111 | /* expecting a second surrogate */ |
| 3112 | if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 3113 | #ifdef Py_UNICODE_WIDE |
| 3114 | *p++ = (((surrogate & 0x3FF)<<10) |
| 3115 | | (outCh & 0x3FF)) + 0x10000; |
| 3116 | #else |
| 3117 | *p++ = surrogate; |
| 3118 | *p++ = outCh; |
| 3119 | #endif |
| 3120 | surrogate = 0; |
| 3121 | } |
| 3122 | else { |
| 3123 | surrogate = 0; |
| 3124 | errmsg = "second surrogate missing"; |
| 3125 | goto utf7Error; |
| 3126 | } |
| 3127 | } |
| 3128 | else if (outCh >= 0xD800 && outCh <= 0xDBFF) { |
| 3129 | /* first surrogate */ |
| 3130 | surrogate = outCh; |
| 3131 | } |
| 3132 | else if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 3133 | errmsg = "unexpected second surrogate"; |
| 3134 | goto utf7Error; |
| 3135 | } |
| 3136 | else { |
| 3137 | *p++ = outCh; |
| 3138 | } |
| 3139 | } |
| 3140 | } |
| 3141 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3142 | inShift = 0; |
| 3143 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3144 | if (surrogate) { |
| 3145 | errmsg = "second surrogate missing at end of shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3146 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3147 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3148 | if (base64bits > 0) { /* left-over bits */ |
| 3149 | if (base64bits >= 6) { |
| 3150 | /* We've seen at least one base-64 character */ |
| 3151 | errmsg = "partial character in shift sequence"; |
| 3152 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3153 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3154 | else { |
| 3155 | /* Some bits remain; they should be zero */ |
| 3156 | if (base64buffer != 0) { |
| 3157 | errmsg = "non-zero padding bits in shift sequence"; |
| 3158 | goto utf7Error; |
| 3159 | } |
| 3160 | } |
| 3161 | } |
| 3162 | if (ch != '-') { |
| 3163 | /* '-' is absorbed; other terminating |
| 3164 | characters are preserved */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3165 | *p++ = ch; |
| 3166 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3167 | } |
| 3168 | } |
| 3169 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3170 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3171 | s++; /* consume '+' */ |
| 3172 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3173 | s++; |
| 3174 | *p++ = '+'; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3175 | } |
| 3176 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3177 | inShift = 1; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3178 | shiftOutStart = p; |
| 3179 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3180 | } |
| 3181 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3182 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3183 | *p++ = ch; |
| 3184 | s++; |
| 3185 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3186 | else { |
| 3187 | startinpos = s-starts; |
| 3188 | s++; |
| 3189 | errmsg = "unexpected special character"; |
| 3190 | goto utf7Error; |
| 3191 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3192 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3193 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3194 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 3195 | endinpos = s-starts; |
| 3196 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3197 | errors, &errorHandler, |
| 3198 | "utf7", errmsg, |
| 3199 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 3200 | &unicode, &outpos, &p)) |
| 3201 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3202 | } |
| 3203 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3204 | /* end of string */ |
| 3205 | |
| 3206 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 3207 | /* if we're in an inconsistent state, that's an error */ |
| 3208 | if (surrogate || |
| 3209 | (base64bits >= 6) || |
| 3210 | (base64bits > 0 && base64buffer != 0)) { |
| 3211 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 3212 | endinpos = size; |
| 3213 | if (unicode_decode_call_errorhandler( |
| 3214 | errors, &errorHandler, |
| 3215 | "utf7", "unterminated shift sequence", |
| 3216 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 3217 | &unicode, &outpos, &p)) |
| 3218 | goto onError; |
| 3219 | if (s < e) |
| 3220 | goto restart; |
| 3221 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3222 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3223 | |
| 3224 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3225 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3226 | if (inShift) { |
| 3227 | p = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3228 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3229 | } |
| 3230 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3231 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3232 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3233 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3234 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 3235 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3236 | goto onError; |
| 3237 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3238 | Py_XDECREF(errorHandler); |
| 3239 | Py_XDECREF(exc); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3240 | if (PyUnicode_READY(unicode) == -1) { |
| 3241 | Py_DECREF(unicode); |
| 3242 | return NULL; |
| 3243 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3244 | return (PyObject *)unicode; |
| 3245 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3246 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3247 | Py_XDECREF(errorHandler); |
| 3248 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3249 | Py_DECREF(unicode); |
| 3250 | return NULL; |
| 3251 | } |
| 3252 | |
| 3253 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3254 | PyObject * |
| 3255 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3256 | Py_ssize_t size, |
| 3257 | int base64SetO, |
| 3258 | int base64WhiteSpace, |
| 3259 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3260 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3261 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3262 | /* It might be possible to tighten this worst case */ |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 3263 | Py_ssize_t allocated = 8 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3264 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3265 | Py_ssize_t i = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3266 | unsigned int base64bits = 0; |
| 3267 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3268 | char * out; |
| 3269 | char * start; |
| 3270 | |
| 3271 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3272 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3273 | |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 3274 | if (allocated / 8 != size) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3275 | return PyErr_NoMemory(); |
| 3276 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3277 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3278 | if (v == NULL) |
| 3279 | return NULL; |
| 3280 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3281 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3282 | for (;i < size; ++i) { |
| 3283 | Py_UNICODE ch = s[i]; |
| 3284 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3285 | if (inShift) { |
| 3286 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 3287 | /* shifting out */ |
| 3288 | if (base64bits) { /* output remaining bits */ |
| 3289 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 3290 | base64buffer = 0; |
| 3291 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3292 | } |
| 3293 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3294 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 3295 | so no '-' is required, except if the character is itself a '-' */ |
| 3296 | if (IS_BASE64(ch) || ch == '-') { |
| 3297 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3298 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3299 | *out++ = (char) ch; |
| 3300 | } |
| 3301 | else { |
| 3302 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3303 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3304 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3305 | else { /* not in a shift sequence */ |
| 3306 | if (ch == '+') { |
| 3307 | *out++ = '+'; |
| 3308 | *out++ = '-'; |
| 3309 | } |
| 3310 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 3311 | *out++ = (char) ch; |
| 3312 | } |
| 3313 | else { |
| 3314 | *out++ = '+'; |
| 3315 | inShift = 1; |
| 3316 | goto encode_char; |
| 3317 | } |
| 3318 | } |
| 3319 | continue; |
| 3320 | encode_char: |
| 3321 | #ifdef Py_UNICODE_WIDE |
| 3322 | if (ch >= 0x10000) { |
| 3323 | /* code first surrogate */ |
| 3324 | base64bits += 16; |
| 3325 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 3326 | while (base64bits >= 6) { |
| 3327 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 3328 | base64bits -= 6; |
| 3329 | } |
| 3330 | /* prepare second surrogate */ |
| 3331 | ch = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 3332 | } |
| 3333 | #endif |
| 3334 | base64bits += 16; |
| 3335 | base64buffer = (base64buffer << 16) | ch; |
| 3336 | while (base64bits >= 6) { |
| 3337 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 3338 | base64bits -= 6; |
| 3339 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 3340 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3341 | if (base64bits) |
| 3342 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 3343 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3344 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3345 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 3346 | return NULL; |
| 3347 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3348 | } |
| 3349 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3350 | #undef IS_BASE64 |
| 3351 | #undef FROM_BASE64 |
| 3352 | #undef TO_BASE64 |
| 3353 | #undef DECODE_DIRECT |
| 3354 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3355 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3356 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 3357 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3358 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3359 | char utf8_code_length[256] = { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3360 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 3361 | illegal prefix. See RFC 3629 for details */ |
| 3362 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 3363 | 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] | 3364 | 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] | 3365 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 3366 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 3367 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 3368 | 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] | 3369 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 3370 | 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] | 3371 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3372 | 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] | 3373 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 3374 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 3375 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 3376 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 3377 | 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] | 3378 | }; |
| 3379 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3380 | PyObject * |
| 3381 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3382 | Py_ssize_t size, |
| 3383 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3384 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3385 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 3386 | } |
| 3387 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3388 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 3389 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 3390 | |
| 3391 | /* Mask to quickly check whether a C 'long' contains a |
| 3392 | non-ASCII, UTF8-encoded char. */ |
| 3393 | #if (SIZEOF_LONG == 8) |
| 3394 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 3395 | #elif (SIZEOF_LONG == 4) |
| 3396 | # define ASCII_CHAR_MASK 0x80808080L |
| 3397 | #else |
| 3398 | # error C 'long' size should be either 4 or 8! |
| 3399 | #endif |
| 3400 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3401 | /* Scans a UTF-8 string and returns the maximum character to be expected, |
| 3402 | the size of the decoded unicode string and if any major errors were |
| 3403 | encountered. |
| 3404 | |
| 3405 | This function does check basic UTF-8 sanity, it does however NOT CHECK |
| 3406 | if the string contains surrogates, and if all continuation bytes are |
| 3407 | within the correct ranges, these checks are performed in |
| 3408 | PyUnicode_DecodeUTF8Stateful. |
| 3409 | |
| 3410 | If it sets has_errors to 1, it means the value of unicode_size and max_char |
| 3411 | will be bogus and you should not rely on useful information in them. |
| 3412 | */ |
| 3413 | static Py_UCS4 |
| 3414 | utf8_max_char_size_and_has_errors(const char *s, Py_ssize_t string_size, |
| 3415 | Py_ssize_t *unicode_size, Py_ssize_t* consumed, |
| 3416 | int *has_errors) |
| 3417 | { |
| 3418 | Py_ssize_t n; |
| 3419 | Py_ssize_t char_count = 0; |
| 3420 | Py_UCS4 max_char = 127, new_max; |
| 3421 | Py_UCS4 upper_bound; |
| 3422 | const unsigned char *p = (const unsigned char *)s; |
| 3423 | const unsigned char *end = p + string_size; |
| 3424 | const unsigned char *aligned_end = (const unsigned char *) ((size_t) end & ~LONG_PTR_MASK); |
| 3425 | int err = 0; |
| 3426 | |
| 3427 | for (; p < end && !err; ++p, ++char_count) { |
| 3428 | /* Only check value if it's not a ASCII char... */ |
| 3429 | if (*p < 0x80) { |
| 3430 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 3431 | an explanation. */ |
| 3432 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 3433 | /* Help register allocation */ |
| 3434 | register const unsigned char *_p = p; |
| 3435 | while (_p < aligned_end) { |
| 3436 | unsigned long value = *(unsigned long *) _p; |
| 3437 | if (value & ASCII_CHAR_MASK) |
| 3438 | break; |
| 3439 | _p += SIZEOF_LONG; |
| 3440 | char_count += SIZEOF_LONG; |
| 3441 | } |
| 3442 | p = _p; |
| 3443 | if (p == end) |
| 3444 | break; |
| 3445 | } |
| 3446 | } |
| 3447 | if (*p >= 0x80) { |
| 3448 | n = utf8_code_length[*p]; |
| 3449 | new_max = max_char; |
| 3450 | switch (n) { |
| 3451 | /* invalid start byte */ |
| 3452 | case 0: |
| 3453 | err = 1; |
| 3454 | break; |
| 3455 | case 2: |
| 3456 | /* Code points between 0x00FF and 0x07FF inclusive. |
| 3457 | Approximate the upper bound of the code point, |
| 3458 | if this flips over 255 we can be sure it will be more |
| 3459 | than 255 and the string will need 2 bytes per code coint, |
| 3460 | if it stays under or equal to 255, we can be sure 1 byte |
| 3461 | is enough. |
| 3462 | ((*p & 0b00011111) << 6) | 0b00111111 */ |
| 3463 | upper_bound = ((*p & 0x1F) << 6) | 0x3F; |
| 3464 | if (max_char < upper_bound) |
| 3465 | new_max = upper_bound; |
| 3466 | /* Ensure we track at least that we left ASCII space. */ |
| 3467 | if (new_max < 128) |
| 3468 | new_max = 128; |
| 3469 | break; |
| 3470 | case 3: |
| 3471 | /* Between 0x0FFF and 0xFFFF inclusive, so values are |
| 3472 | always > 255 and <= 65535 and will always need 2 bytes. */ |
| 3473 | if (max_char < 65535) |
| 3474 | new_max = 65535; |
| 3475 | break; |
| 3476 | case 4: |
| 3477 | /* Code point will be above 0xFFFF for sure in this case. */ |
| 3478 | new_max = 65537; |
| 3479 | break; |
| 3480 | /* Internal error, this should be caught by the first if */ |
| 3481 | case 1: |
| 3482 | default: |
| 3483 | assert(0 && "Impossible case in utf8_max_char_and_size"); |
| 3484 | err = 1; |
| 3485 | } |
| 3486 | /* Instead of number of overall bytes for this code point, |
| 3487 | n containts the number of following bytes: */ |
| 3488 | --n; |
| 3489 | /* Check if the follow up chars are all valid continuation bytes */ |
| 3490 | if (n >= 1) { |
| 3491 | const unsigned char *cont; |
| 3492 | if ((p + n) >= end) { |
| 3493 | if (consumed == 0) |
| 3494 | /* incomplete data, non-incremental decoding */ |
| 3495 | err = 1; |
| 3496 | break; |
| 3497 | } |
| 3498 | for (cont = p + 1; cont < (p + n); ++cont) { |
| 3499 | if ((*cont & 0xc0) != 0x80) { |
| 3500 | err = 1; |
| 3501 | break; |
| 3502 | } |
| 3503 | } |
| 3504 | p += n; |
| 3505 | } |
| 3506 | else |
| 3507 | err = 1; |
| 3508 | max_char = new_max; |
| 3509 | } |
| 3510 | } |
| 3511 | |
| 3512 | if (unicode_size) |
| 3513 | *unicode_size = char_count; |
| 3514 | if (has_errors) |
| 3515 | *has_errors = err; |
| 3516 | return max_char; |
| 3517 | } |
| 3518 | |
| 3519 | /* Similar to PyUnicode_WRITE but can also write into wstr field |
| 3520 | of the legacy unicode representation */ |
| 3521 | #define WRITE_FLEXIBLE_OR_WSTR(kind, buf, index, value) \ |
| 3522 | do { \ |
| 3523 | const int k_ = (kind); \ |
| 3524 | if (k_ == PyUnicode_WCHAR_KIND) \ |
| 3525 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \ |
| 3526 | else if (k_ == PyUnicode_1BYTE_KIND) \ |
| 3527 | ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \ |
| 3528 | else if (k_ == PyUnicode_2BYTE_KIND) \ |
| 3529 | ((Py_UCS2 *)(buf))[(index)] = (Py_UCS2)(value); \ |
| 3530 | else \ |
| 3531 | ((Py_UCS4 *)(buf))[(index)] = (Py_UCS4)(value); \ |
| 3532 | } while (0) |
| 3533 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3534 | PyObject * |
| 3535 | PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3536 | Py_ssize_t size, |
| 3537 | const char *errors, |
| 3538 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3539 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3540 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3541 | int n; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3542 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3543 | Py_ssize_t startinpos; |
| 3544 | Py_ssize_t endinpos; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3545 | const char *e, *aligned_end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3546 | PyUnicodeObject *unicode; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 3547 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3548 | PyObject *errorHandler = NULL; |
| 3549 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3550 | Py_UCS4 maxchar = 0; |
| 3551 | Py_ssize_t unicode_size; |
| 3552 | Py_ssize_t i; |
| 3553 | int kind; |
| 3554 | void *data; |
| 3555 | int has_errors; |
| 3556 | Py_UNICODE *error_outptr; |
| 3557 | #if SIZEOF_WCHAR_T == 2 |
| 3558 | Py_ssize_t wchar_offset = 0; |
| 3559 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3560 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3561 | if (size == 0) { |
| 3562 | if (consumed) |
| 3563 | *consumed = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3564 | return (PyObject *)PyUnicode_New(0, 0); |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3565 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3566 | maxchar = utf8_max_char_size_and_has_errors(s, size, &unicode_size, |
| 3567 | consumed, &has_errors); |
| 3568 | if (has_errors) { |
| 3569 | unicode = _PyUnicode_New(size); |
| 3570 | if (!unicode) |
| 3571 | return NULL; |
| 3572 | kind = PyUnicode_WCHAR_KIND; |
| 3573 | data = PyUnicode_AS_UNICODE(unicode); |
| 3574 | assert(data != NULL); |
| 3575 | } |
| 3576 | else { |
| 3577 | unicode = (PyUnicodeObject *)PyUnicode_New(unicode_size, maxchar); |
| 3578 | if (!unicode) |
| 3579 | return NULL; |
| 3580 | /* When the string is ASCII only, just use memcpy and return. |
| 3581 | unicode_size may be != size if there is an incomplete UTF-8 |
| 3582 | sequence at the end of the ASCII block. */ |
| 3583 | if (maxchar < 128 && size == unicode_size) { |
| 3584 | Py_MEMCPY(PyUnicode_1BYTE_DATA(unicode), s, unicode_size); |
| 3585 | return (PyObject *)unicode; |
| 3586 | } |
| 3587 | kind = PyUnicode_KIND(unicode); |
| 3588 | data = PyUnicode_DATA(unicode); |
| 3589 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3590 | /* Unpack UTF-8 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3591 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3592 | e = s + size; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3593 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3594 | |
| 3595 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 3596 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3597 | |
| 3598 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3599 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 3600 | input will consist of an overwhelming majority of ASCII |
| 3601 | characters, we try to optimize for this case by checking |
| 3602 | as many characters as a C 'long' can contain. |
| 3603 | First, check if we can do an aligned read, as most CPUs have |
| 3604 | a penalty for unaligned reads. |
| 3605 | */ |
| 3606 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 3607 | /* Help register allocation */ |
| 3608 | register const char *_s = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3609 | register Py_ssize_t _i = i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3610 | while (_s < aligned_end) { |
| 3611 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 3612 | and do a fast unrolled copy if it only contains ASCII |
| 3613 | characters. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3614 | unsigned long value = *(unsigned long *) _s; |
| 3615 | if (value & ASCII_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3616 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3617 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+0, _s[0]); |
| 3618 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+1, _s[1]); |
| 3619 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+2, _s[2]); |
| 3620 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+3, _s[3]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3621 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3622 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+4, _s[4]); |
| 3623 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+5, _s[5]); |
| 3624 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+6, _s[6]); |
| 3625 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+7, _s[7]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3626 | #endif |
| 3627 | _s += SIZEOF_LONG; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3628 | _i += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3629 | } |
| 3630 | s = _s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3631 | i = _i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3632 | if (s == e) |
| 3633 | break; |
| 3634 | ch = (unsigned char)*s; |
| 3635 | } |
| 3636 | } |
| 3637 | |
| 3638 | if (ch < 0x80) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3639 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3640 | s++; |
| 3641 | continue; |
| 3642 | } |
| 3643 | |
| 3644 | n = utf8_code_length[ch]; |
| 3645 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 3646 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3647 | if (consumed) |
| 3648 | break; |
| 3649 | else { |
| 3650 | errmsg = "unexpected end of data"; |
| 3651 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3652 | endinpos = startinpos+1; |
| 3653 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 3654 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3655 | goto utf8Error; |
| 3656 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3657 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3658 | |
| 3659 | switch (n) { |
| 3660 | |
| 3661 | case 0: |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3662 | errmsg = "invalid start byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3663 | startinpos = s-starts; |
| 3664 | endinpos = startinpos+1; |
| 3665 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3666 | |
| 3667 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 3668 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3669 | startinpos = s-starts; |
| 3670 | endinpos = startinpos+1; |
| 3671 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3672 | |
| 3673 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 3674 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3675 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3676 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3677 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3678 | goto utf8Error; |
| 3679 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3680 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3681 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3682 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3683 | break; |
| 3684 | |
| 3685 | case 3: |
Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 3686 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 3687 | will result in surrogates in range d800-dfff. Surrogates are |
| 3688 | not valid UTF-8 so they are rejected. |
| 3689 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 3690 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3691 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3692 | (s[2] & 0xc0) != 0x80 || |
| 3693 | ((unsigned char)s[0] == 0xE0 && |
| 3694 | (unsigned char)s[1] < 0xA0) || |
| 3695 | ((unsigned char)s[0] == 0xED && |
| 3696 | (unsigned char)s[1] > 0x9F)) { |
| 3697 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3698 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3699 | endinpos = startinpos + 1; |
| 3700 | |
| 3701 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 3702 | continuation byte is s[2], so increment endinpos by 1, |
| 3703 | if not, s[1] is invalid and endinpos doesn't need to |
| 3704 | be incremented. */ |
| 3705 | if ((s[1] & 0xC0) == 0x80) |
| 3706 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3707 | goto utf8Error; |
| 3708 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3709 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3710 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3711 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 3712 | break; |
| 3713 | |
| 3714 | case 4: |
| 3715 | if ((s[1] & 0xc0) != 0x80 || |
| 3716 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3717 | (s[3] & 0xc0) != 0x80 || |
| 3718 | ((unsigned char)s[0] == 0xF0 && |
| 3719 | (unsigned char)s[1] < 0x90) || |
| 3720 | ((unsigned char)s[0] == 0xF4 && |
| 3721 | (unsigned char)s[1] > 0x8F)) { |
| 3722 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3723 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3724 | endinpos = startinpos + 1; |
| 3725 | if ((s[1] & 0xC0) == 0x80) { |
| 3726 | endinpos++; |
| 3727 | if ((s[2] & 0xC0) == 0x80) |
| 3728 | endinpos++; |
| 3729 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3730 | goto utf8Error; |
| 3731 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 3732 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3733 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 3734 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 3735 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3736 | /* If the string is flexible or we have native UCS-4, write |
| 3737 | directly.. */ |
| 3738 | if (sizeof(Py_UNICODE) > 2 || kind != PyUnicode_WCHAR_KIND) |
| 3739 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3740 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3741 | else { |
| 3742 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3743 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3744 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 3745 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3746 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3747 | /* high surrogate = top 10 bits added to D800 */ |
| 3748 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, |
| 3749 | (Py_UNICODE)(0xD800 + (ch >> 10))); |
| 3750 | |
| 3751 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 3752 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, |
| 3753 | (Py_UNICODE)(0xDC00 + (ch & 0x03FF))); |
| 3754 | } |
| 3755 | #if SIZEOF_WCHAR_T == 2 |
| 3756 | wchar_offset++; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3757 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3758 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3759 | } |
| 3760 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3761 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3762 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3763 | utf8Error: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3764 | /* If this is not yet a resizable string, make it one.. */ |
| 3765 | if (kind != PyUnicode_WCHAR_KIND) { |
| 3766 | const Py_UNICODE *u; |
| 3767 | PyUnicodeObject *new_unicode = _PyUnicode_New(size); |
| 3768 | if (!new_unicode) |
| 3769 | goto onError; |
| 3770 | u = PyUnicode_AsUnicode((PyObject *)unicode); |
| 3771 | if (!u) |
| 3772 | goto onError; |
| 3773 | #if SIZEOF_WCHAR_T == 2 |
| 3774 | i += wchar_offset; |
| 3775 | #endif |
| 3776 | Py_UNICODE_COPY(PyUnicode_AS_UNICODE(new_unicode), u, i); |
| 3777 | Py_DECREF(unicode); |
| 3778 | unicode = new_unicode; |
| 3779 | kind = 0; |
| 3780 | data = PyUnicode_AS_UNICODE(new_unicode); |
| 3781 | assert(data != NULL); |
| 3782 | } |
| 3783 | error_outptr = PyUnicode_AS_UNICODE(unicode) + i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3784 | if (unicode_decode_call_errorhandler( |
| 3785 | errors, &errorHandler, |
| 3786 | "utf8", errmsg, |
| 3787 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3788 | &unicode, &i, &error_outptr)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3789 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3790 | /* Update data because unicode_decode_call_errorhandler might have |
| 3791 | re-created or resized the unicode object. */ |
| 3792 | data = PyUnicode_AS_UNICODE(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3793 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3794 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3795 | /* Ensure the unicode_size calculation above was correct: */ |
| 3796 | assert(kind == PyUnicode_WCHAR_KIND || i == unicode_size); |
| 3797 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3798 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3799 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3800 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3801 | /* Adjust length and ready string when it contained errors and |
| 3802 | is of the old resizable kind. */ |
| 3803 | if (kind == PyUnicode_WCHAR_KIND) { |
| 3804 | if (_PyUnicode_Resize(&unicode, i) < 0 || |
| 3805 | PyUnicode_READY(unicode) == -1) |
| 3806 | goto onError; |
| 3807 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3808 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3809 | Py_XDECREF(errorHandler); |
| 3810 | Py_XDECREF(exc); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3811 | if (PyUnicode_READY(unicode) == -1) { |
| 3812 | Py_DECREF(unicode); |
| 3813 | return NULL; |
| 3814 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3815 | return (PyObject *)unicode; |
| 3816 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3817 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3818 | Py_XDECREF(errorHandler); |
| 3819 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3820 | Py_DECREF(unicode); |
| 3821 | return NULL; |
| 3822 | } |
| 3823 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3824 | #undef WRITE_FLEXIBLE_OR_WSTR |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3825 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 3826 | #ifdef __APPLE__ |
| 3827 | |
| 3828 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
| 3829 | used to decode the command line arguments on Mac OS X. */ |
| 3830 | |
| 3831 | wchar_t* |
| 3832 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 3833 | { |
| 3834 | int n; |
| 3835 | const char *e; |
| 3836 | wchar_t *unicode, *p; |
| 3837 | |
| 3838 | /* Note: size will always be longer than the resulting Unicode |
| 3839 | character count */ |
| 3840 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) { |
| 3841 | PyErr_NoMemory(); |
| 3842 | return NULL; |
| 3843 | } |
| 3844 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 3845 | if (!unicode) |
| 3846 | return NULL; |
| 3847 | |
| 3848 | /* Unpack UTF-8 encoded data */ |
| 3849 | p = unicode; |
| 3850 | e = s + size; |
| 3851 | while (s < e) { |
| 3852 | Py_UCS4 ch = (unsigned char)*s; |
| 3853 | |
| 3854 | if (ch < 0x80) { |
| 3855 | *p++ = (wchar_t)ch; |
| 3856 | s++; |
| 3857 | continue; |
| 3858 | } |
| 3859 | |
| 3860 | n = utf8_code_length[ch]; |
| 3861 | if (s + n > e) { |
| 3862 | goto surrogateescape; |
| 3863 | } |
| 3864 | |
| 3865 | switch (n) { |
| 3866 | case 0: |
| 3867 | case 1: |
| 3868 | goto surrogateescape; |
| 3869 | |
| 3870 | case 2: |
| 3871 | if ((s[1] & 0xc0) != 0x80) |
| 3872 | goto surrogateescape; |
| 3873 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
| 3874 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 3875 | *p++ = (wchar_t)ch; |
| 3876 | break; |
| 3877 | |
| 3878 | case 3: |
| 3879 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 3880 | will result in surrogates in range d800-dfff. Surrogates are |
| 3881 | not valid UTF-8 so they are rejected. |
| 3882 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 3883 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
| 3884 | if ((s[1] & 0xc0) != 0x80 || |
| 3885 | (s[2] & 0xc0) != 0x80 || |
| 3886 | ((unsigned char)s[0] == 0xE0 && |
| 3887 | (unsigned char)s[1] < 0xA0) || |
| 3888 | ((unsigned char)s[0] == 0xED && |
| 3889 | (unsigned char)s[1] > 0x9F)) { |
| 3890 | |
| 3891 | goto surrogateescape; |
| 3892 | } |
| 3893 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
| 3894 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3895 | *p++ = (wchar_t)ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 3896 | break; |
| 3897 | |
| 3898 | case 4: |
| 3899 | if ((s[1] & 0xc0) != 0x80 || |
| 3900 | (s[2] & 0xc0) != 0x80 || |
| 3901 | (s[3] & 0xc0) != 0x80 || |
| 3902 | ((unsigned char)s[0] == 0xF0 && |
| 3903 | (unsigned char)s[1] < 0x90) || |
| 3904 | ((unsigned char)s[0] == 0xF4 && |
| 3905 | (unsigned char)s[1] > 0x8F)) { |
| 3906 | goto surrogateescape; |
| 3907 | } |
| 3908 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 3909 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 3910 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 3911 | |
| 3912 | #if SIZEOF_WCHAR_T == 4 |
| 3913 | *p++ = (wchar_t)ch; |
| 3914 | #else |
| 3915 | /* compute and append the two surrogates: */ |
| 3916 | |
| 3917 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 3918 | ch -= 0x10000; |
| 3919 | |
| 3920 | /* high surrogate = top 10 bits added to D800 */ |
| 3921 | *p++ = (wchar_t)(0xD800 + (ch >> 10)); |
| 3922 | |
| 3923 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 3924 | *p++ = (wchar_t)(0xDC00 + (ch & 0x03FF)); |
| 3925 | #endif |
| 3926 | break; |
| 3927 | } |
| 3928 | s += n; |
| 3929 | continue; |
| 3930 | |
| 3931 | surrogateescape: |
| 3932 | *p++ = 0xDC00 + ch; |
| 3933 | s++; |
| 3934 | } |
| 3935 | *p = L'\0'; |
| 3936 | return unicode; |
| 3937 | } |
| 3938 | |
| 3939 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3940 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3941 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 3942 | |
| 3943 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 3944 | and allocate exactly as much space needed at the end. Else allocate the |
| 3945 | maximum possible needed (4 result bytes per Unicode character), and return |
| 3946 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 3947 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 3948 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3949 | _PyUnicode_AsUTF8String(PyObject *obj, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3950 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 3951 | #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] | 3952 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3953 | Py_ssize_t i; /* index into s of next input byte */ |
| 3954 | PyObject *result; /* result string object */ |
| 3955 | char *p; /* next free byte in output buffer */ |
| 3956 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 3957 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 3958 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 3959 | PyObject *errorHandler = NULL; |
| 3960 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3961 | int kind; |
| 3962 | void *data; |
| 3963 | Py_ssize_t size; |
| 3964 | PyUnicodeObject *unicode = (PyUnicodeObject *)obj; |
| 3965 | #if SIZEOF_WCHAR_T == 2 |
| 3966 | Py_ssize_t wchar_offset = 0; |
| 3967 | #endif |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 3968 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3969 | if (!PyUnicode_Check(unicode)) { |
| 3970 | PyErr_BadArgument(); |
| 3971 | return NULL; |
| 3972 | } |
| 3973 | |
| 3974 | if (PyUnicode_READY(unicode) == -1) |
| 3975 | return NULL; |
| 3976 | |
| 3977 | if (_PyUnicode_UTF8(unicode)) |
| 3978 | return PyBytes_FromStringAndSize(_PyUnicode_UTF8(unicode), |
| 3979 | _PyUnicode_UTF8_LENGTH(unicode)); |
| 3980 | |
| 3981 | kind = PyUnicode_KIND(unicode); |
| 3982 | data = PyUnicode_DATA(unicode); |
| 3983 | size = PyUnicode_GET_LENGTH(unicode); |
| 3984 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 3985 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3986 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 3987 | if (size <= MAX_SHORT_UNICHARS) { |
| 3988 | /* Write into the stack buffer; nallocated can't overflow. |
| 3989 | * At the end, we'll allocate exactly as much heap space as it |
| 3990 | * turns out we need. |
| 3991 | */ |
| 3992 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3993 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 3994 | p = stackbuf; |
| 3995 | } |
| 3996 | else { |
| 3997 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 3998 | nallocated = size * 4; |
| 3999 | if (nallocated / 4 != size) /* overflow! */ |
| 4000 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4001 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4002 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4003 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4004 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4005 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4006 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4007 | for (i = 0; i < size;) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4008 | Py_UCS4 ch = PyUnicode_READ(kind, data, i++); |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4009 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4010 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4011 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4012 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4013 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4014 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4015 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 4016 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 4017 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4018 | } else if (0xD800 <= ch && ch <= 0xDFFF) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4019 | Py_ssize_t newpos; |
| 4020 | PyObject *rep; |
| 4021 | Py_ssize_t repsize, k, startpos; |
| 4022 | startpos = i-1; |
| 4023 | #if SIZEOF_WCHAR_T == 2 |
| 4024 | startpos += wchar_offset; |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 4025 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4026 | rep = unicode_encode_call_errorhandler( |
| 4027 | errors, &errorHandler, "utf-8", "surrogates not allowed", |
| 4028 | PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), |
| 4029 | &exc, startpos, startpos+1, &newpos); |
| 4030 | if (!rep) |
| 4031 | goto error; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4032 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4033 | if (PyBytes_Check(rep)) |
| 4034 | repsize = PyBytes_GET_SIZE(rep); |
| 4035 | else |
| 4036 | repsize = PyUnicode_GET_SIZE(rep); |
| 4037 | |
| 4038 | if (repsize > 4) { |
| 4039 | Py_ssize_t offset; |
| 4040 | |
| 4041 | if (result == NULL) |
| 4042 | offset = p - stackbuf; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4043 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4044 | offset = p - PyBytes_AS_STRING(result); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4045 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4046 | if (nallocated > PY_SSIZE_T_MAX - repsize + 4) { |
| 4047 | /* integer overflow */ |
| 4048 | PyErr_NoMemory(); |
| 4049 | goto error; |
| 4050 | } |
| 4051 | nallocated += repsize - 4; |
| 4052 | if (result != NULL) { |
| 4053 | if (_PyBytes_Resize(&result, nallocated) < 0) |
| 4054 | goto error; |
| 4055 | } else { |
| 4056 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4057 | if (result == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4058 | goto error; |
| 4059 | Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset); |
| 4060 | } |
| 4061 | p = PyBytes_AS_STRING(result) + offset; |
| 4062 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4063 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4064 | if (PyBytes_Check(rep)) { |
| 4065 | char *prep = PyBytes_AS_STRING(rep); |
| 4066 | for(k = repsize; k > 0; k--) |
| 4067 | *p++ = *prep++; |
| 4068 | } else /* rep is unicode */ { |
| 4069 | const Py_UNICODE *prep = PyUnicode_AS_UNICODE(rep); |
| 4070 | Py_UNICODE c; |
| 4071 | |
| 4072 | for(k=0; k<repsize; k++) { |
| 4073 | c = prep[k]; |
| 4074 | if (0x80 <= c) { |
| 4075 | raise_encode_exception(&exc, "utf-8", |
| 4076 | PyUnicode_AS_UNICODE(unicode), |
| 4077 | size, i-1, i, |
| 4078 | "surrogates not allowed"); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4079 | goto error; |
| 4080 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4081 | *p++ = (char)prep[k]; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4082 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4083 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4084 | Py_DECREF(rep); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4085 | } else if (ch < 0x10000) { |
| 4086 | *p++ = (char)(0xe0 | (ch >> 12)); |
| 4087 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4088 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 4089 | } else /* ch >= 0x10000 */ { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4090 | /* Encode UCS4 Unicode ordinals */ |
| 4091 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 4092 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 4093 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4094 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4095 | #if SIZEOF_WCHAR_T == 2 |
| 4096 | wchar_offset++; |
| 4097 | #endif |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4098 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4099 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 4100 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4101 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4102 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4103 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4104 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4105 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4106 | } |
| 4107 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 4108 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4109 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4110 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4111 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4112 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4113 | |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4114 | Py_XDECREF(errorHandler); |
| 4115 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4116 | return result; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4117 | error: |
| 4118 | Py_XDECREF(errorHandler); |
| 4119 | Py_XDECREF(exc); |
| 4120 | Py_XDECREF(result); |
| 4121 | return NULL; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4122 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4123 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4124 | } |
| 4125 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4126 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4127 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 4128 | Py_ssize_t size, |
| 4129 | const char *errors) |
| 4130 | { |
| 4131 | PyObject *v, *unicode; |
| 4132 | |
| 4133 | unicode = PyUnicode_FromUnicode(s, size); |
| 4134 | if (unicode == NULL) |
| 4135 | return NULL; |
| 4136 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 4137 | Py_DECREF(unicode); |
| 4138 | return v; |
| 4139 | } |
| 4140 | |
| 4141 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4142 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4143 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4144 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4145 | } |
| 4146 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4147 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 4148 | |
| 4149 | PyObject * |
| 4150 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4151 | Py_ssize_t size, |
| 4152 | const char *errors, |
| 4153 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4154 | { |
| 4155 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 4156 | } |
| 4157 | |
| 4158 | PyObject * |
| 4159 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4160 | Py_ssize_t size, |
| 4161 | const char *errors, |
| 4162 | int *byteorder, |
| 4163 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4164 | { |
| 4165 | const char *starts = s; |
| 4166 | Py_ssize_t startinpos; |
| 4167 | Py_ssize_t endinpos; |
| 4168 | Py_ssize_t outpos; |
| 4169 | PyUnicodeObject *unicode; |
| 4170 | Py_UNICODE *p; |
| 4171 | #ifndef Py_UNICODE_WIDE |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4172 | int pairs = 0; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4173 | const unsigned char *qq; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4174 | #else |
| 4175 | const int pairs = 0; |
| 4176 | #endif |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4177 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4178 | int bo = 0; /* assume native ordering by default */ |
| 4179 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4180 | /* Offsets from q for retrieving bytes in the right order. */ |
| 4181 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4182 | int iorder[] = {0, 1, 2, 3}; |
| 4183 | #else |
| 4184 | int iorder[] = {3, 2, 1, 0}; |
| 4185 | #endif |
| 4186 | PyObject *errorHandler = NULL; |
| 4187 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4188 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4189 | q = (unsigned char *)s; |
| 4190 | e = q + size; |
| 4191 | |
| 4192 | if (byteorder) |
| 4193 | bo = *byteorder; |
| 4194 | |
| 4195 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4196 | byte order setting accordingly. In native mode, the leading BOM |
| 4197 | mark is skipped, in all other modes, it is copied to the output |
| 4198 | stream as-is (giving a ZWNBSP character). */ |
| 4199 | if (bo == 0) { |
| 4200 | if (size >= 4) { |
| 4201 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4202 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4203 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4204 | if (bom == 0x0000FEFF) { |
| 4205 | q += 4; |
| 4206 | bo = -1; |
| 4207 | } |
| 4208 | else if (bom == 0xFFFE0000) { |
| 4209 | q += 4; |
| 4210 | bo = 1; |
| 4211 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4212 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4213 | if (bom == 0x0000FEFF) { |
| 4214 | q += 4; |
| 4215 | bo = 1; |
| 4216 | } |
| 4217 | else if (bom == 0xFFFE0000) { |
| 4218 | q += 4; |
| 4219 | bo = -1; |
| 4220 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4221 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4222 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4223 | } |
| 4224 | |
| 4225 | if (bo == -1) { |
| 4226 | /* force LE */ |
| 4227 | iorder[0] = 0; |
| 4228 | iorder[1] = 1; |
| 4229 | iorder[2] = 2; |
| 4230 | iorder[3] = 3; |
| 4231 | } |
| 4232 | else if (bo == 1) { |
| 4233 | /* force BE */ |
| 4234 | iorder[0] = 3; |
| 4235 | iorder[1] = 2; |
| 4236 | iorder[2] = 1; |
| 4237 | iorder[3] = 0; |
| 4238 | } |
| 4239 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4240 | /* On narrow builds we split characters outside the BMP into two |
| 4241 | codepoints => count how much extra space we need. */ |
| 4242 | #ifndef Py_UNICODE_WIDE |
| 4243 | for (qq = q; qq < e; qq += 4) |
| 4244 | if (qq[iorder[2]] != 0 || qq[iorder[3]] != 0) |
| 4245 | pairs++; |
| 4246 | #endif |
| 4247 | |
| 4248 | /* This might be one to much, because of a BOM */ |
| 4249 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 4250 | if (!unicode) |
| 4251 | return NULL; |
| 4252 | if (size == 0) |
| 4253 | return (PyObject *)unicode; |
| 4254 | |
| 4255 | /* Unpack UTF-32 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4256 | p = PyUnicode_AS_UNICODE(unicode); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4257 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4258 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4259 | Py_UCS4 ch; |
| 4260 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 4261 | if (e-q<4) { |
| 4262 | if (consumed) |
| 4263 | break; |
| 4264 | errmsg = "truncated data"; |
| 4265 | startinpos = ((const char *)q)-starts; |
| 4266 | endinpos = ((const char *)e)-starts; |
| 4267 | goto utf32Error; |
| 4268 | /* The remaining input chars are ignored if the callback |
| 4269 | chooses to skip the input */ |
| 4270 | } |
| 4271 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 4272 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4273 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4274 | if (ch >= 0x110000) |
| 4275 | { |
| 4276 | errmsg = "codepoint not in range(0x110000)"; |
| 4277 | startinpos = ((const char *)q)-starts; |
| 4278 | endinpos = startinpos+4; |
| 4279 | goto utf32Error; |
| 4280 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4281 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4282 | if (ch >= 0x10000) |
| 4283 | { |
| 4284 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 4285 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 4286 | } |
| 4287 | else |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4288 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4289 | *p++ = ch; |
| 4290 | q += 4; |
| 4291 | continue; |
| 4292 | utf32Error: |
| 4293 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 4294 | if (unicode_decode_call_errorhandler( |
| 4295 | errors, &errorHandler, |
| 4296 | "utf32", errmsg, |
| 4297 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
| 4298 | &unicode, &outpos, &p)) |
| 4299 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4300 | } |
| 4301 | |
| 4302 | if (byteorder) |
| 4303 | *byteorder = bo; |
| 4304 | |
| 4305 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4306 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4307 | |
| 4308 | /* Adjust length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4309 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4310 | goto onError; |
| 4311 | |
| 4312 | Py_XDECREF(errorHandler); |
| 4313 | Py_XDECREF(exc); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4314 | if (PyUnicode_READY(unicode) == -1) { |
| 4315 | Py_DECREF(unicode); |
| 4316 | return NULL; |
| 4317 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4318 | return (PyObject *)unicode; |
| 4319 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4320 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4321 | Py_DECREF(unicode); |
| 4322 | Py_XDECREF(errorHandler); |
| 4323 | Py_XDECREF(exc); |
| 4324 | return NULL; |
| 4325 | } |
| 4326 | |
| 4327 | PyObject * |
| 4328 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4329 | Py_ssize_t size, |
| 4330 | const char *errors, |
| 4331 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4332 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4333 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4334 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4335 | Py_ssize_t nsize, bytesize; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4336 | #ifndef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4337 | Py_ssize_t i, pairs; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4338 | #else |
| 4339 | const int pairs = 0; |
| 4340 | #endif |
| 4341 | /* Offsets from p for storing byte pairs in the right order. */ |
| 4342 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4343 | int iorder[] = {0, 1, 2, 3}; |
| 4344 | #else |
| 4345 | int iorder[] = {3, 2, 1, 0}; |
| 4346 | #endif |
| 4347 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4348 | #define STORECHAR(CH) \ |
| 4349 | do { \ |
| 4350 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 4351 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 4352 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 4353 | p[iorder[0]] = (CH) & 0xff; \ |
| 4354 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4355 | } while(0) |
| 4356 | |
| 4357 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 4358 | so we need less space. */ |
| 4359 | #ifndef Py_UNICODE_WIDE |
| 4360 | for (i = pairs = 0; i < size-1; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4361 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 4362 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 4363 | pairs++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4364 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4365 | nsize = (size - pairs + (byteorder == 0)); |
| 4366 | bytesize = nsize * 4; |
| 4367 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4368 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4369 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4370 | if (v == NULL) |
| 4371 | return NULL; |
| 4372 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4373 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4374 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4375 | STORECHAR(0xFEFF); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4376 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4377 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4378 | |
| 4379 | if (byteorder == -1) { |
| 4380 | /* force LE */ |
| 4381 | iorder[0] = 0; |
| 4382 | iorder[1] = 1; |
| 4383 | iorder[2] = 2; |
| 4384 | iorder[3] = 3; |
| 4385 | } |
| 4386 | else if (byteorder == 1) { |
| 4387 | /* force BE */ |
| 4388 | iorder[0] = 3; |
| 4389 | iorder[1] = 2; |
| 4390 | iorder[2] = 1; |
| 4391 | iorder[3] = 0; |
| 4392 | } |
| 4393 | |
| 4394 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4395 | Py_UCS4 ch = *s++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4396 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4397 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 4398 | Py_UCS4 ch2 = *s; |
| 4399 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 4400 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 4401 | s++; |
| 4402 | size--; |
| 4403 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4404 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4405 | #endif |
| 4406 | STORECHAR(ch); |
| 4407 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4408 | |
| 4409 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4410 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4411 | #undef STORECHAR |
| 4412 | } |
| 4413 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4414 | PyObject * |
| 4415 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4416 | { |
| 4417 | if (!PyUnicode_Check(unicode)) { |
| 4418 | PyErr_BadArgument(); |
| 4419 | return NULL; |
| 4420 | } |
| 4421 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4422 | PyUnicode_GET_SIZE(unicode), |
| 4423 | NULL, |
| 4424 | 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4425 | } |
| 4426 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4427 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 4428 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4429 | PyObject * |
| 4430 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4431 | Py_ssize_t size, |
| 4432 | const char *errors, |
| 4433 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4434 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4435 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 4436 | } |
| 4437 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4438 | /* Two masks for fast checking of whether a C 'long' may contain |
| 4439 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 4440 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 4441 | rare in most input. |
| 4442 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 4443 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4444 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4445 | #if (SIZEOF_LONG == 8) |
| 4446 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 4447 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 4448 | #elif (SIZEOF_LONG == 4) |
| 4449 | # define FAST_CHAR_MASK 0x80008000L |
| 4450 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 4451 | #else |
| 4452 | # error C 'long' size should be either 4 or 8! |
| 4453 | #endif |
| 4454 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4455 | PyObject * |
| 4456 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4457 | Py_ssize_t size, |
| 4458 | const char *errors, |
| 4459 | int *byteorder, |
| 4460 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4461 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4462 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4463 | Py_ssize_t startinpos; |
| 4464 | Py_ssize_t endinpos; |
| 4465 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4466 | PyUnicodeObject *unicode; |
| 4467 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4468 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4469 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4470 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4471 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4472 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 4473 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4474 | int ihi = 1, ilo = 0; |
| 4475 | #else |
| 4476 | int ihi = 0, ilo = 1; |
| 4477 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4478 | PyObject *errorHandler = NULL; |
| 4479 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4480 | |
| 4481 | /* Note: size will always be longer than the resulting Unicode |
| 4482 | character count */ |
| 4483 | unicode = _PyUnicode_New(size); |
| 4484 | if (!unicode) |
| 4485 | return NULL; |
| 4486 | if (size == 0) |
| 4487 | return (PyObject *)unicode; |
| 4488 | |
| 4489 | /* Unpack UTF-16 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4490 | p = PyUnicode_AS_UNICODE(unicode); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4491 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4492 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4493 | |
| 4494 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4495 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4496 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 4497 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4498 | byte order setting accordingly. In native mode, the leading BOM |
| 4499 | mark is skipped, in all other modes, it is copied to the output |
| 4500 | stream as-is (giving a ZWNBSP character). */ |
| 4501 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4502 | if (size >= 2) { |
| 4503 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 4504 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4505 | if (bom == 0xFEFF) { |
| 4506 | q += 2; |
| 4507 | bo = -1; |
| 4508 | } |
| 4509 | else if (bom == 0xFFFE) { |
| 4510 | q += 2; |
| 4511 | bo = 1; |
| 4512 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4513 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4514 | if (bom == 0xFEFF) { |
| 4515 | q += 2; |
| 4516 | bo = 1; |
| 4517 | } |
| 4518 | else if (bom == 0xFFFE) { |
| 4519 | q += 2; |
| 4520 | bo = -1; |
| 4521 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 4522 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4523 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 4524 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4525 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4526 | if (bo == -1) { |
| 4527 | /* force LE */ |
| 4528 | ihi = 1; |
| 4529 | ilo = 0; |
| 4530 | } |
| 4531 | else if (bo == 1) { |
| 4532 | /* force BE */ |
| 4533 | ihi = 0; |
| 4534 | ilo = 1; |
| 4535 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4536 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4537 | native_ordering = ilo < ihi; |
| 4538 | #else |
| 4539 | native_ordering = ilo > ihi; |
| 4540 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4541 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4542 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4543 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4544 | Py_UNICODE ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4545 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 4546 | reads are more expensive, better to defer to another iteration. */ |
| 4547 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 4548 | /* Fast path for runs of non-surrogate chars. */ |
| 4549 | register const unsigned char *_q = q; |
| 4550 | Py_UNICODE *_p = p; |
| 4551 | if (native_ordering) { |
| 4552 | /* Native ordering is simple: as long as the input cannot |
| 4553 | possibly contain a surrogate char, do an unrolled copy |
| 4554 | of several 16-bit code points to the target object. |
| 4555 | The non-surrogate check is done on several input bytes |
| 4556 | at a time (as many as a C 'long' can contain). */ |
| 4557 | while (_q < aligned_end) { |
| 4558 | unsigned long data = * (unsigned long *) _q; |
| 4559 | if (data & FAST_CHAR_MASK) |
| 4560 | break; |
| 4561 | _p[0] = ((unsigned short *) _q)[0]; |
| 4562 | _p[1] = ((unsigned short *) _q)[1]; |
| 4563 | #if (SIZEOF_LONG == 8) |
| 4564 | _p[2] = ((unsigned short *) _q)[2]; |
| 4565 | _p[3] = ((unsigned short *) _q)[3]; |
| 4566 | #endif |
| 4567 | _q += SIZEOF_LONG; |
| 4568 | _p += SIZEOF_LONG / 2; |
| 4569 | } |
| 4570 | } |
| 4571 | else { |
| 4572 | /* Byteswapped ordering is similar, but we must decompose |
| 4573 | the copy bytewise, and take care of zero'ing out the |
| 4574 | upper bytes if the target object is in 32-bit units |
| 4575 | (that is, in UCS-4 builds). */ |
| 4576 | while (_q < aligned_end) { |
| 4577 | unsigned long data = * (unsigned long *) _q; |
| 4578 | if (data & SWAPPED_FAST_CHAR_MASK) |
| 4579 | break; |
| 4580 | /* Zero upper bytes in UCS-4 builds */ |
| 4581 | #if (Py_UNICODE_SIZE > 2) |
| 4582 | _p[0] = 0; |
| 4583 | _p[1] = 0; |
| 4584 | #if (SIZEOF_LONG == 8) |
| 4585 | _p[2] = 0; |
| 4586 | _p[3] = 0; |
| 4587 | #endif |
| 4588 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 4589 | /* Issue #4916; UCS-4 builds on big endian machines must |
| 4590 | fill the two last bytes of each 4-byte unit. */ |
| 4591 | #if (!defined(BYTEORDER_IS_LITTLE_ENDIAN) && Py_UNICODE_SIZE > 2) |
| 4592 | # define OFF 2 |
| 4593 | #else |
| 4594 | # define OFF 0 |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4595 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 4596 | ((unsigned char *) _p)[OFF + 1] = _q[0]; |
| 4597 | ((unsigned char *) _p)[OFF + 0] = _q[1]; |
| 4598 | ((unsigned char *) _p)[OFF + 1 + Py_UNICODE_SIZE] = _q[2]; |
| 4599 | ((unsigned char *) _p)[OFF + 0 + Py_UNICODE_SIZE] = _q[3]; |
| 4600 | #if (SIZEOF_LONG == 8) |
| 4601 | ((unsigned char *) _p)[OFF + 1 + 2 * Py_UNICODE_SIZE] = _q[4]; |
| 4602 | ((unsigned char *) _p)[OFF + 0 + 2 * Py_UNICODE_SIZE] = _q[5]; |
| 4603 | ((unsigned char *) _p)[OFF + 1 + 3 * Py_UNICODE_SIZE] = _q[6]; |
| 4604 | ((unsigned char *) _p)[OFF + 0 + 3 * Py_UNICODE_SIZE] = _q[7]; |
| 4605 | #endif |
| 4606 | #undef OFF |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4607 | _q += SIZEOF_LONG; |
| 4608 | _p += SIZEOF_LONG / 2; |
| 4609 | } |
| 4610 | } |
| 4611 | p = _p; |
| 4612 | q = _q; |
| 4613 | if (q >= e) |
| 4614 | break; |
| 4615 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4616 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4617 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4618 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4619 | |
| 4620 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 4621 | *p++ = ch; |
| 4622 | continue; |
| 4623 | } |
| 4624 | |
| 4625 | /* UTF-16 code pair: */ |
| 4626 | if (q > e) { |
| 4627 | errmsg = "unexpected end of data"; |
| 4628 | startinpos = (((const char *)q) - 2) - starts; |
| 4629 | endinpos = ((const char *)e) + 1 - starts; |
| 4630 | goto utf16Error; |
| 4631 | } |
| 4632 | if (0xD800 <= ch && ch <= 0xDBFF) { |
| 4633 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 4634 | q += 2; |
| 4635 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 4636 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4637 | *p++ = ch; |
| 4638 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4639 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4640 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4641 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4642 | continue; |
| 4643 | } |
| 4644 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4645 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4646 | startinpos = (((const char *)q)-4)-starts; |
| 4647 | endinpos = startinpos+2; |
| 4648 | goto utf16Error; |
| 4649 | } |
| 4650 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4651 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4652 | errmsg = "illegal encoding"; |
| 4653 | startinpos = (((const char *)q)-2)-starts; |
| 4654 | endinpos = startinpos+2; |
| 4655 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4656 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4657 | utf16Error: |
| 4658 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 4659 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4660 | errors, |
| 4661 | &errorHandler, |
| 4662 | "utf16", errmsg, |
| 4663 | &starts, |
| 4664 | (const char **)&e, |
| 4665 | &startinpos, |
| 4666 | &endinpos, |
| 4667 | &exc, |
| 4668 | (const char **)&q, |
| 4669 | &unicode, |
| 4670 | &outpos, |
| 4671 | &p)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4672 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4673 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4674 | /* remaining byte at the end? (size should be even) */ |
| 4675 | if (e == q) { |
| 4676 | if (!consumed) { |
| 4677 | errmsg = "truncated data"; |
| 4678 | startinpos = ((const char *)q) - starts; |
| 4679 | endinpos = ((const char *)e) + 1 - starts; |
| 4680 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 4681 | if (unicode_decode_call_errorhandler( |
| 4682 | errors, |
| 4683 | &errorHandler, |
| 4684 | "utf16", errmsg, |
| 4685 | &starts, |
| 4686 | (const char **)&e, |
| 4687 | &startinpos, |
| 4688 | &endinpos, |
| 4689 | &exc, |
| 4690 | (const char **)&q, |
| 4691 | &unicode, |
| 4692 | &outpos, |
| 4693 | &p)) |
| 4694 | goto onError; |
| 4695 | /* The remaining input chars are ignored if the callback |
| 4696 | chooses to skip the input */ |
| 4697 | } |
| 4698 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4699 | |
| 4700 | if (byteorder) |
| 4701 | *byteorder = bo; |
| 4702 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4703 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4704 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4705 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4706 | /* Adjust length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4707 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4708 | goto onError; |
| 4709 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4710 | Py_XDECREF(errorHandler); |
| 4711 | Py_XDECREF(exc); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4712 | if (PyUnicode_READY(unicode) == -1) { |
| 4713 | Py_DECREF(unicode); |
| 4714 | return NULL; |
| 4715 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4716 | return (PyObject *)unicode; |
| 4717 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4718 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4719 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4720 | Py_XDECREF(errorHandler); |
| 4721 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4722 | return NULL; |
| 4723 | } |
| 4724 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4725 | #undef FAST_CHAR_MASK |
| 4726 | #undef SWAPPED_FAST_CHAR_MASK |
| 4727 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4728 | PyObject * |
| 4729 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4730 | Py_ssize_t size, |
| 4731 | const char *errors, |
| 4732 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4733 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4734 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4735 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4736 | Py_ssize_t nsize, bytesize; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4737 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4738 | Py_ssize_t i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4739 | #else |
| 4740 | const int pairs = 0; |
| 4741 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4742 | /* Offsets from p for storing byte pairs in the right order. */ |
| 4743 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4744 | int ihi = 1, ilo = 0; |
| 4745 | #else |
| 4746 | int ihi = 0, ilo = 1; |
| 4747 | #endif |
| 4748 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4749 | #define STORECHAR(CH) \ |
| 4750 | do { \ |
| 4751 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 4752 | p[ilo] = (CH) & 0xff; \ |
| 4753 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4754 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4755 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4756 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4757 | for (i = pairs = 0; i < size; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4758 | if (s[i] >= 0x10000) |
| 4759 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4760 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4761 | /* 2 * (size + pairs + (byteorder == 0)) */ |
| 4762 | if (size > PY_SSIZE_T_MAX || |
| 4763 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4764 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4765 | nsize = size + pairs + (byteorder == 0); |
| 4766 | bytesize = nsize * 2; |
| 4767 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4768 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4769 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4770 | if (v == NULL) |
| 4771 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4772 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4773 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4774 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4775 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 4776 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4777 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4778 | |
| 4779 | if (byteorder == -1) { |
| 4780 | /* force LE */ |
| 4781 | ihi = 1; |
| 4782 | ilo = 0; |
| 4783 | } |
| 4784 | else if (byteorder == 1) { |
| 4785 | /* force BE */ |
| 4786 | ihi = 0; |
| 4787 | ilo = 1; |
| 4788 | } |
| 4789 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4790 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4791 | Py_UNICODE ch = *s++; |
| 4792 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4793 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4794 | if (ch >= 0x10000) { |
| 4795 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 4796 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 4797 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4798 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4799 | STORECHAR(ch); |
| 4800 | if (ch2) |
| 4801 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4802 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4803 | |
| 4804 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4805 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4806 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4807 | } |
| 4808 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4809 | PyObject * |
| 4810 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4811 | { |
| 4812 | if (!PyUnicode_Check(unicode)) { |
| 4813 | PyErr_BadArgument(); |
| 4814 | return NULL; |
| 4815 | } |
| 4816 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4817 | PyUnicode_GET_SIZE(unicode), |
| 4818 | NULL, |
| 4819 | 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4820 | } |
| 4821 | |
| 4822 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 4823 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4824 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 4825 | if all the escapes in the string make it still a valid ASCII string. |
| 4826 | Returns -1 if any escapes were found which cause the string to |
| 4827 | pop out of ASCII range. Otherwise returns the length of the |
| 4828 | required buffer to hold the string. |
| 4829 | */ |
| 4830 | Py_ssize_t |
| 4831 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 4832 | { |
| 4833 | const unsigned char *p = (const unsigned char *)s; |
| 4834 | const unsigned char *end = p + size; |
| 4835 | Py_ssize_t length = 0; |
| 4836 | |
| 4837 | if (size < 0) |
| 4838 | return -1; |
| 4839 | |
| 4840 | for (; p < end; ++p) { |
| 4841 | if (*p > 127) { |
| 4842 | /* Non-ASCII */ |
| 4843 | return -1; |
| 4844 | } |
| 4845 | else if (*p != '\\') { |
| 4846 | /* Normal character */ |
| 4847 | ++length; |
| 4848 | } |
| 4849 | else { |
| 4850 | /* Backslash-escape, check next char */ |
| 4851 | ++p; |
| 4852 | /* Escape sequence reaches till end of string or |
| 4853 | non-ASCII follow-up. */ |
| 4854 | if (p >= end || *p > 127) |
| 4855 | return -1; |
| 4856 | switch (*p) { |
| 4857 | case '\n': |
| 4858 | /* backslash + \n result in zero characters */ |
| 4859 | break; |
| 4860 | case '\\': case '\'': case '\"': |
| 4861 | case 'b': case 'f': case 't': |
| 4862 | case 'n': case 'r': case 'v': case 'a': |
| 4863 | ++length; |
| 4864 | break; |
| 4865 | case '0': case '1': case '2': case '3': |
| 4866 | case '4': case '5': case '6': case '7': |
| 4867 | case 'x': case 'u': case 'U': case 'N': |
| 4868 | /* these do not guarantee ASCII characters */ |
| 4869 | return -1; |
| 4870 | default: |
| 4871 | /* count the backslash + the other character */ |
| 4872 | length += 2; |
| 4873 | } |
| 4874 | } |
| 4875 | } |
| 4876 | return length; |
| 4877 | } |
| 4878 | |
| 4879 | /* Similar to PyUnicode_WRITE but either write into wstr field |
| 4880 | or treat string as ASCII. */ |
| 4881 | #define WRITE_ASCII_OR_WSTR(kind, buf, index, value) \ |
| 4882 | do { \ |
| 4883 | if ((kind) != PyUnicode_WCHAR_KIND) \ |
| 4884 | ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \ |
| 4885 | else \ |
| 4886 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \ |
| 4887 | } while (0) |
| 4888 | |
| 4889 | #define WRITE_WSTR(buf, index, value) \ |
| 4890 | assert(kind == PyUnicode_WCHAR_KIND), \ |
| 4891 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value) |
| 4892 | |
| 4893 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 4894 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 4895 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4896 | PyObject * |
| 4897 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4898 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 4899 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4900 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4901 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4902 | Py_ssize_t startinpos; |
| 4903 | Py_ssize_t endinpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4904 | int j; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4905 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4906 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4907 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 4908 | char* message; |
| 4909 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4910 | PyObject *errorHandler = NULL; |
| 4911 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4912 | Py_ssize_t ascii_length; |
| 4913 | Py_ssize_t i; |
| 4914 | int kind; |
| 4915 | void *data; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 4916 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4917 | ascii_length = length_of_escaped_ascii_string(s, size); |
| 4918 | |
| 4919 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 4920 | either the string is pure ASCII with named escapes like \n, etc. |
| 4921 | and we determined it's exact size (common case) |
| 4922 | or it contains \x, \u, ... escape sequences. then we create a |
| 4923 | legacy wchar string and resize it at the end of this function. */ |
| 4924 | if (ascii_length >= 0) { |
| 4925 | v = (PyUnicodeObject *)PyUnicode_New(ascii_length, 127); |
| 4926 | if (!v) |
| 4927 | goto onError; |
| 4928 | assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND); |
| 4929 | kind = PyUnicode_1BYTE_KIND; |
| 4930 | data = PyUnicode_DATA(v); |
| 4931 | } |
| 4932 | else { |
| 4933 | /* Escaped strings will always be longer than the resulting |
| 4934 | Unicode string, so we start with size here and then reduce the |
| 4935 | length after conversion to the true value. |
| 4936 | (but if the error callback returns a long replacement string |
| 4937 | we'll have to allocate more space) */ |
| 4938 | v = _PyUnicode_New(size); |
| 4939 | if (!v) |
| 4940 | goto onError; |
| 4941 | kind = PyUnicode_WCHAR_KIND; |
| 4942 | data = PyUnicode_AS_UNICODE(v); |
| 4943 | } |
| 4944 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4945 | if (size == 0) |
| 4946 | return (PyObject *)v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4947 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4948 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 4949 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4950 | while (s < end) { |
| 4951 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 4952 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4953 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4954 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4955 | if (kind == PyUnicode_WCHAR_KIND) { |
| 4956 | assert(i < _PyUnicode_WSTR_LENGTH(v)); |
| 4957 | } |
| 4958 | else { |
| 4959 | /* The only case in which i == ascii_length is a backslash |
| 4960 | followed by a newline. */ |
| 4961 | assert(i <= ascii_length); |
| 4962 | } |
| 4963 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4964 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 4965 | if (*s != '\\') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4966 | WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char) *s++); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4967 | continue; |
| 4968 | } |
| 4969 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4970 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4971 | /* \ - Escapes */ |
| 4972 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 4973 | c = *s++; |
| 4974 | if (s > end) |
| 4975 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4976 | |
| 4977 | if (kind == PyUnicode_WCHAR_KIND) { |
| 4978 | assert(i < _PyUnicode_WSTR_LENGTH(v)); |
| 4979 | } |
| 4980 | else { |
| 4981 | /* The only case in which i == ascii_length is a backslash |
| 4982 | followed by a newline. */ |
| 4983 | assert(i < ascii_length || (i == ascii_length && c == '\n')); |
| 4984 | } |
| 4985 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 4986 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4987 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4988 | /* \x escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4989 | case '\n': break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4990 | case '\\': WRITE_ASCII_OR_WSTR(kind, data, i++, '\\'); break; |
| 4991 | case '\'': WRITE_ASCII_OR_WSTR(kind, data, i++, '\''); break; |
| 4992 | case '\"': WRITE_ASCII_OR_WSTR(kind, data, i++, '\"'); break; |
| 4993 | case 'b': WRITE_ASCII_OR_WSTR(kind, data, i++, '\b'); break; |
| 4994 | /* FF */ |
| 4995 | case 'f': WRITE_ASCII_OR_WSTR(kind, data, i++, '\014'); break; |
| 4996 | case 't': WRITE_ASCII_OR_WSTR(kind, data, i++, '\t'); break; |
| 4997 | case 'n': WRITE_ASCII_OR_WSTR(kind, data, i++, '\n'); break; |
| 4998 | case 'r': WRITE_ASCII_OR_WSTR(kind, data, i++, '\r'); break; |
| 4999 | /* VT */ |
| 5000 | case 'v': WRITE_ASCII_OR_WSTR(kind, data, i++, '\013'); break; |
| 5001 | /* BEL, not classic C */ |
| 5002 | case 'a': WRITE_ASCII_OR_WSTR(kind, data, i++, '\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5003 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5004 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5005 | case '0': case '1': case '2': case '3': |
| 5006 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5007 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5008 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5009 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5010 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5011 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5012 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5013 | WRITE_WSTR(data, i++, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5014 | break; |
| 5015 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5016 | /* hex escapes */ |
| 5017 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5018 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5019 | digits = 2; |
| 5020 | message = "truncated \\xXX escape"; |
| 5021 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5022 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5023 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5024 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5025 | digits = 4; |
| 5026 | message = "truncated \\uXXXX escape"; |
| 5027 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5028 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5029 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5030 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5031 | digits = 8; |
| 5032 | message = "truncated \\UXXXXXXXX escape"; |
| 5033 | hexescape: |
| 5034 | chr = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5035 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5036 | if (s+digits>end) { |
| 5037 | endinpos = size; |
| 5038 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5039 | errors, &errorHandler, |
| 5040 | "unicodeescape", "end of string in escape sequence", |
| 5041 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5042 | &v, &i, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5043 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5044 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5045 | goto nextByte; |
| 5046 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5047 | for (j = 0; j < digits; ++j) { |
| 5048 | c = (unsigned char) s[j]; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5049 | if (!Py_ISXDIGIT(c)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5050 | endinpos = (s+j+1)-starts; |
| 5051 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5052 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5053 | errors, &errorHandler, |
| 5054 | "unicodeescape", message, |
| 5055 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5056 | &v, &i, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5057 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5058 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5059 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5060 | } |
| 5061 | chr = (chr<<4) & ~0xF; |
| 5062 | if (c >= '0' && c <= '9') |
| 5063 | chr += c - '0'; |
| 5064 | else if (c >= 'a' && c <= 'f') |
| 5065 | chr += 10 + c - 'a'; |
| 5066 | else |
| 5067 | chr += 10 + c - 'A'; |
| 5068 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5069 | s += j; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5070 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5071 | /* _decoding_error will have already written into the |
| 5072 | target buffer. */ |
| 5073 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5074 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5075 | /* when we get here, chr is a 32-bit unicode character */ |
| 5076 | if (chr <= 0xffff) |
| 5077 | /* UCS-2 character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5078 | WRITE_WSTR(data, i++, chr); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5079 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5080 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5081 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 5082 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5083 | WRITE_WSTR(data, i++, chr); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5084 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5085 | chr -= 0x10000L; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5086 | WRITE_WSTR(data, i++, 0xD800 + (Py_UNICODE) (chr >> 10)); |
| 5087 | WRITE_WSTR(data, i++, 0xDC00 + (Py_UNICODE) (chr & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5088 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5089 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5090 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5091 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5092 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5093 | errors, &errorHandler, |
| 5094 | "unicodeescape", "illegal Unicode character", |
| 5095 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5096 | &v, &i, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5097 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5098 | data = PyUnicode_AS_UNICODE(v); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5099 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5100 | break; |
| 5101 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5102 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5103 | case 'N': |
| 5104 | message = "malformed \\N character escape"; |
| 5105 | if (ucnhash_CAPI == NULL) { |
| 5106 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5107 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5108 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5109 | if (ucnhash_CAPI == NULL) |
| 5110 | goto ucnhashError; |
| 5111 | } |
| 5112 | if (*s == '{') { |
| 5113 | const char *start = s+1; |
| 5114 | /* look for the closing brace */ |
| 5115 | while (*s != '}' && s < end) |
| 5116 | s++; |
| 5117 | if (s > start && s < end && *s == '}') { |
| 5118 | /* found a name. look it up in the unicode database */ |
| 5119 | message = "unknown Unicode character name"; |
| 5120 | s++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5121 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
| 5122 | &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5123 | goto store; |
| 5124 | } |
| 5125 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5126 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5127 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5128 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5129 | errors, &errorHandler, |
| 5130 | "unicodeescape", message, |
| 5131 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5132 | &v, &i, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5133 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5134 | data = PyUnicode_AS_UNICODE(v); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5135 | break; |
| 5136 | |
| 5137 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5138 | if (s > end) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5139 | assert(kind == PyUnicode_WCHAR_KIND); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5140 | message = "\\ at end of string"; |
| 5141 | s--; |
| 5142 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5143 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5144 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5145 | errors, &errorHandler, |
| 5146 | "unicodeescape", message, |
| 5147 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5148 | &v, &i, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5149 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5150 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5151 | } |
| 5152 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5153 | WRITE_ASCII_OR_WSTR(kind, data, i++, '\\'); |
| 5154 | WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char)s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5155 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5156 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5157 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5158 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5159 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5160 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5161 | /* Ensure the length prediction worked in case of ASCII strings */ |
| 5162 | assert(kind == PyUnicode_WCHAR_KIND || i == ascii_length); |
| 5163 | |
| 5164 | if (kind == PyUnicode_WCHAR_KIND && (_PyUnicode_Resize(&v, i) < 0 || |
| 5165 | PyUnicode_READY(v) == -1)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5166 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5167 | Py_XDECREF(errorHandler); |
| 5168 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5169 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5170 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5171 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5172 | PyErr_SetString( |
| 5173 | PyExc_UnicodeError, |
| 5174 | "\\N escapes not supported (can't load unicodedata module)" |
| 5175 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5176 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5177 | Py_XDECREF(errorHandler); |
| 5178 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5179 | return NULL; |
| 5180 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5181 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5182 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5183 | Py_XDECREF(errorHandler); |
| 5184 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5185 | return NULL; |
| 5186 | } |
| 5187 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5188 | #undef WRITE_ASCII_OR_WSTR |
| 5189 | #undef WRITE_WSTR |
| 5190 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5191 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5192 | |
| 5193 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5194 | appropriate. |
| 5195 | |
| 5196 | */ |
| 5197 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5198 | static const char *hexdigits = "0123456789abcdef"; |
| 5199 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5200 | PyObject * |
| 5201 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5202 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5203 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5204 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5205 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5206 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5207 | #ifdef Py_UNICODE_WIDE |
| 5208 | const Py_ssize_t expandsize = 10; |
| 5209 | #else |
| 5210 | const Py_ssize_t expandsize = 6; |
| 5211 | #endif |
| 5212 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5213 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 5214 | better to choose a different scheme. Perhaps scan the |
| 5215 | first N-chars of the string and allocate based on that size. |
| 5216 | */ |
| 5217 | /* Initial allocation is based on the longest-possible unichr |
| 5218 | escape. |
| 5219 | |
| 5220 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 5221 | unichr, so in this case it's the longest unichr escape. In |
| 5222 | narrow (UTF-16) builds this is five chars per source unichr |
| 5223 | since there are two unichrs in the surrogate pair, so in narrow |
| 5224 | (UTF-16) builds it's not the longest unichr escape. |
| 5225 | |
| 5226 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 5227 | so in the narrow (UTF-16) build case it's the longest unichr |
| 5228 | escape. |
| 5229 | */ |
| 5230 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5231 | if (size == 0) |
| 5232 | return PyBytes_FromStringAndSize(NULL, 0); |
| 5233 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5234 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5235 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5236 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5237 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5238 | 2 |
| 5239 | + expandsize*size |
| 5240 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5241 | if (repr == NULL) |
| 5242 | return NULL; |
| 5243 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5244 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5245 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5246 | while (size-- > 0) { |
| 5247 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5248 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5249 | /* Escape backslashes */ |
| 5250 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5251 | *p++ = '\\'; |
| 5252 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5253 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5254 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5255 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 5256 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5257 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 5258 | else if (ch >= 0x10000) { |
| 5259 | *p++ = '\\'; |
| 5260 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5261 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 5262 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 5263 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 5264 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 5265 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 5266 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 5267 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 5268 | *p++ = hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5269 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5270 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5271 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5272 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 5273 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 5274 | Py_UNICODE ch2; |
| 5275 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5276 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5277 | ch2 = *s++; |
| 5278 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 5279 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5280 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 5281 | *p++ = '\\'; |
| 5282 | *p++ = 'U'; |
| 5283 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 5284 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 5285 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 5286 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 5287 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 5288 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 5289 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 5290 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 5291 | continue; |
| 5292 | } |
| 5293 | /* Fall through: isolated surrogates are copied as-is */ |
| 5294 | s--; |
| 5295 | size++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5296 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5297 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5298 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5299 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5300 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5301 | *p++ = '\\'; |
| 5302 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5303 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 5304 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 5305 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 5306 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5307 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5308 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5309 | /* Map special whitespace to '\t', \n', '\r' */ |
| 5310 | else if (ch == '\t') { |
| 5311 | *p++ = '\\'; |
| 5312 | *p++ = 't'; |
| 5313 | } |
| 5314 | else if (ch == '\n') { |
| 5315 | *p++ = '\\'; |
| 5316 | *p++ = 'n'; |
| 5317 | } |
| 5318 | else if (ch == '\r') { |
| 5319 | *p++ = '\\'; |
| 5320 | *p++ = 'r'; |
| 5321 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5322 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5323 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 5324 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5325 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5326 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5327 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 5328 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5329 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5330 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5331 | /* Copy everything else as-is */ |
| 5332 | else |
| 5333 | *p++ = (char) ch; |
| 5334 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5335 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5336 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 5337 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 5338 | return NULL; |
| 5339 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5340 | } |
| 5341 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5342 | PyObject * |
| 5343 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5344 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 5345 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5346 | if (!PyUnicode_Check(unicode)) { |
| 5347 | PyErr_BadArgument(); |
| 5348 | return NULL; |
| 5349 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5350 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 5351 | PyUnicode_GET_SIZE(unicode)); |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 5352 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5353 | } |
| 5354 | |
| 5355 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 5356 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5357 | PyObject * |
| 5358 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5359 | Py_ssize_t size, |
| 5360 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5361 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5362 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5363 | Py_ssize_t startinpos; |
| 5364 | Py_ssize_t endinpos; |
| 5365 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5366 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5367 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5368 | const char *end; |
| 5369 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5370 | PyObject *errorHandler = NULL; |
| 5371 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5372 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5373 | /* Escaped strings will always be longer than the resulting |
| 5374 | 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] | 5375 | length after conversion to the true value. (But decoding error |
| 5376 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5377 | v = _PyUnicode_New(size); |
| 5378 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5379 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5380 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5381 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5382 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5383 | end = s + size; |
| 5384 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5385 | unsigned char c; |
| 5386 | Py_UCS4 x; |
| 5387 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5388 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5389 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5390 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5391 | if (*s != '\\') { |
| 5392 | *p++ = (unsigned char)*s++; |
| 5393 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5394 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5395 | startinpos = s-starts; |
| 5396 | |
| 5397 | /* \u-escapes are only interpreted iff the number of leading |
| 5398 | backslashes if odd */ |
| 5399 | bs = s; |
| 5400 | for (;s < end;) { |
| 5401 | if (*s != '\\') |
| 5402 | break; |
| 5403 | *p++ = (unsigned char)*s++; |
| 5404 | } |
| 5405 | if (((s - bs) & 1) == 0 || |
| 5406 | s >= end || |
| 5407 | (*s != 'u' && *s != 'U')) { |
| 5408 | continue; |
| 5409 | } |
| 5410 | p--; |
| 5411 | count = *s=='u' ? 4 : 8; |
| 5412 | s++; |
| 5413 | |
| 5414 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
| 5415 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 5416 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 5417 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5418 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5419 | endinpos = s-starts; |
| 5420 | if (unicode_decode_call_errorhandler( |
| 5421 | errors, &errorHandler, |
| 5422 | "rawunicodeescape", "truncated \\uXXXX", |
| 5423 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 5424 | &v, &outpos, &p)) |
| 5425 | goto onError; |
| 5426 | goto nextByte; |
| 5427 | } |
| 5428 | x = (x<<4) & ~0xF; |
| 5429 | if (c >= '0' && c <= '9') |
| 5430 | x += c - '0'; |
| 5431 | else if (c >= 'a' && c <= 'f') |
| 5432 | x += 10 + c - 'a'; |
| 5433 | else |
| 5434 | x += 10 + c - 'A'; |
| 5435 | } |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5436 | if (x <= 0xffff) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5437 | /* UCS-2 character */ |
| 5438 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5439 | else if (x <= 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5440 | /* UCS-4 character. Either store directly, or as |
| 5441 | surrogate pair. */ |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5442 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5443 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5444 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5445 | x -= 0x10000L; |
| 5446 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 5447 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5448 | #endif |
| 5449 | } else { |
| 5450 | endinpos = s-starts; |
| 5451 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5452 | if (unicode_decode_call_errorhandler( |
| 5453 | errors, &errorHandler, |
| 5454 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5455 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 5456 | &v, &outpos, &p)) |
| 5457 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5458 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5459 | nextByte: |
| 5460 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5461 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5462 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5463 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5464 | Py_XDECREF(errorHandler); |
| 5465 | Py_XDECREF(exc); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5466 | if (PyUnicode_READY(v) == -1) { |
| 5467 | Py_DECREF(v); |
| 5468 | return NULL; |
| 5469 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5470 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5471 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5472 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5473 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5474 | Py_XDECREF(errorHandler); |
| 5475 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5476 | return NULL; |
| 5477 | } |
| 5478 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5479 | PyObject * |
| 5480 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5481 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5482 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5483 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5484 | char *p; |
| 5485 | char *q; |
| 5486 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5487 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5488 | const Py_ssize_t expandsize = 10; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5489 | #else |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5490 | const Py_ssize_t expandsize = 6; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5491 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5492 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5493 | if (size > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5494 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5495 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5496 | repr = PyBytes_FromStringAndSize(NULL, expandsize * size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5497 | if (repr == NULL) |
| 5498 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 5499 | if (size == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5500 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5501 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5502 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5503 | while (size-- > 0) { |
| 5504 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5505 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5506 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 5507 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5508 | *p++ = '\\'; |
| 5509 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 5510 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 5511 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 5512 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 5513 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 5514 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 5515 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 5516 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 5517 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5518 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5519 | else |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5520 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5521 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 5522 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 5523 | Py_UNICODE ch2; |
| 5524 | Py_UCS4 ucs; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5525 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5526 | ch2 = *s++; |
| 5527 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 5528 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5529 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 5530 | *p++ = '\\'; |
| 5531 | *p++ = 'U'; |
| 5532 | *p++ = hexdigits[(ucs >> 28) & 0xf]; |
| 5533 | *p++ = hexdigits[(ucs >> 24) & 0xf]; |
| 5534 | *p++ = hexdigits[(ucs >> 20) & 0xf]; |
| 5535 | *p++ = hexdigits[(ucs >> 16) & 0xf]; |
| 5536 | *p++ = hexdigits[(ucs >> 12) & 0xf]; |
| 5537 | *p++ = hexdigits[(ucs >> 8) & 0xf]; |
| 5538 | *p++ = hexdigits[(ucs >> 4) & 0xf]; |
| 5539 | *p++ = hexdigits[ucs & 0xf]; |
| 5540 | continue; |
| 5541 | } |
| 5542 | /* Fall through: isolated surrogates are copied as-is */ |
| 5543 | s--; |
| 5544 | size++; |
| 5545 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5546 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5547 | /* Map 16-bit characters to '\uxxxx' */ |
| 5548 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5549 | *p++ = '\\'; |
| 5550 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 5551 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 5552 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 5553 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 5554 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5555 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5556 | /* Copy everything else as-is */ |
| 5557 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5558 | *p++ = (char) ch; |
| 5559 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5560 | size = p - q; |
| 5561 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5562 | assert(size > 0); |
| 5563 | if (_PyBytes_Resize(&repr, size) < 0) |
| 5564 | return NULL; |
| 5565 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5566 | } |
| 5567 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5568 | PyObject * |
| 5569 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5570 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 5571 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5572 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 5573 | PyErr_BadArgument(); |
| 5574 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5575 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 5576 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 5577 | PyUnicode_GET_SIZE(unicode)); |
| 5578 | |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 5579 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5580 | } |
| 5581 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5582 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 5583 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5584 | PyObject * |
| 5585 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5586 | Py_ssize_t size, |
| 5587 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5588 | { |
| 5589 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5590 | Py_ssize_t startinpos; |
| 5591 | Py_ssize_t endinpos; |
| 5592 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5593 | PyUnicodeObject *v; |
| 5594 | Py_UNICODE *p; |
| 5595 | const char *end; |
| 5596 | const char *reason; |
| 5597 | PyObject *errorHandler = NULL; |
| 5598 | PyObject *exc = NULL; |
| 5599 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 5600 | #ifdef Py_UNICODE_WIDE |
| 5601 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 5602 | #endif |
| 5603 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5604 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5605 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 5606 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5607 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5608 | /* Intentionally PyUnicode_GET_SIZE instead of PyUnicode_GET_LENGTH |
| 5609 | as string was created with the old API. */ |
| 5610 | if (PyUnicode_GET_SIZE(v) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5611 | return (PyObject *)v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5612 | p = PyUnicode_AS_UNICODE(v); |
| 5613 | end = s + size; |
| 5614 | |
| 5615 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5616 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5617 | /* We have to sanity check the raw data, otherwise doom looms for |
| 5618 | some malformed UCS-4 data. */ |
| 5619 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5620 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5621 | *p > unimax || *p < 0 || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5622 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5623 | end-s < Py_UNICODE_SIZE |
| 5624 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5625 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5626 | startinpos = s - starts; |
| 5627 | if (end-s < Py_UNICODE_SIZE) { |
| 5628 | endinpos = end-starts; |
| 5629 | reason = "truncated input"; |
| 5630 | } |
| 5631 | else { |
| 5632 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 5633 | reason = "illegal code point (> 0x10FFFF)"; |
| 5634 | } |
| 5635 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 5636 | if (unicode_decode_call_errorhandler( |
| 5637 | errors, &errorHandler, |
| 5638 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 5639 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 5640 | &v, &outpos, &p)) { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5641 | goto onError; |
| 5642 | } |
| 5643 | } |
| 5644 | else { |
| 5645 | p++; |
| 5646 | s += Py_UNICODE_SIZE; |
| 5647 | } |
| 5648 | } |
| 5649 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5650 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5651 | goto onError; |
| 5652 | Py_XDECREF(errorHandler); |
| 5653 | Py_XDECREF(exc); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5654 | if (PyUnicode_READY(v) == -1) { |
| 5655 | Py_DECREF(v); |
| 5656 | return NULL; |
| 5657 | } |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5658 | return (PyObject *)v; |
| 5659 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5660 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5661 | Py_XDECREF(v); |
| 5662 | Py_XDECREF(errorHandler); |
| 5663 | Py_XDECREF(exc); |
| 5664 | return NULL; |
| 5665 | } |
| 5666 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5667 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 5668 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5669 | PyObject * |
| 5670 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5671 | Py_ssize_t size, |
| 5672 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5673 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5674 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 5675 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5676 | } |
| 5677 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5678 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5679 | static void |
| 5680 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5681 | const char *encoding, |
| 5682 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5683 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5684 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5685 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5686 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5687 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 5688 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5689 | } |
| 5690 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5691 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 5692 | goto onError; |
| 5693 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 5694 | goto onError; |
| 5695 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 5696 | goto onError; |
| 5697 | return; |
| 5698 | onError: |
| 5699 | Py_DECREF(*exceptionObject); |
| 5700 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5701 | } |
| 5702 | } |
| 5703 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5704 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5705 | static void |
| 5706 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5707 | const char *encoding, |
| 5708 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5709 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5710 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5711 | { |
| 5712 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5713 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5714 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5715 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5716 | } |
| 5717 | |
| 5718 | /* error handling callback helper: |
| 5719 | build arguments, call the callback and check the arguments, |
| 5720 | put the result into newpos and return the replacement string, which |
| 5721 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5722 | static PyObject * |
| 5723 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5724 | PyObject **errorHandler, |
| 5725 | const char *encoding, const char *reason, |
| 5726 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 5727 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5728 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5729 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5730 | 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] | 5731 | |
| 5732 | PyObject *restuple; |
| 5733 | PyObject *resunicode; |
| 5734 | |
| 5735 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5736 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5737 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5738 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5739 | } |
| 5740 | |
| 5741 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5742 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5743 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5744 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5745 | |
| 5746 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5747 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5748 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5749 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5750 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5751 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5752 | Py_DECREF(restuple); |
| 5753 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5754 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5755 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5756 | &resunicode, newpos)) { |
| 5757 | Py_DECREF(restuple); |
| 5758 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5759 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5760 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 5761 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 5762 | Py_DECREF(restuple); |
| 5763 | return NULL; |
| 5764 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5765 | if (*newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5766 | *newpos = size+*newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5767 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5768 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 5769 | Py_DECREF(restuple); |
| 5770 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5771 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5772 | Py_INCREF(resunicode); |
| 5773 | Py_DECREF(restuple); |
| 5774 | return resunicode; |
| 5775 | } |
| 5776 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5777 | static PyObject * |
| 5778 | unicode_encode_ucs1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5779 | Py_ssize_t size, |
| 5780 | const char *errors, |
| 5781 | int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5782 | { |
| 5783 | /* output object */ |
| 5784 | PyObject *res; |
| 5785 | /* pointers to the beginning and end+1 of input */ |
| 5786 | const Py_UNICODE *startp = p; |
| 5787 | const Py_UNICODE *endp = p + size; |
| 5788 | /* pointer to the beginning of the unencodable characters */ |
| 5789 | /* const Py_UNICODE *badp = NULL; */ |
| 5790 | /* pointer into the output */ |
| 5791 | char *str; |
| 5792 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5793 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5794 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 5795 | 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] | 5796 | PyObject *errorHandler = NULL; |
| 5797 | PyObject *exc = NULL; |
| 5798 | /* the following variable is used for caching string comparisons |
| 5799 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 5800 | int known_errorHandler = -1; |
| 5801 | |
| 5802 | /* allocate enough for a simple encoding without |
| 5803 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5804 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5805 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5806 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5807 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5808 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5809 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5810 | ressize = size; |
| 5811 | |
| 5812 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5813 | Py_UNICODE c = *p; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5814 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5815 | /* can we encode this? */ |
| 5816 | if (c<limit) { |
| 5817 | /* no overflow check, because we know that the space is enough */ |
| 5818 | *str++ = (char)c; |
| 5819 | ++p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5820 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5821 | else { |
| 5822 | Py_ssize_t unicodepos = p-startp; |
| 5823 | Py_ssize_t requiredsize; |
| 5824 | PyObject *repunicode; |
| 5825 | Py_ssize_t repsize; |
| 5826 | Py_ssize_t newpos; |
| 5827 | Py_ssize_t respos; |
| 5828 | Py_UNICODE *uni2; |
| 5829 | /* startpos for collecting unencodable chars */ |
| 5830 | const Py_UNICODE *collstart = p; |
| 5831 | const Py_UNICODE *collend = p; |
| 5832 | /* find all unecodable characters */ |
| 5833 | while ((collend < endp) && ((*collend)>=limit)) |
| 5834 | ++collend; |
| 5835 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 5836 | if (known_errorHandler==-1) { |
| 5837 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5838 | known_errorHandler = 1; |
| 5839 | else if (!strcmp(errors, "replace")) |
| 5840 | known_errorHandler = 2; |
| 5841 | else if (!strcmp(errors, "ignore")) |
| 5842 | known_errorHandler = 3; |
| 5843 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5844 | known_errorHandler = 4; |
| 5845 | else |
| 5846 | known_errorHandler = 0; |
| 5847 | } |
| 5848 | switch (known_errorHandler) { |
| 5849 | case 1: /* strict */ |
| 5850 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 5851 | goto onError; |
| 5852 | case 2: /* replace */ |
| 5853 | while (collstart++<collend) |
| 5854 | *str++ = '?'; /* fall through */ |
| 5855 | case 3: /* ignore */ |
| 5856 | p = collend; |
| 5857 | break; |
| 5858 | case 4: /* xmlcharrefreplace */ |
| 5859 | respos = str - PyBytes_AS_STRING(res); |
| 5860 | /* determine replacement size (temporarily (mis)uses p) */ |
| 5861 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 5862 | if (*p<10) |
| 5863 | repsize += 2+1+1; |
| 5864 | else if (*p<100) |
| 5865 | repsize += 2+2+1; |
| 5866 | else if (*p<1000) |
| 5867 | repsize += 2+3+1; |
| 5868 | else if (*p<10000) |
| 5869 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 5870 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5871 | else |
| 5872 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 5873 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5874 | else if (*p<100000) |
| 5875 | repsize += 2+5+1; |
| 5876 | else if (*p<1000000) |
| 5877 | repsize += 2+6+1; |
| 5878 | else |
| 5879 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5880 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5881 | } |
| 5882 | requiredsize = respos+repsize+(endp-collend); |
| 5883 | if (requiredsize > ressize) { |
| 5884 | if (requiredsize<2*ressize) |
| 5885 | requiredsize = 2*ressize; |
| 5886 | if (_PyBytes_Resize(&res, requiredsize)) |
| 5887 | goto onError; |
| 5888 | str = PyBytes_AS_STRING(res) + respos; |
| 5889 | ressize = requiredsize; |
| 5890 | } |
| 5891 | /* generate replacement (temporarily (mis)uses p) */ |
| 5892 | for (p = collstart; p < collend; ++p) { |
| 5893 | str += sprintf(str, "&#%d;", (int)*p); |
| 5894 | } |
| 5895 | p = collend; |
| 5896 | break; |
| 5897 | default: |
| 5898 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 5899 | encoding, reason, startp, size, &exc, |
| 5900 | collstart-startp, collend-startp, &newpos); |
| 5901 | if (repunicode == NULL) |
| 5902 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5903 | if (PyBytes_Check(repunicode)) { |
| 5904 | /* Directly copy bytes result to output. */ |
| 5905 | repsize = PyBytes_Size(repunicode); |
| 5906 | if (repsize > 1) { |
| 5907 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 5908 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5909 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 5910 | Py_DECREF(repunicode); |
| 5911 | goto onError; |
| 5912 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 5913 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5914 | ressize += repsize-1; |
| 5915 | } |
| 5916 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 5917 | str += repsize; |
| 5918 | p = startp + newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5919 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5920 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5921 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5922 | /* need more space? (at least enough for what we |
| 5923 | have+the replacement+the rest of the string, so |
| 5924 | we won't have to check space for encodable characters) */ |
| 5925 | respos = str - PyBytes_AS_STRING(res); |
| 5926 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5927 | requiredsize = respos+repsize+(endp-collend); |
| 5928 | if (requiredsize > ressize) { |
| 5929 | if (requiredsize<2*ressize) |
| 5930 | requiredsize = 2*ressize; |
| 5931 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 5932 | Py_DECREF(repunicode); |
| 5933 | goto onError; |
| 5934 | } |
| 5935 | str = PyBytes_AS_STRING(res) + respos; |
| 5936 | ressize = requiredsize; |
| 5937 | } |
| 5938 | /* check if there is anything unencodable in the replacement |
| 5939 | and copy it to the output */ |
| 5940 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 5941 | c = *uni2; |
| 5942 | if (c >= limit) { |
| 5943 | raise_encode_exception(&exc, encoding, startp, size, |
| 5944 | unicodepos, unicodepos+1, reason); |
| 5945 | Py_DECREF(repunicode); |
| 5946 | goto onError; |
| 5947 | } |
| 5948 | *str = (char)c; |
| 5949 | } |
| 5950 | p = startp + newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5951 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5952 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5953 | } |
| 5954 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5955 | /* Resize if we allocated to much */ |
| 5956 | size = str - PyBytes_AS_STRING(res); |
| 5957 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 5958 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5959 | if (_PyBytes_Resize(&res, size) < 0) |
| 5960 | goto onError; |
| 5961 | } |
| 5962 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5963 | Py_XDECREF(errorHandler); |
| 5964 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5965 | return res; |
| 5966 | |
| 5967 | onError: |
| 5968 | Py_XDECREF(res); |
| 5969 | Py_XDECREF(errorHandler); |
| 5970 | Py_XDECREF(exc); |
| 5971 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5972 | } |
| 5973 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5974 | PyObject * |
| 5975 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5976 | Py_ssize_t size, |
| 5977 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5978 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5979 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5980 | } |
| 5981 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5982 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5983 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5984 | { |
| 5985 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5986 | PyErr_BadArgument(); |
| 5987 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5988 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5989 | if (PyUnicode_READY(unicode) == -1) |
| 5990 | return NULL; |
| 5991 | /* Fast path: if it is a one-byte string, construct |
| 5992 | bytes object directly. */ |
| 5993 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 5994 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 5995 | PyUnicode_GET_LENGTH(unicode)); |
| 5996 | /* Non-Latin-1 characters present. Defer to above function to |
| 5997 | raise the exception. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5998 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5999 | PyUnicode_GET_SIZE(unicode), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6000 | errors); |
| 6001 | } |
| 6002 | |
| 6003 | PyObject* |
| 6004 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6005 | { |
| 6006 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6007 | } |
| 6008 | |
| 6009 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6010 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6011 | PyObject * |
| 6012 | PyUnicode_DecodeASCII(const char *s, |
| 6013 | Py_ssize_t size, |
| 6014 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6015 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6016 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6017 | PyUnicodeObject *v; |
| 6018 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6019 | Py_ssize_t startinpos; |
| 6020 | Py_ssize_t endinpos; |
| 6021 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6022 | const char *e; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6023 | unsigned char* d; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6024 | PyObject *errorHandler = NULL; |
| 6025 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6026 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6027 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6028 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6029 | if (size == 1 && *(unsigned char*)s < 128) |
| 6030 | return PyUnicode_FromOrdinal(*(unsigned char*)s); |
| 6031 | |
| 6032 | /* Fast path. Assume the input actually *is* ASCII, and allocate |
| 6033 | a single-block Unicode object with that assumption. If there is |
| 6034 | an error, drop the object and start over. */ |
| 6035 | v = (PyUnicodeObject*)PyUnicode_New(size, 127); |
| 6036 | if (v == NULL) |
| 6037 | goto onError; |
| 6038 | d = PyUnicode_1BYTE_DATA(v); |
| 6039 | for (i = 0; i < size; i++) { |
| 6040 | unsigned char ch = ((unsigned char*)s)[i]; |
| 6041 | if (ch < 128) |
| 6042 | d[i] = ch; |
| 6043 | else |
| 6044 | break; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6045 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6046 | if (i == size) |
| 6047 | return (PyObject*)v; |
| 6048 | Py_DECREF(v); /* start over */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6049 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6050 | v = _PyUnicode_New(size); |
| 6051 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6052 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6053 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6054 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6055 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6056 | e = s + size; |
| 6057 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6058 | register unsigned char c = (unsigned char)*s; |
| 6059 | if (c < 128) { |
| 6060 | *p++ = c; |
| 6061 | ++s; |
| 6062 | } |
| 6063 | else { |
| 6064 | startinpos = s-starts; |
| 6065 | endinpos = startinpos + 1; |
| 6066 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
| 6067 | if (unicode_decode_call_errorhandler( |
| 6068 | errors, &errorHandler, |
| 6069 | "ascii", "ordinal not in range(128)", |
| 6070 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 6071 | &v, &outpos, &p)) |
| 6072 | goto onError; |
| 6073 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6074 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6075 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6076 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 6077 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6078 | Py_XDECREF(errorHandler); |
| 6079 | Py_XDECREF(exc); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6080 | if (PyUnicode_READY(v) == -1) { |
| 6081 | Py_DECREF(v); |
| 6082 | return NULL; |
| 6083 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6084 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6085 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6086 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6087 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6088 | Py_XDECREF(errorHandler); |
| 6089 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6090 | return NULL; |
| 6091 | } |
| 6092 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6093 | PyObject * |
| 6094 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6095 | Py_ssize_t size, |
| 6096 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6097 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6098 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6099 | } |
| 6100 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6101 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6102 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6103 | { |
| 6104 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6105 | PyErr_BadArgument(); |
| 6106 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6107 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6108 | if (PyUnicode_READY(unicode) == -1) |
| 6109 | return NULL; |
| 6110 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6111 | directly. Else defer to above function to raise the exception. */ |
| 6112 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 6113 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6114 | PyUnicode_GET_LENGTH(unicode)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6115 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6116 | PyUnicode_GET_SIZE(unicode), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6117 | errors); |
| 6118 | } |
| 6119 | |
| 6120 | PyObject * |
| 6121 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6122 | { |
| 6123 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6124 | } |
| 6125 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6126 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6127 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6128 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6129 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6130 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6131 | #define NEED_RETRY |
| 6132 | #endif |
| 6133 | |
| 6134 | /* XXX This code is limited to "true" double-byte encodings, as |
| 6135 | a) it assumes an incomplete character consists of a single byte, and |
| 6136 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6137 | encodings, see IsDBCSLeadByteEx documentation. */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6138 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6139 | static int |
| 6140 | is_dbcs_lead_byte(const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6141 | { |
| 6142 | const char *curr = s + offset; |
| 6143 | |
| 6144 | if (IsDBCSLeadByte(*curr)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6145 | const char *prev = CharPrev(s, curr); |
| 6146 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6147 | } |
| 6148 | return 0; |
| 6149 | } |
| 6150 | |
| 6151 | /* |
| 6152 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 6153 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 6154 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6155 | static int |
| 6156 | decode_mbcs(PyUnicodeObject **v, |
| 6157 | const char *s, /* MBCS string */ |
| 6158 | int size, /* sizeof MBCS string */ |
| 6159 | int final, |
| 6160 | const char *errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6161 | { |
| 6162 | Py_UNICODE *p; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6163 | Py_ssize_t n; |
| 6164 | DWORD usize; |
| 6165 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6166 | |
| 6167 | assert(size >= 0); |
| 6168 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6169 | /* check and handle 'errors' arg */ |
| 6170 | if (errors==NULL || strcmp(errors, "strict")==0) |
| 6171 | flags = MB_ERR_INVALID_CHARS; |
| 6172 | else if (strcmp(errors, "ignore")==0) |
| 6173 | flags = 0; |
| 6174 | else { |
| 6175 | PyErr_Format(PyExc_ValueError, |
| 6176 | "mbcs encoding does not support errors='%s'", |
| 6177 | errors); |
| 6178 | return -1; |
| 6179 | } |
| 6180 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6181 | /* Skip trailing lead-byte unless 'final' is set */ |
| 6182 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6183 | --size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6184 | |
| 6185 | /* First get the size of the result */ |
| 6186 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6187 | usize = MultiByteToWideChar(CP_ACP, flags, s, size, NULL, 0); |
| 6188 | if (usize==0) |
| 6189 | goto mbcs_decode_error; |
| 6190 | } else |
| 6191 | usize = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6192 | |
| 6193 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6194 | /* Create unicode object */ |
| 6195 | *v = _PyUnicode_New(usize); |
| 6196 | if (*v == NULL) |
| 6197 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6198 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6199 | } |
| 6200 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6201 | /* Extend unicode object */ |
| 6202 | n = PyUnicode_GET_SIZE(*v); |
| 6203 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 6204 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6205 | } |
| 6206 | |
| 6207 | /* Do the conversion */ |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6208 | if (usize > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6209 | p = PyUnicode_AS_UNICODE(*v) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6210 | if (0 == MultiByteToWideChar(CP_ACP, flags, s, size, p, usize)) { |
| 6211 | goto mbcs_decode_error; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6212 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6213 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6214 | return size; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6215 | |
| 6216 | mbcs_decode_error: |
| 6217 | /* If the last error was ERROR_NO_UNICODE_TRANSLATION, then |
| 6218 | we raise a UnicodeDecodeError - else it is a 'generic' |
| 6219 | windows error |
| 6220 | */ |
| 6221 | if (GetLastError()==ERROR_NO_UNICODE_TRANSLATION) { |
| 6222 | /* Ideally, we should get reason from FormatMessage - this |
| 6223 | is the Windows 2000 English version of the message |
| 6224 | */ |
| 6225 | PyObject *exc = NULL; |
| 6226 | const char *reason = "No mapping for the Unicode character exists " |
| 6227 | "in the target multi-byte code page."; |
| 6228 | make_decode_exception(&exc, "mbcs", s, size, 0, 0, reason); |
| 6229 | if (exc != NULL) { |
| 6230 | PyCodec_StrictErrors(exc); |
| 6231 | Py_DECREF(exc); |
| 6232 | } |
| 6233 | } else { |
| 6234 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 6235 | } |
| 6236 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6237 | } |
| 6238 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6239 | PyObject * |
| 6240 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 6241 | Py_ssize_t size, |
| 6242 | const char *errors, |
| 6243 | Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6244 | { |
| 6245 | PyUnicodeObject *v = NULL; |
| 6246 | int done; |
| 6247 | |
| 6248 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6249 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6250 | |
| 6251 | #ifdef NEED_RETRY |
| 6252 | retry: |
| 6253 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6254 | done = decode_mbcs(&v, s, INT_MAX, 0, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6255 | else |
| 6256 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6257 | done = decode_mbcs(&v, s, (int)size, !consumed, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6258 | |
| 6259 | if (done < 0) { |
| 6260 | Py_XDECREF(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6261 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6262 | } |
| 6263 | |
| 6264 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6265 | *consumed += done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6266 | |
| 6267 | #ifdef NEED_RETRY |
| 6268 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6269 | s += done; |
| 6270 | size -= done; |
| 6271 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6272 | } |
| 6273 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6274 | if (PyUnicode_READY(v) == -1) { |
| 6275 | Py_DECREF(v); |
| 6276 | return NULL; |
| 6277 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6278 | return (PyObject *)v; |
| 6279 | } |
| 6280 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6281 | PyObject * |
| 6282 | PyUnicode_DecodeMBCS(const char *s, |
| 6283 | Py_ssize_t size, |
| 6284 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6285 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6286 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 6287 | } |
| 6288 | |
| 6289 | /* |
| 6290 | * Convert unicode into string object (MBCS). |
| 6291 | * Returns 0 if succeed, -1 otherwise. |
| 6292 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6293 | static int |
| 6294 | encode_mbcs(PyObject **repr, |
| 6295 | const Py_UNICODE *p, /* unicode */ |
| 6296 | int size, /* size of unicode */ |
| 6297 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6298 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6299 | BOOL usedDefaultChar = FALSE; |
| 6300 | BOOL *pusedDefaultChar; |
| 6301 | int mbcssize; |
| 6302 | Py_ssize_t n; |
| 6303 | PyObject *exc = NULL; |
| 6304 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6305 | |
| 6306 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6307 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6308 | /* check and handle 'errors' arg */ |
| 6309 | if (errors==NULL || strcmp(errors, "strict")==0) { |
| 6310 | flags = WC_NO_BEST_FIT_CHARS; |
| 6311 | pusedDefaultChar = &usedDefaultChar; |
| 6312 | } else if (strcmp(errors, "replace")==0) { |
| 6313 | flags = 0; |
| 6314 | pusedDefaultChar = NULL; |
| 6315 | } else { |
| 6316 | PyErr_Format(PyExc_ValueError, |
| 6317 | "mbcs encoding does not support errors='%s'", |
| 6318 | errors); |
| 6319 | return -1; |
| 6320 | } |
| 6321 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6322 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6323 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6324 | mbcssize = WideCharToMultiByte(CP_ACP, flags, p, size, NULL, 0, |
| 6325 | NULL, pusedDefaultChar); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6326 | if (mbcssize == 0) { |
| 6327 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 6328 | return -1; |
| 6329 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6330 | /* If we used a default char, then we failed! */ |
| 6331 | if (pusedDefaultChar && *pusedDefaultChar) |
| 6332 | goto mbcs_encode_error; |
| 6333 | } else { |
| 6334 | mbcssize = 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6335 | } |
| 6336 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6337 | if (*repr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6338 | /* Create string object */ |
| 6339 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); |
| 6340 | if (*repr == NULL) |
| 6341 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6342 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6343 | } |
| 6344 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6345 | /* Extend string object */ |
| 6346 | n = PyBytes_Size(*repr); |
| 6347 | if (_PyBytes_Resize(repr, n + mbcssize) < 0) |
| 6348 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6349 | } |
| 6350 | |
| 6351 | /* Do the conversion */ |
| 6352 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6353 | char *s = PyBytes_AS_STRING(*repr) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6354 | if (0 == WideCharToMultiByte(CP_ACP, flags, p, size, s, mbcssize, |
| 6355 | NULL, pusedDefaultChar)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6356 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 6357 | return -1; |
| 6358 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6359 | if (pusedDefaultChar && *pusedDefaultChar) |
| 6360 | goto mbcs_encode_error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6361 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6362 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6363 | |
| 6364 | mbcs_encode_error: |
| 6365 | raise_encode_exception(&exc, "mbcs", p, size, 0, 0, "invalid character"); |
| 6366 | Py_XDECREF(exc); |
| 6367 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6368 | } |
| 6369 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6370 | PyObject * |
| 6371 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 6372 | Py_ssize_t size, |
| 6373 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6374 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6375 | PyObject *repr = NULL; |
| 6376 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 6377 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6378 | #ifdef NEED_RETRY |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6379 | retry: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6380 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6381 | ret = encode_mbcs(&repr, p, INT_MAX, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6382 | else |
| 6383 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6384 | ret = encode_mbcs(&repr, p, (int)size, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6385 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6386 | if (ret < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6387 | Py_XDECREF(repr); |
| 6388 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6389 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6390 | |
| 6391 | #ifdef NEED_RETRY |
| 6392 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6393 | p += INT_MAX; |
| 6394 | size -= INT_MAX; |
| 6395 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6396 | } |
| 6397 | #endif |
| 6398 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6399 | return repr; |
| 6400 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6401 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6402 | PyObject * |
| 6403 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 6404 | { |
| 6405 | if (!PyUnicode_Check(unicode)) { |
| 6406 | PyErr_BadArgument(); |
| 6407 | return NULL; |
| 6408 | } |
| 6409 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6410 | PyUnicode_GET_SIZE(unicode), |
| 6411 | NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 6412 | } |
| 6413 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6414 | #undef NEED_RETRY |
| 6415 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6416 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6417 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6418 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 6419 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6420 | PyObject * |
| 6421 | PyUnicode_DecodeCharmap(const char *s, |
| 6422 | Py_ssize_t size, |
| 6423 | PyObject *mapping, |
| 6424 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6425 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6426 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6427 | Py_ssize_t startinpos; |
| 6428 | Py_ssize_t endinpos; |
| 6429 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6430 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6431 | PyUnicodeObject *v; |
| 6432 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6433 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6434 | PyObject *errorHandler = NULL; |
| 6435 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 6436 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6437 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6438 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6439 | /* Default to Latin-1 */ |
| 6440 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6441 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6442 | |
| 6443 | v = _PyUnicode_New(size); |
| 6444 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6445 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6446 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6447 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6448 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6449 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 6450 | if (PyUnicode_CheckExact(mapping)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6451 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 6452 | maplen = PyUnicode_GET_SIZE(mapping); |
| 6453 | while (s < e) { |
| 6454 | unsigned char ch = *s; |
| 6455 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6456 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6457 | if (ch < maplen) |
| 6458 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6459 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6460 | if (x == 0xfffe) { |
| 6461 | /* undefined mapping */ |
| 6462 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 6463 | startinpos = s-starts; |
| 6464 | endinpos = startinpos+1; |
| 6465 | if (unicode_decode_call_errorhandler( |
| 6466 | errors, &errorHandler, |
| 6467 | "charmap", "character maps to <undefined>", |
| 6468 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 6469 | &v, &outpos, &p)) { |
| 6470 | goto onError; |
| 6471 | } |
| 6472 | continue; |
| 6473 | } |
| 6474 | *p++ = x; |
| 6475 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6476 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 6477 | } |
| 6478 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6479 | while (s < e) { |
| 6480 | unsigned char ch = *s; |
| 6481 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 6482 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6483 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 6484 | w = PyLong_FromLong((long)ch); |
| 6485 | if (w == NULL) |
| 6486 | goto onError; |
| 6487 | x = PyObject_GetItem(mapping, w); |
| 6488 | Py_DECREF(w); |
| 6489 | if (x == NULL) { |
| 6490 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 6491 | /* No mapping found means: mapping is undefined. */ |
| 6492 | PyErr_Clear(); |
| 6493 | x = Py_None; |
| 6494 | Py_INCREF(x); |
| 6495 | } else |
| 6496 | goto onError; |
| 6497 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6498 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6499 | /* Apply mapping */ |
| 6500 | if (PyLong_Check(x)) { |
| 6501 | long value = PyLong_AS_LONG(x); |
| 6502 | if (value < 0 || value > 65535) { |
| 6503 | PyErr_SetString(PyExc_TypeError, |
| 6504 | "character mapping must be in range(65536)"); |
| 6505 | Py_DECREF(x); |
| 6506 | goto onError; |
| 6507 | } |
| 6508 | *p++ = (Py_UNICODE)value; |
| 6509 | } |
| 6510 | else if (x == Py_None) { |
| 6511 | /* undefined mapping */ |
| 6512 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 6513 | startinpos = s-starts; |
| 6514 | endinpos = startinpos+1; |
| 6515 | if (unicode_decode_call_errorhandler( |
| 6516 | errors, &errorHandler, |
| 6517 | "charmap", "character maps to <undefined>", |
| 6518 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 6519 | &v, &outpos, &p)) { |
| 6520 | Py_DECREF(x); |
| 6521 | goto onError; |
| 6522 | } |
| 6523 | Py_DECREF(x); |
| 6524 | continue; |
| 6525 | } |
| 6526 | else if (PyUnicode_Check(x)) { |
| 6527 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6528 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6529 | if (targetsize == 1) |
| 6530 | /* 1-1 mapping */ |
| 6531 | *p++ = *PyUnicode_AS_UNICODE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6532 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6533 | else if (targetsize > 1) { |
| 6534 | /* 1-n mapping */ |
| 6535 | if (targetsize > extrachars) { |
| 6536 | /* resize first */ |
| 6537 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 6538 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 6539 | (targetsize << 2); |
| 6540 | extrachars += needed; |
| 6541 | /* XXX overflow detection missing */ |
| 6542 | if (_PyUnicode_Resize(&v, |
| 6543 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 6544 | Py_DECREF(x); |
| 6545 | goto onError; |
| 6546 | } |
| 6547 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 6548 | } |
| 6549 | Py_UNICODE_COPY(p, |
| 6550 | PyUnicode_AS_UNICODE(x), |
| 6551 | targetsize); |
| 6552 | p += targetsize; |
| 6553 | extrachars -= targetsize; |
| 6554 | } |
| 6555 | /* 1-0 mapping: skip the character */ |
| 6556 | } |
| 6557 | else { |
| 6558 | /* wrong return value */ |
| 6559 | PyErr_SetString(PyExc_TypeError, |
| 6560 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6561 | Py_DECREF(x); |
| 6562 | goto onError; |
| 6563 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6564 | Py_DECREF(x); |
| 6565 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6566 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6567 | } |
| 6568 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6569 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 6570 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6571 | Py_XDECREF(errorHandler); |
| 6572 | Py_XDECREF(exc); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6573 | if (PyUnicode_READY(v) == -1) { |
| 6574 | Py_DECREF(v); |
| 6575 | return NULL; |
| 6576 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6577 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6578 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6579 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6580 | Py_XDECREF(errorHandler); |
| 6581 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6582 | Py_XDECREF(v); |
| 6583 | return NULL; |
| 6584 | } |
| 6585 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6586 | /* Charmap encoding: the lookup table */ |
| 6587 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6588 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6589 | PyObject_HEAD |
| 6590 | unsigned char level1[32]; |
| 6591 | int count2, count3; |
| 6592 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6593 | }; |
| 6594 | |
| 6595 | static PyObject* |
| 6596 | encoding_map_size(PyObject *obj, PyObject* args) |
| 6597 | { |
| 6598 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6599 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6600 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6601 | } |
| 6602 | |
| 6603 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6604 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6605 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 6606 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6607 | }; |
| 6608 | |
| 6609 | static void |
| 6610 | encoding_map_dealloc(PyObject* o) |
| 6611 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6612 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6613 | } |
| 6614 | |
| 6615 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6616 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6617 | "EncodingMap", /*tp_name*/ |
| 6618 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 6619 | 0, /*tp_itemsize*/ |
| 6620 | /* methods */ |
| 6621 | encoding_map_dealloc, /*tp_dealloc*/ |
| 6622 | 0, /*tp_print*/ |
| 6623 | 0, /*tp_getattr*/ |
| 6624 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 6625 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6626 | 0, /*tp_repr*/ |
| 6627 | 0, /*tp_as_number*/ |
| 6628 | 0, /*tp_as_sequence*/ |
| 6629 | 0, /*tp_as_mapping*/ |
| 6630 | 0, /*tp_hash*/ |
| 6631 | 0, /*tp_call*/ |
| 6632 | 0, /*tp_str*/ |
| 6633 | 0, /*tp_getattro*/ |
| 6634 | 0, /*tp_setattro*/ |
| 6635 | 0, /*tp_as_buffer*/ |
| 6636 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 6637 | 0, /*tp_doc*/ |
| 6638 | 0, /*tp_traverse*/ |
| 6639 | 0, /*tp_clear*/ |
| 6640 | 0, /*tp_richcompare*/ |
| 6641 | 0, /*tp_weaklistoffset*/ |
| 6642 | 0, /*tp_iter*/ |
| 6643 | 0, /*tp_iternext*/ |
| 6644 | encoding_map_methods, /*tp_methods*/ |
| 6645 | 0, /*tp_members*/ |
| 6646 | 0, /*tp_getset*/ |
| 6647 | 0, /*tp_base*/ |
| 6648 | 0, /*tp_dict*/ |
| 6649 | 0, /*tp_descr_get*/ |
| 6650 | 0, /*tp_descr_set*/ |
| 6651 | 0, /*tp_dictoffset*/ |
| 6652 | 0, /*tp_init*/ |
| 6653 | 0, /*tp_alloc*/ |
| 6654 | 0, /*tp_new*/ |
| 6655 | 0, /*tp_free*/ |
| 6656 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6657 | }; |
| 6658 | |
| 6659 | PyObject* |
| 6660 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 6661 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6662 | PyObject *result; |
| 6663 | struct encoding_map *mresult; |
| 6664 | int i; |
| 6665 | int need_dict = 0; |
| 6666 | unsigned char level1[32]; |
| 6667 | unsigned char level2[512]; |
| 6668 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 6669 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6670 | int kind; |
| 6671 | void *data; |
| 6672 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6673 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6674 | if (!PyUnicode_Check(string) || PyUnicode_GET_LENGTH(string) != 256) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6675 | PyErr_BadArgument(); |
| 6676 | return NULL; |
| 6677 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6678 | kind = PyUnicode_KIND(string); |
| 6679 | data = PyUnicode_DATA(string); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6680 | memset(level1, 0xFF, sizeof level1); |
| 6681 | memset(level2, 0xFF, sizeof level2); |
| 6682 | |
| 6683 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 6684 | or if there are non-BMP characters, we need to use |
| 6685 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6686 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6687 | need_dict = 1; |
| 6688 | for (i = 1; i < 256; i++) { |
| 6689 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6690 | ch = PyUnicode_READ(kind, data, i); |
| 6691 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6692 | need_dict = 1; |
| 6693 | break; |
| 6694 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6695 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6696 | /* unmapped character */ |
| 6697 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6698 | l1 = ch >> 11; |
| 6699 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6700 | if (level1[l1] == 0xFF) |
| 6701 | level1[l1] = count2++; |
| 6702 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6703 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6704 | } |
| 6705 | |
| 6706 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 6707 | need_dict = 1; |
| 6708 | |
| 6709 | if (need_dict) { |
| 6710 | PyObject *result = PyDict_New(); |
| 6711 | PyObject *key, *value; |
| 6712 | if (!result) |
| 6713 | return NULL; |
| 6714 | for (i = 0; i < 256; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6715 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6716 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6717 | if (!key || !value) |
| 6718 | goto failed1; |
| 6719 | if (PyDict_SetItem(result, key, value) == -1) |
| 6720 | goto failed1; |
| 6721 | Py_DECREF(key); |
| 6722 | Py_DECREF(value); |
| 6723 | } |
| 6724 | return result; |
| 6725 | failed1: |
| 6726 | Py_XDECREF(key); |
| 6727 | Py_XDECREF(value); |
| 6728 | Py_DECREF(result); |
| 6729 | return NULL; |
| 6730 | } |
| 6731 | |
| 6732 | /* Create a three-level trie */ |
| 6733 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 6734 | 16*count2 + 128*count3 - 1); |
| 6735 | if (!result) |
| 6736 | return PyErr_NoMemory(); |
| 6737 | PyObject_Init(result, &EncodingMapType); |
| 6738 | mresult = (struct encoding_map*)result; |
| 6739 | mresult->count2 = count2; |
| 6740 | mresult->count3 = count3; |
| 6741 | mlevel1 = mresult->level1; |
| 6742 | mlevel2 = mresult->level23; |
| 6743 | mlevel3 = mresult->level23 + 16*count2; |
| 6744 | memcpy(mlevel1, level1, 32); |
| 6745 | memset(mlevel2, 0xFF, 16*count2); |
| 6746 | memset(mlevel3, 0, 128*count3); |
| 6747 | count3 = 0; |
| 6748 | for (i = 1; i < 256; i++) { |
| 6749 | int o1, o2, o3, i2, i3; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6750 | if (PyUnicode_READ(kind, data, i) == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6751 | /* unmapped character */ |
| 6752 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6753 | o1 = PyUnicode_READ(kind, data, i)>>11; |
| 6754 | o2 = (PyUnicode_READ(kind, data, i)>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6755 | i2 = 16*mlevel1[o1] + o2; |
| 6756 | if (mlevel2[i2] == 0xFF) |
| 6757 | mlevel2[i2] = count3++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6758 | o3 = PyUnicode_READ(kind, data, i) & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6759 | i3 = 128*mlevel2[i2] + o3; |
| 6760 | mlevel3[i3] = i; |
| 6761 | } |
| 6762 | return result; |
| 6763 | } |
| 6764 | |
| 6765 | static int |
| 6766 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 6767 | { |
| 6768 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 6769 | int l1 = c>>11; |
| 6770 | int l2 = (c>>7) & 0xF; |
| 6771 | int l3 = c & 0x7F; |
| 6772 | int i; |
| 6773 | |
| 6774 | #ifdef Py_UNICODE_WIDE |
| 6775 | if (c > 0xFFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6776 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6777 | } |
| 6778 | #endif |
| 6779 | if (c == 0) |
| 6780 | return 0; |
| 6781 | /* level 1*/ |
| 6782 | i = map->level1[l1]; |
| 6783 | if (i == 0xFF) { |
| 6784 | return -1; |
| 6785 | } |
| 6786 | /* level 2*/ |
| 6787 | i = map->level23[16*i+l2]; |
| 6788 | if (i == 0xFF) { |
| 6789 | return -1; |
| 6790 | } |
| 6791 | /* level 3 */ |
| 6792 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 6793 | if (i == 0) { |
| 6794 | return -1; |
| 6795 | } |
| 6796 | return i; |
| 6797 | } |
| 6798 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6799 | /* Lookup the character ch in the mapping. If the character |
| 6800 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 6801 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6802 | static PyObject * |
| 6803 | charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6804 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6805 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6806 | PyObject *x; |
| 6807 | |
| 6808 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6809 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6810 | x = PyObject_GetItem(mapping, w); |
| 6811 | Py_DECREF(w); |
| 6812 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6813 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 6814 | /* No mapping found means: mapping is undefined. */ |
| 6815 | PyErr_Clear(); |
| 6816 | x = Py_None; |
| 6817 | Py_INCREF(x); |
| 6818 | return x; |
| 6819 | } else |
| 6820 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6821 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 6822 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6823 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6824 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6825 | long value = PyLong_AS_LONG(x); |
| 6826 | if (value < 0 || value > 255) { |
| 6827 | PyErr_SetString(PyExc_TypeError, |
| 6828 | "character mapping must be in range(256)"); |
| 6829 | Py_DECREF(x); |
| 6830 | return NULL; |
| 6831 | } |
| 6832 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6833 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6834 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6835 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6836 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6837 | /* wrong return value */ |
| 6838 | PyErr_Format(PyExc_TypeError, |
| 6839 | "character mapping must return integer, bytes or None, not %.400s", |
| 6840 | x->ob_type->tp_name); |
| 6841 | Py_DECREF(x); |
| 6842 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6843 | } |
| 6844 | } |
| 6845 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6846 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6847 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6848 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6849 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 6850 | /* exponentially overallocate to minimize reallocations */ |
| 6851 | if (requiredsize < 2*outsize) |
| 6852 | requiredsize = 2*outsize; |
| 6853 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 6854 | return -1; |
| 6855 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6856 | } |
| 6857 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6858 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6859 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6860 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6861 | /* 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] | 6862 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6863 | space is available. Return a new reference to the object that |
| 6864 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 6865 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 6866 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6867 | static charmapencode_result |
| 6868 | charmapencode_output(Py_UNICODE c, PyObject *mapping, |
| 6869 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6870 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6871 | PyObject *rep; |
| 6872 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6873 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6874 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 6875 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6876 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6877 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6878 | if (res == -1) |
| 6879 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6880 | if (outsize<requiredsize) |
| 6881 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 6882 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6883 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6884 | outstart[(*outpos)++] = (char)res; |
| 6885 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6886 | } |
| 6887 | |
| 6888 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6889 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6890 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6891 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6892 | Py_DECREF(rep); |
| 6893 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6894 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6895 | if (PyLong_Check(rep)) { |
| 6896 | Py_ssize_t requiredsize = *outpos+1; |
| 6897 | if (outsize<requiredsize) |
| 6898 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 6899 | Py_DECREF(rep); |
| 6900 | return enc_EXCEPTION; |
| 6901 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6902 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6903 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6904 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6905 | else { |
| 6906 | const char *repchars = PyBytes_AS_STRING(rep); |
| 6907 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 6908 | Py_ssize_t requiredsize = *outpos+repsize; |
| 6909 | if (outsize<requiredsize) |
| 6910 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 6911 | Py_DECREF(rep); |
| 6912 | return enc_EXCEPTION; |
| 6913 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6914 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6915 | memcpy(outstart + *outpos, repchars, repsize); |
| 6916 | *outpos += repsize; |
| 6917 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6918 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6919 | Py_DECREF(rep); |
| 6920 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6921 | } |
| 6922 | |
| 6923 | /* handle an error in PyUnicode_EncodeCharmap |
| 6924 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6925 | static int |
| 6926 | charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6927 | 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] | 6928 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 6929 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6930 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6931 | { |
| 6932 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6933 | Py_ssize_t repsize; |
| 6934 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6935 | Py_UNICODE *uni2; |
| 6936 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6937 | Py_ssize_t collstartpos = *inpos; |
| 6938 | Py_ssize_t collendpos = *inpos+1; |
| 6939 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6940 | char *encoding = "charmap"; |
| 6941 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6942 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6943 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6944 | /* find all unencodable characters */ |
| 6945 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6946 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 6947 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6948 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 6949 | if (res != -1) |
| 6950 | break; |
| 6951 | ++collendpos; |
| 6952 | continue; |
| 6953 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6954 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6955 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 6956 | if (rep==NULL) |
| 6957 | return -1; |
| 6958 | else if (rep!=Py_None) { |
| 6959 | Py_DECREF(rep); |
| 6960 | break; |
| 6961 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6962 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6963 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6964 | } |
| 6965 | /* cache callback name lookup |
| 6966 | * (if not done yet, i.e. it's the first error) */ |
| 6967 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6968 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6969 | *known_errorHandler = 1; |
| 6970 | else if (!strcmp(errors, "replace")) |
| 6971 | *known_errorHandler = 2; |
| 6972 | else if (!strcmp(errors, "ignore")) |
| 6973 | *known_errorHandler = 3; |
| 6974 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6975 | *known_errorHandler = 4; |
| 6976 | else |
| 6977 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6978 | } |
| 6979 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6980 | case 1: /* strict */ |
| 6981 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 6982 | return -1; |
| 6983 | case 2: /* replace */ |
| 6984 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6985 | x = charmapencode_output('?', mapping, res, respos); |
| 6986 | if (x==enc_EXCEPTION) { |
| 6987 | return -1; |
| 6988 | } |
| 6989 | else if (x==enc_FAILED) { |
| 6990 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 6991 | return -1; |
| 6992 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6993 | } |
| 6994 | /* fall through */ |
| 6995 | case 3: /* ignore */ |
| 6996 | *inpos = collendpos; |
| 6997 | break; |
| 6998 | case 4: /* xmlcharrefreplace */ |
| 6999 | /* generate replacement (temporarily (mis)uses p) */ |
| 7000 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7001 | char buffer[2+29+1+1]; |
| 7002 | char *cp; |
| 7003 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 7004 | for (cp = buffer; *cp; ++cp) { |
| 7005 | x = charmapencode_output(*cp, mapping, res, respos); |
| 7006 | if (x==enc_EXCEPTION) |
| 7007 | return -1; |
| 7008 | else if (x==enc_FAILED) { |
| 7009 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7010 | return -1; |
| 7011 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7012 | } |
| 7013 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7014 | *inpos = collendpos; |
| 7015 | break; |
| 7016 | default: |
| 7017 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7018 | encoding, reason, p, size, exceptionObject, |
| 7019 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7020 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7021 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7022 | if (PyBytes_Check(repunicode)) { |
| 7023 | /* Directly copy bytes result to output. */ |
| 7024 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 7025 | Py_ssize_t requiredsize; |
| 7026 | repsize = PyBytes_Size(repunicode); |
| 7027 | requiredsize = *respos + repsize; |
| 7028 | if (requiredsize > outsize) |
| 7029 | /* Make room for all additional bytes. */ |
| 7030 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 7031 | Py_DECREF(repunicode); |
| 7032 | return -1; |
| 7033 | } |
| 7034 | memcpy(PyBytes_AsString(*res) + *respos, |
| 7035 | PyBytes_AsString(repunicode), repsize); |
| 7036 | *respos += repsize; |
| 7037 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7038 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7039 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7040 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7041 | /* generate replacement */ |
| 7042 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 7043 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7044 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 7045 | if (x==enc_EXCEPTION) { |
| 7046 | return -1; |
| 7047 | } |
| 7048 | else if (x==enc_FAILED) { |
| 7049 | Py_DECREF(repunicode); |
| 7050 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7051 | return -1; |
| 7052 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7053 | } |
| 7054 | *inpos = newpos; |
| 7055 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7056 | } |
| 7057 | return 0; |
| 7058 | } |
| 7059 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7060 | PyObject * |
| 7061 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 7062 | Py_ssize_t size, |
| 7063 | PyObject *mapping, |
| 7064 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7065 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7066 | /* output object */ |
| 7067 | PyObject *res = NULL; |
| 7068 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7069 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7070 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7071 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7072 | PyObject *errorHandler = NULL; |
| 7073 | PyObject *exc = NULL; |
| 7074 | /* the following variable is used for caching string comparisons |
| 7075 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 7076 | * 3=ignore, 4=xmlcharrefreplace */ |
| 7077 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7078 | |
| 7079 | /* Default to Latin-1 */ |
| 7080 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7081 | return PyUnicode_EncodeLatin1(p, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7082 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7083 | /* allocate enough for a simple encoding without |
| 7084 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7085 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7086 | if (res == NULL) |
| 7087 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 7088 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7089 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7090 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7091 | while (inpos<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7092 | /* try to encode it */ |
| 7093 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 7094 | if (x==enc_EXCEPTION) /* error */ |
| 7095 | goto onError; |
| 7096 | if (x==enc_FAILED) { /* unencodable character */ |
| 7097 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 7098 | &exc, |
| 7099 | &known_errorHandler, &errorHandler, errors, |
| 7100 | &res, &respos)) { |
| 7101 | goto onError; |
| 7102 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7103 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7104 | else |
| 7105 | /* done with this character => adjust input position */ |
| 7106 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7107 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7108 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7109 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7110 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 7111 | if (_PyBytes_Resize(&res, respos) < 0) |
| 7112 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7113 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7114 | Py_XDECREF(exc); |
| 7115 | Py_XDECREF(errorHandler); |
| 7116 | return res; |
| 7117 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7118 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7119 | Py_XDECREF(res); |
| 7120 | Py_XDECREF(exc); |
| 7121 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7122 | return NULL; |
| 7123 | } |
| 7124 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7125 | PyObject * |
| 7126 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 7127 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7128 | { |
| 7129 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7130 | PyErr_BadArgument(); |
| 7131 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7132 | } |
| 7133 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7134 | PyUnicode_GET_SIZE(unicode), |
| 7135 | mapping, |
| 7136 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7137 | } |
| 7138 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7139 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7140 | static void |
| 7141 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7142 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7143 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7144 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7145 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7146 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7147 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 7148 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7149 | } |
| 7150 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7151 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 7152 | goto onError; |
| 7153 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 7154 | goto onError; |
| 7155 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 7156 | goto onError; |
| 7157 | return; |
| 7158 | onError: |
| 7159 | Py_DECREF(*exceptionObject); |
| 7160 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7161 | } |
| 7162 | } |
| 7163 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7164 | /* raises a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7165 | static void |
| 7166 | raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7167 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7168 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7169 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7170 | { |
| 7171 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7172 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7173 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7174 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7175 | } |
| 7176 | |
| 7177 | /* error handling callback helper: |
| 7178 | build arguments, call the callback and check the arguments, |
| 7179 | put the result into newpos and return the replacement string, which |
| 7180 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7181 | static PyObject * |
| 7182 | unicode_translate_call_errorhandler(const char *errors, |
| 7183 | PyObject **errorHandler, |
| 7184 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7185 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7186 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7187 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7188 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7189 | 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] | 7190 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7191 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7192 | PyObject *restuple; |
| 7193 | PyObject *resunicode; |
| 7194 | |
| 7195 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7196 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7197 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7198 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7199 | } |
| 7200 | |
| 7201 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7202 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7203 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7204 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7205 | |
| 7206 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7207 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7208 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7209 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7210 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 7211 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7212 | Py_DECREF(restuple); |
| 7213 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7214 | } |
| 7215 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7216 | &resunicode, &i_newpos)) { |
| 7217 | Py_DECREF(restuple); |
| 7218 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7219 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7220 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7221 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7222 | else |
| 7223 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7224 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7225 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 7226 | Py_DECREF(restuple); |
| 7227 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 7228 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7229 | Py_INCREF(resunicode); |
| 7230 | Py_DECREF(restuple); |
| 7231 | return resunicode; |
| 7232 | } |
| 7233 | |
| 7234 | /* Lookup the character ch in the mapping and put the result in result, |
| 7235 | which must be decrefed by the caller. |
| 7236 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7237 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7238 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7239 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7240 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7241 | PyObject *x; |
| 7242 | |
| 7243 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7244 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7245 | x = PyObject_GetItem(mapping, w); |
| 7246 | Py_DECREF(w); |
| 7247 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7248 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7249 | /* No mapping found means: use 1:1 mapping. */ |
| 7250 | PyErr_Clear(); |
| 7251 | *result = NULL; |
| 7252 | return 0; |
| 7253 | } else |
| 7254 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7255 | } |
| 7256 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7257 | *result = x; |
| 7258 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7259 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7260 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7261 | long value = PyLong_AS_LONG(x); |
| 7262 | long max = PyUnicode_GetMax(); |
| 7263 | if (value < 0 || value > max) { |
| 7264 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 7265 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7266 | Py_DECREF(x); |
| 7267 | return -1; |
| 7268 | } |
| 7269 | *result = x; |
| 7270 | return 0; |
| 7271 | } |
| 7272 | else if (PyUnicode_Check(x)) { |
| 7273 | *result = x; |
| 7274 | return 0; |
| 7275 | } |
| 7276 | else { |
| 7277 | /* wrong return value */ |
| 7278 | PyErr_SetString(PyExc_TypeError, |
| 7279 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7280 | Py_DECREF(x); |
| 7281 | return -1; |
| 7282 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7283 | } |
| 7284 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7285 | if not reallocate and adjust various state variables. |
| 7286 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7287 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7288 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7289 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7290 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7291 | Py_ssize_t oldsize = *psize; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 7292 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7293 | /* exponentially overallocate to minimize reallocations */ |
| 7294 | if (requiredsize < 2 * oldsize) |
| 7295 | requiredsize = 2 * oldsize; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7296 | *outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 7297 | if (*outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7298 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7299 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7300 | } |
| 7301 | return 0; |
| 7302 | } |
| 7303 | /* lookup the character, put the result in the output string and adjust |
| 7304 | various state variables. Return a new reference to the object that |
| 7305 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 7306 | undefined (in which case no character was written). |
| 7307 | The called must decref result. |
| 7308 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7309 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7310 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 7311 | PyObject *mapping, Py_UCS4 **output, |
| 7312 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7313 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7314 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7315 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 7316 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7317 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7318 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7319 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7320 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7321 | } |
| 7322 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7323 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7324 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7325 | /* 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] | 7326 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7327 | } |
| 7328 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7329 | Py_ssize_t repsize; |
| 7330 | if (PyUnicode_READY(*res) == -1) |
| 7331 | return -1; |
| 7332 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7333 | if (repsize==1) { |
| 7334 | /* 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] | 7335 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7336 | } |
| 7337 | else if (repsize!=0) { |
| 7338 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7339 | Py_ssize_t requiredsize = *opos + |
| 7340 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7341 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7342 | Py_ssize_t i; |
| 7343 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7344 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7345 | for(i = 0; i < repsize; i++) |
| 7346 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7347 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7348 | } |
| 7349 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7350 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7351 | return 0; |
| 7352 | } |
| 7353 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7354 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7355 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 7356 | PyObject *mapping, |
| 7357 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7358 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7359 | /* input object */ |
| 7360 | char *idata; |
| 7361 | Py_ssize_t size, i; |
| 7362 | int kind; |
| 7363 | /* output buffer */ |
| 7364 | Py_UCS4 *output = NULL; |
| 7365 | Py_ssize_t osize; |
| 7366 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7367 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7368 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7369 | char *reason = "character maps to <undefined>"; |
| 7370 | PyObject *errorHandler = NULL; |
| 7371 | PyObject *exc = NULL; |
| 7372 | /* the following variable is used for caching string comparisons |
| 7373 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 7374 | * 3=ignore, 4=xmlcharrefreplace */ |
| 7375 | int known_errorHandler = -1; |
| 7376 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7377 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7378 | PyErr_BadArgument(); |
| 7379 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7380 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7381 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7382 | if (PyUnicode_READY(input) == -1) |
| 7383 | return NULL; |
| 7384 | idata = (char*)PyUnicode_DATA(input); |
| 7385 | kind = PyUnicode_KIND(input); |
| 7386 | size = PyUnicode_GET_LENGTH(input); |
| 7387 | i = 0; |
| 7388 | |
| 7389 | if (size == 0) { |
| 7390 | Py_INCREF(input); |
| 7391 | return input; |
| 7392 | } |
| 7393 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7394 | /* allocate enough for a simple 1:1 translation without |
| 7395 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7396 | osize = size; |
| 7397 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 7398 | opos = 0; |
| 7399 | if (output == NULL) { |
| 7400 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7401 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7402 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7403 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7404 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7405 | /* try to encode it */ |
| 7406 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7407 | if (charmaptranslate_output(input, i, mapping, |
| 7408 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7409 | Py_XDECREF(x); |
| 7410 | goto onError; |
| 7411 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7412 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7413 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7414 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7415 | else { /* untranslatable character */ |
| 7416 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 7417 | Py_ssize_t repsize; |
| 7418 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7419 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7420 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7421 | Py_ssize_t collstart = i; |
| 7422 | Py_ssize_t collend = i+1; |
| 7423 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7424 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7425 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7426 | while (collend < size) { |
| 7427 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7428 | goto onError; |
| 7429 | Py_XDECREF(x); |
| 7430 | if (x!=Py_None) |
| 7431 | break; |
| 7432 | ++collend; |
| 7433 | } |
| 7434 | /* cache callback name lookup |
| 7435 | * (if not done yet, i.e. it's the first error) */ |
| 7436 | if (known_errorHandler==-1) { |
| 7437 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 7438 | known_errorHandler = 1; |
| 7439 | else if (!strcmp(errors, "replace")) |
| 7440 | known_errorHandler = 2; |
| 7441 | else if (!strcmp(errors, "ignore")) |
| 7442 | known_errorHandler = 3; |
| 7443 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 7444 | known_errorHandler = 4; |
| 7445 | else |
| 7446 | known_errorHandler = 0; |
| 7447 | } |
| 7448 | switch (known_errorHandler) { |
| 7449 | case 1: /* strict */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7450 | raise_translate_exception(&exc, input, collstart, |
| 7451 | collend, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7452 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7453 | case 2: /* replace */ |
| 7454 | /* 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] | 7455 | for (coll = collstart; coll<collend; coll++) |
| 7456 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7457 | /* fall through */ |
| 7458 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7459 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7460 | break; |
| 7461 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7462 | /* generate replacement (temporarily (mis)uses i) */ |
| 7463 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7464 | char buffer[2+29+1+1]; |
| 7465 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7466 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 7467 | if (charmaptranslate_makespace(&output, &osize, |
| 7468 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7469 | goto onError; |
| 7470 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7471 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7472 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7473 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7474 | break; |
| 7475 | default: |
| 7476 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7477 | reason, input, &exc, |
| 7478 | collstart, collend, &newpos); |
| 7479 | if (repunicode == NULL || PyUnicode_READY(repunicode) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7480 | goto onError; |
| 7481 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7482 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 7483 | if (charmaptranslate_makespace(&output, &osize, |
| 7484 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7485 | Py_DECREF(repunicode); |
| 7486 | goto onError; |
| 7487 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7488 | for (uni2 = 0; repsize-->0; ++uni2) |
| 7489 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 7490 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7491 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7492 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7493 | } |
| 7494 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7495 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 7496 | if (!res) |
| 7497 | goto onError; |
| 7498 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7499 | Py_XDECREF(exc); |
| 7500 | Py_XDECREF(errorHandler); |
| 7501 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7502 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7503 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7504 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7505 | Py_XDECREF(exc); |
| 7506 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7507 | return NULL; |
| 7508 | } |
| 7509 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7510 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 7511 | PyObject * |
| 7512 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 7513 | Py_ssize_t size, |
| 7514 | PyObject *mapping, |
| 7515 | const char *errors) |
| 7516 | { |
| 7517 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 7518 | if (!unicode) |
| 7519 | return NULL; |
| 7520 | return _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 7521 | } |
| 7522 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7523 | PyObject * |
| 7524 | PyUnicode_Translate(PyObject *str, |
| 7525 | PyObject *mapping, |
| 7526 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7527 | { |
| 7528 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7529 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7530 | str = PyUnicode_FromObject(str); |
| 7531 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7532 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7533 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7534 | Py_DECREF(str); |
| 7535 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7536 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7537 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7538 | Py_XDECREF(str); |
| 7539 | return NULL; |
| 7540 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7541 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7542 | static Py_UCS4 |
| 7543 | fix_decimal_and_space_to_ascii(PyUnicodeObject *self) |
| 7544 | { |
| 7545 | /* No need to call PyUnicode_READY(self) because this function is only |
| 7546 | called as a callback from fixup() which does it already. */ |
| 7547 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 7548 | const int kind = PyUnicode_KIND(self); |
| 7549 | void *data = PyUnicode_DATA(self); |
| 7550 | Py_UCS4 maxchar = 0, ch, fixed; |
| 7551 | Py_ssize_t i; |
| 7552 | |
| 7553 | for (i = 0; i < len; ++i) { |
| 7554 | ch = PyUnicode_READ(kind, data, i); |
| 7555 | fixed = 0; |
| 7556 | if (ch > 127) { |
| 7557 | if (Py_UNICODE_ISSPACE(ch)) |
| 7558 | fixed = ' '; |
| 7559 | else { |
| 7560 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 7561 | if (decimal >= 0) |
| 7562 | fixed = '0' + decimal; |
| 7563 | } |
| 7564 | if (fixed != 0) { |
| 7565 | if (fixed > maxchar) |
| 7566 | maxchar = fixed; |
| 7567 | PyUnicode_WRITE(kind, data, i, fixed); |
| 7568 | } |
| 7569 | else if (ch > maxchar) |
| 7570 | maxchar = ch; |
| 7571 | } |
| 7572 | else if (ch > maxchar) |
| 7573 | maxchar = ch; |
| 7574 | } |
| 7575 | |
| 7576 | return maxchar; |
| 7577 | } |
| 7578 | |
| 7579 | PyObject * |
| 7580 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 7581 | { |
| 7582 | if (!PyUnicode_Check(unicode)) { |
| 7583 | PyErr_BadInternalCall(); |
| 7584 | return NULL; |
| 7585 | } |
| 7586 | if (PyUnicode_READY(unicode) == -1) |
| 7587 | return NULL; |
| 7588 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 7589 | /* If the string is already ASCII, just return the same string */ |
| 7590 | Py_INCREF(unicode); |
| 7591 | return unicode; |
| 7592 | } |
| 7593 | return fixup((PyUnicodeObject *)unicode, fix_decimal_and_space_to_ascii); |
| 7594 | } |
| 7595 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 7596 | PyObject * |
| 7597 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 7598 | Py_ssize_t length) |
| 7599 | { |
| 7600 | PyObject *result; |
| 7601 | Py_UNICODE *p; /* write pointer into result */ |
| 7602 | Py_ssize_t i; |
| 7603 | /* Copy to a new string */ |
| 7604 | result = (PyObject *)_PyUnicode_New(length); |
| 7605 | Py_UNICODE_COPY(PyUnicode_AS_UNICODE(result), s, length); |
| 7606 | if (result == NULL) |
| 7607 | return result; |
| 7608 | p = PyUnicode_AS_UNICODE(result); |
| 7609 | /* Iterate over code points */ |
| 7610 | for (i = 0; i < length; i++) { |
| 7611 | Py_UNICODE ch =s[i]; |
| 7612 | if (ch > 127) { |
| 7613 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 7614 | if (decimal >= 0) |
| 7615 | p[i] = '0' + decimal; |
| 7616 | } |
| 7617 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7618 | if (PyUnicode_READY((PyUnicodeObject*)result) == -1) { |
| 7619 | Py_DECREF(result); |
| 7620 | return NULL; |
| 7621 | } |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 7622 | return result; |
| 7623 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 7624 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 7625 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7626 | int |
| 7627 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 7628 | Py_ssize_t length, |
| 7629 | char *output, |
| 7630 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 7631 | { |
| 7632 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7633 | PyObject *errorHandler = NULL; |
| 7634 | PyObject *exc = NULL; |
| 7635 | const char *encoding = "decimal"; |
| 7636 | const char *reason = "invalid decimal Unicode string"; |
| 7637 | /* the following variable is used for caching string comparisons |
| 7638 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 7639 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 7640 | |
| 7641 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7642 | PyErr_BadArgument(); |
| 7643 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 7644 | } |
| 7645 | |
| 7646 | p = s; |
| 7647 | end = s + length; |
| 7648 | while (p < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7649 | register Py_UNICODE ch = *p; |
| 7650 | int decimal; |
| 7651 | PyObject *repunicode; |
| 7652 | Py_ssize_t repsize; |
| 7653 | Py_ssize_t newpos; |
| 7654 | Py_UNICODE *uni2; |
| 7655 | Py_UNICODE *collstart; |
| 7656 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7657 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7658 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7659 | *output++ = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7660 | ++p; |
| 7661 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7662 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7663 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 7664 | if (decimal >= 0) { |
| 7665 | *output++ = '0' + decimal; |
| 7666 | ++p; |
| 7667 | continue; |
| 7668 | } |
| 7669 | if (0 < ch && ch < 256) { |
| 7670 | *output++ = (char)ch; |
| 7671 | ++p; |
| 7672 | continue; |
| 7673 | } |
| 7674 | /* All other characters are considered unencodable */ |
| 7675 | collstart = p; |
| 7676 | collend = p+1; |
| 7677 | while (collend < end) { |
| 7678 | if ((0 < *collend && *collend < 256) || |
| 7679 | !Py_UNICODE_ISSPACE(*collend) || |
| 7680 | Py_UNICODE_TODECIMAL(*collend)) |
| 7681 | break; |
| 7682 | } |
| 7683 | /* cache callback name lookup |
| 7684 | * (if not done yet, i.e. it's the first error) */ |
| 7685 | if (known_errorHandler==-1) { |
| 7686 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 7687 | known_errorHandler = 1; |
| 7688 | else if (!strcmp(errors, "replace")) |
| 7689 | known_errorHandler = 2; |
| 7690 | else if (!strcmp(errors, "ignore")) |
| 7691 | known_errorHandler = 3; |
| 7692 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 7693 | known_errorHandler = 4; |
| 7694 | else |
| 7695 | known_errorHandler = 0; |
| 7696 | } |
| 7697 | switch (known_errorHandler) { |
| 7698 | case 1: /* strict */ |
| 7699 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 7700 | goto onError; |
| 7701 | case 2: /* replace */ |
| 7702 | for (p = collstart; p < collend; ++p) |
| 7703 | *output++ = '?'; |
| 7704 | /* fall through */ |
| 7705 | case 3: /* ignore */ |
| 7706 | p = collend; |
| 7707 | break; |
| 7708 | case 4: /* xmlcharrefreplace */ |
| 7709 | /* generate replacement (temporarily (mis)uses p) */ |
| 7710 | for (p = collstart; p < collend; ++p) |
| 7711 | output += sprintf(output, "&#%d;", (int)*p); |
| 7712 | p = collend; |
| 7713 | break; |
| 7714 | default: |
| 7715 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 7716 | encoding, reason, s, length, &exc, |
| 7717 | collstart-s, collend-s, &newpos); |
| 7718 | if (repunicode == NULL) |
| 7719 | goto onError; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7720 | if (!PyUnicode_Check(repunicode)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7721 | /* Byte results not supported, since they have no decimal property. */ |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7722 | PyErr_SetString(PyExc_TypeError, "error handler should return unicode"); |
| 7723 | Py_DECREF(repunicode); |
| 7724 | goto onError; |
| 7725 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7726 | /* generate replacement */ |
| 7727 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 7728 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 7729 | Py_UNICODE ch = *uni2; |
| 7730 | if (Py_UNICODE_ISSPACE(ch)) |
| 7731 | *output++ = ' '; |
| 7732 | else { |
| 7733 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 7734 | if (decimal >= 0) |
| 7735 | *output++ = '0' + decimal; |
| 7736 | else if (0 < ch && ch < 256) |
| 7737 | *output++ = (char)ch; |
| 7738 | else { |
| 7739 | Py_DECREF(repunicode); |
| 7740 | raise_encode_exception(&exc, encoding, |
| 7741 | s, length, collstart-s, collend-s, reason); |
| 7742 | goto onError; |
| 7743 | } |
| 7744 | } |
| 7745 | } |
| 7746 | p = s + newpos; |
| 7747 | Py_DECREF(repunicode); |
| 7748 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 7749 | } |
| 7750 | /* 0-terminate the output string */ |
| 7751 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7752 | Py_XDECREF(exc); |
| 7753 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 7754 | return 0; |
| 7755 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7756 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7757 | Py_XDECREF(exc); |
| 7758 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 7759 | return -1; |
| 7760 | } |
| 7761 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7762 | /* --- Helpers ------------------------------------------------------------ */ |
| 7763 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7764 | #include "stringlib/ucs1lib.h" |
| 7765 | #include "stringlib/fastsearch.h" |
| 7766 | #include "stringlib/partition.h" |
| 7767 | #include "stringlib/split.h" |
| 7768 | #include "stringlib/count.h" |
| 7769 | #include "stringlib/find.h" |
| 7770 | #include "stringlib/localeutil.h" |
| 7771 | #include "stringlib/undef.h" |
| 7772 | |
| 7773 | #include "stringlib/ucs2lib.h" |
| 7774 | #include "stringlib/fastsearch.h" |
| 7775 | #include "stringlib/partition.h" |
| 7776 | #include "stringlib/split.h" |
| 7777 | #include "stringlib/count.h" |
| 7778 | #include "stringlib/find.h" |
| 7779 | #include "stringlib/localeutil.h" |
| 7780 | #include "stringlib/undef.h" |
| 7781 | |
| 7782 | #include "stringlib/ucs4lib.h" |
| 7783 | #include "stringlib/fastsearch.h" |
| 7784 | #include "stringlib/partition.h" |
| 7785 | #include "stringlib/split.h" |
| 7786 | #include "stringlib/count.h" |
| 7787 | #include "stringlib/find.h" |
| 7788 | #include "stringlib/localeutil.h" |
| 7789 | #include "stringlib/undef.h" |
| 7790 | |
| 7791 | static Py_ssize_t |
| 7792 | any_find_slice(Py_ssize_t Py_LOCAL_CALLBACK(ucs1)(const Py_UCS1*, Py_ssize_t, |
| 7793 | const Py_UCS1*, Py_ssize_t, |
| 7794 | Py_ssize_t, Py_ssize_t), |
| 7795 | Py_ssize_t Py_LOCAL_CALLBACK(ucs2)(const Py_UCS2*, Py_ssize_t, |
| 7796 | const Py_UCS2*, Py_ssize_t, |
| 7797 | Py_ssize_t, Py_ssize_t), |
| 7798 | Py_ssize_t Py_LOCAL_CALLBACK(ucs4)(const Py_UCS4*, Py_ssize_t, |
| 7799 | const Py_UCS4*, Py_ssize_t, |
| 7800 | Py_ssize_t, Py_ssize_t), |
| 7801 | PyObject* s1, PyObject* s2, |
| 7802 | Py_ssize_t start, |
| 7803 | Py_ssize_t end) |
| 7804 | { |
| 7805 | int kind1, kind2, kind; |
| 7806 | void *buf1, *buf2; |
| 7807 | Py_ssize_t len1, len2, result; |
| 7808 | |
| 7809 | kind1 = PyUnicode_KIND(s1); |
| 7810 | kind2 = PyUnicode_KIND(s2); |
| 7811 | kind = kind1 > kind2 ? kind1 : kind2; |
| 7812 | buf1 = PyUnicode_DATA(s1); |
| 7813 | buf2 = PyUnicode_DATA(s2); |
| 7814 | if (kind1 != kind) |
| 7815 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 7816 | if (!buf1) |
| 7817 | return -2; |
| 7818 | if (kind2 != kind) |
| 7819 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 7820 | if (!buf2) { |
| 7821 | if (kind1 != kind) PyMem_Free(buf1); |
| 7822 | return -2; |
| 7823 | } |
| 7824 | len1 = PyUnicode_GET_LENGTH(s1); |
| 7825 | len2 = PyUnicode_GET_LENGTH(s2); |
| 7826 | |
| 7827 | switch(kind) { |
| 7828 | case PyUnicode_1BYTE_KIND: |
| 7829 | result = ucs1(buf1, len1, buf2, len2, start, end); |
| 7830 | break; |
| 7831 | case PyUnicode_2BYTE_KIND: |
| 7832 | result = ucs2(buf1, len1, buf2, len2, start, end); |
| 7833 | break; |
| 7834 | case PyUnicode_4BYTE_KIND: |
| 7835 | result = ucs4(buf1, len1, buf2, len2, start, end); |
| 7836 | break; |
| 7837 | default: |
| 7838 | assert(0); result = -2; |
| 7839 | } |
| 7840 | |
| 7841 | if (kind1 != kind) |
| 7842 | PyMem_Free(buf1); |
| 7843 | if (kind2 != kind) |
| 7844 | PyMem_Free(buf2); |
| 7845 | |
| 7846 | return result; |
| 7847 | } |
| 7848 | |
| 7849 | Py_ssize_t |
| 7850 | _PyUnicode_InsertThousandsGrouping(int kind, void *data, |
| 7851 | Py_ssize_t n_buffer, |
| 7852 | void *digits, Py_ssize_t n_digits, |
| 7853 | Py_ssize_t min_width, |
| 7854 | const char *grouping, |
| 7855 | const char *thousands_sep) |
| 7856 | { |
| 7857 | switch(kind) { |
| 7858 | case PyUnicode_1BYTE_KIND: |
| 7859 | return _PyUnicode_ucs1_InsertThousandsGrouping( |
| 7860 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 7861 | min_width, grouping, thousands_sep); |
| 7862 | case PyUnicode_2BYTE_KIND: |
| 7863 | return _PyUnicode_ucs2_InsertThousandsGrouping( |
| 7864 | (Py_UCS2*)data, n_buffer, (Py_UCS2*)digits, n_digits, |
| 7865 | min_width, grouping, thousands_sep); |
| 7866 | case PyUnicode_4BYTE_KIND: |
| 7867 | return _PyUnicode_ucs4_InsertThousandsGrouping( |
| 7868 | (Py_UCS4*)data, n_buffer, (Py_UCS4*)digits, n_digits, |
| 7869 | min_width, grouping, thousands_sep); |
| 7870 | } |
| 7871 | assert(0); |
| 7872 | return -1; |
| 7873 | } |
| 7874 | |
| 7875 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 7876 | #include "stringlib/unicodedefs.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7877 | #include "stringlib/fastsearch.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 7878 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7879 | #include "stringlib/count.h" |
| 7880 | #include "stringlib/find.h" |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 7881 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7882 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 7883 | #define ADJUST_INDICES(start, end, len) \ |
| 7884 | if (end > len) \ |
| 7885 | end = len; \ |
| 7886 | else if (end < 0) { \ |
| 7887 | end += len; \ |
| 7888 | if (end < 0) \ |
| 7889 | end = 0; \ |
| 7890 | } \ |
| 7891 | if (start < 0) { \ |
| 7892 | start += len; \ |
| 7893 | if (start < 0) \ |
| 7894 | start = 0; \ |
| 7895 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7896 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7897 | Py_ssize_t |
| 7898 | PyUnicode_Count(PyObject *str, |
| 7899 | PyObject *substr, |
| 7900 | Py_ssize_t start, |
| 7901 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7902 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7903 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7904 | PyUnicodeObject* str_obj; |
| 7905 | PyUnicodeObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7906 | int kind1, kind2, kind; |
| 7907 | void *buf1 = NULL, *buf2 = NULL; |
| 7908 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7909 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7910 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7911 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7912 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7913 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7914 | if (!sub_obj || PyUnicode_READY(str_obj) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7915 | Py_DECREF(str_obj); |
| 7916 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7917 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7918 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7919 | kind1 = PyUnicode_KIND(str_obj); |
| 7920 | kind2 = PyUnicode_KIND(sub_obj); |
| 7921 | kind = kind1 > kind2 ? kind1 : kind2; |
| 7922 | buf1 = PyUnicode_DATA(str_obj); |
| 7923 | if (kind1 != kind) |
| 7924 | buf1 = _PyUnicode_AsKind((PyObject*)str_obj, kind); |
| 7925 | if (!buf1) |
| 7926 | goto onError; |
| 7927 | buf2 = PyUnicode_DATA(sub_obj); |
| 7928 | if (kind2 != kind) |
| 7929 | buf2 = _PyUnicode_AsKind((PyObject*)sub_obj, kind); |
| 7930 | if (!buf2) |
| 7931 | goto onError; |
| 7932 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 7933 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 7934 | |
| 7935 | ADJUST_INDICES(start, end, len1); |
| 7936 | switch(kind) { |
| 7937 | case PyUnicode_1BYTE_KIND: |
| 7938 | result = ucs1lib_count( |
| 7939 | ((Py_UCS1*)buf1) + start, end - start, |
| 7940 | buf2, len2, PY_SSIZE_T_MAX |
| 7941 | ); |
| 7942 | break; |
| 7943 | case PyUnicode_2BYTE_KIND: |
| 7944 | result = ucs2lib_count( |
| 7945 | ((Py_UCS2*)buf1) + start, end - start, |
| 7946 | buf2, len2, PY_SSIZE_T_MAX |
| 7947 | ); |
| 7948 | break; |
| 7949 | case PyUnicode_4BYTE_KIND: |
| 7950 | result = ucs4lib_count( |
| 7951 | ((Py_UCS4*)buf1) + start, end - start, |
| 7952 | buf2, len2, PY_SSIZE_T_MAX |
| 7953 | ); |
| 7954 | break; |
| 7955 | default: |
| 7956 | assert(0); result = 0; |
| 7957 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7958 | |
| 7959 | Py_DECREF(sub_obj); |
| 7960 | Py_DECREF(str_obj); |
| 7961 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7962 | if (kind1 != kind) |
| 7963 | PyMem_Free(buf1); |
| 7964 | if (kind2 != kind) |
| 7965 | PyMem_Free(buf2); |
| 7966 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7967 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7968 | onError: |
| 7969 | Py_DECREF(sub_obj); |
| 7970 | Py_DECREF(str_obj); |
| 7971 | if (kind1 != kind && buf1) |
| 7972 | PyMem_Free(buf1); |
| 7973 | if (kind2 != kind && buf2) |
| 7974 | PyMem_Free(buf2); |
| 7975 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7976 | } |
| 7977 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7978 | Py_ssize_t |
| 7979 | PyUnicode_Find(PyObject *str, |
| 7980 | PyObject *sub, |
| 7981 | Py_ssize_t start, |
| 7982 | Py_ssize_t end, |
| 7983 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7984 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7985 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7986 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7987 | str = PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7988 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7989 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7990 | sub = PyUnicode_FromObject(sub); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7991 | if (!sub || PyUnicode_READY(sub) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7992 | Py_DECREF(str); |
| 7993 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7994 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7995 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7996 | if (direction > 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7997 | result = any_find_slice( |
| 7998 | ucs1lib_find_slice, ucs2lib_find_slice, ucs4lib_find_slice, |
| 7999 | str, sub, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8000 | ); |
| 8001 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8002 | result = any_find_slice( |
| 8003 | ucs1lib_rfind_slice, ucs2lib_rfind_slice, ucs4lib_rfind_slice, |
| 8004 | str, sub, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8005 | ); |
| 8006 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8007 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8008 | Py_DECREF(sub); |
| 8009 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8010 | return result; |
| 8011 | } |
| 8012 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8013 | Py_ssize_t |
| 8014 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 8015 | Py_ssize_t start, Py_ssize_t end, |
| 8016 | int direction) |
| 8017 | { |
| 8018 | char *result; |
| 8019 | int kind; |
| 8020 | if (PyUnicode_READY(str) == -1) |
| 8021 | return -2; |
| 8022 | if (end > PyUnicode_GET_LENGTH(str)) |
| 8023 | end = PyUnicode_GET_LENGTH(str); |
| 8024 | kind = PyUnicode_KIND(str); |
| 8025 | result = findchar(PyUnicode_1BYTE_DATA(str) |
| 8026 | + PyUnicode_KIND_SIZE(kind, start), |
| 8027 | kind, |
| 8028 | end-start, ch, direction); |
| 8029 | if (!result) |
| 8030 | return -1; |
| 8031 | return (result-(char*)PyUnicode_DATA(str)) >> (kind-1); |
| 8032 | } |
| 8033 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8034 | static int |
| 8035 | tailmatch(PyUnicodeObject *self, |
| 8036 | PyUnicodeObject *substring, |
| 8037 | Py_ssize_t start, |
| 8038 | Py_ssize_t end, |
| 8039 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8040 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8041 | int kind_self; |
| 8042 | int kind_sub; |
| 8043 | void *data_self; |
| 8044 | void *data_sub; |
| 8045 | Py_ssize_t offset; |
| 8046 | Py_ssize_t i; |
| 8047 | Py_ssize_t end_sub; |
| 8048 | |
| 8049 | if (PyUnicode_READY(self) == -1 || |
| 8050 | PyUnicode_READY(substring) == -1) |
| 8051 | return 0; |
| 8052 | |
| 8053 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8054 | return 1; |
| 8055 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8056 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 8057 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8058 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8059 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8060 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8061 | kind_self = PyUnicode_KIND(self); |
| 8062 | data_self = PyUnicode_DATA(self); |
| 8063 | kind_sub = PyUnicode_KIND(substring); |
| 8064 | data_sub = PyUnicode_DATA(substring); |
| 8065 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 8066 | |
| 8067 | if (direction > 0) |
| 8068 | offset = end; |
| 8069 | else |
| 8070 | offset = start; |
| 8071 | |
| 8072 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 8073 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 8074 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 8075 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 8076 | /* If both are of the same kind, memcmp is sufficient */ |
| 8077 | if (kind_self == kind_sub) { |
| 8078 | return ! memcmp((char *)data_self + |
| 8079 | (offset * PyUnicode_CHARACTER_SIZE(substring)), |
| 8080 | data_sub, |
| 8081 | PyUnicode_GET_LENGTH(substring) * |
| 8082 | PyUnicode_CHARACTER_SIZE(substring)); |
| 8083 | } |
| 8084 | /* otherwise we have to compare each character by first accesing it */ |
| 8085 | else { |
| 8086 | /* We do not need to compare 0 and len(substring)-1 because |
| 8087 | the if statement above ensured already that they are equal |
| 8088 | when we end up here. */ |
| 8089 | // TODO: honor direction and do a forward or backwards search |
| 8090 | for (i = 1; i < end_sub; ++i) { |
| 8091 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 8092 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 8093 | return 0; |
| 8094 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8095 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8096 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8097 | } |
| 8098 | |
| 8099 | return 0; |
| 8100 | } |
| 8101 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8102 | Py_ssize_t |
| 8103 | PyUnicode_Tailmatch(PyObject *str, |
| 8104 | PyObject *substr, |
| 8105 | Py_ssize_t start, |
| 8106 | Py_ssize_t end, |
| 8107 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8108 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8109 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8110 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8111 | str = PyUnicode_FromObject(str); |
| 8112 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8113 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8114 | substr = PyUnicode_FromObject(substr); |
| 8115 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8116 | Py_DECREF(str); |
| 8117 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8118 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8119 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8120 | result = tailmatch((PyUnicodeObject *)str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8121 | (PyUnicodeObject *)substr, |
| 8122 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8123 | Py_DECREF(str); |
| 8124 | Py_DECREF(substr); |
| 8125 | return result; |
| 8126 | } |
| 8127 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8128 | /* Apply fixfct filter to the Unicode object self and return a |
| 8129 | reference to the modified object */ |
| 8130 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8131 | static PyObject * |
| 8132 | fixup(PyUnicodeObject *self, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8133 | Py_UCS4 (*fixfct)(PyUnicodeObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8134 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8135 | PyObject *u; |
| 8136 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8137 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8138 | if (PyUnicode_READY(self) == -1) |
| 8139 | return NULL; |
| 8140 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(self); |
| 8141 | u = PyUnicode_New(PyUnicode_GET_LENGTH(self), |
| 8142 | maxchar_old); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8143 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8144 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8145 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8146 | Py_MEMCPY(PyUnicode_1BYTE_DATA(u), PyUnicode_1BYTE_DATA(self), |
| 8147 | PyUnicode_GET_LENGTH(u) * PyUnicode_CHARACTER_SIZE(u)); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8148 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8149 | /* fix functions return the new maximum character in a string, |
| 8150 | if the kind of the resulting unicode object does not change, |
| 8151 | everything is fine. Otherwise we need to change the string kind |
| 8152 | and re-run the fix function. */ |
| 8153 | maxchar_new = fixfct((PyUnicodeObject*)u); |
| 8154 | if (maxchar_new == 0) |
| 8155 | /* do nothing, keep maxchar_new at 0 which means no changes. */; |
| 8156 | else if (maxchar_new <= 127) |
| 8157 | maxchar_new = 127; |
| 8158 | else if (maxchar_new <= 255) |
| 8159 | maxchar_new = 255; |
| 8160 | else if (maxchar_new <= 65535) |
| 8161 | maxchar_new = 65535; |
| 8162 | else |
| 8163 | maxchar_new = 1114111; /* 0x10ffff */ |
| 8164 | |
| 8165 | if (!maxchar_new && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8166 | /* fixfct should return TRUE if it modified the buffer. If |
| 8167 | FALSE, return a reference to the original buffer instead |
| 8168 | (to save space, not time) */ |
| 8169 | Py_INCREF(self); |
| 8170 | Py_DECREF(u); |
| 8171 | return (PyObject*) self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8172 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8173 | else if (maxchar_new == maxchar_old) { |
| 8174 | return u; |
| 8175 | } |
| 8176 | else { |
| 8177 | /* In case the maximum character changed, we need to |
| 8178 | convert the string to the new category. */ |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8179 | PyObject *v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8180 | if (v == NULL) { |
| 8181 | Py_DECREF(u); |
| 8182 | return NULL; |
| 8183 | } |
| 8184 | if (maxchar_new > maxchar_old) { |
| 8185 | /* If the maxchar increased so that the kind changed, not all |
| 8186 | characters are representable anymore and we need to fix the |
| 8187 | string again. This only happens in very few cases. */ |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 8188 | if (PyUnicode_CopyCharacters(v, 0, |
| 8189 | (PyObject*)self, 0, |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8190 | PyUnicode_GET_LENGTH(self)) < 0) |
| 8191 | { |
| 8192 | Py_DECREF(u); |
| 8193 | return NULL; |
| 8194 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8195 | maxchar_old = fixfct((PyUnicodeObject*)v); |
| 8196 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
| 8197 | } |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8198 | else { |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 8199 | if (PyUnicode_CopyCharacters(v, 0, |
| 8200 | u, 0, |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8201 | PyUnicode_GET_LENGTH(self)) < 0) |
| 8202 | { |
| 8203 | Py_DECREF(u); |
| 8204 | return NULL; |
| 8205 | } |
| 8206 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8207 | |
| 8208 | Py_DECREF(u); |
| 8209 | return v; |
| 8210 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8211 | } |
| 8212 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8213 | static Py_UCS4 |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8214 | fixupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8215 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8216 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8217 | called as a callback from fixup() which does it already. */ |
| 8218 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8219 | const int kind = PyUnicode_KIND(self); |
| 8220 | void *data = PyUnicode_DATA(self); |
| 8221 | int touched = 0; |
| 8222 | Py_UCS4 maxchar = 0; |
| 8223 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8224 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8225 | for (i = 0; i < len; ++i) { |
| 8226 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8227 | const Py_UCS4 up = Py_UNICODE_TOUPPER(ch); |
| 8228 | if (up != ch) { |
| 8229 | if (up > maxchar) |
| 8230 | maxchar = up; |
| 8231 | PyUnicode_WRITE(kind, data, i, up); |
| 8232 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8233 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8234 | else if (ch > maxchar) |
| 8235 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8236 | } |
| 8237 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8238 | if (touched) |
| 8239 | return maxchar; |
| 8240 | else |
| 8241 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8242 | } |
| 8243 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8244 | static Py_UCS4 |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8245 | fixlower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8246 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8247 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8248 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8249 | const int kind = PyUnicode_KIND(self); |
| 8250 | void *data = PyUnicode_DATA(self); |
| 8251 | int touched = 0; |
| 8252 | Py_UCS4 maxchar = 0; |
| 8253 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8254 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8255 | for(i = 0; i < len; ++i) { |
| 8256 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8257 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 8258 | if (lo != ch) { |
| 8259 | if (lo > maxchar) |
| 8260 | maxchar = lo; |
| 8261 | PyUnicode_WRITE(kind, data, i, lo); |
| 8262 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8263 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8264 | else if (ch > maxchar) |
| 8265 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8266 | } |
| 8267 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8268 | if (touched) |
| 8269 | return maxchar; |
| 8270 | else |
| 8271 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8272 | } |
| 8273 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8274 | static Py_UCS4 |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8275 | fixswapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8276 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8277 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8278 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8279 | const int kind = PyUnicode_KIND(self); |
| 8280 | void *data = PyUnicode_DATA(self); |
| 8281 | int touched = 0; |
| 8282 | Py_UCS4 maxchar = 0; |
| 8283 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8284 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8285 | for(i = 0; i < len; ++i) { |
| 8286 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8287 | Py_UCS4 nu = 0; |
| 8288 | |
| 8289 | if (Py_UNICODE_ISUPPER(ch)) |
| 8290 | nu = Py_UNICODE_TOLOWER(ch); |
| 8291 | else if (Py_UNICODE_ISLOWER(ch)) |
| 8292 | nu = Py_UNICODE_TOUPPER(ch); |
| 8293 | |
| 8294 | if (nu != 0) { |
| 8295 | if (nu > maxchar) |
| 8296 | maxchar = nu; |
| 8297 | PyUnicode_WRITE(kind, data, i, nu); |
| 8298 | touched = 1; |
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 | else if (ch > maxchar) |
| 8301 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8302 | } |
| 8303 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8304 | if (touched) |
| 8305 | return maxchar; |
| 8306 | else |
| 8307 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8308 | } |
| 8309 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8310 | static Py_UCS4 |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8311 | fixcapitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8312 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8313 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8314 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8315 | const int kind = PyUnicode_KIND(self); |
| 8316 | void *data = PyUnicode_DATA(self); |
| 8317 | int touched = 0; |
| 8318 | Py_UCS4 maxchar = 0; |
| 8319 | Py_ssize_t i = 0; |
| 8320 | Py_UCS4 ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8321 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 8322 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8323 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8324 | |
| 8325 | ch = PyUnicode_READ(kind, data, i); |
| 8326 | if (!Py_UNICODE_ISUPPER(ch)) { |
| 8327 | maxchar = Py_UNICODE_TOUPPER(ch); |
| 8328 | PyUnicode_WRITE(kind, data, i, maxchar); |
| 8329 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8330 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8331 | ++i; |
| 8332 | for(; i < len; ++i) { |
| 8333 | ch = PyUnicode_READ(kind, data, i); |
| 8334 | if (!Py_UNICODE_ISLOWER(ch)) { |
| 8335 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 8336 | if (lo > maxchar) |
| 8337 | maxchar = lo; |
| 8338 | PyUnicode_WRITE(kind, data, i, lo); |
| 8339 | touched = 1; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 8340 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8341 | else if (ch > maxchar) |
| 8342 | maxchar = ch; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 8343 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8344 | |
| 8345 | if (touched) |
| 8346 | return maxchar; |
| 8347 | else |
| 8348 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8349 | } |
| 8350 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8351 | static Py_UCS4 |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8352 | fixtitle(PyUnicodeObject *self) |
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 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8355 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8356 | const int kind = PyUnicode_KIND(self); |
| 8357 | void *data = PyUnicode_DATA(self); |
| 8358 | Py_UCS4 maxchar = 0; |
| 8359 | Py_ssize_t i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8360 | int previous_is_cased; |
| 8361 | |
| 8362 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8363 | if (len == 1) { |
| 8364 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8365 | const Py_UCS4 ti = Py_UNICODE_TOTITLE(ch); |
| 8366 | if (ti != ch) { |
| 8367 | PyUnicode_WRITE(kind, data, i, ti); |
| 8368 | return ti; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8369 | } |
| 8370 | else |
| 8371 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8372 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8373 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8374 | for(; i < len; ++i) { |
| 8375 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8376 | Py_UCS4 nu; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8377 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8378 | if (previous_is_cased) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8379 | nu = Py_UNICODE_TOLOWER(ch); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8380 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8381 | nu = Py_UNICODE_TOTITLE(ch); |
| 8382 | |
| 8383 | if (nu > maxchar) |
| 8384 | maxchar = nu; |
| 8385 | PyUnicode_WRITE(kind, data, i, nu); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8386 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8387 | if (Py_UNICODE_ISLOWER(ch) || |
| 8388 | Py_UNICODE_ISUPPER(ch) || |
| 8389 | Py_UNICODE_ISTITLE(ch)) |
| 8390 | previous_is_cased = 1; |
| 8391 | else |
| 8392 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8393 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8394 | return maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8395 | } |
| 8396 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8397 | PyObject * |
| 8398 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8399 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8400 | PyObject *sep = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8401 | Py_ssize_t seplen = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8402 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8403 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8404 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 8405 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8406 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8407 | Py_ssize_t sz, i, res_offset; |
| 8408 | Py_UCS4 maxchar = 0; |
| 8409 | Py_UCS4 item_maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8410 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8411 | fseq = PySequence_Fast(seq, ""); |
| 8412 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8413 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8414 | } |
| 8415 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8416 | /* NOTE: the following code can't call back into Python code, |
| 8417 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 8418 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8419 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8420 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 8421 | /* If empty sequence, return u"". */ |
| 8422 | if (seqlen == 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8423 | res = PyUnicode_New(0, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8424 | goto Done; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8425 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8426 | items = PySequence_Fast_ITEMS(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8427 | /* If singleton sequence with an exact Unicode, return that. */ |
| 8428 | if (seqlen == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8429 | item = items[0]; |
| 8430 | if (PyUnicode_CheckExact(item)) { |
| 8431 | Py_INCREF(item); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8432 | res = item; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8433 | goto Done; |
| 8434 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8435 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8436 | else { |
| 8437 | /* Set up sep and seplen */ |
| 8438 | if (separator == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8439 | /* fall back to a blank space separator */ |
| 8440 | sep = PyUnicode_FromOrdinal(' '); |
| 8441 | if (!sep || PyUnicode_READY(sep) == -1) |
| 8442 | goto onError; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8443 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8444 | else { |
| 8445 | if (!PyUnicode_Check(separator)) { |
| 8446 | PyErr_Format(PyExc_TypeError, |
| 8447 | "separator: expected str instance," |
| 8448 | " %.80s found", |
| 8449 | Py_TYPE(separator)->tp_name); |
| 8450 | goto onError; |
| 8451 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8452 | if (PyUnicode_READY(separator) == -1) |
| 8453 | goto onError; |
| 8454 | sep = separator; |
| 8455 | seplen = PyUnicode_GET_LENGTH(separator); |
| 8456 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 8457 | /* inc refcount to keep this code path symetric with the |
| 8458 | above case of a blank separator */ |
| 8459 | Py_INCREF(sep); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8460 | } |
| 8461 | } |
| 8462 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8463 | /* There are at least two things to join, or else we have a subclass |
| 8464 | * of str in the sequence. |
| 8465 | * Do a pre-pass to figure out the total amount of space we'll |
| 8466 | * need (sz), and see whether all argument are strings. |
| 8467 | */ |
| 8468 | sz = 0; |
| 8469 | for (i = 0; i < seqlen; i++) { |
| 8470 | const Py_ssize_t old_sz = sz; |
| 8471 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8472 | if (!PyUnicode_Check(item)) { |
| 8473 | PyErr_Format(PyExc_TypeError, |
| 8474 | "sequence item %zd: expected str instance," |
| 8475 | " %.80s found", |
| 8476 | i, Py_TYPE(item)->tp_name); |
| 8477 | goto onError; |
| 8478 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8479 | if (PyUnicode_READY(item) == -1) |
| 8480 | goto onError; |
| 8481 | sz += PyUnicode_GET_LENGTH(item); |
| 8482 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
| 8483 | if (item_maxchar > maxchar) |
| 8484 | maxchar = item_maxchar; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8485 | if (i != 0) |
| 8486 | sz += seplen; |
| 8487 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 8488 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8489 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8490 | goto onError; |
| 8491 | } |
| 8492 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8493 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8494 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8495 | if (res == NULL) |
| 8496 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 8497 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8498 | /* Catenate everything. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8499 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8500 | Py_ssize_t itemlen; |
| 8501 | item = items[i]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8502 | itemlen = PyUnicode_GET_LENGTH(item); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8503 | /* Copy item, and maybe the separator. */ |
| 8504 | if (i) { |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8505 | if (PyUnicode_CopyCharacters(res, res_offset, |
| 8506 | sep, 0, seplen) < 0) |
| 8507 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8508 | res_offset += seplen; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8509 | } |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8510 | if (PyUnicode_CopyCharacters(res, res_offset, |
| 8511 | item, 0, itemlen) < 0) |
| 8512 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8513 | res_offset += itemlen; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8514 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8515 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8516 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8517 | Done: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8518 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8519 | Py_XDECREF(sep); |
| 8520 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8521 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8522 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8523 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8524 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8525 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8526 | return NULL; |
| 8527 | } |
| 8528 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8529 | #define FILL(kind, data, value, start, length) \ |
| 8530 | do { \ |
| 8531 | Py_ssize_t i_ = 0; \ |
| 8532 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 8533 | switch ((kind)) { \ |
| 8534 | case PyUnicode_1BYTE_KIND: { \ |
| 8535 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
| 8536 | memset(to_, (unsigned char)value, length); \ |
| 8537 | break; \ |
| 8538 | } \ |
| 8539 | case PyUnicode_2BYTE_KIND: { \ |
| 8540 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 8541 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 8542 | break; \ |
| 8543 | } \ |
| 8544 | default: { \ |
| 8545 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 8546 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 8547 | break; \ |
| 8548 | } \ |
| 8549 | } \ |
| 8550 | } while (0) |
| 8551 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8552 | static PyUnicodeObject * |
| 8553 | pad(PyUnicodeObject *self, |
| 8554 | Py_ssize_t left, |
| 8555 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8556 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8557 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8558 | PyObject *u; |
| 8559 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8560 | int kind; |
| 8561 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8562 | |
| 8563 | if (left < 0) |
| 8564 | left = 0; |
| 8565 | if (right < 0) |
| 8566 | right = 0; |
| 8567 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 8568 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8569 | Py_INCREF(self); |
| 8570 | return self; |
| 8571 | } |
| 8572 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8573 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 8574 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 8575 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 8576 | return NULL; |
| 8577 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8578 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 8579 | if (fill > maxchar) |
| 8580 | maxchar = fill; |
| 8581 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8582 | if (!u) |
| 8583 | return NULL; |
| 8584 | |
| 8585 | kind = PyUnicode_KIND(u); |
| 8586 | data = PyUnicode_DATA(u); |
| 8587 | if (left) |
| 8588 | FILL(kind, data, fill, 0, left); |
| 8589 | if (right) |
| 8590 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 8591 | if (PyUnicode_CopyCharacters(u, left, |
| 8592 | (PyObject*)self, 0, |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8593 | _PyUnicode_LENGTH(self)) < 0) |
| 8594 | { |
| 8595 | Py_DECREF(u); |
| 8596 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8597 | } |
| 8598 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8599 | return (PyUnicodeObject*)u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8600 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8601 | #undef FILL |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8602 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8603 | PyObject * |
| 8604 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8605 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8606 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8607 | |
| 8608 | string = PyUnicode_FromObject(string); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8609 | if (string == NULL || PyUnicode_READY(string) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8610 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8611 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8612 | switch(PyUnicode_KIND(string)) { |
| 8613 | case PyUnicode_1BYTE_KIND: |
| 8614 | list = ucs1lib_splitlines( |
| 8615 | (PyObject*) string, PyUnicode_1BYTE_DATA(string), |
| 8616 | PyUnicode_GET_LENGTH(string), keepends); |
| 8617 | break; |
| 8618 | case PyUnicode_2BYTE_KIND: |
| 8619 | list = ucs2lib_splitlines( |
| 8620 | (PyObject*) string, PyUnicode_2BYTE_DATA(string), |
| 8621 | PyUnicode_GET_LENGTH(string), keepends); |
| 8622 | break; |
| 8623 | case PyUnicode_4BYTE_KIND: |
| 8624 | list = ucs4lib_splitlines( |
| 8625 | (PyObject*) string, PyUnicode_4BYTE_DATA(string), |
| 8626 | PyUnicode_GET_LENGTH(string), keepends); |
| 8627 | break; |
| 8628 | default: |
| 8629 | assert(0); |
| 8630 | list = 0; |
| 8631 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8632 | Py_DECREF(string); |
| 8633 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8634 | } |
| 8635 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8636 | static PyObject * |
| 8637 | split(PyUnicodeObject *self, |
| 8638 | PyUnicodeObject *substring, |
| 8639 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8640 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8641 | int kind1, kind2, kind; |
| 8642 | void *buf1, *buf2; |
| 8643 | Py_ssize_t len1, len2; |
| 8644 | PyObject* out; |
| 8645 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8646 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8647 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8648 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8649 | if (PyUnicode_READY(self) == -1) |
| 8650 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8651 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8652 | if (substring == NULL) |
| 8653 | switch(PyUnicode_KIND(self)) { |
| 8654 | case PyUnicode_1BYTE_KIND: |
| 8655 | return ucs1lib_split_whitespace( |
| 8656 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 8657 | PyUnicode_GET_LENGTH(self), maxcount |
| 8658 | ); |
| 8659 | case PyUnicode_2BYTE_KIND: |
| 8660 | return ucs2lib_split_whitespace( |
| 8661 | (PyObject*) self, PyUnicode_2BYTE_DATA(self), |
| 8662 | PyUnicode_GET_LENGTH(self), maxcount |
| 8663 | ); |
| 8664 | case PyUnicode_4BYTE_KIND: |
| 8665 | return ucs4lib_split_whitespace( |
| 8666 | (PyObject*) self, PyUnicode_4BYTE_DATA(self), |
| 8667 | PyUnicode_GET_LENGTH(self), maxcount |
| 8668 | ); |
| 8669 | default: |
| 8670 | assert(0); |
| 8671 | return NULL; |
| 8672 | } |
| 8673 | |
| 8674 | if (PyUnicode_READY(substring) == -1) |
| 8675 | return NULL; |
| 8676 | |
| 8677 | kind1 = PyUnicode_KIND(self); |
| 8678 | kind2 = PyUnicode_KIND(substring); |
| 8679 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8680 | buf1 = PyUnicode_DATA(self); |
| 8681 | buf2 = PyUnicode_DATA(substring); |
| 8682 | if (kind1 != kind) |
| 8683 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 8684 | if (!buf1) |
| 8685 | return NULL; |
| 8686 | if (kind2 != kind) |
| 8687 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 8688 | if (!buf2) { |
| 8689 | if (kind1 != kind) PyMem_Free(buf1); |
| 8690 | return NULL; |
| 8691 | } |
| 8692 | len1 = PyUnicode_GET_LENGTH(self); |
| 8693 | len2 = PyUnicode_GET_LENGTH(substring); |
| 8694 | |
| 8695 | switch(kind) { |
| 8696 | case PyUnicode_1BYTE_KIND: |
| 8697 | out = ucs1lib_split( |
| 8698 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 8699 | break; |
| 8700 | case PyUnicode_2BYTE_KIND: |
| 8701 | out = ucs2lib_split( |
| 8702 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 8703 | break; |
| 8704 | case PyUnicode_4BYTE_KIND: |
| 8705 | out = ucs4lib_split( |
| 8706 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 8707 | break; |
| 8708 | default: |
| 8709 | out = NULL; |
| 8710 | } |
| 8711 | if (kind1 != kind) |
| 8712 | PyMem_Free(buf1); |
| 8713 | if (kind2 != kind) |
| 8714 | PyMem_Free(buf2); |
| 8715 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8716 | } |
| 8717 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8718 | static PyObject * |
| 8719 | rsplit(PyUnicodeObject *self, |
| 8720 | PyUnicodeObject *substring, |
| 8721 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8722 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8723 | int kind1, kind2, kind; |
| 8724 | void *buf1, *buf2; |
| 8725 | Py_ssize_t len1, len2; |
| 8726 | PyObject* out; |
| 8727 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8728 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8729 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8730 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8731 | if (PyUnicode_READY(self) == -1) |
| 8732 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8733 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8734 | if (substring == NULL) |
| 8735 | switch(PyUnicode_KIND(self)) { |
| 8736 | case PyUnicode_1BYTE_KIND: |
| 8737 | return ucs1lib_rsplit_whitespace( |
| 8738 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 8739 | PyUnicode_GET_LENGTH(self), maxcount |
| 8740 | ); |
| 8741 | case PyUnicode_2BYTE_KIND: |
| 8742 | return ucs2lib_rsplit_whitespace( |
| 8743 | (PyObject*) self, PyUnicode_2BYTE_DATA(self), |
| 8744 | PyUnicode_GET_LENGTH(self), maxcount |
| 8745 | ); |
| 8746 | case PyUnicode_4BYTE_KIND: |
| 8747 | return ucs4lib_rsplit_whitespace( |
| 8748 | (PyObject*) self, PyUnicode_4BYTE_DATA(self), |
| 8749 | PyUnicode_GET_LENGTH(self), maxcount |
| 8750 | ); |
| 8751 | default: |
| 8752 | assert(0); |
| 8753 | return NULL; |
| 8754 | } |
| 8755 | |
| 8756 | if (PyUnicode_READY(substring) == -1) |
| 8757 | return NULL; |
| 8758 | |
| 8759 | kind1 = PyUnicode_KIND(self); |
| 8760 | kind2 = PyUnicode_KIND(substring); |
| 8761 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8762 | buf1 = PyUnicode_DATA(self); |
| 8763 | buf2 = PyUnicode_DATA(substring); |
| 8764 | if (kind1 != kind) |
| 8765 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 8766 | if (!buf1) |
| 8767 | return NULL; |
| 8768 | if (kind2 != kind) |
| 8769 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 8770 | if (!buf2) { |
| 8771 | if (kind1 != kind) PyMem_Free(buf1); |
| 8772 | return NULL; |
| 8773 | } |
| 8774 | len1 = PyUnicode_GET_LENGTH(self); |
| 8775 | len2 = PyUnicode_GET_LENGTH(substring); |
| 8776 | |
| 8777 | switch(kind) { |
| 8778 | case PyUnicode_1BYTE_KIND: |
| 8779 | out = ucs1lib_rsplit( |
| 8780 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 8781 | break; |
| 8782 | case PyUnicode_2BYTE_KIND: |
| 8783 | out = ucs2lib_rsplit( |
| 8784 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 8785 | break; |
| 8786 | case PyUnicode_4BYTE_KIND: |
| 8787 | out = ucs4lib_rsplit( |
| 8788 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 8789 | break; |
| 8790 | default: |
| 8791 | out = NULL; |
| 8792 | } |
| 8793 | if (kind1 != kind) |
| 8794 | PyMem_Free(buf1); |
| 8795 | if (kind2 != kind) |
| 8796 | PyMem_Free(buf2); |
| 8797 | return out; |
| 8798 | } |
| 8799 | |
| 8800 | static Py_ssize_t |
| 8801 | anylib_find(int kind, void *buf1, Py_ssize_t len1, |
| 8802 | void *buf2, Py_ssize_t len2, Py_ssize_t offset) |
| 8803 | { |
| 8804 | switch(kind) { |
| 8805 | case PyUnicode_1BYTE_KIND: |
| 8806 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
| 8807 | case PyUnicode_2BYTE_KIND: |
| 8808 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 8809 | case PyUnicode_4BYTE_KIND: |
| 8810 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 8811 | } |
| 8812 | assert(0); |
| 8813 | return -1; |
| 8814 | } |
| 8815 | |
| 8816 | static Py_ssize_t |
| 8817 | anylib_count(int kind, void* sbuf, Py_ssize_t slen, |
| 8818 | void *buf1, Py_ssize_t len1, Py_ssize_t maxcount) |
| 8819 | { |
| 8820 | switch(kind) { |
| 8821 | case PyUnicode_1BYTE_KIND: |
| 8822 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
| 8823 | case PyUnicode_2BYTE_KIND: |
| 8824 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 8825 | case PyUnicode_4BYTE_KIND: |
| 8826 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 8827 | } |
| 8828 | assert(0); |
| 8829 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8830 | } |
| 8831 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8832 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8833 | replace(PyObject *self, PyObject *str1, |
| 8834 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8835 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8836 | PyObject *u; |
| 8837 | char *sbuf = PyUnicode_DATA(self); |
| 8838 | char *buf1 = PyUnicode_DATA(str1); |
| 8839 | char *buf2 = PyUnicode_DATA(str2); |
| 8840 | int srelease = 0, release1 = 0, release2 = 0; |
| 8841 | int skind = PyUnicode_KIND(self); |
| 8842 | int kind1 = PyUnicode_KIND(str1); |
| 8843 | int kind2 = PyUnicode_KIND(str2); |
| 8844 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 8845 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 8846 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8847 | |
| 8848 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8849 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8850 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8851 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8852 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8853 | if (skind < kind1) |
| 8854 | /* substring too wide to be present */ |
| 8855 | goto nothing; |
| 8856 | |
| 8857 | if (len1 == len2) { |
Antoine Pitrou | cbfdee3 | 2010-01-13 08:58:08 +0000 | [diff] [blame] | 8858 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8859 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8860 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8861 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8862 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8863 | /* replace characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8864 | Py_UCS4 u1, u2, maxchar; |
| 8865 | int mayshrink, rkind; |
| 8866 | u1 = PyUnicode_READ_CHAR(str1, 0); |
| 8867 | if (!findchar(sbuf, PyUnicode_KIND(self), |
| 8868 | slen, u1, 1)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8869 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8870 | u2 = PyUnicode_READ_CHAR(str2, 0); |
| 8871 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 8872 | /* Replacing u1 with u2 may cause a maxchar reduction in the |
| 8873 | result string. */ |
| 8874 | mayshrink = maxchar > 127; |
| 8875 | if (u2 > maxchar) { |
| 8876 | maxchar = u2; |
| 8877 | mayshrink = 0; |
| 8878 | } |
| 8879 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8880 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8881 | goto error; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8882 | if (PyUnicode_CopyCharacters(u, 0, |
| 8883 | (PyObject*)self, 0, slen) < 0) |
| 8884 | { |
| 8885 | Py_DECREF(u); |
| 8886 | return NULL; |
| 8887 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8888 | rkind = PyUnicode_KIND(u); |
| 8889 | for (i = 0; i < PyUnicode_GET_LENGTH(u); i++) |
| 8890 | if (PyUnicode_READ(rkind, PyUnicode_DATA(u), i) == u1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8891 | if (--maxcount < 0) |
| 8892 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8893 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), i, u2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8894 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8895 | if (mayshrink) { |
| 8896 | PyObject *tmp = u; |
| 8897 | u = PyUnicode_FromKindAndData(rkind, PyUnicode_DATA(tmp), |
| 8898 | PyUnicode_GET_LENGTH(tmp)); |
| 8899 | Py_DECREF(tmp); |
| 8900 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8901 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8902 | int rkind = skind; |
| 8903 | char *res; |
| 8904 | if (kind1 < rkind) { |
| 8905 | /* widen substring */ |
| 8906 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 8907 | if (!buf1) goto error; |
| 8908 | release1 = 1; |
| 8909 | } |
| 8910 | i = anylib_find(rkind, sbuf, slen, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8911 | if (i < 0) |
| 8912 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8913 | if (rkind > kind2) { |
| 8914 | /* widen replacement */ |
| 8915 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 8916 | if (!buf2) goto error; |
| 8917 | release2 = 1; |
| 8918 | } |
| 8919 | else if (rkind < kind2) { |
| 8920 | /* widen self and buf1 */ |
| 8921 | rkind = kind2; |
| 8922 | if (release1) PyMem_Free(buf1); |
| 8923 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 8924 | if (!sbuf) goto error; |
| 8925 | srelease = 1; |
| 8926 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 8927 | if (!buf1) goto error; |
| 8928 | release1 = 1; |
| 8929 | } |
| 8930 | res = PyMem_Malloc(PyUnicode_KIND_SIZE(rkind, slen)); |
| 8931 | if (!res) { |
| 8932 | PyErr_NoMemory(); |
| 8933 | goto error; |
| 8934 | } |
| 8935 | memcpy(res, sbuf, PyUnicode_KIND_SIZE(rkind, slen)); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8936 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8937 | memcpy(res + PyUnicode_KIND_SIZE(rkind, i), |
| 8938 | buf2, |
| 8939 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 8940 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8941 | |
| 8942 | while ( --maxcount > 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8943 | i = anylib_find(rkind, sbuf+PyUnicode_KIND_SIZE(rkind, i), |
| 8944 | slen-i, |
| 8945 | buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8946 | if (i == -1) |
| 8947 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8948 | memcpy(res + PyUnicode_KIND_SIZE(rkind, i), |
| 8949 | buf2, |
| 8950 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 8951 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8952 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8953 | |
| 8954 | u = PyUnicode_FromKindAndData(rkind, res, slen); |
| 8955 | PyMem_Free(res); |
| 8956 | if (!u) goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8957 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8958 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8959 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8960 | Py_ssize_t n, i, j, ires; |
| 8961 | Py_ssize_t product, new_size; |
| 8962 | int rkind = skind; |
| 8963 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8964 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8965 | if (kind1 < rkind) { |
| 8966 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 8967 | if (!buf1) goto error; |
| 8968 | release1 = 1; |
| 8969 | } |
| 8970 | n = anylib_count(rkind, sbuf, slen, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8971 | if (n == 0) |
| 8972 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8973 | if (kind2 < rkind) { |
| 8974 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 8975 | if (!buf2) goto error; |
| 8976 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8977 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8978 | else if (kind2 > rkind) { |
| 8979 | rkind = kind2; |
| 8980 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 8981 | if (!sbuf) goto error; |
| 8982 | srelease = 1; |
| 8983 | if (release1) PyMem_Free(buf1); |
| 8984 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 8985 | if (!buf1) goto error; |
| 8986 | release1 = 1; |
| 8987 | } |
| 8988 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 8989 | PyUnicode_GET_LENGTH(str1))); */ |
| 8990 | product = n * (len2-len1); |
| 8991 | if ((product / (len2-len1)) != n) { |
| 8992 | PyErr_SetString(PyExc_OverflowError, |
| 8993 | "replace string is too long"); |
| 8994 | goto error; |
| 8995 | } |
| 8996 | new_size = slen + product; |
| 8997 | if (new_size < 0 || new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
| 8998 | PyErr_SetString(PyExc_OverflowError, |
| 8999 | "replace string is too long"); |
| 9000 | goto error; |
| 9001 | } |
| 9002 | res = PyMem_Malloc(PyUnicode_KIND_SIZE(rkind, new_size)); |
| 9003 | if (!res) |
| 9004 | goto error; |
| 9005 | ires = i = 0; |
| 9006 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9007 | while (n-- > 0) { |
| 9008 | /* look for next match */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9009 | j = anylib_find(rkind, |
| 9010 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9011 | slen-i, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9012 | if (j == -1) |
| 9013 | break; |
| 9014 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9015 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9016 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9017 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9018 | PyUnicode_KIND_SIZE(rkind, j-i)); |
| 9019 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9020 | } |
| 9021 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9022 | if (len2 > 0) { |
| 9023 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9024 | buf2, |
| 9025 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 9026 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9027 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9028 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9029 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9030 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9031 | /* copy tail [i:] */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9032 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9033 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9034 | PyUnicode_KIND_SIZE(rkind, slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9035 | } else { |
| 9036 | /* interleave */ |
| 9037 | while (n > 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9038 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9039 | buf2, |
| 9040 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 9041 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9042 | if (--n <= 0) |
| 9043 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9044 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9045 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9046 | PyUnicode_KIND_SIZE(rkind, 1)); |
| 9047 | ires++; |
| 9048 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9049 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9050 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9051 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9052 | PyUnicode_KIND_SIZE(rkind, slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9053 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9054 | u = PyUnicode_FromKindAndData(rkind, res, new_size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9055 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9056 | if (srelease) |
| 9057 | PyMem_FREE(sbuf); |
| 9058 | if (release1) |
| 9059 | PyMem_FREE(buf1); |
| 9060 | if (release2) |
| 9061 | PyMem_FREE(buf2); |
| 9062 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9063 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9064 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9065 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9066 | if (srelease) |
| 9067 | PyMem_FREE(sbuf); |
| 9068 | if (release1) |
| 9069 | PyMem_FREE(buf1); |
| 9070 | if (release2) |
| 9071 | PyMem_FREE(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9072 | if (PyUnicode_CheckExact(self)) { |
| 9073 | Py_INCREF(self); |
| 9074 | return (PyObject *) self; |
| 9075 | } |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 9076 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9077 | error: |
| 9078 | if (srelease && sbuf) |
| 9079 | PyMem_FREE(sbuf); |
| 9080 | if (release1 && buf1) |
| 9081 | PyMem_FREE(buf1); |
| 9082 | if (release2 && buf2) |
| 9083 | PyMem_FREE(buf2); |
| 9084 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9085 | } |
| 9086 | |
| 9087 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 9088 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9089 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9090 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9091 | \n\ |
| 9092 | 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] | 9093 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9094 | |
| 9095 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9096 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9097 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9098 | return fixup(self, fixtitle); |
| 9099 | } |
| 9100 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9101 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9102 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9103 | \n\ |
| 9104 | 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] | 9105 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9106 | |
| 9107 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9108 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9109 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9110 | return fixup(self, fixcapitalize); |
| 9111 | } |
| 9112 | |
| 9113 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9114 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9115 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9116 | \n\ |
| 9117 | 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] | 9118 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9119 | |
| 9120 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9121 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9122 | { |
| 9123 | PyObject *list; |
| 9124 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9125 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9126 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9127 | /* Split into words */ |
| 9128 | list = split(self, NULL, -1); |
| 9129 | if (!list) |
| 9130 | return NULL; |
| 9131 | |
| 9132 | /* Capitalize each word */ |
| 9133 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 9134 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9135 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9136 | if (item == NULL) |
| 9137 | goto onError; |
| 9138 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 9139 | PyList_SET_ITEM(list, i, item); |
| 9140 | } |
| 9141 | |
| 9142 | /* Join the words to form a new string */ |
| 9143 | item = PyUnicode_Join(NULL, list); |
| 9144 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9145 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9146 | Py_DECREF(list); |
| 9147 | return (PyObject *)item; |
| 9148 | } |
| 9149 | #endif |
| 9150 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9151 | /* Argument converter. Coerces to a single unicode character */ |
| 9152 | |
| 9153 | static int |
| 9154 | convert_uc(PyObject *obj, void *addr) |
| 9155 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9156 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9157 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9158 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9159 | uniobj = PyUnicode_FromObject(obj); |
| 9160 | if (uniobj == NULL) { |
| 9161 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9162 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9163 | return 0; |
| 9164 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9165 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9166 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9167 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9168 | Py_DECREF(uniobj); |
| 9169 | return 0; |
| 9170 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9171 | if (PyUnicode_READY(uniobj)) { |
| 9172 | Py_DECREF(uniobj); |
| 9173 | return 0; |
| 9174 | } |
| 9175 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9176 | Py_DECREF(uniobj); |
| 9177 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9178 | } |
| 9179 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9180 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9181 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9182 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 9183 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9184 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9185 | |
| 9186 | static PyObject * |
| 9187 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 9188 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9189 | Py_ssize_t marg, left; |
| 9190 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9191 | Py_UCS4 fillchar = ' '; |
| 9192 | |
| 9193 | if (PyUnicode_READY(self) == -1) |
| 9194 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9195 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 9196 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9197 | return NULL; |
| 9198 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9199 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9200 | Py_INCREF(self); |
| 9201 | return (PyObject*) self; |
| 9202 | } |
| 9203 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9204 | marg = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9205 | left = marg / 2 + (marg & width & 1); |
| 9206 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9207 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9208 | } |
| 9209 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9210 | #if 0 |
| 9211 | |
| 9212 | /* This code should go into some future Unicode collation support |
| 9213 | module. The basic comparison should compare ordinals on a naive |
Georg Brandl | c6c3178 | 2009-06-08 13:41:29 +0000 | [diff] [blame] | 9214 | basis (this is what Java does and thus Jython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9215 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9216 | /* speedy UTF-16 code point order comparison */ |
| 9217 | /* gleaned from: */ |
| 9218 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 9219 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 9220 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9221 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9222 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9223 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 9224 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 9225 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9226 | }; |
| 9227 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9228 | static int |
| 9229 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 9230 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9231 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9232 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9233 | Py_UNICODE *s1 = str1->str; |
| 9234 | Py_UNICODE *s2 = str2->str; |
| 9235 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9236 | len1 = str1->_base._base.length; |
| 9237 | len2 = str2->_base._base.length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9238 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9239 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9240 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9241 | |
| 9242 | c1 = *s1++; |
| 9243 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 9244 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9245 | if (c1 > (1<<11) * 26) |
| 9246 | c1 += utf16Fixup[c1>>11]; |
| 9247 | if (c2 > (1<<11) * 26) |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9248 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9249 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 9250 | |
| 9251 | if (c1 != c2) |
| 9252 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9253 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9254 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9255 | } |
| 9256 | |
| 9257 | return (len1 < len2) ? -1 : (len1 != len2); |
| 9258 | } |
| 9259 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9260 | #else |
| 9261 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9262 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 9263 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9264 | static int |
| 9265 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 9266 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9267 | int kind1, kind2; |
| 9268 | void *data1, *data2; |
| 9269 | Py_ssize_t len1, len2, i; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9270 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9271 | kind1 = PyUnicode_KIND(str1); |
| 9272 | kind2 = PyUnicode_KIND(str2); |
| 9273 | data1 = PyUnicode_DATA(str1); |
| 9274 | data2 = PyUnicode_DATA(str2); |
| 9275 | len1 = PyUnicode_GET_LENGTH(str1); |
| 9276 | len2 = PyUnicode_GET_LENGTH(str2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9277 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9278 | for (i = 0; i < len1 && i < len2; ++i) { |
| 9279 | Py_UCS4 c1, c2; |
| 9280 | c1 = PyUnicode_READ(kind1, data1, i); |
| 9281 | c2 = PyUnicode_READ(kind2, data2, i); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 9282 | |
| 9283 | if (c1 != c2) |
| 9284 | return (c1 < c2) ? -1 : 1; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9285 | } |
| 9286 | |
| 9287 | return (len1 < len2) ? -1 : (len1 != len2); |
| 9288 | } |
| 9289 | |
| 9290 | #endif |
| 9291 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9292 | int |
| 9293 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9294 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9295 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 9296 | if (PyUnicode_READY(left) == -1 || |
| 9297 | PyUnicode_READY(right) == -1) |
| 9298 | return -1; |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 9299 | return unicode_compare((PyUnicodeObject *)left, |
| 9300 | (PyUnicodeObject *)right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9301 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 9302 | PyErr_Format(PyExc_TypeError, |
| 9303 | "Can't compare %.100s and %.100s", |
| 9304 | left->ob_type->tp_name, |
| 9305 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9306 | return -1; |
| 9307 | } |
| 9308 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9309 | int |
| 9310 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 9311 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9312 | Py_ssize_t i; |
| 9313 | int kind; |
| 9314 | void *data; |
| 9315 | Py_UCS4 chr; |
| 9316 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9317 | assert(PyUnicode_Check(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9318 | if (PyUnicode_READY(uni) == -1) |
| 9319 | return -1; |
| 9320 | kind = PyUnicode_KIND(uni); |
| 9321 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9322 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9323 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 9324 | if (chr != str[i]) |
| 9325 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 9326 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 9327 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9328 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9329 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9330 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9331 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9332 | return 0; |
| 9333 | } |
| 9334 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9335 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9336 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9337 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9338 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9339 | PyObject * |
| 9340 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9341 | { |
| 9342 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9343 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9344 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 9345 | PyObject *v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9346 | if (PyUnicode_READY(left) == -1 || |
| 9347 | PyUnicode_READY(right) == -1) |
| 9348 | return NULL; |
| 9349 | if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) || |
| 9350 | PyUnicode_KIND(left) != PyUnicode_KIND(right)) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9351 | if (op == Py_EQ) { |
| 9352 | Py_INCREF(Py_False); |
| 9353 | return Py_False; |
| 9354 | } |
| 9355 | if (op == Py_NE) { |
| 9356 | Py_INCREF(Py_True); |
| 9357 | return Py_True; |
| 9358 | } |
| 9359 | } |
| 9360 | if (left == right) |
| 9361 | result = 0; |
| 9362 | else |
| 9363 | result = unicode_compare((PyUnicodeObject *)left, |
| 9364 | (PyUnicodeObject *)right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9365 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9366 | /* Convert the return value to a Boolean */ |
| 9367 | switch (op) { |
| 9368 | case Py_EQ: |
| 9369 | v = TEST_COND(result == 0); |
| 9370 | break; |
| 9371 | case Py_NE: |
| 9372 | v = TEST_COND(result != 0); |
| 9373 | break; |
| 9374 | case Py_LE: |
| 9375 | v = TEST_COND(result <= 0); |
| 9376 | break; |
| 9377 | case Py_GE: |
| 9378 | v = TEST_COND(result >= 0); |
| 9379 | break; |
| 9380 | case Py_LT: |
| 9381 | v = TEST_COND(result == -1); |
| 9382 | break; |
| 9383 | case Py_GT: |
| 9384 | v = TEST_COND(result == 1); |
| 9385 | break; |
| 9386 | default: |
| 9387 | PyErr_BadArgument(); |
| 9388 | return NULL; |
| 9389 | } |
| 9390 | Py_INCREF(v); |
| 9391 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9392 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9393 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 9394 | Py_RETURN_NOTIMPLEMENTED; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9395 | } |
| 9396 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9397 | int |
| 9398 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9399 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9400 | PyObject *str, *sub; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9401 | int kind1, kind2, kind; |
| 9402 | void *buf1, *buf2; |
| 9403 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9404 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9405 | |
| 9406 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9407 | sub = PyUnicode_FromObject(element); |
| 9408 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9409 | PyErr_Format(PyExc_TypeError, |
| 9410 | "'in <string>' requires string as left operand, not %s", |
| 9411 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9412 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9413 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9414 | if (PyUnicode_READY(sub) == -1) |
| 9415 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9416 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9417 | str = PyUnicode_FromObject(container); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9418 | if (!str || PyUnicode_READY(container) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9419 | Py_DECREF(sub); |
| 9420 | return -1; |
| 9421 | } |
| 9422 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9423 | kind1 = PyUnicode_KIND(str); |
| 9424 | kind2 = PyUnicode_KIND(sub); |
| 9425 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9426 | buf1 = PyUnicode_DATA(str); |
| 9427 | buf2 = PyUnicode_DATA(sub); |
| 9428 | if (kind1 != kind) |
| 9429 | buf1 = _PyUnicode_AsKind((PyObject*)str, kind); |
| 9430 | if (!buf1) { |
| 9431 | Py_DECREF(sub); |
| 9432 | return -1; |
| 9433 | } |
| 9434 | if (kind2 != kind) |
| 9435 | buf2 = _PyUnicode_AsKind((PyObject*)sub, kind); |
| 9436 | if (!buf2) { |
| 9437 | Py_DECREF(sub); |
| 9438 | if (kind1 != kind) PyMem_Free(buf1); |
| 9439 | return -1; |
| 9440 | } |
| 9441 | len1 = PyUnicode_GET_LENGTH(str); |
| 9442 | len2 = PyUnicode_GET_LENGTH(sub); |
| 9443 | |
| 9444 | switch(kind) { |
| 9445 | case PyUnicode_1BYTE_KIND: |
| 9446 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 9447 | break; |
| 9448 | case PyUnicode_2BYTE_KIND: |
| 9449 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 9450 | break; |
| 9451 | case PyUnicode_4BYTE_KIND: |
| 9452 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 9453 | break; |
| 9454 | default: |
| 9455 | result = -1; |
| 9456 | assert(0); |
| 9457 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9458 | |
| 9459 | Py_DECREF(str); |
| 9460 | Py_DECREF(sub); |
| 9461 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9462 | if (kind1 != kind) |
| 9463 | PyMem_Free(buf1); |
| 9464 | if (kind2 != kind) |
| 9465 | PyMem_Free(buf2); |
| 9466 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9467 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9468 | } |
| 9469 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9470 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 9471 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9472 | PyObject * |
| 9473 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9474 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9475 | PyObject *u = NULL, *v = NULL, *w; |
| 9476 | Py_UCS4 maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9477 | |
| 9478 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9479 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9480 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9481 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9482 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9483 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9484 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9485 | |
| 9486 | /* Shortcuts */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9487 | if (v == (PyObject*)unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9488 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9489 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9490 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9491 | if (u == (PyObject*)unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9492 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9493 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9494 | } |
| 9495 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9496 | if (PyUnicode_READY(u) == -1 || PyUnicode_READY(v) == -1) |
| 9497 | goto onError; |
| 9498 | |
| 9499 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | ff9e50f | 2011-09-28 22:17:19 +0200 | [diff] [blame] | 9500 | maxchar = Py_MAX(maxchar, PyUnicode_MAX_CHAR_VALUE(v)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9501 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9502 | /* Concat the two Unicode strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9503 | w = PyUnicode_New( |
| 9504 | PyUnicode_GET_LENGTH(u) + PyUnicode_GET_LENGTH(v), |
| 9505 | maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9506 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9507 | goto onError; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9508 | if (PyUnicode_CopyCharacters(w, 0, u, 0, PyUnicode_GET_LENGTH(u)) < 0) |
| 9509 | goto onError; |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 9510 | if (PyUnicode_CopyCharacters(w, PyUnicode_GET_LENGTH(u), |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9511 | v, 0, |
| 9512 | PyUnicode_GET_LENGTH(v)) < 0) |
| 9513 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9514 | Py_DECREF(u); |
| 9515 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9516 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9517 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9518 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9519 | Py_XDECREF(u); |
| 9520 | Py_XDECREF(v); |
| 9521 | return NULL; |
| 9522 | } |
| 9523 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 9524 | void |
| 9525 | PyUnicode_Append(PyObject **pleft, PyObject *right) |
| 9526 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9527 | PyObject *new; |
| 9528 | if (*pleft == NULL) |
| 9529 | return; |
| 9530 | if (right == NULL || !PyUnicode_Check(*pleft)) { |
| 9531 | Py_DECREF(*pleft); |
| 9532 | *pleft = NULL; |
| 9533 | return; |
| 9534 | } |
| 9535 | new = PyUnicode_Concat(*pleft, right); |
| 9536 | Py_DECREF(*pleft); |
| 9537 | *pleft = new; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 9538 | } |
| 9539 | |
| 9540 | void |
| 9541 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 9542 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9543 | PyUnicode_Append(pleft, right); |
| 9544 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 9545 | } |
| 9546 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9547 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9548 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9549 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9550 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 9551 | 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] | 9552 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9553 | |
| 9554 | static PyObject * |
| 9555 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 9556 | { |
| 9557 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9558 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9559 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9560 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9561 | int kind1, kind2, kind; |
| 9562 | void *buf1, *buf2; |
| 9563 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9564 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 9565 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 9566 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9567 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9568 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9569 | kind1 = PyUnicode_KIND(self); |
| 9570 | kind2 = PyUnicode_KIND(substring); |
| 9571 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9572 | buf1 = PyUnicode_DATA(self); |
| 9573 | buf2 = PyUnicode_DATA(substring); |
| 9574 | if (kind1 != kind) |
| 9575 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 9576 | if (!buf1) { |
| 9577 | Py_DECREF(substring); |
| 9578 | return NULL; |
| 9579 | } |
| 9580 | if (kind2 != kind) |
| 9581 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 9582 | if (!buf2) { |
| 9583 | Py_DECREF(substring); |
| 9584 | if (kind1 != kind) PyMem_Free(buf1); |
| 9585 | return NULL; |
| 9586 | } |
| 9587 | len1 = PyUnicode_GET_LENGTH(self); |
| 9588 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9589 | |
| 9590 | ADJUST_INDICES(start, end, len1); |
| 9591 | switch(kind) { |
| 9592 | case PyUnicode_1BYTE_KIND: |
| 9593 | iresult = ucs1lib_count( |
| 9594 | ((Py_UCS1*)buf1) + start, end - start, |
| 9595 | buf2, len2, PY_SSIZE_T_MAX |
| 9596 | ); |
| 9597 | break; |
| 9598 | case PyUnicode_2BYTE_KIND: |
| 9599 | iresult = ucs2lib_count( |
| 9600 | ((Py_UCS2*)buf1) + start, end - start, |
| 9601 | buf2, len2, PY_SSIZE_T_MAX |
| 9602 | ); |
| 9603 | break; |
| 9604 | case PyUnicode_4BYTE_KIND: |
| 9605 | iresult = ucs4lib_count( |
| 9606 | ((Py_UCS4*)buf1) + start, end - start, |
| 9607 | buf2, len2, PY_SSIZE_T_MAX |
| 9608 | ); |
| 9609 | break; |
| 9610 | default: |
| 9611 | assert(0); iresult = 0; |
| 9612 | } |
| 9613 | |
| 9614 | result = PyLong_FromSsize_t(iresult); |
| 9615 | |
| 9616 | if (kind1 != kind) |
| 9617 | PyMem_Free(buf1); |
| 9618 | if (kind2 != kind) |
| 9619 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9620 | |
| 9621 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9622 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9623 | return result; |
| 9624 | } |
| 9625 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9626 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 9627 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9628 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 9629 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 9630 | 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] | 9631 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 9632 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 9633 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 9634 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9635 | |
| 9636 | static PyObject * |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 9637 | unicode_encode(PyUnicodeObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9638 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 9639 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9640 | char *encoding = NULL; |
| 9641 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 9642 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 9643 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 9644 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9645 | return NULL; |
Georg Brandl | 3b9406b | 2010-12-03 07:54:09 +0000 | [diff] [blame] | 9646 | return PyUnicode_AsEncodedString((PyObject *)self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 9647 | } |
| 9648 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9649 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9650 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9651 | \n\ |
| 9652 | 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] | 9653 | 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] | 9654 | |
| 9655 | static PyObject* |
| 9656 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 9657 | { |
| 9658 | Py_UNICODE *e; |
| 9659 | Py_UNICODE *p; |
| 9660 | Py_UNICODE *q; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9661 | Py_UNICODE *qe; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9662 | Py_ssize_t i, j, incr, wstr_length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9663 | PyUnicodeObject *u; |
| 9664 | int tabsize = 8; |
| 9665 | |
| 9666 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9667 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9668 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9669 | if (PyUnicode_AsUnicodeAndSize((PyObject *)self, &wstr_length) == NULL) |
| 9670 | return NULL; |
| 9671 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 9672 | /* First pass: determine size of output string */ |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9673 | i = 0; /* chars up to and including most recent \n or \r */ |
| 9674 | 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] | 9675 | e = _PyUnicode_WSTR(self) + wstr_length; /* end of input */ |
| 9676 | for (p = _PyUnicode_WSTR(self); p < e; p++) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9677 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9678 | if (tabsize > 0) { |
| 9679 | incr = tabsize - (j % tabsize); /* cannot overflow */ |
| 9680 | if (j > PY_SSIZE_T_MAX - incr) |
| 9681 | goto overflow1; |
| 9682 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9683 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9684 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9685 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9686 | if (j > PY_SSIZE_T_MAX - 1) |
| 9687 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9688 | j++; |
| 9689 | if (*p == '\n' || *p == '\r') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9690 | if (i > PY_SSIZE_T_MAX - j) |
| 9691 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9692 | i += j; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9693 | j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9694 | } |
| 9695 | } |
| 9696 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9697 | if (i > PY_SSIZE_T_MAX - j) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9698 | goto overflow1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 9699 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9700 | /* Second pass: create output string and fill it */ |
| 9701 | u = _PyUnicode_New(i + j); |
| 9702 | if (!u) |
| 9703 | return NULL; |
| 9704 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9705 | j = 0; /* same as in first pass */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9706 | q = _PyUnicode_WSTR(u); /* next output char */ |
| 9707 | qe = _PyUnicode_WSTR(u) + PyUnicode_GET_SIZE(u); /* end of output */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9708 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9709 | for (p = _PyUnicode_WSTR(self); p < e; p++) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9710 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9711 | if (tabsize > 0) { |
| 9712 | i = tabsize - (j % tabsize); |
| 9713 | j += i; |
| 9714 | while (i--) { |
| 9715 | if (q >= qe) |
| 9716 | goto overflow2; |
| 9717 | *q++ = ' '; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9718 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9719 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9720 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9721 | else { |
| 9722 | if (q >= qe) |
| 9723 | goto overflow2; |
| 9724 | *q++ = *p; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9725 | j++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9726 | if (*p == '\n' || *p == '\r') |
| 9727 | j = 0; |
| 9728 | } |
| 9729 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9730 | if (PyUnicode_READY(u) == -1) { |
| 9731 | Py_DECREF(u); |
| 9732 | return NULL; |
| 9733 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9734 | return (PyObject*) u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9735 | |
| 9736 | overflow2: |
| 9737 | Py_DECREF(u); |
| 9738 | overflow1: |
| 9739 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 9740 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9741 | } |
| 9742 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9743 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9744 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9745 | \n\ |
| 9746 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 9747 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9748 | arguments start and end are interpreted as in slice notation.\n\ |
| 9749 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9750 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9751 | |
| 9752 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9753 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9754 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 9755 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 9756 | Py_ssize_t start; |
| 9757 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9758 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9759 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 9760 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 9761 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9762 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9763 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9764 | if (PyUnicode_READY(self) == -1) |
| 9765 | return NULL; |
| 9766 | if (PyUnicode_READY(substring) == -1) |
| 9767 | return NULL; |
| 9768 | |
| 9769 | result = any_find_slice( |
| 9770 | ucs1lib_find_slice, ucs2lib_find_slice, ucs4lib_find_slice, |
| 9771 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9772 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9773 | |
| 9774 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9775 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9776 | if (result == -2) |
| 9777 | return NULL; |
| 9778 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9779 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9780 | } |
| 9781 | |
| 9782 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9783 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9784 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9785 | Py_UCS4 ch; |
| 9786 | |
| 9787 | if (PyUnicode_READY(self) == -1) |
| 9788 | return NULL; |
| 9789 | if (index < 0 || index >= _PyUnicode_LENGTH(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9790 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9791 | return NULL; |
| 9792 | } |
| 9793 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9794 | ch = PyUnicode_READ(PyUnicode_KIND(self), PyUnicode_DATA(self), index); |
| 9795 | return PyUnicode_FromOrdinal(ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9796 | } |
| 9797 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 9798 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 9799 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 9800 | static Py_hash_t |
Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 9801 | unicode_hash(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9802 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 9803 | Py_ssize_t len; |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 9804 | Py_uhash_t x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 9805 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9806 | if (_PyUnicode_HASH(self) != -1) |
| 9807 | return _PyUnicode_HASH(self); |
| 9808 | if (PyUnicode_READY(self) == -1) |
| 9809 | return -1; |
| 9810 | len = PyUnicode_GET_LENGTH(self); |
| 9811 | |
| 9812 | /* The hash function as a macro, gets expanded three times below. */ |
| 9813 | #define HASH(P) \ |
| 9814 | x = (Py_uhash_t)*P << 7; \ |
| 9815 | while (--len >= 0) \ |
| 9816 | x = (1000003*x) ^ (Py_uhash_t)*P++; |
| 9817 | |
| 9818 | switch (PyUnicode_KIND(self)) { |
| 9819 | case PyUnicode_1BYTE_KIND: { |
| 9820 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 9821 | HASH(c); |
| 9822 | break; |
| 9823 | } |
| 9824 | case PyUnicode_2BYTE_KIND: { |
| 9825 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 9826 | HASH(s); |
| 9827 | break; |
| 9828 | } |
| 9829 | default: { |
| 9830 | Py_UCS4 *l; |
| 9831 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 9832 | "Impossible switch case in unicode_hash"); |
| 9833 | l = PyUnicode_4BYTE_DATA(self); |
| 9834 | HASH(l); |
| 9835 | break; |
| 9836 | } |
| 9837 | } |
| 9838 | x ^= (Py_uhash_t)PyUnicode_GET_LENGTH(self); |
| 9839 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 9840 | if (x == -1) |
| 9841 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9842 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 9843 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9844 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9845 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9846 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9847 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9848 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9849 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9850 | 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] | 9851 | |
| 9852 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9853 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9854 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9855 | Py_ssize_t result; |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 9856 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 9857 | Py_ssize_t start; |
| 9858 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9859 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 9860 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 9861 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9862 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9863 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9864 | if (PyUnicode_READY(self) == -1) |
| 9865 | return NULL; |
| 9866 | if (PyUnicode_READY(substring) == -1) |
| 9867 | return NULL; |
| 9868 | |
| 9869 | result = any_find_slice( |
| 9870 | ucs1lib_find_slice, ucs2lib_find_slice, ucs4lib_find_slice, |
| 9871 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9872 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9873 | |
| 9874 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9875 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9876 | if (result == -2) |
| 9877 | return NULL; |
| 9878 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9879 | if (result < 0) { |
| 9880 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 9881 | return NULL; |
| 9882 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9883 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9884 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9885 | } |
| 9886 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9887 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9888 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9889 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 9890 | 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] | 9891 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9892 | |
| 9893 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9894 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9895 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9896 | Py_ssize_t i, length; |
| 9897 | int kind; |
| 9898 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9899 | int cased; |
| 9900 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9901 | if (PyUnicode_READY(self) == -1) |
| 9902 | return NULL; |
| 9903 | length = PyUnicode_GET_LENGTH(self); |
| 9904 | kind = PyUnicode_KIND(self); |
| 9905 | data = PyUnicode_DATA(self); |
| 9906 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9907 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9908 | if (length == 1) |
| 9909 | return PyBool_FromLong( |
| 9910 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9911 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 9912 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9913 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9914 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 9915 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9916 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9917 | for (i = 0; i < length; i++) { |
| 9918 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9919 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9920 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 9921 | return PyBool_FromLong(0); |
| 9922 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 9923 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9924 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 9925 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9926 | } |
| 9927 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9928 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9929 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9930 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 9931 | 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] | 9932 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9933 | |
| 9934 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9935 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9936 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9937 | Py_ssize_t i, length; |
| 9938 | int kind; |
| 9939 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9940 | int cased; |
| 9941 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9942 | if (PyUnicode_READY(self) == -1) |
| 9943 | return NULL; |
| 9944 | length = PyUnicode_GET_LENGTH(self); |
| 9945 | kind = PyUnicode_KIND(self); |
| 9946 | data = PyUnicode_DATA(self); |
| 9947 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9948 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9949 | if (length == 1) |
| 9950 | return PyBool_FromLong( |
| 9951 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9952 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 9953 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9954 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9955 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 9956 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9957 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9958 | for (i = 0; i < length; i++) { |
| 9959 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9960 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9961 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 9962 | return PyBool_FromLong(0); |
| 9963 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 9964 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9965 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 9966 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9967 | } |
| 9968 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9969 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9970 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9971 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 9972 | Return True if S is a titlecased string and there is at least one\n\ |
| 9973 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 9974 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 9975 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9976 | |
| 9977 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9978 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9979 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9980 | Py_ssize_t i, length; |
| 9981 | int kind; |
| 9982 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9983 | int cased, previous_is_cased; |
| 9984 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9985 | if (PyUnicode_READY(self) == -1) |
| 9986 | return NULL; |
| 9987 | length = PyUnicode_GET_LENGTH(self); |
| 9988 | kind = PyUnicode_KIND(self); |
| 9989 | data = PyUnicode_DATA(self); |
| 9990 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9991 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9992 | if (length == 1) { |
| 9993 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 9994 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 9995 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 9996 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9997 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 9998 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9999 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10000 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10001 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10002 | cased = 0; |
| 10003 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10004 | for (i = 0; i < length; i++) { |
| 10005 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10006 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10007 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 10008 | if (previous_is_cased) |
| 10009 | return PyBool_FromLong(0); |
| 10010 | previous_is_cased = 1; |
| 10011 | cased = 1; |
| 10012 | } |
| 10013 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 10014 | if (!previous_is_cased) |
| 10015 | return PyBool_FromLong(0); |
| 10016 | previous_is_cased = 1; |
| 10017 | cased = 1; |
| 10018 | } |
| 10019 | else |
| 10020 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10021 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10022 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10023 | } |
| 10024 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10025 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10026 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10027 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10028 | Return True if all characters in S are whitespace\n\ |
| 10029 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10030 | |
| 10031 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10032 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10033 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10034 | Py_ssize_t i, length; |
| 10035 | int kind; |
| 10036 | void *data; |
| 10037 | |
| 10038 | if (PyUnicode_READY(self) == -1) |
| 10039 | return NULL; |
| 10040 | length = PyUnicode_GET_LENGTH(self); |
| 10041 | kind = PyUnicode_KIND(self); |
| 10042 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10043 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10044 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10045 | if (length == 1) |
| 10046 | return PyBool_FromLong( |
| 10047 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10048 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10049 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10050 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10051 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10052 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10053 | for (i = 0; i < length; i++) { |
| 10054 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 10055 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10056 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10057 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10058 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10059 | } |
| 10060 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10061 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10062 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10063 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10064 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10065 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10066 | |
| 10067 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10068 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10069 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10070 | Py_ssize_t i, length; |
| 10071 | int kind; |
| 10072 | void *data; |
| 10073 | |
| 10074 | if (PyUnicode_READY(self) == -1) |
| 10075 | return NULL; |
| 10076 | length = PyUnicode_GET_LENGTH(self); |
| 10077 | kind = PyUnicode_KIND(self); |
| 10078 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10079 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10080 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10081 | if (length == 1) |
| 10082 | return PyBool_FromLong( |
| 10083 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10084 | |
| 10085 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10086 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10087 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10088 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10089 | for (i = 0; i < length; i++) { |
| 10090 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10091 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10092 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10093 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10094 | } |
| 10095 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10096 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10097 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10098 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10099 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10100 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10101 | |
| 10102 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10103 | unicode_isalnum(PyUnicodeObject *self) |
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 | int kind; |
| 10106 | void *data; |
| 10107 | Py_ssize_t len, i; |
| 10108 | |
| 10109 | if (PyUnicode_READY(self) == -1) |
| 10110 | return NULL; |
| 10111 | |
| 10112 | kind = PyUnicode_KIND(self); |
| 10113 | data = PyUnicode_DATA(self); |
| 10114 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10115 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10116 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10117 | if (len == 1) { |
| 10118 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 10119 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 10120 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10121 | |
| 10122 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10123 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10124 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10125 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10126 | for (i = 0; i < len; i++) { |
| 10127 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 10128 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10129 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10130 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10131 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10132 | } |
| 10133 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10134 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10135 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10136 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10137 | 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] | 10138 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10139 | |
| 10140 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10141 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10142 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10143 | Py_ssize_t i, length; |
| 10144 | int kind; |
| 10145 | void *data; |
| 10146 | |
| 10147 | if (PyUnicode_READY(self) == -1) |
| 10148 | return NULL; |
| 10149 | length = PyUnicode_GET_LENGTH(self); |
| 10150 | kind = PyUnicode_KIND(self); |
| 10151 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10152 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10153 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10154 | if (length == 1) |
| 10155 | return PyBool_FromLong( |
| 10156 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10157 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10158 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10159 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10160 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10161 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10162 | for (i = 0; i < length; i++) { |
| 10163 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10164 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10165 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10166 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10167 | } |
| 10168 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10169 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10170 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10171 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10172 | Return True if all characters in S are digits\n\ |
| 10173 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10174 | |
| 10175 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10176 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10177 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10178 | Py_ssize_t i, length; |
| 10179 | int kind; |
| 10180 | void *data; |
| 10181 | |
| 10182 | if (PyUnicode_READY(self) == -1) |
| 10183 | return NULL; |
| 10184 | length = PyUnicode_GET_LENGTH(self); |
| 10185 | kind = PyUnicode_KIND(self); |
| 10186 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10187 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10188 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10189 | if (length == 1) { |
| 10190 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 10191 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 10192 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10193 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10194 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10195 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10196 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10197 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10198 | for (i = 0; i < length; i++) { |
| 10199 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10200 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10201 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10202 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10203 | } |
| 10204 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10205 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10206 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10207 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10208 | 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] | 10209 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10210 | |
| 10211 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10212 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10213 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10214 | Py_ssize_t i, length; |
| 10215 | int kind; |
| 10216 | void *data; |
| 10217 | |
| 10218 | if (PyUnicode_READY(self) == -1) |
| 10219 | return NULL; |
| 10220 | length = PyUnicode_GET_LENGTH(self); |
| 10221 | kind = PyUnicode_KIND(self); |
| 10222 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10223 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10224 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10225 | if (length == 1) |
| 10226 | return PyBool_FromLong( |
| 10227 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10228 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10229 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10230 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10231 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10232 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10233 | for (i = 0; i < length; i++) { |
| 10234 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10235 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10236 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10237 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10238 | } |
| 10239 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10240 | int |
| 10241 | PyUnicode_IsIdentifier(PyObject *self) |
| 10242 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10243 | int kind; |
| 10244 | void *data; |
| 10245 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 10246 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10247 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10248 | if (PyUnicode_READY(self) == -1) { |
| 10249 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10250 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10251 | } |
| 10252 | |
| 10253 | /* Special case for empty strings */ |
| 10254 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 10255 | return 0; |
| 10256 | kind = PyUnicode_KIND(self); |
| 10257 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10258 | |
| 10259 | /* PEP 3131 says that the first character must be in |
| 10260 | XID_Start and subsequent characters in XID_Continue, |
| 10261 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10262 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10263 | letters, digits, underscore). However, given the current |
| 10264 | definition of XID_Start and XID_Continue, it is sufficient |
| 10265 | to check just for these, except that _ must be allowed |
| 10266 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10267 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 10268 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10269 | return 0; |
| 10270 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 10271 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10272 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10273 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10274 | return 1; |
| 10275 | } |
| 10276 | |
| 10277 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10278 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10279 | \n\ |
| 10280 | Return True if S is a valid identifier according\n\ |
| 10281 | to the language definition."); |
| 10282 | |
| 10283 | static PyObject* |
| 10284 | unicode_isidentifier(PyObject *self) |
| 10285 | { |
| 10286 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 10287 | } |
| 10288 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10289 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10290 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10291 | \n\ |
| 10292 | Return True if all characters in S are considered\n\ |
| 10293 | printable in repr() or S is empty, False otherwise."); |
| 10294 | |
| 10295 | static PyObject* |
| 10296 | unicode_isprintable(PyObject *self) |
| 10297 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10298 | Py_ssize_t i, length; |
| 10299 | int kind; |
| 10300 | void *data; |
| 10301 | |
| 10302 | if (PyUnicode_READY(self) == -1) |
| 10303 | return NULL; |
| 10304 | length = PyUnicode_GET_LENGTH(self); |
| 10305 | kind = PyUnicode_KIND(self); |
| 10306 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10307 | |
| 10308 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10309 | if (length == 1) |
| 10310 | return PyBool_FromLong( |
| 10311 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10312 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10313 | for (i = 0; i < length; i++) { |
| 10314 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10315 | Py_RETURN_FALSE; |
| 10316 | } |
| 10317 | } |
| 10318 | Py_RETURN_TRUE; |
| 10319 | } |
| 10320 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10321 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 10322 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10323 | \n\ |
| 10324 | 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] | 10325 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10326 | |
| 10327 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10328 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10329 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10330 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10331 | } |
| 10332 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10333 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10334 | unicode_length(PyUnicodeObject *self) |
| 10335 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10336 | if (PyUnicode_READY(self) == -1) |
| 10337 | return -1; |
| 10338 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10339 | } |
| 10340 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10341 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10342 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10343 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 10344 | 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] | 10345 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10346 | |
| 10347 | static PyObject * |
| 10348 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 10349 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10350 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10351 | Py_UCS4 fillchar = ' '; |
| 10352 | |
| 10353 | if (PyUnicode_READY(self) == -1) |
| 10354 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10355 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10356 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10357 | return NULL; |
| 10358 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10359 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10360 | Py_INCREF(self); |
| 10361 | return (PyObject*) self; |
| 10362 | } |
| 10363 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10364 | return (PyObject*) pad(self, 0, width - _PyUnicode_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10365 | } |
| 10366 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10367 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10368 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10369 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10370 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10371 | |
| 10372 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10373 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10374 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10375 | return fixup(self, fixlower); |
| 10376 | } |
| 10377 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10378 | #define LEFTSTRIP 0 |
| 10379 | #define RIGHTSTRIP 1 |
| 10380 | #define BOTHSTRIP 2 |
| 10381 | |
| 10382 | /* Arrays indexed by above */ |
| 10383 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 10384 | |
| 10385 | #define STRIPNAME(i) (stripformat[i]+3) |
| 10386 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10387 | /* externally visible for str.strip(unicode) */ |
| 10388 | PyObject * |
| 10389 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 10390 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10391 | void *data; |
| 10392 | int kind; |
| 10393 | Py_ssize_t i, j, len; |
| 10394 | BLOOM_MASK sepmask; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10395 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10396 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 10397 | return NULL; |
| 10398 | |
| 10399 | kind = PyUnicode_KIND(self); |
| 10400 | data = PyUnicode_DATA(self); |
| 10401 | len = PyUnicode_GET_LENGTH(self); |
| 10402 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 10403 | PyUnicode_DATA(sepobj), |
| 10404 | PyUnicode_GET_LENGTH(sepobj)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10405 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10406 | i = 0; |
| 10407 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10408 | while (i < len && |
| 10409 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10410 | i++; |
| 10411 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10412 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10413 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10414 | j = len; |
| 10415 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10416 | do { |
| 10417 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10418 | } while (j >= i && |
| 10419 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10420 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10421 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10422 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10423 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10424 | Py_INCREF(self); |
| 10425 | return (PyObject*)self; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10426 | } |
| 10427 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10428 | return PyUnicode_Substring((PyObject*)self, i, j); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10429 | } |
| 10430 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10431 | /* Assumes an already ready self string. */ |
| 10432 | |
| 10433 | static PyObject * |
| 10434 | substring(PyUnicodeObject *self, Py_ssize_t start, Py_ssize_t len) |
| 10435 | { |
| 10436 | const int kind = PyUnicode_KIND(self); |
| 10437 | void *data = PyUnicode_DATA(self); |
| 10438 | Py_UCS4 maxchar = 0; |
| 10439 | Py_ssize_t i; |
| 10440 | PyObject *unicode; |
| 10441 | |
| 10442 | if (start < 0 || len < 0 || (start + len) > PyUnicode_GET_LENGTH(self)) { |
| 10443 | PyErr_BadInternalCall(); |
| 10444 | return NULL; |
| 10445 | } |
| 10446 | |
| 10447 | if (len == PyUnicode_GET_LENGTH(self) && PyUnicode_CheckExact(self)) { |
| 10448 | Py_INCREF(self); |
| 10449 | return (PyObject*)self; |
| 10450 | } |
| 10451 | |
| 10452 | for (i = 0; i < len; ++i) { |
| 10453 | const Py_UCS4 ch = PyUnicode_READ(kind, data, start + i); |
| 10454 | if (ch > maxchar) |
| 10455 | maxchar = ch; |
| 10456 | } |
| 10457 | |
| 10458 | unicode = PyUnicode_New(len, maxchar); |
| 10459 | if (unicode == NULL) |
| 10460 | return NULL; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 10461 | if (PyUnicode_CopyCharacters(unicode, 0, |
| 10462 | (PyObject*)self, start, len) < 0) |
| 10463 | { |
| 10464 | Py_DECREF(unicode); |
| 10465 | return NULL; |
| 10466 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10467 | return unicode; |
| 10468 | } |
| 10469 | |
| 10470 | PyObject* |
| 10471 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 10472 | { |
| 10473 | unsigned char *data; |
| 10474 | int kind; |
| 10475 | |
| 10476 | if (start == 0 && end == PyUnicode_GET_LENGTH(self) |
| 10477 | && PyUnicode_CheckExact(self)) |
| 10478 | { |
| 10479 | Py_INCREF(self); |
| 10480 | return (PyObject *)self; |
| 10481 | } |
| 10482 | |
| 10483 | if ((end - start) == 1) |
| 10484 | return unicode_getitem((PyUnicodeObject*)self, start); |
| 10485 | |
| 10486 | if (PyUnicode_READY(self) == -1) |
| 10487 | return NULL; |
| 10488 | kind = PyUnicode_KIND(self); |
| 10489 | data = PyUnicode_1BYTE_DATA(self); |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 10490 | return PyUnicode_FromKindAndData(kind, |
| 10491 | data + PyUnicode_KIND_SIZE(kind, start), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10492 | end-start); |
| 10493 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10494 | |
| 10495 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10496 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10497 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10498 | int kind; |
| 10499 | void *data; |
| 10500 | Py_ssize_t len, i, j; |
| 10501 | |
| 10502 | if (PyUnicode_READY(self) == -1) |
| 10503 | return NULL; |
| 10504 | |
| 10505 | kind = PyUnicode_KIND(self); |
| 10506 | data = PyUnicode_DATA(self); |
| 10507 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10508 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10509 | i = 0; |
| 10510 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10511 | while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10512 | i++; |
| 10513 | } |
| 10514 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10515 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10516 | j = len; |
| 10517 | if (striptype != LEFTSTRIP) { |
| 10518 | do { |
| 10519 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10520 | } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10521 | j++; |
| 10522 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10523 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10524 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 10525 | Py_INCREF(self); |
| 10526 | return (PyObject*)self; |
| 10527 | } |
| 10528 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10529 | return substring(self, i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10530 | } |
| 10531 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10532 | |
| 10533 | static PyObject * |
| 10534 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 10535 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10536 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10537 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10538 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 10539 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10540 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10541 | if (sep != NULL && sep != Py_None) { |
| 10542 | if (PyUnicode_Check(sep)) |
| 10543 | return _PyUnicode_XStrip(self, striptype, sep); |
| 10544 | else { |
| 10545 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10546 | "%s arg must be None or str", |
| 10547 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10548 | return NULL; |
| 10549 | } |
| 10550 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10551 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10552 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10553 | } |
| 10554 | |
| 10555 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10556 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10557 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10558 | \n\ |
| 10559 | Return a copy of the string S with leading and trailing\n\ |
| 10560 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10561 | 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] | 10562 | |
| 10563 | static PyObject * |
| 10564 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 10565 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10566 | if (PyTuple_GET_SIZE(args) == 0) |
| 10567 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 10568 | else |
| 10569 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10570 | } |
| 10571 | |
| 10572 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10573 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10574 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10575 | \n\ |
| 10576 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10577 | 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] | 10578 | |
| 10579 | static PyObject * |
| 10580 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 10581 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10582 | if (PyTuple_GET_SIZE(args) == 0) |
| 10583 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 10584 | else |
| 10585 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10586 | } |
| 10587 | |
| 10588 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10589 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10590 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10591 | \n\ |
| 10592 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10593 | 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] | 10594 | |
| 10595 | static PyObject * |
| 10596 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 10597 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10598 | if (PyTuple_GET_SIZE(args) == 0) |
| 10599 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 10600 | else |
| 10601 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10602 | } |
| 10603 | |
| 10604 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10605 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10606 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10607 | { |
| 10608 | PyUnicodeObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10609 | Py_ssize_t nchars, n; |
| 10610 | size_t nbytes, char_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10611 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 10612 | if (len < 1) { |
| 10613 | Py_INCREF(unicode_empty); |
| 10614 | return (PyObject *)unicode_empty; |
| 10615 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10616 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 10617 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10618 | /* no repeat, return original string */ |
| 10619 | Py_INCREF(str); |
| 10620 | return (PyObject*) str; |
| 10621 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 10622 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10623 | if (PyUnicode_READY(str) == -1) |
| 10624 | return NULL; |
| 10625 | |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 10626 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 10627 | * needed doesn't overflow size_t |
| 10628 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10629 | nchars = len * PyUnicode_GET_LENGTH(str); |
| 10630 | if (nchars / len != PyUnicode_GET_LENGTH(str)) { |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 10631 | PyErr_SetString(PyExc_OverflowError, |
| 10632 | "repeated string is too long"); |
| 10633 | return NULL; |
| 10634 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10635 | char_size = PyUnicode_CHARACTER_SIZE(str); |
| 10636 | nbytes = (nchars + 1) * char_size; |
| 10637 | if (nbytes / char_size != (size_t)(nchars + 1)) { |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 10638 | PyErr_SetString(PyExc_OverflowError, |
| 10639 | "repeated string is too long"); |
| 10640 | return NULL; |
| 10641 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10642 | u = (PyUnicodeObject *)PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10643 | if (!u) |
| 10644 | return NULL; |
| 10645 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10646 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 10647 | const int kind = PyUnicode_KIND(str); |
| 10648 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
| 10649 | void *to = PyUnicode_DATA(u); |
| 10650 | for (n = 0; n < len; ++n) |
| 10651 | PyUnicode_WRITE(kind, to, n, fill_char); |
| 10652 | } |
| 10653 | else { |
| 10654 | /* number of characters copied this far */ |
| 10655 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
| 10656 | const Py_ssize_t char_size = PyUnicode_CHARACTER_SIZE(str); |
| 10657 | char *to = (char *) PyUnicode_DATA(u); |
| 10658 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 10659 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10660 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10661 | n = (done <= nchars-done) ? done : nchars-done; |
| 10662 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10663 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10664 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10665 | } |
| 10666 | |
| 10667 | return (PyObject*) u; |
| 10668 | } |
| 10669 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10670 | PyObject * |
| 10671 | PyUnicode_Replace(PyObject *obj, |
| 10672 | PyObject *subobj, |
| 10673 | PyObject *replobj, |
| 10674 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10675 | { |
| 10676 | PyObject *self; |
| 10677 | PyObject *str1; |
| 10678 | PyObject *str2; |
| 10679 | PyObject *result; |
| 10680 | |
| 10681 | self = PyUnicode_FromObject(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10682 | if (self == NULL || PyUnicode_READY(obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10683 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10684 | str1 = PyUnicode_FromObject(subobj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10685 | if (str1 == NULL || PyUnicode_READY(obj) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10686 | Py_DECREF(self); |
| 10687 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10688 | } |
| 10689 | str2 = PyUnicode_FromObject(replobj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10690 | if (str2 == NULL || PyUnicode_READY(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10691 | Py_DECREF(self); |
| 10692 | Py_DECREF(str1); |
| 10693 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10694 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10695 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10696 | Py_DECREF(self); |
| 10697 | Py_DECREF(str1); |
| 10698 | Py_DECREF(str2); |
| 10699 | return result; |
| 10700 | } |
| 10701 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10702 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 10703 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10704 | \n\ |
| 10705 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 10706 | old replaced by new. If the optional argument count is\n\ |
| 10707 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10708 | |
| 10709 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10710 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10711 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10712 | PyObject *str1; |
| 10713 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10714 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10715 | PyObject *result; |
| 10716 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10717 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10718 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10719 | if (!PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10720 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10721 | str1 = PyUnicode_FromObject(str1); |
| 10722 | if (str1 == NULL || PyUnicode_READY(str1) == -1) |
| 10723 | return NULL; |
| 10724 | str2 = PyUnicode_FromObject(str2); |
| 10725 | if (str2 == NULL || PyUnicode_READY(str1) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10726 | Py_DECREF(str1); |
| 10727 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 10728 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10729 | |
| 10730 | result = replace(self, str1, str2, maxcount); |
| 10731 | |
| 10732 | Py_DECREF(str1); |
| 10733 | Py_DECREF(str2); |
| 10734 | return result; |
| 10735 | } |
| 10736 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10737 | static PyObject * |
| 10738 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10739 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10740 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10741 | Py_ssize_t isize; |
| 10742 | Py_ssize_t osize, squote, dquote, i, o; |
| 10743 | Py_UCS4 max, quote; |
| 10744 | int ikind, okind; |
| 10745 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10746 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10747 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10748 | return NULL; |
| 10749 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10750 | isize = PyUnicode_GET_LENGTH(unicode); |
| 10751 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10752 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10753 | /* Compute length of output, quote characters, and |
| 10754 | maximum character */ |
| 10755 | osize = 2; /* quotes */ |
| 10756 | max = 127; |
| 10757 | squote = dquote = 0; |
| 10758 | ikind = PyUnicode_KIND(unicode); |
| 10759 | for (i = 0; i < isize; i++) { |
| 10760 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 10761 | switch (ch) { |
| 10762 | case '\'': squote++; osize++; break; |
| 10763 | case '"': dquote++; osize++; break; |
| 10764 | case '\\': case '\t': case '\r': case '\n': |
| 10765 | osize += 2; break; |
| 10766 | default: |
| 10767 | /* Fast-path ASCII */ |
| 10768 | if (ch < ' ' || ch == 0x7f) |
| 10769 | osize += 4; /* \xHH */ |
| 10770 | else if (ch < 0x7f) |
| 10771 | osize++; |
| 10772 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 10773 | osize++; |
| 10774 | max = ch > max ? ch : max; |
| 10775 | } |
| 10776 | else if (ch < 0x100) |
| 10777 | osize += 4; /* \xHH */ |
| 10778 | else if (ch < 0x10000) |
| 10779 | osize += 6; /* \uHHHH */ |
| 10780 | else |
| 10781 | osize += 10; /* \uHHHHHHHH */ |
| 10782 | } |
| 10783 | } |
| 10784 | |
| 10785 | quote = '\''; |
| 10786 | if (squote) { |
| 10787 | if (dquote) |
| 10788 | /* Both squote and dquote present. Use squote, |
| 10789 | and escape them */ |
| 10790 | osize += squote; |
| 10791 | else |
| 10792 | quote = '"'; |
| 10793 | } |
| 10794 | |
| 10795 | repr = PyUnicode_New(osize, max); |
| 10796 | if (repr == NULL) |
| 10797 | return NULL; |
| 10798 | okind = PyUnicode_KIND(repr); |
| 10799 | odata = PyUnicode_DATA(repr); |
| 10800 | |
| 10801 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 10802 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
| 10803 | |
| 10804 | for (i = 0, o = 1; i < isize; i++) { |
| 10805 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10806 | |
| 10807 | /* Escape quotes and backslashes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10808 | if ((ch == quote) || (ch == '\\')) { |
| 10809 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 10810 | PyUnicode_WRITE(okind, odata, o++, ch); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10811 | continue; |
| 10812 | } |
| 10813 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10814 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10815 | if (ch == '\t') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10816 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 10817 | PyUnicode_WRITE(okind, odata, o++, 't'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10818 | } |
| 10819 | else if (ch == '\n') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10820 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 10821 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10822 | } |
| 10823 | else if (ch == '\r') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10824 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 10825 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10826 | } |
| 10827 | |
| 10828 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10829 | else if (ch < ' ' || ch == 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10830 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 10831 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 10832 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0x000F]); |
| 10833 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0x000F]); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10834 | } |
| 10835 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10836 | /* Copy ASCII characters as-is */ |
| 10837 | else if (ch < 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10838 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10839 | } |
| 10840 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10841 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10842 | else { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10843 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10844 | (categories Z* and C* except ASCII space) |
| 10845 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10846 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10847 | /* Map 8-bit characters to '\xhh' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10848 | if (ch <= 0xff) { |
| 10849 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 10850 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 10851 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0x000F]); |
| 10852 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0x000F]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10853 | } |
| 10854 | /* Map 21-bit characters to '\U00xxxxxx' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10855 | else if (ch >= 0x10000) { |
| 10856 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 10857 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
| 10858 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 28) & 0xF]); |
| 10859 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 24) & 0xF]); |
| 10860 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 20) & 0xF]); |
| 10861 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 16) & 0xF]); |
| 10862 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 12) & 0xF]); |
| 10863 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 8) & 0xF]); |
| 10864 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0xF]); |
| 10865 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10866 | } |
| 10867 | /* Map 16-bit characters to '\uxxxx' */ |
| 10868 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10869 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 10870 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
| 10871 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 12) & 0xF]); |
| 10872 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 8) & 0xF]); |
| 10873 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0xF]); |
| 10874 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10875 | } |
| 10876 | } |
| 10877 | /* Copy characters as-is */ |
| 10878 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10879 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10880 | } |
| 10881 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10882 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10883 | /* Closing quote already added at the beginning */ |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10884 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10885 | } |
| 10886 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10887 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10888 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10889 | \n\ |
| 10890 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 10891 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10892 | arguments start and end are interpreted as in slice notation.\n\ |
| 10893 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10894 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10895 | |
| 10896 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10897 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10898 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10899 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10900 | Py_ssize_t start; |
| 10901 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10902 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10903 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10904 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 10905 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10906 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10907 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10908 | if (PyUnicode_READY(self) == -1) |
| 10909 | return NULL; |
| 10910 | if (PyUnicode_READY(substring) == -1) |
| 10911 | return NULL; |
| 10912 | |
| 10913 | result = any_find_slice( |
| 10914 | ucs1lib_rfind_slice, ucs2lib_rfind_slice, ucs4lib_rfind_slice, |
| 10915 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10916 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10917 | |
| 10918 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10919 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10920 | if (result == -2) |
| 10921 | return NULL; |
| 10922 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10923 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10924 | } |
| 10925 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10926 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10927 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10928 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10929 | 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] | 10930 | |
| 10931 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10932 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10933 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10934 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10935 | Py_ssize_t start; |
| 10936 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10937 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10938 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10939 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 10940 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10941 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10942 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10943 | if (PyUnicode_READY(self) == -1) |
| 10944 | return NULL; |
| 10945 | if (PyUnicode_READY(substring) == -1) |
| 10946 | return NULL; |
| 10947 | |
| 10948 | result = any_find_slice( |
| 10949 | ucs1lib_rfind_slice, ucs2lib_rfind_slice, ucs4lib_rfind_slice, |
| 10950 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10951 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10952 | |
| 10953 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10954 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10955 | if (result == -2) |
| 10956 | return NULL; |
| 10957 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10958 | if (result < 0) { |
| 10959 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 10960 | return NULL; |
| 10961 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10962 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10963 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10964 | } |
| 10965 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10966 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10967 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10968 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 10969 | 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] | 10970 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10971 | |
| 10972 | static PyObject * |
| 10973 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 10974 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10975 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10976 | Py_UCS4 fillchar = ' '; |
| 10977 | |
| 10978 | if (PyUnicode_READY(self) == -1) |
| 10979 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10980 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10981 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10982 | return NULL; |
| 10983 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10984 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10985 | Py_INCREF(self); |
| 10986 | return (PyObject*) self; |
| 10987 | } |
| 10988 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10989 | return (PyObject*) pad(self, width - _PyUnicode_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10990 | } |
| 10991 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10992 | PyObject * |
| 10993 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10994 | { |
| 10995 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10996 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10997 | s = PyUnicode_FromObject(s); |
| 10998 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10999 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11000 | if (sep != NULL) { |
| 11001 | sep = PyUnicode_FromObject(sep); |
| 11002 | if (sep == NULL) { |
| 11003 | Py_DECREF(s); |
| 11004 | return NULL; |
| 11005 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11006 | } |
| 11007 | |
| 11008 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 11009 | |
| 11010 | Py_DECREF(s); |
| 11011 | Py_XDECREF(sep); |
| 11012 | return result; |
| 11013 | } |
| 11014 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11015 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11016 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11017 | \n\ |
| 11018 | Return a list of the words in S, using sep as the\n\ |
| 11019 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 11020 | 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] | 11021 | whitespace string is a separator and empty strings are\n\ |
| 11022 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11023 | |
| 11024 | static PyObject* |
| 11025 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 11026 | { |
| 11027 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11028 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11029 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11030 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11031 | return NULL; |
| 11032 | |
| 11033 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11034 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11035 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11036 | return split(self, (PyUnicodeObject *)substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11037 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11038 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11039 | } |
| 11040 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11041 | PyObject * |
| 11042 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 11043 | { |
| 11044 | PyObject* str_obj; |
| 11045 | PyObject* sep_obj; |
| 11046 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11047 | int kind1, kind2, kind; |
| 11048 | void *buf1 = NULL, *buf2 = NULL; |
| 11049 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11050 | |
| 11051 | str_obj = PyUnicode_FromObject(str_in); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11052 | if (!str_obj || PyUnicode_READY(str_in) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11053 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11054 | sep_obj = PyUnicode_FromObject(sep_in); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11055 | if (!sep_obj || PyUnicode_READY(sep_obj) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11056 | Py_DECREF(str_obj); |
| 11057 | return NULL; |
| 11058 | } |
| 11059 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11060 | kind1 = PyUnicode_KIND(str_in); |
| 11061 | kind2 = PyUnicode_KIND(sep_obj); |
| 11062 | kind = kind1 > kind2 ? kind1 : kind2; |
| 11063 | buf1 = PyUnicode_DATA(str_in); |
| 11064 | if (kind1 != kind) |
| 11065 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 11066 | if (!buf1) |
| 11067 | goto onError; |
| 11068 | buf2 = PyUnicode_DATA(sep_obj); |
| 11069 | if (kind2 != kind) |
| 11070 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 11071 | if (!buf2) |
| 11072 | goto onError; |
| 11073 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 11074 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 11075 | |
| 11076 | switch(PyUnicode_KIND(str_in)) { |
| 11077 | case PyUnicode_1BYTE_KIND: |
| 11078 | out = ucs1lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11079 | break; |
| 11080 | case PyUnicode_2BYTE_KIND: |
| 11081 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11082 | break; |
| 11083 | case PyUnicode_4BYTE_KIND: |
| 11084 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11085 | break; |
| 11086 | default: |
| 11087 | assert(0); |
| 11088 | out = 0; |
| 11089 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11090 | |
| 11091 | Py_DECREF(sep_obj); |
| 11092 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11093 | if (kind1 != kind) |
| 11094 | PyMem_Free(buf1); |
| 11095 | if (kind2 != kind) |
| 11096 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11097 | |
| 11098 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11099 | onError: |
| 11100 | Py_DECREF(sep_obj); |
| 11101 | Py_DECREF(str_obj); |
| 11102 | if (kind1 != kind && buf1) |
| 11103 | PyMem_Free(buf1); |
| 11104 | if (kind2 != kind && buf2) |
| 11105 | PyMem_Free(buf2); |
| 11106 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11107 | } |
| 11108 | |
| 11109 | |
| 11110 | PyObject * |
| 11111 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 11112 | { |
| 11113 | PyObject* str_obj; |
| 11114 | PyObject* sep_obj; |
| 11115 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11116 | int kind1, kind2, kind; |
| 11117 | void *buf1 = NULL, *buf2 = NULL; |
| 11118 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11119 | |
| 11120 | str_obj = PyUnicode_FromObject(str_in); |
| 11121 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11122 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11123 | sep_obj = PyUnicode_FromObject(sep_in); |
| 11124 | if (!sep_obj) { |
| 11125 | Py_DECREF(str_obj); |
| 11126 | return NULL; |
| 11127 | } |
| 11128 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11129 | kind1 = PyUnicode_KIND(str_in); |
| 11130 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 11131 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11132 | buf1 = PyUnicode_DATA(str_in); |
| 11133 | if (kind1 != kind) |
| 11134 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 11135 | if (!buf1) |
| 11136 | goto onError; |
| 11137 | buf2 = PyUnicode_DATA(sep_obj); |
| 11138 | if (kind2 != kind) |
| 11139 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 11140 | if (!buf2) |
| 11141 | goto onError; |
| 11142 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 11143 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 11144 | |
| 11145 | switch(PyUnicode_KIND(str_in)) { |
| 11146 | case PyUnicode_1BYTE_KIND: |
| 11147 | out = ucs1lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11148 | break; |
| 11149 | case PyUnicode_2BYTE_KIND: |
| 11150 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11151 | break; |
| 11152 | case PyUnicode_4BYTE_KIND: |
| 11153 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11154 | break; |
| 11155 | default: |
| 11156 | assert(0); |
| 11157 | out = 0; |
| 11158 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11159 | |
| 11160 | Py_DECREF(sep_obj); |
| 11161 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11162 | if (kind1 != kind) |
| 11163 | PyMem_Free(buf1); |
| 11164 | if (kind2 != kind) |
| 11165 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11166 | |
| 11167 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11168 | onError: |
| 11169 | Py_DECREF(sep_obj); |
| 11170 | Py_DECREF(str_obj); |
| 11171 | if (kind1 != kind && buf1) |
| 11172 | PyMem_Free(buf1); |
| 11173 | if (kind2 != kind && buf2) |
| 11174 | PyMem_Free(buf2); |
| 11175 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11176 | } |
| 11177 | |
| 11178 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11179 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11180 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 11181 | 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] | 11182 | 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] | 11183 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11184 | |
| 11185 | static PyObject* |
| 11186 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
| 11187 | { |
| 11188 | return PyUnicode_Partition((PyObject *)self, separator); |
| 11189 | } |
| 11190 | |
| 11191 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 11192 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11193 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 11194 | 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] | 11195 | 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] | 11196 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11197 | |
| 11198 | static PyObject* |
| 11199 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 11200 | { |
| 11201 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 11202 | } |
| 11203 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11204 | PyObject * |
| 11205 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11206 | { |
| 11207 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11208 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11209 | s = PyUnicode_FromObject(s); |
| 11210 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11211 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11212 | if (sep != NULL) { |
| 11213 | sep = PyUnicode_FromObject(sep); |
| 11214 | if (sep == NULL) { |
| 11215 | Py_DECREF(s); |
| 11216 | return NULL; |
| 11217 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11218 | } |
| 11219 | |
| 11220 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 11221 | |
| 11222 | Py_DECREF(s); |
| 11223 | Py_XDECREF(sep); |
| 11224 | return result; |
| 11225 | } |
| 11226 | |
| 11227 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11228 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11229 | \n\ |
| 11230 | Return a list of the words in S, using sep as the\n\ |
| 11231 | delimiter string, starting at the end of the string and\n\ |
| 11232 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 11233 | splits are done. If sep is not specified, any whitespace string\n\ |
| 11234 | is a separator."); |
| 11235 | |
| 11236 | static PyObject* |
| 11237 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 11238 | { |
| 11239 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11240 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11241 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11242 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11243 | return NULL; |
| 11244 | |
| 11245 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11246 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11247 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11248 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11249 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11250 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11251 | } |
| 11252 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11253 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11254 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11255 | \n\ |
| 11256 | 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] | 11257 | 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] | 11258 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11259 | |
| 11260 | static PyObject* |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 11261 | unicode_splitlines(PyUnicodeObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11262 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 11263 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 11264 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11265 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 11266 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 11267 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11268 | return NULL; |
| 11269 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 11270 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11271 | } |
| 11272 | |
| 11273 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 11274 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11275 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 11276 | if (PyUnicode_CheckExact(self)) { |
| 11277 | Py_INCREF(self); |
| 11278 | return self; |
| 11279 | } else |
| 11280 | /* Subtype -- return genuine unicode string with the same value. */ |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 11281 | return PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11282 | } |
| 11283 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11284 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11285 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11286 | \n\ |
| 11287 | 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] | 11288 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11289 | |
| 11290 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11291 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11292 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11293 | return fixup(self, fixswapcase); |
| 11294 | } |
| 11295 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11296 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11297 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11298 | \n\ |
| 11299 | Return a translation table usable for str.translate().\n\ |
| 11300 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 11301 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11302 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11303 | If there are two arguments, they must be strings of equal length, and\n\ |
| 11304 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 11305 | character at the same position in y. If there is a third argument, it\n\ |
| 11306 | must be a string, whose characters will be mapped to None in the result."); |
| 11307 | |
| 11308 | static PyObject* |
| 11309 | unicode_maketrans(PyUnicodeObject *null, PyObject *args) |
| 11310 | { |
| 11311 | PyObject *x, *y = NULL, *z = NULL; |
| 11312 | PyObject *new = NULL, *key, *value; |
| 11313 | Py_ssize_t i = 0; |
| 11314 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11315 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11316 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 11317 | return NULL; |
| 11318 | new = PyDict_New(); |
| 11319 | if (!new) |
| 11320 | return NULL; |
| 11321 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11322 | int x_kind, y_kind, z_kind; |
| 11323 | void *x_data, *y_data, *z_data; |
| 11324 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11325 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11326 | if (!PyUnicode_Check(x)) { |
| 11327 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 11328 | "be a string if there is a second argument"); |
| 11329 | goto err; |
| 11330 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11331 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11332 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 11333 | "arguments must have equal length"); |
| 11334 | goto err; |
| 11335 | } |
| 11336 | /* 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] | 11337 | x_kind = PyUnicode_KIND(x); |
| 11338 | y_kind = PyUnicode_KIND(y); |
| 11339 | x_data = PyUnicode_DATA(x); |
| 11340 | y_data = PyUnicode_DATA(y); |
| 11341 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 11342 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
| 11343 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11344 | if (!key || !value) |
| 11345 | goto err; |
| 11346 | res = PyDict_SetItem(new, key, value); |
| 11347 | Py_DECREF(key); |
| 11348 | Py_DECREF(value); |
| 11349 | if (res < 0) |
| 11350 | goto err; |
| 11351 | } |
| 11352 | /* create entries for deleting chars in z */ |
| 11353 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11354 | z_kind = PyUnicode_KIND(z); |
| 11355 | z_data = PyUnicode_DATA(z); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11356 | for (i = 0; i < PyUnicode_GET_SIZE(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11357 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11358 | if (!key) |
| 11359 | goto err; |
| 11360 | res = PyDict_SetItem(new, key, Py_None); |
| 11361 | Py_DECREF(key); |
| 11362 | if (res < 0) |
| 11363 | goto err; |
| 11364 | } |
| 11365 | } |
| 11366 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11367 | int kind; |
| 11368 | void *data; |
| 11369 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11370 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 11371 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11372 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 11373 | "to maketrans it must be a dict"); |
| 11374 | goto err; |
| 11375 | } |
| 11376 | /* copy entries into the new dict, converting string keys to int keys */ |
| 11377 | while (PyDict_Next(x, &i, &key, &value)) { |
| 11378 | if (PyUnicode_Check(key)) { |
| 11379 | /* convert string keys to integer keys */ |
| 11380 | PyObject *newkey; |
| 11381 | if (PyUnicode_GET_SIZE(key) != 1) { |
| 11382 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 11383 | "table must be of length 1"); |
| 11384 | goto err; |
| 11385 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11386 | kind = PyUnicode_KIND(key); |
| 11387 | data = PyUnicode_DATA(key); |
| 11388 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11389 | if (!newkey) |
| 11390 | goto err; |
| 11391 | res = PyDict_SetItem(new, newkey, value); |
| 11392 | Py_DECREF(newkey); |
| 11393 | if (res < 0) |
| 11394 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11395 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11396 | /* just keep integer keys */ |
| 11397 | if (PyDict_SetItem(new, key, value) < 0) |
| 11398 | goto err; |
| 11399 | } else { |
| 11400 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 11401 | "be strings or integers"); |
| 11402 | goto err; |
| 11403 | } |
| 11404 | } |
| 11405 | } |
| 11406 | return new; |
| 11407 | err: |
| 11408 | Py_DECREF(new); |
| 11409 | return NULL; |
| 11410 | } |
| 11411 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11412 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11413 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11414 | \n\ |
| 11415 | Return a copy of the string S, where all characters have been mapped\n\ |
| 11416 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11417 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 11418 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 11419 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11420 | |
| 11421 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11422 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11423 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11424 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11425 | } |
| 11426 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11427 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11428 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11429 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11430 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11431 | |
| 11432 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11433 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11434 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11435 | return fixup(self, fixupper); |
| 11436 | } |
| 11437 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11438 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11439 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11440 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 11441 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 11442 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11443 | |
| 11444 | static PyObject * |
| 11445 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 11446 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11447 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11448 | PyUnicodeObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11449 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11450 | int kind; |
| 11451 | void *data; |
| 11452 | Py_UCS4 chr; |
| 11453 | |
| 11454 | if (PyUnicode_READY(self) == -1) |
| 11455 | return NULL; |
| 11456 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11457 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11458 | return NULL; |
| 11459 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11460 | if (PyUnicode_GET_LENGTH(self) >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 11461 | if (PyUnicode_CheckExact(self)) { |
| 11462 | Py_INCREF(self); |
| 11463 | return (PyObject*) self; |
| 11464 | } |
| 11465 | else |
Victor Stinner | 2219e0a | 2011-10-01 01:16:59 +0200 | [diff] [blame^] | 11466 | return PyUnicode_Copy((PyObject*)self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11467 | } |
| 11468 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11469 | fill = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11470 | |
| 11471 | u = pad(self, fill, 0, '0'); |
| 11472 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 11473 | if (u == NULL) |
| 11474 | return NULL; |
| 11475 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11476 | kind = PyUnicode_KIND(u); |
| 11477 | data = PyUnicode_DATA(u); |
| 11478 | chr = PyUnicode_READ(kind, data, fill); |
| 11479 | |
| 11480 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11481 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11482 | PyUnicode_WRITE(kind, data, 0, chr); |
| 11483 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11484 | } |
| 11485 | |
| 11486 | return (PyObject*) u; |
| 11487 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11488 | |
| 11489 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 11490 | static PyObject * |
| 11491 | unicode__decimal2ascii(PyObject *self) |
| 11492 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11493 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 11494 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11495 | #endif |
| 11496 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11497 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11498 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11499 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 11500 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 11501 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11502 | With optional end, stop comparing S at that position.\n\ |
| 11503 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11504 | |
| 11505 | static PyObject * |
| 11506 | unicode_startswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11507 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11508 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11509 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11510 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11511 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11512 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11513 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11514 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11515 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11516 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11517 | if (PyTuple_Check(subobj)) { |
| 11518 | Py_ssize_t i; |
| 11519 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 11520 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11521 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11522 | if (substring == NULL) |
| 11523 | return NULL; |
| 11524 | result = tailmatch(self, substring, start, end, -1); |
| 11525 | Py_DECREF(substring); |
| 11526 | if (result) { |
| 11527 | Py_RETURN_TRUE; |
| 11528 | } |
| 11529 | } |
| 11530 | /* nothing matched */ |
| 11531 | Py_RETURN_FALSE; |
| 11532 | } |
| 11533 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 11534 | if (substring == NULL) { |
| 11535 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 11536 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 11537 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11538 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 11539 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11540 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11541 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11542 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11543 | } |
| 11544 | |
| 11545 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11546 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11547 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11548 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 11549 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 11550 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11551 | With optional end, stop comparing S at that position.\n\ |
| 11552 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11553 | |
| 11554 | static PyObject * |
| 11555 | unicode_endswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11556 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11557 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11558 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11559 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11560 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11561 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11562 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11563 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11564 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11565 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11566 | if (PyTuple_Check(subobj)) { |
| 11567 | Py_ssize_t i; |
| 11568 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 11569 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11570 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11571 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11572 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11573 | result = tailmatch(self, substring, start, end, +1); |
| 11574 | Py_DECREF(substring); |
| 11575 | if (result) { |
| 11576 | Py_RETURN_TRUE; |
| 11577 | } |
| 11578 | } |
| 11579 | Py_RETURN_FALSE; |
| 11580 | } |
| 11581 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 11582 | if (substring == NULL) { |
| 11583 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 11584 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 11585 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11586 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 11587 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11588 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11589 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11590 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11591 | } |
| 11592 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11593 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 11594 | |
| 11595 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11596 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 11597 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 11598 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 11599 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 11600 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 11601 | PyDoc_STRVAR(format_map__doc__, |
| 11602 | "S.format_map(mapping) -> str\n\ |
| 11603 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 11604 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 11605 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 11606 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 11607 | static PyObject * |
| 11608 | unicode__format__(PyObject* self, PyObject* args) |
| 11609 | { |
| 11610 | PyObject *format_spec; |
| 11611 | |
| 11612 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 11613 | return NULL; |
| 11614 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11615 | return _PyUnicode_FormatAdvanced(self, format_spec, 0, |
| 11616 | PyUnicode_GET_LENGTH(format_spec)); |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 11617 | } |
| 11618 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 11619 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11620 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 11621 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 11622 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 11623 | |
| 11624 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 11625 | unicode__sizeof__(PyUnicodeObject *v) |
| 11626 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11627 | Py_ssize_t size; |
| 11628 | |
| 11629 | /* If it's a compact object, account for base structure + |
| 11630 | character data. */ |
| 11631 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 11632 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 11633 | else if (PyUnicode_IS_COMPACT(v)) |
| 11634 | size = sizeof(PyCompactUnicodeObject) + |
| 11635 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_CHARACTER_SIZE(v); |
| 11636 | else { |
| 11637 | /* If it is a two-block object, account for base object, and |
| 11638 | for character block if present. */ |
| 11639 | size = sizeof(PyUnicodeObject); |
| 11640 | if (v->data.any) |
| 11641 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
| 11642 | PyUnicode_CHARACTER_SIZE(v); |
| 11643 | } |
| 11644 | /* If the wstr pointer is present, account for it unless it is shared |
| 11645 | with the data pointer. Since PyUnicode_DATA will crash if the object |
| 11646 | is not ready, check whether it's either not ready (in which case the |
| 11647 | data is entirely in wstr) or if the data is not shared. */ |
| 11648 | if (_PyUnicode_WSTR(v) && |
| 11649 | (!PyUnicode_IS_READY(v) || |
| 11650 | (PyUnicode_DATA(v) != _PyUnicode_WSTR(v)))) |
| 11651 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
| 11652 | if (_PyUnicode_UTF8(v) && _PyUnicode_UTF8(v) != PyUnicode_DATA(v)) |
| 11653 | size += _PyUnicode_UTF8_LENGTH(v) + 1; |
| 11654 | |
| 11655 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 11656 | } |
| 11657 | |
| 11658 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11659 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 11660 | |
| 11661 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 11662 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 11663 | { |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 11664 | PyObject *copy = PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11665 | if (!copy) |
| 11666 | return NULL; |
| 11667 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 11668 | } |
| 11669 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11670 | static PyMethodDef unicode_methods[] = { |
| 11671 | |
| 11672 | /* Order is according to common usage: often used methods should |
| 11673 | appear first, since lookup is done sequentially. */ |
| 11674 | |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 11675 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11676 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 11677 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11678 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11679 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 11680 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 11681 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 11682 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 11683 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 11684 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 11685 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11686 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11687 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 11688 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 11689 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11690 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11691 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 11692 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 11693 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11694 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11695 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 11696 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11697 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11698 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 11699 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 11700 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 11701 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 11702 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 11703 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 11704 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 11705 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 11706 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 11707 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 11708 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 11709 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 11710 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 11711 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11712 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11713 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11714 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 11715 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 11716 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 11717 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11718 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 11719 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 11720 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 11721 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11722 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11723 | #endif |
| 11724 | |
| 11725 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 11726 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 11727 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11728 | #endif |
| 11729 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11730 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11731 | {NULL, NULL} |
| 11732 | }; |
| 11733 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 11734 | static PyObject * |
| 11735 | unicode_mod(PyObject *v, PyObject *w) |
| 11736 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 11737 | if (!PyUnicode_Check(v)) |
| 11738 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11739 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 11740 | } |
| 11741 | |
| 11742 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11743 | 0, /*nb_add*/ |
| 11744 | 0, /*nb_subtract*/ |
| 11745 | 0, /*nb_multiply*/ |
| 11746 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 11747 | }; |
| 11748 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11749 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11750 | (lenfunc) unicode_length, /* sq_length */ |
| 11751 | PyUnicode_Concat, /* sq_concat */ |
| 11752 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 11753 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 11754 | 0, /* sq_slice */ |
| 11755 | 0, /* sq_ass_item */ |
| 11756 | 0, /* sq_ass_slice */ |
| 11757 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11758 | }; |
| 11759 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11760 | static PyObject* |
| 11761 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 11762 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11763 | if (PyUnicode_READY(self) == -1) |
| 11764 | return NULL; |
| 11765 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 11766 | if (PyIndex_Check(item)) { |
| 11767 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11768 | if (i == -1 && PyErr_Occurred()) |
| 11769 | return NULL; |
| 11770 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11771 | i += PyUnicode_GET_LENGTH(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11772 | return unicode_getitem(self, i); |
| 11773 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11774 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11775 | const Py_UNICODE* source_buf; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11776 | Py_UNICODE* result_buf; |
| 11777 | PyObject* result; |
| 11778 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11779 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11780 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11781 | return NULL; |
| 11782 | } |
| 11783 | |
| 11784 | if (slicelength <= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11785 | return PyUnicode_New(0, 0); |
| 11786 | } else if (start == 0 && step == 1 && |
| 11787 | slicelength == PyUnicode_GET_LENGTH(self) && |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 11788 | PyUnicode_CheckExact(self)) { |
| 11789 | Py_INCREF(self); |
| 11790 | return (PyObject *)self; |
| 11791 | } else if (step == 1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11792 | return substring(self, start, slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11793 | } else { |
| 11794 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 11795 | result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* |
| 11796 | sizeof(Py_UNICODE)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11797 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11798 | if (result_buf == NULL) |
| 11799 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11800 | |
| 11801 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 11802 | result_buf[i] = source_buf[cur]; |
| 11803 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11804 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11805 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 11806 | PyObject_FREE(result_buf); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11807 | return result; |
| 11808 | } |
| 11809 | } else { |
| 11810 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 11811 | return NULL; |
| 11812 | } |
| 11813 | } |
| 11814 | |
| 11815 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11816 | (lenfunc)unicode_length, /* mp_length */ |
| 11817 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 11818 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11819 | }; |
| 11820 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11821 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11822 | /* Helpers for PyUnicode_Format() */ |
| 11823 | |
| 11824 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11825 | 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] | 11826 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11827 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11828 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11829 | (*p_argidx)++; |
| 11830 | if (arglen < 0) |
| 11831 | return args; |
| 11832 | else |
| 11833 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11834 | } |
| 11835 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11836 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11837 | return NULL; |
| 11838 | } |
| 11839 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 11840 | /* 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] | 11841 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 11842 | static PyObject * |
| 11843 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11844 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 11845 | char *p; |
| 11846 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11847 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11848 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11849 | x = PyFloat_AsDouble(v); |
| 11850 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 11851 | return NULL; |
| 11852 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11853 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11854 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 11855 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 11856 | p = PyOS_double_to_string(x, type, prec, |
| 11857 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 11858 | if (p == NULL) |
| 11859 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11860 | result = PyUnicode_DecodeASCII(p, strlen(p), NULL); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 11861 | PyMem_Free(p); |
| 11862 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11863 | } |
| 11864 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 11865 | static PyObject* |
| 11866 | formatlong(PyObject *val, int flags, int prec, int type) |
| 11867 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11868 | char *buf; |
| 11869 | int len; |
| 11870 | PyObject *str; /* temporary string object. */ |
| 11871 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 11872 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11873 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 11874 | if (!str) |
| 11875 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11876 | result = PyUnicode_DecodeASCII(buf, len, NULL); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11877 | Py_DECREF(str); |
| 11878 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 11879 | } |
| 11880 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11881 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11882 | formatchar(Py_UCS4 *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 11883 | size_t buflen, |
| 11884 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11885 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 11886 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 11887 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11888 | if (PyUnicode_GET_LENGTH(v) == 1) { |
| 11889 | buf[0] = PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11890 | buf[1] = '\0'; |
| 11891 | return 1; |
| 11892 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11893 | goto onError; |
| 11894 | } |
| 11895 | else { |
| 11896 | /* Integer input truncated to a character */ |
| 11897 | long x; |
| 11898 | x = PyLong_AsLong(v); |
| 11899 | if (x == -1 && PyErr_Occurred()) |
| 11900 | goto onError; |
| 11901 | |
| 11902 | if (x < 0 || x > 0x10ffff) { |
| 11903 | PyErr_SetString(PyExc_OverflowError, |
| 11904 | "%c arg not in range(0x110000)"); |
| 11905 | return -1; |
| 11906 | } |
| 11907 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11908 | buf[0] = (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11909 | buf[1] = '\0'; |
| 11910 | return 1; |
| 11911 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 11912 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11913 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 11914 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11915 | "%c requires int or char"); |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 11916 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11917 | } |
| 11918 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 11919 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 11920 | 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] | 11921 | */ |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 11922 | #define FORMATBUFLEN (size_t)10 |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 11923 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11924 | PyObject * |
| 11925 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11926 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11927 | void *fmt; |
| 11928 | int fmtkind; |
| 11929 | PyObject *result; |
| 11930 | Py_UCS4 *res, *res0; |
| 11931 | Py_UCS4 max; |
| 11932 | int kind; |
| 11933 | Py_ssize_t fmtcnt, fmtpos, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11934 | int args_owned = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11935 | PyObject *dict = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11936 | PyUnicodeObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11937 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11938 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11939 | PyErr_BadInternalCall(); |
| 11940 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11941 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11942 | uformat = (PyUnicodeObject*)PyUnicode_FromObject(format); |
| 11943 | if (uformat == NULL || PyUnicode_READY(uformat) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11944 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11945 | fmt = PyUnicode_DATA(uformat); |
| 11946 | fmtkind = PyUnicode_KIND(uformat); |
| 11947 | fmtcnt = PyUnicode_GET_LENGTH(uformat); |
| 11948 | fmtpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11949 | |
| 11950 | reslen = rescnt = fmtcnt + 100; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11951 | res = res0 = PyMem_Malloc(reslen * sizeof(Py_UCS4)); |
| 11952 | if (res0 == NULL) { |
| 11953 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11954 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11955 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11956 | |
| 11957 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11958 | arglen = PyTuple_Size(args); |
| 11959 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11960 | } |
| 11961 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11962 | arglen = -1; |
| 11963 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11964 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 11965 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 11966 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11967 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11968 | |
| 11969 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11970 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11971 | if (--rescnt < 0) { |
| 11972 | rescnt = fmtcnt + 100; |
| 11973 | reslen += rescnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11974 | res0 = PyMem_Realloc(res0, reslen*sizeof(Py_UCS4)); |
| 11975 | if (res0 == NULL){ |
| 11976 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11977 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11978 | } |
| 11979 | res = res0 + reslen - rescnt; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11980 | --rescnt; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11981 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11982 | *res++ = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11983 | } |
| 11984 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11985 | /* Got a format specifier */ |
| 11986 | int flags = 0; |
| 11987 | Py_ssize_t width = -1; |
| 11988 | int prec = -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11989 | Py_UCS4 c = '\0'; |
| 11990 | Py_UCS4 fill; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11991 | int isnumok; |
| 11992 | PyObject *v = NULL; |
| 11993 | PyObject *temp = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11994 | void *pbuf; |
| 11995 | Py_ssize_t pindex; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11996 | Py_UNICODE sign; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11997 | Py_ssize_t len, len1; |
| 11998 | Py_UCS4 formatbuf[FORMATBUFLEN]; /* For formatchar() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11999 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12000 | fmtpos++; |
| 12001 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') { |
| 12002 | Py_ssize_t keystart; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12003 | Py_ssize_t keylen; |
| 12004 | PyObject *key; |
| 12005 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 12006 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12007 | if (dict == NULL) { |
| 12008 | PyErr_SetString(PyExc_TypeError, |
| 12009 | "format requires a mapping"); |
| 12010 | goto onError; |
| 12011 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12012 | ++fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12013 | --fmtcnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12014 | keystart = fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12015 | /* Skip over balanced parentheses */ |
| 12016 | while (pcount > 0 && --fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12017 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == ')') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12018 | --pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12019 | else if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12020 | ++pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12021 | fmtpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12022 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12023 | keylen = fmtpos - keystart - 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12024 | if (fmtcnt < 0 || pcount > 0) { |
| 12025 | PyErr_SetString(PyExc_ValueError, |
| 12026 | "incomplete format key"); |
| 12027 | goto onError; |
| 12028 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12029 | key = substring(uformat, keystart, keylen); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12030 | if (key == NULL) |
| 12031 | goto onError; |
| 12032 | if (args_owned) { |
| 12033 | Py_DECREF(args); |
| 12034 | args_owned = 0; |
| 12035 | } |
| 12036 | args = PyObject_GetItem(dict, key); |
| 12037 | Py_DECREF(key); |
| 12038 | if (args == NULL) { |
| 12039 | goto onError; |
| 12040 | } |
| 12041 | args_owned = 1; |
| 12042 | arglen = -1; |
| 12043 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12044 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12045 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12046 | switch (c = PyUnicode_READ(fmtkind, fmt, fmtpos++)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12047 | case '-': flags |= F_LJUST; continue; |
| 12048 | case '+': flags |= F_SIGN; continue; |
| 12049 | case ' ': flags |= F_BLANK; continue; |
| 12050 | case '#': flags |= F_ALT; continue; |
| 12051 | case '0': flags |= F_ZERO; continue; |
| 12052 | } |
| 12053 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12054 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12055 | if (c == '*') { |
| 12056 | v = getnextarg(args, arglen, &argidx); |
| 12057 | if (v == NULL) |
| 12058 | goto onError; |
| 12059 | if (!PyLong_Check(v)) { |
| 12060 | PyErr_SetString(PyExc_TypeError, |
| 12061 | "* wants int"); |
| 12062 | goto onError; |
| 12063 | } |
| 12064 | width = PyLong_AsLong(v); |
| 12065 | if (width == -1 && PyErr_Occurred()) |
| 12066 | goto onError; |
| 12067 | if (width < 0) { |
| 12068 | flags |= F_LJUST; |
| 12069 | width = -width; |
| 12070 | } |
| 12071 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12072 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12073 | } |
| 12074 | else if (c >= '0' && c <= '9') { |
| 12075 | width = c - '0'; |
| 12076 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12077 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12078 | if (c < '0' || c > '9') |
| 12079 | break; |
| 12080 | if ((width*10) / 10 != width) { |
| 12081 | PyErr_SetString(PyExc_ValueError, |
| 12082 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12083 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12084 | } |
| 12085 | width = width*10 + (c - '0'); |
| 12086 | } |
| 12087 | } |
| 12088 | if (c == '.') { |
| 12089 | prec = 0; |
| 12090 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12091 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12092 | if (c == '*') { |
| 12093 | v = getnextarg(args, arglen, &argidx); |
| 12094 | if (v == NULL) |
| 12095 | goto onError; |
| 12096 | if (!PyLong_Check(v)) { |
| 12097 | PyErr_SetString(PyExc_TypeError, |
| 12098 | "* wants int"); |
| 12099 | goto onError; |
| 12100 | } |
| 12101 | prec = PyLong_AsLong(v); |
| 12102 | if (prec == -1 && PyErr_Occurred()) |
| 12103 | goto onError; |
| 12104 | if (prec < 0) |
| 12105 | prec = 0; |
| 12106 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12107 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12108 | } |
| 12109 | else if (c >= '0' && c <= '9') { |
| 12110 | prec = c - '0'; |
| 12111 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12112 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12113 | if (c < '0' || c > '9') |
| 12114 | break; |
| 12115 | if ((prec*10) / 10 != prec) { |
| 12116 | PyErr_SetString(PyExc_ValueError, |
| 12117 | "prec too big"); |
| 12118 | goto onError; |
| 12119 | } |
| 12120 | prec = prec*10 + (c - '0'); |
| 12121 | } |
| 12122 | } |
| 12123 | } /* prec */ |
| 12124 | if (fmtcnt >= 0) { |
| 12125 | if (c == 'h' || c == 'l' || c == 'L') { |
| 12126 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12127 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12128 | } |
| 12129 | } |
| 12130 | if (fmtcnt < 0) { |
| 12131 | PyErr_SetString(PyExc_ValueError, |
| 12132 | "incomplete format"); |
| 12133 | goto onError; |
| 12134 | } |
| 12135 | if (c != '%') { |
| 12136 | v = getnextarg(args, arglen, &argidx); |
| 12137 | if (v == NULL) |
| 12138 | goto onError; |
| 12139 | } |
| 12140 | sign = 0; |
| 12141 | fill = ' '; |
| 12142 | switch (c) { |
| 12143 | |
| 12144 | case '%': |
| 12145 | pbuf = formatbuf; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12146 | kind = PyUnicode_4BYTE_KIND; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12147 | /* presume that buffer length is at least 1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12148 | PyUnicode_WRITE(kind, pbuf, 0, '%'); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12149 | len = 1; |
| 12150 | break; |
| 12151 | |
| 12152 | case 's': |
| 12153 | case 'r': |
| 12154 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 12155 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12156 | temp = v; |
| 12157 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12158 | } |
| 12159 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12160 | if (c == 's') |
| 12161 | temp = PyObject_Str(v); |
| 12162 | else if (c == 'r') |
| 12163 | temp = PyObject_Repr(v); |
| 12164 | else |
| 12165 | temp = PyObject_ASCII(v); |
| 12166 | if (temp == NULL) |
| 12167 | goto onError; |
| 12168 | if (PyUnicode_Check(temp)) |
| 12169 | /* nothing to do */; |
| 12170 | else { |
| 12171 | Py_DECREF(temp); |
| 12172 | PyErr_SetString(PyExc_TypeError, |
| 12173 | "%s argument has non-string str()"); |
| 12174 | goto onError; |
| 12175 | } |
| 12176 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12177 | if (PyUnicode_READY(temp) == -1) { |
| 12178 | Py_CLEAR(temp); |
| 12179 | goto onError; |
| 12180 | } |
| 12181 | pbuf = PyUnicode_DATA(temp); |
| 12182 | kind = PyUnicode_KIND(temp); |
| 12183 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12184 | if (prec >= 0 && len > prec) |
| 12185 | len = prec; |
| 12186 | break; |
| 12187 | |
| 12188 | case 'i': |
| 12189 | case 'd': |
| 12190 | case 'u': |
| 12191 | case 'o': |
| 12192 | case 'x': |
| 12193 | case 'X': |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12194 | isnumok = 0; |
| 12195 | if (PyNumber_Check(v)) { |
| 12196 | PyObject *iobj=NULL; |
| 12197 | |
| 12198 | if (PyLong_Check(v)) { |
| 12199 | iobj = v; |
| 12200 | Py_INCREF(iobj); |
| 12201 | } |
| 12202 | else { |
| 12203 | iobj = PyNumber_Long(v); |
| 12204 | } |
| 12205 | if (iobj!=NULL) { |
| 12206 | if (PyLong_Check(iobj)) { |
| 12207 | isnumok = 1; |
Senthil Kumaran | 9ebe08d | 2011-07-03 21:03:16 -0700 | [diff] [blame] | 12208 | temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12209 | Py_DECREF(iobj); |
| 12210 | if (!temp) |
| 12211 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12212 | if (PyUnicode_READY(temp) == -1) { |
| 12213 | Py_CLEAR(temp); |
| 12214 | goto onError; |
| 12215 | } |
| 12216 | pbuf = PyUnicode_DATA(temp); |
| 12217 | kind = PyUnicode_KIND(temp); |
| 12218 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12219 | sign = 1; |
| 12220 | } |
| 12221 | else { |
| 12222 | Py_DECREF(iobj); |
| 12223 | } |
| 12224 | } |
| 12225 | } |
| 12226 | if (!isnumok) { |
| 12227 | PyErr_Format(PyExc_TypeError, |
| 12228 | "%%%c format: a number is required, " |
| 12229 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 12230 | goto onError; |
| 12231 | } |
| 12232 | if (flags & F_ZERO) |
| 12233 | fill = '0'; |
| 12234 | break; |
| 12235 | |
| 12236 | case 'e': |
| 12237 | case 'E': |
| 12238 | case 'f': |
| 12239 | case 'F': |
| 12240 | case 'g': |
| 12241 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12242 | temp = formatfloat(v, flags, prec, c); |
| 12243 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12244 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12245 | if (PyUnicode_READY(temp) == -1) { |
| 12246 | Py_CLEAR(temp); |
| 12247 | goto onError; |
| 12248 | } |
| 12249 | pbuf = PyUnicode_DATA(temp); |
| 12250 | kind = PyUnicode_KIND(temp); |
| 12251 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12252 | sign = 1; |
| 12253 | if (flags & F_ZERO) |
| 12254 | fill = '0'; |
| 12255 | break; |
| 12256 | |
| 12257 | case 'c': |
| 12258 | pbuf = formatbuf; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12259 | kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | b9dcffb | 2011-09-29 00:39:24 +0200 | [diff] [blame] | 12260 | len = formatchar(pbuf, Py_ARRAY_LENGTH(formatbuf), v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12261 | if (len < 0) |
| 12262 | goto onError; |
| 12263 | break; |
| 12264 | |
| 12265 | default: |
| 12266 | PyErr_Format(PyExc_ValueError, |
| 12267 | "unsupported format character '%c' (0x%x) " |
| 12268 | "at index %zd", |
| 12269 | (31<=c && c<=126) ? (char)c : '?', |
| 12270 | (int)c, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12271 | fmtpos - 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12272 | goto onError; |
| 12273 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12274 | /* pbuf is initialized here. */ |
| 12275 | pindex = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12276 | if (sign) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12277 | if (PyUnicode_READ(kind, pbuf, pindex) == '-' || |
| 12278 | PyUnicode_READ(kind, pbuf, pindex) == '+') { |
| 12279 | sign = PyUnicode_READ(kind, pbuf, pindex++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12280 | len--; |
| 12281 | } |
| 12282 | else if (flags & F_SIGN) |
| 12283 | sign = '+'; |
| 12284 | else if (flags & F_BLANK) |
| 12285 | sign = ' '; |
| 12286 | else |
| 12287 | sign = 0; |
| 12288 | } |
| 12289 | if (width < len) |
| 12290 | width = len; |
| 12291 | if (rescnt - (sign != 0) < width) { |
| 12292 | reslen -= rescnt; |
| 12293 | rescnt = width + fmtcnt + 100; |
| 12294 | reslen += rescnt; |
| 12295 | if (reslen < 0) { |
| 12296 | Py_XDECREF(temp); |
| 12297 | PyErr_NoMemory(); |
| 12298 | goto onError; |
| 12299 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12300 | res0 = PyMem_Realloc(res0, reslen*sizeof(Py_UCS4)); |
| 12301 | if (res0 == 0) { |
| 12302 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12303 | Py_XDECREF(temp); |
| 12304 | goto onError; |
| 12305 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12306 | res = res0 + reslen - rescnt; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12307 | } |
| 12308 | if (sign) { |
| 12309 | if (fill != ' ') |
| 12310 | *res++ = sign; |
| 12311 | rescnt--; |
| 12312 | if (width > len) |
| 12313 | width--; |
| 12314 | } |
| 12315 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12316 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 12317 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12318 | if (fill != ' ') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12319 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
| 12320 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12321 | } |
| 12322 | rescnt -= 2; |
| 12323 | width -= 2; |
| 12324 | if (width < 0) |
| 12325 | width = 0; |
| 12326 | len -= 2; |
| 12327 | } |
| 12328 | if (width > len && !(flags & F_LJUST)) { |
| 12329 | do { |
| 12330 | --rescnt; |
| 12331 | *res++ = fill; |
| 12332 | } while (--width > len); |
| 12333 | } |
| 12334 | if (fill == ' ') { |
| 12335 | if (sign) |
| 12336 | *res++ = sign; |
| 12337 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12338 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 12339 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
| 12340 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
| 12341 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12342 | } |
| 12343 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12344 | /* Copy all characters, preserving len */ |
| 12345 | len1 = len; |
| 12346 | while (len1--) { |
| 12347 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
| 12348 | rescnt--; |
| 12349 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12350 | while (--width >= len) { |
| 12351 | --rescnt; |
| 12352 | *res++ = ' '; |
| 12353 | } |
| 12354 | if (dict && (argidx < arglen) && c != '%') { |
| 12355 | PyErr_SetString(PyExc_TypeError, |
| 12356 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 12357 | Py_XDECREF(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12358 | goto onError; |
| 12359 | } |
| 12360 | Py_XDECREF(temp); |
| 12361 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12362 | } /* until end */ |
| 12363 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12364 | PyErr_SetString(PyExc_TypeError, |
| 12365 | "not all arguments converted during string formatting"); |
| 12366 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12367 | } |
| 12368 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12369 | |
| 12370 | for (max=0, res = res0; res < res0+reslen-rescnt; res++) |
| 12371 | if (*res > max) |
| 12372 | max = *res; |
| 12373 | result = PyUnicode_New(reslen - rescnt, max); |
| 12374 | if (!result) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12375 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12376 | kind = PyUnicode_KIND(result); |
| 12377 | for (res = res0; res < res0+reslen-rescnt; res++) |
| 12378 | PyUnicode_WRITE(kind, PyUnicode_DATA(result), res-res0, *res); |
| 12379 | PyMem_Free(res0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12380 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12381 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12382 | } |
| 12383 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12384 | return (PyObject *)result; |
| 12385 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12386 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12387 | PyMem_Free(res0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12388 | Py_DECREF(uformat); |
| 12389 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12390 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12391 | } |
| 12392 | return NULL; |
| 12393 | } |
| 12394 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 12395 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 12396 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 12397 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12398 | static PyObject * |
| 12399 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 12400 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12401 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12402 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 12403 | char *encoding = NULL; |
| 12404 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12405 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12406 | if (type != &PyUnicode_Type) |
| 12407 | return unicode_subtype_new(type, args, kwds); |
| 12408 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12409 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12410 | return NULL; |
| 12411 | if (x == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12412 | return (PyObject *)PyUnicode_New(0, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12413 | if (encoding == NULL && errors == NULL) |
| 12414 | return PyObject_Str(x); |
| 12415 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12416 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12417 | } |
| 12418 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 12419 | static PyObject * |
| 12420 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 12421 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12422 | PyUnicodeObject *tmp, *pnew; |
| 12423 | Py_ssize_t n; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12424 | PyObject *err = NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 12425 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12426 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 12427 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 12428 | if (tmp == NULL) |
| 12429 | return NULL; |
| 12430 | assert(PyUnicode_Check(tmp)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12431 | // TODO: Verify the PyUnicode_GET_SIZE does the right thing. |
| 12432 | // it seems kind of strange that tp_alloc gets passed the size |
| 12433 | // of the unicode string because there will follow another |
| 12434 | // malloc. |
| 12435 | pnew = (PyUnicodeObject *) type->tp_alloc(type, |
| 12436 | n = PyUnicode_GET_SIZE(tmp)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12437 | if (pnew == NULL) { |
| 12438 | Py_DECREF(tmp); |
| 12439 | return NULL; |
| 12440 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12441 | _PyUnicode_WSTR(pnew) = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1)); |
| 12442 | if (_PyUnicode_WSTR(pnew) == NULL) { |
| 12443 | err = PyErr_NoMemory(); |
| 12444 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12445 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12446 | Py_UNICODE_COPY(_PyUnicode_WSTR(pnew), PyUnicode_AS_UNICODE(tmp), n+1); |
| 12447 | _PyUnicode_WSTR_LENGTH(pnew) = n; |
| 12448 | _PyUnicode_HASH(pnew) = _PyUnicode_HASH(tmp); |
| 12449 | _PyUnicode_STATE(pnew).interned = 0; |
| 12450 | _PyUnicode_STATE(pnew).kind = 0; |
| 12451 | _PyUnicode_STATE(pnew).compact = 0; |
| 12452 | _PyUnicode_STATE(pnew).ready = 0; |
| 12453 | _PyUnicode_STATE(pnew).ascii = 0; |
| 12454 | pnew->data.any = NULL; |
| 12455 | _PyUnicode_LENGTH(pnew) = 0; |
| 12456 | pnew->_base.utf8 = NULL; |
| 12457 | pnew->_base.utf8_length = 0; |
| 12458 | |
| 12459 | if (PyUnicode_READY(pnew) == -1) { |
| 12460 | PyObject_FREE(_PyUnicode_WSTR(pnew)); |
| 12461 | goto onError; |
| 12462 | } |
| 12463 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12464 | Py_DECREF(tmp); |
| 12465 | return (PyObject *)pnew; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12466 | |
| 12467 | onError: |
| 12468 | _Py_ForgetReference((PyObject *)pnew); |
| 12469 | PyObject_Del(pnew); |
| 12470 | Py_DECREF(tmp); |
| 12471 | return err; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 12472 | } |
| 12473 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12474 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12475 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12476 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 12477 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 12478 | encoding defaults to the current default string encoding.\n\ |
| 12479 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12480 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12481 | static PyObject *unicode_iter(PyObject *seq); |
| 12482 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12483 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 12484 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12485 | "str", /* tp_name */ |
| 12486 | sizeof(PyUnicodeObject), /* tp_size */ |
| 12487 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12488 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12489 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 12490 | 0, /* tp_print */ |
| 12491 | 0, /* tp_getattr */ |
| 12492 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 12493 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12494 | unicode_repr, /* tp_repr */ |
| 12495 | &unicode_as_number, /* tp_as_number */ |
| 12496 | &unicode_as_sequence, /* tp_as_sequence */ |
| 12497 | &unicode_as_mapping, /* tp_as_mapping */ |
| 12498 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 12499 | 0, /* tp_call*/ |
| 12500 | (reprfunc) unicode_str, /* tp_str */ |
| 12501 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 12502 | 0, /* tp_setattro */ |
| 12503 | 0, /* tp_as_buffer */ |
| 12504 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12505 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12506 | unicode_doc, /* tp_doc */ |
| 12507 | 0, /* tp_traverse */ |
| 12508 | 0, /* tp_clear */ |
| 12509 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 12510 | 0, /* tp_weaklistoffset */ |
| 12511 | unicode_iter, /* tp_iter */ |
| 12512 | 0, /* tp_iternext */ |
| 12513 | unicode_methods, /* tp_methods */ |
| 12514 | 0, /* tp_members */ |
| 12515 | 0, /* tp_getset */ |
| 12516 | &PyBaseObject_Type, /* tp_base */ |
| 12517 | 0, /* tp_dict */ |
| 12518 | 0, /* tp_descr_get */ |
| 12519 | 0, /* tp_descr_set */ |
| 12520 | 0, /* tp_dictoffset */ |
| 12521 | 0, /* tp_init */ |
| 12522 | 0, /* tp_alloc */ |
| 12523 | unicode_new, /* tp_new */ |
| 12524 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12525 | }; |
| 12526 | |
| 12527 | /* Initialize the Unicode implementation */ |
| 12528 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 12529 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12530 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 12531 | int i; |
| 12532 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12533 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12534 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12535 | 0x000A, /* LINE FEED */ |
| 12536 | 0x000D, /* CARRIAGE RETURN */ |
| 12537 | 0x001C, /* FILE SEPARATOR */ |
| 12538 | 0x001D, /* GROUP SEPARATOR */ |
| 12539 | 0x001E, /* RECORD SEPARATOR */ |
| 12540 | 0x0085, /* NEXT LINE */ |
| 12541 | 0x2028, /* LINE SEPARATOR */ |
| 12542 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 12543 | }; |
| 12544 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 12545 | /* Init the implementation */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12546 | unicode_empty = (PyUnicodeObject *) PyUnicode_New(0, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12547 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12548 | Py_FatalError("Can't create empty string"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12549 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 12550 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12551 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 12552 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12553 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12554 | |
| 12555 | /* initialize the linebreak bloom filter */ |
| 12556 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12557 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 12558 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12559 | |
| 12560 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12561 | } |
| 12562 | |
| 12563 | /* Finalize the Unicode implementation */ |
| 12564 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 12565 | int |
| 12566 | PyUnicode_ClearFreeList(void) |
| 12567 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12568 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 12569 | } |
| 12570 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12571 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 12572 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12573 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 12574 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12575 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 12576 | Py_XDECREF(unicode_empty); |
| 12577 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 12578 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 12579 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12580 | if (unicode_latin1[i]) { |
| 12581 | Py_DECREF(unicode_latin1[i]); |
| 12582 | unicode_latin1[i] = NULL; |
| 12583 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 12584 | } |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 12585 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12586 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 12587 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12588 | void |
| 12589 | PyUnicode_InternInPlace(PyObject **p) |
| 12590 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12591 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 12592 | PyObject *t; |
| 12593 | if (s == NULL || !PyUnicode_Check(s)) |
| 12594 | Py_FatalError( |
| 12595 | "PyUnicode_InternInPlace: unicode strings only please!"); |
| 12596 | /* If it's a subclass, we don't really know what putting |
| 12597 | it in the interned dict might do. */ |
| 12598 | if (!PyUnicode_CheckExact(s)) |
| 12599 | return; |
| 12600 | if (PyUnicode_CHECK_INTERNED(s)) |
| 12601 | return; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12602 | if (PyUnicode_READY(s) == -1) { |
| 12603 | assert(0 && "ready fail in intern..."); |
| 12604 | return; |
| 12605 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12606 | if (interned == NULL) { |
| 12607 | interned = PyDict_New(); |
| 12608 | if (interned == NULL) { |
| 12609 | PyErr_Clear(); /* Don't leave an exception */ |
| 12610 | return; |
| 12611 | } |
| 12612 | } |
| 12613 | /* It might be that the GetItem call fails even |
| 12614 | though the key is present in the dictionary, |
| 12615 | namely when this happens during a stack overflow. */ |
| 12616 | Py_ALLOW_RECURSION |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12617 | t = PyDict_GetItem(interned, (PyObject *)s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12618 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 12619 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12620 | if (t) { |
| 12621 | Py_INCREF(t); |
| 12622 | Py_DECREF(*p); |
| 12623 | *p = t; |
| 12624 | return; |
| 12625 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12626 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12627 | PyThreadState_GET()->recursion_critical = 1; |
| 12628 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 12629 | PyErr_Clear(); |
| 12630 | PyThreadState_GET()->recursion_critical = 0; |
| 12631 | return; |
| 12632 | } |
| 12633 | PyThreadState_GET()->recursion_critical = 0; |
| 12634 | /* The two references in interned are not counted by refcnt. |
| 12635 | The deallocator will take care of this */ |
| 12636 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12637 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12638 | } |
| 12639 | |
| 12640 | void |
| 12641 | PyUnicode_InternImmortal(PyObject **p) |
| 12642 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12643 | PyUnicodeObject *u = (PyUnicodeObject *)*p; |
| 12644 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12645 | PyUnicode_InternInPlace(p); |
| 12646 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12647 | _PyUnicode_STATE(u).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12648 | Py_INCREF(*p); |
| 12649 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12650 | } |
| 12651 | |
| 12652 | PyObject * |
| 12653 | PyUnicode_InternFromString(const char *cp) |
| 12654 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12655 | PyObject *s = PyUnicode_FromString(cp); |
| 12656 | if (s == NULL) |
| 12657 | return NULL; |
| 12658 | PyUnicode_InternInPlace(&s); |
| 12659 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12660 | } |
| 12661 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12662 | void |
| 12663 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12664 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12665 | PyObject *keys; |
| 12666 | PyUnicodeObject *s; |
| 12667 | Py_ssize_t i, n; |
| 12668 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12669 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12670 | if (interned == NULL || !PyDict_Check(interned)) |
| 12671 | return; |
| 12672 | keys = PyDict_Keys(interned); |
| 12673 | if (keys == NULL || !PyList_Check(keys)) { |
| 12674 | PyErr_Clear(); |
| 12675 | return; |
| 12676 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12677 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12678 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 12679 | detector, interned unicode strings are not forcibly deallocated; |
| 12680 | rather, we give them their stolen references back, and then clear |
| 12681 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12682 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12683 | n = PyList_GET_SIZE(keys); |
| 12684 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12685 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12686 | for (i = 0; i < n; i++) { |
| 12687 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12688 | if (PyUnicode_READY(s) == -1) |
| 12689 | fprintf(stderr, "could not ready string\n"); |
| 12690 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12691 | case SSTATE_NOT_INTERNED: |
| 12692 | /* XXX Shouldn't happen */ |
| 12693 | break; |
| 12694 | case SSTATE_INTERNED_IMMORTAL: |
| 12695 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12696 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12697 | break; |
| 12698 | case SSTATE_INTERNED_MORTAL: |
| 12699 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12700 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12701 | break; |
| 12702 | default: |
| 12703 | Py_FatalError("Inconsistent interned string state."); |
| 12704 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12705 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12706 | } |
| 12707 | fprintf(stderr, "total size of all interned strings: " |
| 12708 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 12709 | "mortal/immortal\n", mortal_size, immortal_size); |
| 12710 | Py_DECREF(keys); |
| 12711 | PyDict_Clear(interned); |
| 12712 | Py_DECREF(interned); |
| 12713 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12714 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12715 | |
| 12716 | |
| 12717 | /********************* Unicode Iterator **************************/ |
| 12718 | |
| 12719 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12720 | PyObject_HEAD |
| 12721 | Py_ssize_t it_index; |
| 12722 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12723 | } unicodeiterobject; |
| 12724 | |
| 12725 | static void |
| 12726 | unicodeiter_dealloc(unicodeiterobject *it) |
| 12727 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12728 | _PyObject_GC_UNTRACK(it); |
| 12729 | Py_XDECREF(it->it_seq); |
| 12730 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12731 | } |
| 12732 | |
| 12733 | static int |
| 12734 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 12735 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12736 | Py_VISIT(it->it_seq); |
| 12737 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12738 | } |
| 12739 | |
| 12740 | static PyObject * |
| 12741 | unicodeiter_next(unicodeiterobject *it) |
| 12742 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12743 | PyUnicodeObject *seq; |
| 12744 | PyObject *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12745 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12746 | assert(it != NULL); |
| 12747 | seq = it->it_seq; |
| 12748 | if (seq == NULL) |
| 12749 | return NULL; |
| 12750 | assert(PyUnicode_Check(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12751 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12752 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 12753 | int kind = PyUnicode_KIND(seq); |
| 12754 | void *data = PyUnicode_DATA(seq); |
| 12755 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 12756 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12757 | if (item != NULL) |
| 12758 | ++it->it_index; |
| 12759 | return item; |
| 12760 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12761 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12762 | Py_DECREF(seq); |
| 12763 | it->it_seq = NULL; |
| 12764 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12765 | } |
| 12766 | |
| 12767 | static PyObject * |
| 12768 | unicodeiter_len(unicodeiterobject *it) |
| 12769 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12770 | Py_ssize_t len = 0; |
| 12771 | if (it->it_seq) |
| 12772 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
| 12773 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12774 | } |
| 12775 | |
| 12776 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 12777 | |
| 12778 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12779 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12780 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12781 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12782 | }; |
| 12783 | |
| 12784 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12785 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 12786 | "str_iterator", /* tp_name */ |
| 12787 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 12788 | 0, /* tp_itemsize */ |
| 12789 | /* methods */ |
| 12790 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 12791 | 0, /* tp_print */ |
| 12792 | 0, /* tp_getattr */ |
| 12793 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 12794 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12795 | 0, /* tp_repr */ |
| 12796 | 0, /* tp_as_number */ |
| 12797 | 0, /* tp_as_sequence */ |
| 12798 | 0, /* tp_as_mapping */ |
| 12799 | 0, /* tp_hash */ |
| 12800 | 0, /* tp_call */ |
| 12801 | 0, /* tp_str */ |
| 12802 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 12803 | 0, /* tp_setattro */ |
| 12804 | 0, /* tp_as_buffer */ |
| 12805 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 12806 | 0, /* tp_doc */ |
| 12807 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 12808 | 0, /* tp_clear */ |
| 12809 | 0, /* tp_richcompare */ |
| 12810 | 0, /* tp_weaklistoffset */ |
| 12811 | PyObject_SelfIter, /* tp_iter */ |
| 12812 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 12813 | unicodeiter_methods, /* tp_methods */ |
| 12814 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12815 | }; |
| 12816 | |
| 12817 | static PyObject * |
| 12818 | unicode_iter(PyObject *seq) |
| 12819 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12820 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12821 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12822 | if (!PyUnicode_Check(seq)) { |
| 12823 | PyErr_BadInternalCall(); |
| 12824 | return NULL; |
| 12825 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12826 | if (PyUnicode_READY(seq) == -1) |
| 12827 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12828 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 12829 | if (it == NULL) |
| 12830 | return NULL; |
| 12831 | it->it_index = 0; |
| 12832 | Py_INCREF(seq); |
| 12833 | it->it_seq = (PyUnicodeObject *)seq; |
| 12834 | _PyObject_GC_TRACK(it); |
| 12835 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12836 | } |
| 12837 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12838 | #define UNIOP(x) Py_UNICODE_##x |
| 12839 | #define UNIOP_t Py_UNICODE |
| 12840 | #include "uniops.h" |
| 12841 | #undef UNIOP |
| 12842 | #undef UNIOP_t |
| 12843 | #define UNIOP(x) Py_UCS4_##x |
| 12844 | #define UNIOP_t Py_UCS4 |
| 12845 | #include "uniops.h" |
| 12846 | #undef UNIOP |
| 12847 | #undef UNIOP_t |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 12848 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 12849 | Py_UNICODE* |
Victor Stinner | 4640860 | 2010-09-03 16:18:00 +0000 | [diff] [blame] | 12850 | PyUnicode_AsUnicodeCopy(PyObject *object) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 12851 | { |
| 12852 | PyUnicodeObject *unicode = (PyUnicodeObject *)object; |
| 12853 | Py_UNICODE *copy; |
| 12854 | Py_ssize_t size; |
| 12855 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12856 | if (!PyUnicode_Check(unicode)) { |
| 12857 | PyErr_BadArgument(); |
| 12858 | return NULL; |
| 12859 | } |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 12860 | /* Ensure we won't overflow the size. */ |
| 12861 | if (PyUnicode_GET_SIZE(unicode) > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 12862 | PyErr_NoMemory(); |
| 12863 | return NULL; |
| 12864 | } |
| 12865 | size = PyUnicode_GET_SIZE(unicode) + 1; /* copy the nul character */ |
| 12866 | size *= sizeof(Py_UNICODE); |
| 12867 | copy = PyMem_Malloc(size); |
| 12868 | if (copy == NULL) { |
| 12869 | PyErr_NoMemory(); |
| 12870 | return NULL; |
| 12871 | } |
| 12872 | memcpy(copy, PyUnicode_AS_UNICODE(unicode), size); |
| 12873 | return copy; |
| 12874 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 12875 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 12876 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 12877 | to the string.Formatter class implemented in Python. */ |
| 12878 | |
| 12879 | static PyMethodDef _string_methods[] = { |
| 12880 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 12881 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 12882 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 12883 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 12884 | {NULL, NULL} |
| 12885 | }; |
| 12886 | |
| 12887 | static struct PyModuleDef _string_module = { |
| 12888 | PyModuleDef_HEAD_INIT, |
| 12889 | "_string", |
| 12890 | PyDoc_STR("string helper module"), |
| 12891 | 0, |
| 12892 | _string_methods, |
| 12893 | NULL, |
| 12894 | NULL, |
| 12895 | NULL, |
| 12896 | NULL |
| 12897 | }; |
| 12898 | |
| 12899 | PyMODINIT_FUNC |
| 12900 | PyInit__string(void) |
| 12901 | { |
| 12902 | return PyModule_Create(&_string_module); |
| 12903 | } |
| 12904 | |
| 12905 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12906 | #ifdef __cplusplus |
| 12907 | } |
| 12908 | #endif |