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 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 93 | #define _PyUnicode_WSTR(op) (((PyASCIIObject*)(op))->wstr) |
| 94 | #define _PyUnicode_WSTR_LENGTH(op) (((PyCompactUnicodeObject*)(op))->wstr_length) |
| 95 | #define _PyUnicode_LENGTH(op) (((PyASCIIObject *)(op))->length) |
| 96 | #define _PyUnicode_STATE(op) (((PyASCIIObject *)(op))->state) |
| 97 | #define _PyUnicode_HASH(op) (((PyASCIIObject *)(op))->hash) |
| 98 | #define _PyUnicode_KIND(op) \ |
| 99 | (assert(PyUnicode_Check(op)), \ |
| 100 | ((PyASCIIObject *)(op))->state.kind) |
| 101 | #define _PyUnicode_GET_LENGTH(op) \ |
| 102 | (assert(PyUnicode_Check(op)), \ |
| 103 | ((PyASCIIObject *)(op))->length) |
| 104 | |
| 105 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 106 | /* This dictionary holds all interned unicode strings. Note that references |
| 107 | to strings in this dictionary are *not* counted in the string's ob_refcnt. |
| 108 | When the interned string reaches a refcnt of 0 the string deallocation |
| 109 | function will delete the reference from this dictionary. |
| 110 | |
| 111 | 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] | 112 | 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] | 113 | */ |
| 114 | static PyObject *interned; |
| 115 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 116 | /* The empty Unicode object is shared to improve performance. */ |
| 117 | static PyUnicodeObject *unicode_empty; |
| 118 | |
| 119 | /* Single character Unicode strings in the Latin-1 range are being |
| 120 | shared as well. */ |
| 121 | static PyUnicodeObject *unicode_latin1[256]; |
| 122 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 123 | /* Fast detection of the most frequent whitespace characters */ |
| 124 | const unsigned char _Py_ascii_whitespace[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 125 | 0, 0, 0, 0, 0, 0, 0, 0, |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 126 | /* case 0x0009: * CHARACTER TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 127 | /* case 0x000A: * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 128 | /* case 0x000B: * LINE TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 129 | /* case 0x000C: * FORM FEED */ |
| 130 | /* case 0x000D: * CARRIAGE RETURN */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 131 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 132 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 133 | /* case 0x001C: * FILE SEPARATOR */ |
| 134 | /* case 0x001D: * GROUP SEPARATOR */ |
| 135 | /* case 0x001E: * RECORD SEPARATOR */ |
| 136 | /* case 0x001F: * UNIT SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 137 | 0, 0, 0, 0, 1, 1, 1, 1, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 138 | /* case 0x0020: * SPACE */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 139 | 1, 0, 0, 0, 0, 0, 0, 0, |
| 140 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 141 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 142 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 143 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 144 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 145 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 146 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 147 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 148 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 149 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 150 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 151 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 152 | }; |
| 153 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 154 | static PyObject * |
| 155 | unicode_encode_call_errorhandler(const char *errors, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 156 | PyObject **errorHandler,const char *encoding, const char *reason, |
| 157 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 158 | Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos); |
| 159 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 160 | static void |
| 161 | raise_encode_exception(PyObject **exceptionObject, |
| 162 | const char *encoding, |
| 163 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 164 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 165 | const char *reason); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 166 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 167 | /* Same for linebreaks */ |
| 168 | static unsigned char ascii_linebreak[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 169 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 170 | /* 0x000A, * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 171 | /* 0x000B, * LINE TABULATION */ |
| 172 | /* 0x000C, * FORM FEED */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 173 | /* 0x000D, * CARRIAGE RETURN */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 174 | 0, 0, 1, 1, 1, 1, 0, 0, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 175 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 176 | /* 0x001C, * FILE SEPARATOR */ |
| 177 | /* 0x001D, * GROUP SEPARATOR */ |
| 178 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 179 | 0, 0, 0, 0, 1, 1, 1, 0, |
| 180 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 181 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 182 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 183 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 184 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 185 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 186 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 187 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 188 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 189 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 190 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 191 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 192 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 193 | }; |
| 194 | |
| 195 | |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 196 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 197 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 198 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 199 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 200 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 201 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 202 | /* This is actually an illegal character, so it should |
| 203 | not be passed to unichr. */ |
| 204 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 205 | #endif |
| 206 | } |
| 207 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 208 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 209 | |
| 210 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 211 | to keep things simple, we use a single bitmask, using the least 5 |
| 212 | bits from each unicode characters as the bit index. */ |
| 213 | |
| 214 | /* the linebreak mask is set up by Unicode_Init below */ |
| 215 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 216 | #if LONG_BIT >= 128 |
| 217 | #define BLOOM_WIDTH 128 |
| 218 | #elif LONG_BIT >= 64 |
| 219 | #define BLOOM_WIDTH 64 |
| 220 | #elif LONG_BIT >= 32 |
| 221 | #define BLOOM_WIDTH 32 |
| 222 | #else |
| 223 | #error "LONG_BIT is smaller than 32" |
| 224 | #endif |
| 225 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 226 | #define BLOOM_MASK unsigned long |
| 227 | |
| 228 | static BLOOM_MASK bloom_linebreak; |
| 229 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 230 | #define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
| 231 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 232 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 233 | #define BLOOM_LINEBREAK(ch) \ |
| 234 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 235 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 236 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 237 | Py_LOCAL_INLINE(BLOOM_MASK) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 238 | make_bloom_mask(int kind, void* ptr, Py_ssize_t len) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 239 | { |
| 240 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 241 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 242 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 243 | Py_ssize_t i; |
| 244 | |
| 245 | mask = 0; |
| 246 | for (i = 0; i < len; i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 247 | BLOOM_ADD(mask, PyUnicode_READ(kind, ptr, i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 248 | |
| 249 | return mask; |
| 250 | } |
| 251 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 252 | #define BLOOM_MEMBER(mask, chr, str) \ |
| 253 | (BLOOM(mask, chr) \ |
| 254 | && (PyUnicode_FindChar(str, chr, 0, PyUnicode_GET_LENGTH(str), 1) >= 0)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 255 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 256 | /* --- Unicode Object ----------------------------------------------------- */ |
| 257 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 258 | static PyObject * |
| 259 | substring(PyUnicodeObject *self, Py_ssize_t start, Py_ssize_t len); |
| 260 | |
| 261 | static PyObject * |
| 262 | fixup(PyUnicodeObject *self, Py_UCS4 (*fixfct)(PyUnicodeObject *s)); |
| 263 | |
| 264 | Py_LOCAL_INLINE(char *) findchar(void *s, int kind, |
| 265 | Py_ssize_t size, Py_UCS4 ch, |
| 266 | int direction) |
| 267 | { |
| 268 | /* like wcschr, but doesn't stop at NULL characters */ |
| 269 | Py_ssize_t i; |
| 270 | if (direction == 1) { |
| 271 | for(i = 0; i < size; i++) |
| 272 | if (PyUnicode_READ(kind, s, i) == ch) |
| 273 | return (char*)s + PyUnicode_KIND_SIZE(kind, i); |
| 274 | } |
| 275 | else { |
| 276 | for(i = size-1; i >= 0; i--) |
| 277 | if (PyUnicode_READ(kind, s, i) == ch) |
| 278 | return (char*)s + PyUnicode_KIND_SIZE(kind, i); |
| 279 | } |
| 280 | return NULL; |
| 281 | } |
| 282 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 283 | static int |
| 284 | unicode_resize(register PyUnicodeObject *unicode, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 285 | Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 286 | { |
| 287 | void *oldstr; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 288 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 289 | /* Resizing is only supported for old unicode objects. */ |
| 290 | assert(!PyUnicode_IS_COMPACT(unicode)); |
| 291 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 292 | |
| 293 | /* ... and only if they have not been readied yet, because |
| 294 | callees usually rely on the wstr representation when resizing. */ |
| 295 | assert(unicode->data.any == NULL); |
| 296 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 297 | /* Shortcut if there's nothing much to do. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 298 | if (_PyUnicode_WSTR_LENGTH(unicode) == length) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 299 | goto reset; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 300 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 301 | /* Resizing shared object (unicode_empty or single character |
| 302 | objects) in-place is not allowed. Use PyUnicode_Resize() |
| 303 | instead ! */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 304 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 305 | if (unicode == unicode_empty || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 306 | (_PyUnicode_WSTR_LENGTH(unicode) == 1 && |
| 307 | _PyUnicode_WSTR(unicode)[0] < 256U && |
| 308 | unicode_latin1[_PyUnicode_WSTR(unicode)[0]] == unicode)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 309 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 310 | "can't resize shared str objects"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 311 | return -1; |
| 312 | } |
| 313 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 314 | /* We allocate one more byte to make sure the string is Ux0000 terminated. |
| 315 | The overallocation is also used by fastsearch, which assumes that it's |
| 316 | safe to look at str[length] (without making any assumptions about what |
| 317 | it contains). */ |
| 318 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 319 | oldstr = _PyUnicode_WSTR(unicode); |
| 320 | _PyUnicode_WSTR(unicode) = PyObject_REALLOC(_PyUnicode_WSTR(unicode), |
| 321 | sizeof(Py_UNICODE) * (length + 1)); |
| 322 | if (!_PyUnicode_WSTR(unicode)) { |
| 323 | _PyUnicode_WSTR(unicode) = (Py_UNICODE *)oldstr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 324 | PyErr_NoMemory(); |
| 325 | return -1; |
| 326 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 327 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 328 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 329 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 330 | reset: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 331 | if (unicode->data.any != NULL) { |
| 332 | PyObject_FREE(unicode->data.any); |
| 333 | if (unicode->_base.utf8 && unicode->_base.utf8 != unicode->data.any) { |
| 334 | PyObject_FREE(unicode->_base.utf8); |
| 335 | } |
| 336 | unicode->_base.utf8 = NULL; |
| 337 | unicode->_base.utf8_length = 0; |
| 338 | unicode->data.any = NULL; |
| 339 | _PyUnicode_LENGTH(unicode) = 0; |
| 340 | _PyUnicode_STATE(unicode).interned = _PyUnicode_STATE(unicode).interned; |
| 341 | _PyUnicode_STATE(unicode).kind = PyUnicode_WCHAR_KIND; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 342 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 343 | _PyUnicode_HASH(unicode) = -1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 344 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | /* 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] | 349 | Ux0000 terminated; some code (e.g. new_identifier) |
| 350 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 351 | |
| 352 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 353 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 354 | |
| 355 | */ |
| 356 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 357 | #ifdef Py_DEBUG |
| 358 | int unicode_old_new_calls = 0; |
| 359 | #endif |
| 360 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 361 | static PyUnicodeObject * |
| 362 | _PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 363 | { |
| 364 | register PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 365 | size_t new_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 366 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 367 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 368 | if (length == 0 && unicode_empty != NULL) { |
| 369 | Py_INCREF(unicode_empty); |
| 370 | return unicode_empty; |
| 371 | } |
| 372 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 373 | /* Ensure we won't overflow the size. */ |
| 374 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 375 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 376 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 377 | if (length < 0) { |
| 378 | PyErr_SetString(PyExc_SystemError, |
| 379 | "Negative size passed to _PyUnicode_New"); |
| 380 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 383 | #ifdef Py_DEBUG |
| 384 | ++unicode_old_new_calls; |
| 385 | #endif |
| 386 | |
| 387 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
| 388 | if (unicode == NULL) |
| 389 | return NULL; |
| 390 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 391 | _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); |
| 392 | if (!_PyUnicode_WSTR(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 393 | PyErr_NoMemory(); |
| 394 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 395 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 396 | |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 397 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 398 | * the caller fails before initializing str -- unicode_resize() |
| 399 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 400 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 401 | * We don't want unicode_resize to read uninitialized memory in |
| 402 | * that case. |
| 403 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 404 | _PyUnicode_WSTR(unicode)[0] = 0; |
| 405 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 406 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 407 | _PyUnicode_HASH(unicode) = -1; |
| 408 | _PyUnicode_STATE(unicode).interned = 0; |
| 409 | _PyUnicode_STATE(unicode).kind = 0; |
| 410 | _PyUnicode_STATE(unicode).compact = 0; |
| 411 | _PyUnicode_STATE(unicode).ready = 0; |
| 412 | _PyUnicode_STATE(unicode).ascii = 0; |
| 413 | unicode->data.any = NULL; |
| 414 | _PyUnicode_LENGTH(unicode) = 0; |
| 415 | unicode->_base.utf8 = NULL; |
| 416 | unicode->_base.utf8_length = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 417 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 418 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 419 | onError: |
Amaury Forgeot d'Arc | 7888d08 | 2008-08-01 01:06:32 +0000 | [diff] [blame] | 420 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
| 421 | _Py_DEC_REFTOTAL; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 422 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 423 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 424 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 427 | #ifdef Py_DEBUG |
| 428 | int unicode_new_new_calls = 0; |
| 429 | |
| 430 | /* Functions wrapping macros for use in debugger */ |
| 431 | char *_PyUnicode_utf8(void *unicode){ |
| 432 | return _PyUnicode_UTF8(unicode); |
| 433 | } |
| 434 | |
| 435 | void *_PyUnicode_compact_data(void *unicode) { |
| 436 | return _PyUnicode_COMPACT_DATA(unicode); |
| 437 | } |
| 438 | void *_PyUnicode_data(void *unicode){ |
| 439 | printf("obj %p\n", unicode); |
| 440 | printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); |
| 441 | printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); |
| 442 | printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); |
| 443 | printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); |
| 444 | printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); |
| 445 | return PyUnicode_DATA(unicode); |
| 446 | } |
| 447 | #endif |
| 448 | |
| 449 | PyObject * |
| 450 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) |
| 451 | { |
| 452 | PyObject *obj; |
| 453 | PyCompactUnicodeObject *unicode; |
| 454 | void *data; |
| 455 | int kind_state; |
| 456 | int is_sharing = 0, is_ascii = 0; |
| 457 | Py_ssize_t char_size; |
| 458 | Py_ssize_t struct_size; |
| 459 | |
| 460 | /* Optimization for empty strings */ |
| 461 | if (size == 0 && unicode_empty != NULL) { |
| 462 | Py_INCREF(unicode_empty); |
| 463 | return (PyObject *)unicode_empty; |
| 464 | } |
| 465 | |
| 466 | #ifdef Py_DEBUG |
| 467 | ++unicode_new_new_calls; |
| 468 | #endif |
| 469 | |
| 470 | struct_size = sizeof(PyCompactUnicodeObject); |
| 471 | if (maxchar < 128) { |
| 472 | kind_state = PyUnicode_1BYTE_KIND; |
| 473 | char_size = 1; |
| 474 | is_ascii = 1; |
| 475 | struct_size = sizeof(PyASCIIObject); |
| 476 | } |
| 477 | else if (maxchar < 256) { |
| 478 | kind_state = PyUnicode_1BYTE_KIND; |
| 479 | char_size = 1; |
| 480 | } |
| 481 | else if (maxchar < 65536) { |
| 482 | kind_state = PyUnicode_2BYTE_KIND; |
| 483 | char_size = 2; |
| 484 | if (sizeof(wchar_t) == 2) |
| 485 | is_sharing = 1; |
| 486 | } |
| 487 | else { |
| 488 | kind_state = PyUnicode_4BYTE_KIND; |
| 489 | char_size = 4; |
| 490 | if (sizeof(wchar_t) == 4) |
| 491 | is_sharing = 1; |
| 492 | } |
| 493 | |
| 494 | /* Ensure we won't overflow the size. */ |
| 495 | if (size < 0) { |
| 496 | PyErr_SetString(PyExc_SystemError, |
| 497 | "Negative size passed to PyUnicode_New"); |
| 498 | return NULL; |
| 499 | } |
| 500 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) |
| 501 | return PyErr_NoMemory(); |
| 502 | |
| 503 | /* Duplicated allocation code from _PyObject_New() instead of a call to |
| 504 | * PyObject_New() so we are able to allocate space for the object and |
| 505 | * it's data buffer. |
| 506 | */ |
| 507 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); |
| 508 | if (obj == NULL) |
| 509 | return PyErr_NoMemory(); |
| 510 | obj = PyObject_INIT(obj, &PyUnicode_Type); |
| 511 | if (obj == NULL) |
| 512 | return NULL; |
| 513 | |
| 514 | unicode = (PyCompactUnicodeObject *)obj; |
| 515 | if (is_ascii) |
| 516 | data = ((PyASCIIObject*)obj) + 1; |
| 517 | else |
| 518 | data = unicode + 1; |
| 519 | _PyUnicode_LENGTH(unicode) = size; |
| 520 | _PyUnicode_HASH(unicode) = -1; |
| 521 | _PyUnicode_STATE(unicode).interned = 0; |
| 522 | _PyUnicode_STATE(unicode).kind = kind_state; |
| 523 | _PyUnicode_STATE(unicode).compact = 1; |
| 524 | _PyUnicode_STATE(unicode).ready = 1; |
| 525 | _PyUnicode_STATE(unicode).ascii = is_ascii; |
| 526 | if (is_ascii) { |
| 527 | ((char*)data)[size] = 0; |
| 528 | _PyUnicode_WSTR(unicode) = NULL; |
| 529 | } |
| 530 | else if (kind_state == PyUnicode_1BYTE_KIND) { |
| 531 | ((char*)data)[size] = 0; |
| 532 | _PyUnicode_WSTR(unicode) = NULL; |
| 533 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 534 | unicode->utf8_length = 0; |
| 535 | unicode->utf8 = NULL; |
| 536 | } |
| 537 | else { |
| 538 | unicode->utf8 = NULL; |
| 539 | if (kind_state == PyUnicode_2BYTE_KIND) |
| 540 | ((Py_UCS2*)data)[size] = 0; |
| 541 | else /* kind_state == PyUnicode_4BYTE_KIND */ |
| 542 | ((Py_UCS4*)data)[size] = 0; |
| 543 | if (is_sharing) { |
| 544 | _PyUnicode_WSTR_LENGTH(unicode) = size; |
| 545 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; |
| 546 | } |
| 547 | else { |
| 548 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 549 | _PyUnicode_WSTR(unicode) = NULL; |
| 550 | } |
| 551 | } |
| 552 | return obj; |
| 553 | } |
| 554 | |
| 555 | #if SIZEOF_WCHAR_T == 2 |
| 556 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this |
| 557 | will decode surrogate pairs, the other conversions are implemented as macros |
| 558 | for efficency. |
| 559 | |
| 560 | This function assumes that unicode can hold one more code point than wstr |
| 561 | characters for a terminating null character. */ |
| 562 | static int |
| 563 | unicode_convert_wchar_to_ucs4(const wchar_t *begin, const wchar_t *end, |
| 564 | PyUnicodeObject *unicode) |
| 565 | { |
| 566 | const wchar_t *iter; |
| 567 | Py_UCS4 *ucs4_out; |
| 568 | |
| 569 | assert(unicode && PyUnicode_Check(unicode)); |
| 570 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 571 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); |
| 572 | |
| 573 | for (iter = begin; iter < end; ) { |
| 574 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + |
| 575 | _PyUnicode_GET_LENGTH(unicode))); |
| 576 | if (*iter >= 0xD800 && *iter <= 0xDBFF |
| 577 | && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) |
| 578 | { |
| 579 | *ucs4_out++ = (((iter[0] & 0x3FF)<<10) | (iter[1] & 0x3FF)) + 0x10000; |
| 580 | iter += 2; |
| 581 | } |
| 582 | else { |
| 583 | *ucs4_out++ = *iter; |
| 584 | iter++; |
| 585 | } |
| 586 | } |
| 587 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 588 | _PyUnicode_GET_LENGTH(unicode))); |
| 589 | |
| 590 | return 0; |
| 591 | } |
| 592 | #endif |
| 593 | |
| 594 | int |
| 595 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 596 | PyObject *from, Py_ssize_t from_start, |
| 597 | Py_ssize_t how_many) |
| 598 | { |
| 599 | int from_kind; |
| 600 | int to_kind; |
| 601 | |
| 602 | assert(PyUnicode_Check(from)); |
| 603 | assert(PyUnicode_Check(to)); |
| 604 | |
| 605 | if (PyUnicode_READY(from)) |
| 606 | return -1; |
| 607 | if (PyUnicode_READY(to)) |
| 608 | return -1; |
| 609 | |
| 610 | from_kind = PyUnicode_KIND(from); |
| 611 | to_kind = PyUnicode_KIND(to); |
| 612 | |
| 613 | if (from_kind == to_kind) { |
| 614 | const Py_ssize_t char_size = PyUnicode_CHARACTER_SIZE(to); |
| 615 | Py_MEMCPY(PyUnicode_1BYTE_DATA(to) + (to_start * char_size), |
| 616 | PyUnicode_1BYTE_DATA(from) + (from_start * char_size), |
| 617 | how_many * char_size); |
| 618 | return 0; |
| 619 | } |
| 620 | |
| 621 | switch (from_kind) { |
| 622 | case PyUnicode_1BYTE_KIND: |
| 623 | switch (to_kind) { |
| 624 | case PyUnicode_2BYTE_KIND: |
| 625 | PyUnicode_CONVERT_BYTES( |
| 626 | unsigned char, Py_UCS2, |
| 627 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 628 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 629 | PyUnicode_2BYTE_DATA(to) + to_start |
| 630 | ); |
| 631 | break; |
| 632 | case PyUnicode_4BYTE_KIND: |
| 633 | PyUnicode_CONVERT_BYTES( |
| 634 | unsigned char, Py_UCS4, |
| 635 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 636 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 637 | PyUnicode_4BYTE_DATA(to) + to_start |
| 638 | ); |
| 639 | break; |
| 640 | default: |
| 641 | goto invalid_state; |
| 642 | } |
| 643 | break; |
| 644 | case PyUnicode_2BYTE_KIND: |
| 645 | switch (to_kind) { |
| 646 | case PyUnicode_1BYTE_KIND: |
| 647 | PyUnicode_CONVERT_BYTES( |
| 648 | Py_UCS2, unsigned char, |
| 649 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 650 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 651 | PyUnicode_1BYTE_DATA(to) + to_start |
| 652 | ); |
| 653 | break; |
| 654 | case PyUnicode_4BYTE_KIND: |
| 655 | PyUnicode_CONVERT_BYTES( |
| 656 | Py_UCS2, Py_UCS4, |
| 657 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 658 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 659 | PyUnicode_4BYTE_DATA(to) + to_start |
| 660 | ); |
| 661 | break; |
| 662 | default: |
| 663 | goto invalid_state; |
| 664 | } |
| 665 | break; |
| 666 | case PyUnicode_4BYTE_KIND: |
| 667 | switch (to_kind) { |
| 668 | case PyUnicode_1BYTE_KIND: |
| 669 | PyUnicode_CONVERT_BYTES( |
| 670 | Py_UCS4, unsigned char, |
| 671 | PyUnicode_4BYTE_DATA(from) + from_start, |
| 672 | PyUnicode_4BYTE_DATA(from) + from_start + how_many, |
| 673 | PyUnicode_1BYTE_DATA(to) + to_start |
| 674 | ); |
| 675 | break; |
| 676 | case PyUnicode_2BYTE_KIND: |
| 677 | PyUnicode_CONVERT_BYTES( |
| 678 | Py_UCS4, Py_UCS2, |
| 679 | PyUnicode_4BYTE_DATA(from) + from_start, |
| 680 | PyUnicode_4BYTE_DATA(from) + from_start + how_many, |
| 681 | PyUnicode_2BYTE_DATA(to) + to_start |
| 682 | ); |
| 683 | break; |
| 684 | default: |
| 685 | goto invalid_state; |
| 686 | } |
| 687 | break; |
| 688 | default: |
| 689 | goto invalid_state; |
| 690 | } |
| 691 | return 0; |
| 692 | |
| 693 | invalid_state: |
| 694 | PyErr_Format(PyExc_ValueError, |
| 695 | "Impossible kind state (from=%i, to=%i) " |
| 696 | "in PyUnicode_CopyCharacters", |
| 697 | from_kind, to_kind); |
| 698 | return -1; |
| 699 | } |
| 700 | |
| 701 | int |
| 702 | _PyUnicode_FindMaxCharAndNumSurrogatePairs(const wchar_t *begin, |
| 703 | const wchar_t *end, |
| 704 | Py_UCS4 *maxchar, |
| 705 | Py_ssize_t *num_surrogates) |
| 706 | { |
| 707 | const wchar_t *iter; |
| 708 | |
| 709 | if (num_surrogates == NULL || maxchar == NULL) { |
| 710 | PyErr_SetString(PyExc_SystemError, |
| 711 | "unexpected NULL arguments to " |
| 712 | "PyUnicode_FindMaxCharAndNumSurrogatePairs"); |
| 713 | return -1; |
| 714 | } |
| 715 | |
| 716 | *num_surrogates = 0; |
| 717 | *maxchar = 0; |
| 718 | |
| 719 | for (iter = begin; iter < end; ) { |
| 720 | if (*iter > *maxchar) |
| 721 | *maxchar = *iter; |
| 722 | #if SIZEOF_WCHAR_T == 2 |
| 723 | if (*iter >= 0xD800 && *iter <= 0xDBFF |
| 724 | && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) |
| 725 | { |
| 726 | Py_UCS4 surrogate_val; |
| 727 | surrogate_val = (((iter[0] & 0x3FF)<<10) |
| 728 | | (iter[1] & 0x3FF)) + 0x10000; |
| 729 | ++(*num_surrogates); |
| 730 | if (surrogate_val > *maxchar) |
| 731 | *maxchar = surrogate_val; |
| 732 | iter += 2; |
| 733 | } |
| 734 | else |
| 735 | iter++; |
| 736 | #else |
| 737 | iter++; |
| 738 | #endif |
| 739 | } |
| 740 | return 0; |
| 741 | } |
| 742 | |
| 743 | #ifdef Py_DEBUG |
| 744 | int unicode_ready_calls = 0; |
| 745 | #endif |
| 746 | |
| 747 | int |
| 748 | _PyUnicode_Ready(PyUnicodeObject *unicode) |
| 749 | { |
| 750 | wchar_t *end; |
| 751 | Py_UCS4 maxchar = 0; |
| 752 | Py_ssize_t num_surrogates; |
| 753 | #if SIZEOF_WCHAR_T == 2 |
| 754 | Py_ssize_t length_wo_surrogates; |
| 755 | #endif |
| 756 | |
| 757 | assert(PyUnicode_Check(unicode)); |
| 758 | |
| 759 | if (unicode->data.any != NULL) { |
| 760 | assert(PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND); |
| 761 | return 0; |
| 762 | } |
| 763 | |
| 764 | /* _PyUnicode_Ready() is only intented for old-style API usage where |
| 765 | * strings were created using _PyObject_New() and where no canonical |
| 766 | * representation (the str field) has been set yet aka strings |
| 767 | * which are not yet ready. |
| 768 | */ |
| 769 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 770 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
| 771 | assert(!PyUnicode_IS_COMPACT(unicode)); |
| 772 | assert(!PyUnicode_IS_READY(unicode)); |
| 773 | /* Actually, it should neither be interned nor be anything else: */ |
| 774 | assert(_PyUnicode_STATE(unicode).interned == 0); |
| 775 | assert(unicode->_base.utf8 == NULL); |
| 776 | |
| 777 | #ifdef Py_DEBUG |
| 778 | ++unicode_ready_calls; |
| 779 | #endif |
| 780 | |
| 781 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
| 782 | if (_PyUnicode_FindMaxCharAndNumSurrogatePairs(_PyUnicode_WSTR(unicode), end, |
| 783 | &maxchar, |
| 784 | &num_surrogates) == -1) { |
| 785 | assert(0 && "PyUnicode_FindMaxCharAndNumSurrogatePairs failed"); |
| 786 | return -1; |
| 787 | } |
| 788 | |
| 789 | if (maxchar < 256) { |
| 790 | unicode->data.any = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 791 | if (!unicode->data.any) { |
| 792 | PyErr_NoMemory(); |
| 793 | return -1; |
| 794 | } |
| 795 | PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
| 796 | _PyUnicode_WSTR(unicode), end, |
| 797 | PyUnicode_1BYTE_DATA(unicode)); |
| 798 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 799 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 800 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 801 | if (maxchar < 128) { |
| 802 | unicode->_base.utf8 = unicode->data.any; |
| 803 | unicode->_base.utf8_length = _PyUnicode_WSTR_LENGTH(unicode); |
| 804 | } |
| 805 | else { |
| 806 | unicode->_base.utf8 = NULL; |
| 807 | unicode->_base.utf8_length = 0; |
| 808 | } |
| 809 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 810 | _PyUnicode_WSTR(unicode) = NULL; |
| 811 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 812 | } |
| 813 | /* In this case we might have to convert down from 4-byte native |
| 814 | wchar_t to 2-byte unicode. */ |
| 815 | else if (maxchar < 65536) { |
| 816 | assert(num_surrogates == 0 && |
| 817 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 818 | |
| 819 | if (sizeof(wchar_t) == 2) { |
| 820 | /* We can share representations and are done. */ |
| 821 | unicode->data.any = _PyUnicode_WSTR(unicode); |
| 822 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 823 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 824 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
| 825 | unicode->_base.utf8 = NULL; |
| 826 | unicode->_base.utf8_length = 0; |
| 827 | } |
| 828 | else { |
| 829 | assert(sizeof(wchar_t) == 4); |
| 830 | |
| 831 | unicode->data.any = PyObject_MALLOC( |
| 832 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
| 833 | if (!unicode->data.any) { |
| 834 | PyErr_NoMemory(); |
| 835 | return -1; |
| 836 | } |
| 837 | PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 838 | _PyUnicode_WSTR(unicode), end, |
| 839 | PyUnicode_2BYTE_DATA(unicode)); |
| 840 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 841 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 842 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
| 843 | unicode->_base.utf8 = NULL; |
| 844 | unicode->_base.utf8_length = 0; |
| 845 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 846 | _PyUnicode_WSTR(unicode) = NULL; |
| 847 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 848 | } |
| 849 | } |
| 850 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 851 | else { |
| 852 | #if SIZEOF_WCHAR_T == 2 |
| 853 | /* in case the native representation is 2-bytes, we need to allocate a |
| 854 | new normalized 4-byte version. */ |
| 855 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
| 856 | unicode->data.any = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 857 | if (!unicode->data.any) { |
| 858 | PyErr_NoMemory(); |
| 859 | return -1; |
| 860 | } |
| 861 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 862 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 863 | unicode->_base.utf8 = NULL; |
| 864 | unicode->_base.utf8_length = 0; |
| 865 | if (unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, |
| 866 | unicode) < 0) { |
| 867 | assert(0 && "ConvertWideCharToUCS4 failed"); |
| 868 | return -1; |
| 869 | } |
| 870 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 871 | _PyUnicode_WSTR(unicode) = NULL; |
| 872 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 873 | #else |
| 874 | assert(num_surrogates == 0); |
| 875 | |
| 876 | unicode->data.any = _PyUnicode_WSTR(unicode); |
| 877 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 878 | unicode->_base.utf8 = NULL; |
| 879 | unicode->_base.utf8_length = 0; |
| 880 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 881 | #endif |
| 882 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 883 | } |
| 884 | _PyUnicode_STATE(unicode).ready = 1; |
| 885 | return 0; |
| 886 | } |
| 887 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 888 | static void |
| 889 | unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 890 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 891 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 892 | case SSTATE_NOT_INTERNED: |
| 893 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 894 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 895 | case SSTATE_INTERNED_MORTAL: |
| 896 | /* revive dead object temporarily for DelItem */ |
| 897 | Py_REFCNT(unicode) = 3; |
| 898 | if (PyDict_DelItem(interned, (PyObject *)unicode) != 0) |
| 899 | Py_FatalError( |
| 900 | "deletion of interned string failed"); |
| 901 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 902 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 903 | case SSTATE_INTERNED_IMMORTAL: |
| 904 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 905 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 906 | default: |
| 907 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 908 | } |
| 909 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 910 | if (_PyUnicode_WSTR(unicode) && |
| 911 | (!PyUnicode_IS_READY(unicode) || |
| 912 | _PyUnicode_WSTR(unicode) != PyUnicode_DATA(unicode))) |
| 913 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
| 914 | if (_PyUnicode_UTF8(unicode) && _PyUnicode_UTF8(unicode) != PyUnicode_DATA(unicode)) |
| 915 | PyObject_DEL(unicode->_base.utf8); |
| 916 | |
| 917 | if (PyUnicode_IS_COMPACT(unicode)) { |
| 918 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 919 | } |
| 920 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 921 | if (unicode->data.any) |
| 922 | PyObject_DEL(unicode->data.any); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 923 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 924 | } |
| 925 | } |
| 926 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 927 | static int |
| 928 | _PyUnicode_Resize(PyUnicodeObject **unicode, Py_ssize_t length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 929 | { |
| 930 | register PyUnicodeObject *v; |
| 931 | |
| 932 | /* Argument checks */ |
| 933 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 934 | PyErr_BadInternalCall(); |
| 935 | return -1; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 936 | } |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 937 | v = *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 938 | if (v == NULL || !PyUnicode_Check(v) || Py_REFCNT(v) != 1 || length < 0 || |
| 939 | PyUnicode_IS_COMPACT(v) || _PyUnicode_WSTR(v) == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 940 | PyErr_BadInternalCall(); |
| 941 | return -1; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | /* Resizing unicode_empty and single character objects is not |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 945 | possible since these are being shared. |
| 946 | The same goes for new-representation unicode objects or objects which |
| 947 | have already been readied. |
| 948 | For these, we simply return a fresh copy with the same Unicode content. |
| 949 | */ |
| 950 | if ((_PyUnicode_WSTR_LENGTH(v) != length && |
| 951 | (v == unicode_empty || _PyUnicode_WSTR_LENGTH(v) == 1)) || |
| 952 | PyUnicode_IS_COMPACT(v) || v->data.any) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 953 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 954 | if (w == NULL) |
| 955 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 956 | Py_UNICODE_COPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(v), |
| 957 | length < _PyUnicode_WSTR_LENGTH(v) ? length : _PyUnicode_WSTR_LENGTH(v)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 958 | Py_DECREF(*unicode); |
| 959 | *unicode = w; |
| 960 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 964 | objects, since we can modify them in-place. */ |
| 965 | return unicode_resize(v, length); |
| 966 | } |
| 967 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 968 | int |
| 969 | PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 970 | { |
| 971 | return _PyUnicode_Resize((PyUnicodeObject **)unicode, length); |
| 972 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 973 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 974 | static PyObject* |
| 975 | get_latin1_char(unsigned char ch) |
| 976 | { |
| 977 | PyUnicodeObject *unicode = unicode_latin1[ch]; |
| 978 | if (!unicode) { |
| 979 | unicode = (PyUnicodeObject *)PyUnicode_New(1, ch); |
| 980 | if (!unicode) |
| 981 | return NULL; |
| 982 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
| 983 | unicode_latin1[ch] = unicode; |
| 984 | } |
| 985 | Py_INCREF(unicode); |
| 986 | return (PyObject *)unicode; |
| 987 | } |
| 988 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 989 | PyObject * |
| 990 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 991 | { |
| 992 | PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 993 | Py_UCS4 maxchar = 0; |
| 994 | Py_ssize_t num_surrogates; |
| 995 | |
| 996 | if (u == NULL) |
| 997 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 998 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 999 | /* If the Unicode data is known at construction time, we can apply |
| 1000 | some optimizations which share commonly used objects. */ |
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 | /* Optimization for empty strings */ |
| 1003 | if (size == 0 && unicode_empty != NULL) { |
| 1004 | Py_INCREF(unicode_empty); |
| 1005 | return (PyObject *)unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1006 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1007 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1008 | /* Single character Unicode objects in the Latin-1 range are |
| 1009 | shared when using this constructor */ |
| 1010 | if (size == 1 && *u < 256) |
| 1011 | return get_latin1_char((unsigned char)*u); |
| 1012 | |
| 1013 | /* If not empty and not single character, copy the Unicode data |
| 1014 | into the new object */ |
| 1015 | if (_PyUnicode_FindMaxCharAndNumSurrogatePairs(u, u + size, &maxchar, |
| 1016 | &num_surrogates) == -1) |
| 1017 | return NULL; |
| 1018 | |
| 1019 | unicode = (PyUnicodeObject *) PyUnicode_New(size - num_surrogates, |
| 1020 | maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1021 | if (!unicode) |
| 1022 | return NULL; |
| 1023 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1024 | switch (PyUnicode_KIND(unicode)) { |
| 1025 | case PyUnicode_1BYTE_KIND: |
| 1026 | PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
| 1027 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1028 | break; |
| 1029 | case PyUnicode_2BYTE_KIND: |
| 1030 | #if Py_UNICODE_SIZE == 2 |
| 1031 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1032 | #else |
| 1033 | PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
| 1034 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1035 | #endif |
| 1036 | break; |
| 1037 | case PyUnicode_4BYTE_KIND: |
| 1038 | #if SIZEOF_WCHAR_T == 2 |
| 1039 | /* This is the only case which has to process surrogates, thus |
| 1040 | a simple copy loop is not enough and we need a function. */ |
| 1041 | if (unicode_convert_wchar_to_ucs4(u, u + size, unicode) < 0) { |
| 1042 | Py_DECREF(unicode); |
| 1043 | return NULL; |
| 1044 | } |
| 1045 | #else |
| 1046 | assert(num_surrogates == 0); |
| 1047 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1048 | #endif |
| 1049 | break; |
| 1050 | default: |
| 1051 | assert(0 && "Impossible state"); |
| 1052 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1053 | |
| 1054 | return (PyObject *)unicode; |
| 1055 | } |
| 1056 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1057 | PyObject * |
| 1058 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1059 | { |
| 1060 | PyUnicodeObject *unicode; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1061 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1062 | if (size < 0) { |
| 1063 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1064 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1065 | return NULL; |
| 1066 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1067 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1068 | /* 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] | 1069 | some optimizations which share commonly used objects. |
| 1070 | Also, this means the input must be UTF-8, so fall back to the |
| 1071 | UTF-8 decoder at the end. */ |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1072 | if (u != NULL) { |
| 1073 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1074 | /* Optimization for empty strings */ |
| 1075 | if (size == 0 && unicode_empty != NULL) { |
| 1076 | Py_INCREF(unicode_empty); |
| 1077 | return (PyObject *)unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1078 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1079 | |
| 1080 | /* Single characters are shared when using this constructor. |
| 1081 | Restrict to ASCII, since the input must be UTF-8. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1082 | if (size == 1 && Py_CHARMASK(*u) < 128) |
| 1083 | return get_latin1_char(Py_CHARMASK(*u)); |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 1084 | |
| 1085 | return PyUnicode_DecodeUTF8(u, size, NULL); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
Walter Dörwald | 5550731 | 2007-05-18 13:12:10 +0000 | [diff] [blame] | 1088 | unicode = _PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1089 | if (!unicode) |
| 1090 | return NULL; |
| 1091 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1092 | return (PyObject *)unicode; |
| 1093 | } |
| 1094 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1095 | PyObject * |
| 1096 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1097 | { |
| 1098 | size_t size = strlen(u); |
| 1099 | if (size > PY_SSIZE_T_MAX) { |
| 1100 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1101 | return NULL; |
| 1102 | } |
| 1103 | |
| 1104 | return PyUnicode_FromStringAndSize(u, size); |
| 1105 | } |
| 1106 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1107 | PyObject* |
| 1108 | PyUnicode_FromUCS1(const unsigned char* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1109 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1110 | PyObject *res; |
| 1111 | unsigned char max = 127; |
| 1112 | Py_ssize_t i; |
| 1113 | for (i = 0; i < size; i++) { |
| 1114 | if (u[i] & 0x80) { |
| 1115 | max = 255; |
| 1116 | break; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1117 | } |
| 1118 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1119 | res = PyUnicode_New(size, max); |
| 1120 | if (!res) |
| 1121 | return NULL; |
| 1122 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
| 1123 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1124 | } |
| 1125 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1126 | PyObject* |
| 1127 | PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
| 1128 | { |
| 1129 | PyObject *res; |
| 1130 | Py_UCS2 max = 0; |
| 1131 | Py_ssize_t i; |
| 1132 | for (i = 0; i < size; i++) |
| 1133 | if (u[i] > max) |
| 1134 | max = u[i]; |
| 1135 | res = PyUnicode_New(size, max); |
| 1136 | if (!res) |
| 1137 | return NULL; |
| 1138 | if (max >= 256) |
| 1139 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
| 1140 | else |
| 1141 | for (i = 0; i < size; i++) |
| 1142 | PyUnicode_1BYTE_DATA(res)[i] = (Py_UCS1)u[i]; |
| 1143 | return res; |
| 1144 | } |
| 1145 | |
| 1146 | PyObject* |
| 1147 | PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
| 1148 | { |
| 1149 | PyObject *res; |
| 1150 | Py_UCS4 max = 0; |
| 1151 | Py_ssize_t i; |
| 1152 | for (i = 0; i < size; i++) |
| 1153 | if (u[i] > max) |
| 1154 | max = u[i]; |
| 1155 | res = PyUnicode_New(size, max); |
| 1156 | if (!res) |
| 1157 | return NULL; |
| 1158 | if (max >= 0x10000) |
| 1159 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
| 1160 | else { |
| 1161 | int kind = PyUnicode_KIND(res); |
| 1162 | void *data = PyUnicode_DATA(res); |
| 1163 | for (i = 0; i < size; i++) |
| 1164 | PyUnicode_WRITE(kind, data, i, u[i]); |
| 1165 | } |
| 1166 | return res; |
| 1167 | } |
| 1168 | |
| 1169 | PyObject* |
| 1170 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 1171 | { |
| 1172 | switch(kind) { |
| 1173 | case PyUnicode_1BYTE_KIND: |
| 1174 | return PyUnicode_FromUCS1(buffer, size); |
| 1175 | case PyUnicode_2BYTE_KIND: |
| 1176 | return PyUnicode_FromUCS2(buffer, size); |
| 1177 | case PyUnicode_4BYTE_KIND: |
| 1178 | return PyUnicode_FromUCS4(buffer, size); |
| 1179 | } |
| 1180 | assert(0); |
| 1181 | return NULL; |
| 1182 | } |
| 1183 | |
| 1184 | |
| 1185 | /* Widen Unicode objects to larger buffers. |
| 1186 | Return NULL if the string is too wide already. */ |
| 1187 | |
| 1188 | void* |
| 1189 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 1190 | { |
| 1191 | Py_ssize_t i; |
| 1192 | Py_ssize_t len = PyUnicode_GET_LENGTH(s); |
| 1193 | void *d = PyUnicode_DATA(s); |
| 1194 | unsigned int skind = PyUnicode_KIND(s); |
| 1195 | if (PyUnicode_KIND(s) >= kind) { |
| 1196 | PyErr_SetString(PyExc_RuntimeError, "invalid widening attempt"); |
| 1197 | return NULL; |
| 1198 | } |
| 1199 | switch(kind) { |
| 1200 | case PyUnicode_2BYTE_KIND: { |
| 1201 | Py_UCS2 *result = PyMem_Malloc(PyUnicode_GET_LENGTH(s) * sizeof(Py_UCS2)); |
| 1202 | if (!result) { |
| 1203 | PyErr_NoMemory(); |
| 1204 | return 0; |
| 1205 | } |
| 1206 | for (i = 0; i < len; i++) |
| 1207 | result[i] = ((Py_UCS1*)d)[i]; |
| 1208 | return result; |
| 1209 | } |
| 1210 | case PyUnicode_4BYTE_KIND: { |
| 1211 | Py_UCS4 *result = PyMem_Malloc(PyUnicode_GET_LENGTH(s) * sizeof(Py_UCS4)); |
| 1212 | if (!result) { |
| 1213 | PyErr_NoMemory(); |
| 1214 | return 0; |
| 1215 | } |
| 1216 | for (i = 0; i < len; i++) |
| 1217 | result[i] = PyUnicode_READ(skind, d, i); |
| 1218 | return result; |
| 1219 | } |
| 1220 | } |
| 1221 | Py_FatalError("invalid kind"); |
| 1222 | return NULL; |
| 1223 | } |
| 1224 | |
| 1225 | static Py_UCS4* |
| 1226 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 1227 | int copy_null) |
| 1228 | { |
| 1229 | int kind; |
| 1230 | void *data; |
| 1231 | Py_ssize_t len, targetlen; |
| 1232 | if (PyUnicode_READY(string) == -1) |
| 1233 | return NULL; |
| 1234 | kind = PyUnicode_KIND(string); |
| 1235 | data = PyUnicode_DATA(string); |
| 1236 | len = PyUnicode_GET_LENGTH(string); |
| 1237 | targetlen = len; |
| 1238 | if (copy_null) |
| 1239 | targetlen++; |
| 1240 | if (!target) { |
| 1241 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 1242 | PyErr_NoMemory(); |
| 1243 | return NULL; |
| 1244 | } |
| 1245 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 1246 | if (!target) { |
| 1247 | PyErr_NoMemory(); |
| 1248 | return NULL; |
| 1249 | } |
| 1250 | } |
| 1251 | else { |
| 1252 | if (targetsize < targetlen) { |
| 1253 | PyErr_Format(PyExc_SystemError, |
| 1254 | "string is longer than the buffer"); |
| 1255 | if (copy_null && 0 < targetsize) |
| 1256 | target[0] = 0; |
| 1257 | return NULL; |
| 1258 | } |
| 1259 | } |
| 1260 | if (kind != PyUnicode_4BYTE_KIND) { |
| 1261 | Py_ssize_t i; |
| 1262 | for (i = 0; i < len; i++) |
| 1263 | target[i] = PyUnicode_READ(kind, data, i); |
| 1264 | } |
| 1265 | else |
| 1266 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
| 1267 | if (copy_null) |
| 1268 | target[len] = 0; |
| 1269 | return target; |
| 1270 | } |
| 1271 | |
| 1272 | Py_UCS4* |
| 1273 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 1274 | int copy_null) |
| 1275 | { |
| 1276 | if (target == NULL || targetsize < 1) { |
| 1277 | PyErr_BadInternalCall(); |
| 1278 | return NULL; |
| 1279 | } |
| 1280 | return as_ucs4(string, target, targetsize, copy_null); |
| 1281 | } |
| 1282 | |
| 1283 | Py_UCS4* |
| 1284 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 1285 | { |
| 1286 | return as_ucs4(string, NULL, 0, 1); |
| 1287 | } |
| 1288 | |
| 1289 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1290 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1291 | PyObject * |
| 1292 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1293 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1294 | if (w == NULL) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 1295 | if (size == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1296 | return PyUnicode_New(0, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1297 | PyErr_BadInternalCall(); |
| 1298 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 1301 | if (size == -1) { |
| 1302 | size = wcslen(w); |
| 1303 | } |
| 1304 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1305 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1306 | } |
| 1307 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1308 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1309 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1310 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1311 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 1312 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1313 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1314 | *fmt++ = '%'; |
| 1315 | if (width) { |
| 1316 | if (zeropad) |
| 1317 | *fmt++ = '0'; |
| 1318 | fmt += sprintf(fmt, "%d", width); |
| 1319 | } |
| 1320 | if (precision) |
| 1321 | fmt += sprintf(fmt, ".%d", precision); |
| 1322 | if (longflag) |
| 1323 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1324 | else if (longlongflag) { |
| 1325 | /* longlongflag should only ever be nonzero on machines with |
| 1326 | HAVE_LONG_LONG defined */ |
| 1327 | #ifdef HAVE_LONG_LONG |
| 1328 | char *f = PY_FORMAT_LONG_LONG; |
| 1329 | while (*f) |
| 1330 | *fmt++ = *f++; |
| 1331 | #else |
| 1332 | /* we shouldn't ever get here */ |
| 1333 | assert(0); |
| 1334 | *fmt++ = 'l'; |
| 1335 | #endif |
| 1336 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1337 | else if (size_tflag) { |
| 1338 | char *f = PY_FORMAT_SIZE_T; |
| 1339 | while (*f) |
| 1340 | *fmt++ = *f++; |
| 1341 | } |
| 1342 | *fmt++ = c; |
| 1343 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1346 | /* helper for PyUnicode_FromFormatV() */ |
| 1347 | |
| 1348 | static const char* |
| 1349 | parse_format_flags(const char *f, |
| 1350 | int *p_width, int *p_precision, |
| 1351 | int *p_longflag, int *p_longlongflag, int *p_size_tflag) |
| 1352 | { |
| 1353 | int width, precision, longflag, longlongflag, size_tflag; |
| 1354 | |
| 1355 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ |
| 1356 | f++; |
| 1357 | width = 0; |
| 1358 | while (Py_ISDIGIT((unsigned)*f)) |
| 1359 | width = (width*10) + *f++ - '0'; |
| 1360 | precision = 0; |
| 1361 | if (*f == '.') { |
| 1362 | f++; |
| 1363 | while (Py_ISDIGIT((unsigned)*f)) |
| 1364 | precision = (precision*10) + *f++ - '0'; |
| 1365 | if (*f == '%') { |
| 1366 | /* "%.3%s" => f points to "3" */ |
| 1367 | f--; |
| 1368 | } |
| 1369 | } |
| 1370 | if (*f == '\0') { |
| 1371 | /* bogus format "%.1" => go backward, f points to "1" */ |
| 1372 | f--; |
| 1373 | } |
| 1374 | if (p_width != NULL) |
| 1375 | *p_width = width; |
| 1376 | if (p_precision != NULL) |
| 1377 | *p_precision = precision; |
| 1378 | |
| 1379 | /* Handle %ld, %lu, %lld and %llu. */ |
| 1380 | longflag = 0; |
| 1381 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 1382 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1383 | |
| 1384 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 1385 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1386 | longflag = 1; |
| 1387 | ++f; |
| 1388 | } |
| 1389 | #ifdef HAVE_LONG_LONG |
| 1390 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 1391 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1392 | longlongflag = 1; |
| 1393 | f += 2; |
| 1394 | } |
| 1395 | #endif |
| 1396 | } |
| 1397 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 1398 | 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] | 1399 | size_tflag = 1; |
| 1400 | ++f; |
| 1401 | } |
| 1402 | if (p_longflag != NULL) |
| 1403 | *p_longflag = longflag; |
| 1404 | if (p_longlongflag != NULL) |
| 1405 | *p_longlongflag = longlongflag; |
| 1406 | if (p_size_tflag != NULL) |
| 1407 | *p_size_tflag = size_tflag; |
| 1408 | return f; |
| 1409 | } |
| 1410 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1411 | /* maximum number of characters required for output of %ld. 21 characters |
| 1412 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 1413 | #define MAX_LONG_CHARS 21 |
| 1414 | /* maximum number of characters required for output of %lld. |
| 1415 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 1416 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 1417 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 1418 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1419 | PyObject * |
| 1420 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 1421 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1422 | va_list count; |
| 1423 | Py_ssize_t callcount = 0; |
| 1424 | PyObject **callresults = NULL; |
| 1425 | PyObject **callresult = NULL; |
| 1426 | Py_ssize_t n = 0; |
| 1427 | int width = 0; |
| 1428 | int precision = 0; |
| 1429 | int zeropad; |
| 1430 | const char* f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1431 | PyUnicodeObject *string; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1432 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1433 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1434 | Py_UCS4 maxchar = 127; /* result is ASCII by default */ |
| 1435 | Py_UCS4 argmaxchar; |
| 1436 | Py_ssize_t numbersize = 0; |
| 1437 | char *numberresults = NULL; |
| 1438 | char *numberresult = NULL; |
| 1439 | Py_ssize_t i; |
| 1440 | int kind; |
| 1441 | void *data; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1442 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 1443 | Py_VA_COPY(count, vargs); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1444 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 1445 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 1446 | * 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] | 1447 | * result in an array) |
| 1448 | * also esimate a upper bound for all the number formats in the string, |
| 1449 | * numbers will be formated in step 3 and be keept in a '\0'-separated |
| 1450 | * buffer before putting everything together. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1451 | for (f = format; *f; f++) { |
| 1452 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1453 | int longlongflag; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1454 | /* skip width or width.precision (eg. "1.2" of "%1.2f") */ |
| 1455 | f = parse_format_flags(f, &width, NULL, NULL, &longlongflag, NULL); |
| 1456 | if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V') |
| 1457 | ++callcount; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1458 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1459 | else if (*f == 'd' || *f=='u' || *f=='i' || *f=='x' || *f=='p') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1460 | #ifdef HAVE_LONG_LONG |
| 1461 | if (longlongflag) { |
| 1462 | if (width < MAX_LONG_LONG_CHARS) |
| 1463 | width = MAX_LONG_LONG_CHARS; |
| 1464 | } |
| 1465 | else |
| 1466 | #endif |
| 1467 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 1468 | including sign. Decimal takes the most space. This |
| 1469 | isn't enough for octal. If a width is specified we |
| 1470 | need more (which we allocate later). */ |
| 1471 | if (width < MAX_LONG_CHARS) |
| 1472 | width = MAX_LONG_CHARS; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1473 | |
| 1474 | /* account for the size + '\0' to separate numbers |
| 1475 | inside of the numberresults buffer */ |
| 1476 | numbersize += (width + 1); |
| 1477 | } |
| 1478 | } |
| 1479 | else if ((unsigned char)*f > 127) { |
| 1480 | PyErr_Format(PyExc_ValueError, |
| 1481 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 1482 | "string, got a non-ASCII byte: 0x%02x", |
| 1483 | (unsigned char)*f); |
| 1484 | return NULL; |
| 1485 | } |
| 1486 | } |
| 1487 | /* step 2: allocate memory for the results of |
| 1488 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
| 1489 | if (callcount) { |
| 1490 | callresults = PyObject_Malloc(sizeof(PyObject *) * callcount); |
| 1491 | if (!callresults) { |
| 1492 | PyErr_NoMemory(); |
| 1493 | return NULL; |
| 1494 | } |
| 1495 | callresult = callresults; |
| 1496 | } |
| 1497 | /* step 2.5: allocate memory for the results of formating numbers */ |
| 1498 | if (numbersize) { |
| 1499 | numberresults = PyObject_Malloc(numbersize); |
| 1500 | if (!numberresults) { |
| 1501 | PyErr_NoMemory(); |
| 1502 | goto fail; |
| 1503 | } |
| 1504 | numberresult = numberresults; |
| 1505 | } |
| 1506 | |
| 1507 | /* step 3: format numbers and figure out how large a buffer we need */ |
| 1508 | for (f = format; *f; f++) { |
| 1509 | if (*f == '%') { |
| 1510 | const char* p; |
| 1511 | int longflag; |
| 1512 | int longlongflag; |
| 1513 | int size_tflag; |
| 1514 | int numprinted; |
| 1515 | |
| 1516 | p = f; |
| 1517 | zeropad = (f[1] == '0'); |
| 1518 | f = parse_format_flags(f, &width, &precision, |
| 1519 | &longflag, &longlongflag, &size_tflag); |
| 1520 | switch (*f) { |
| 1521 | case 'c': |
| 1522 | { |
| 1523 | Py_UCS4 ordinal = va_arg(count, int); |
| 1524 | maxchar = PY_MAX(maxchar, ordinal); |
| 1525 | n++; |
| 1526 | break; |
| 1527 | } |
| 1528 | case '%': |
| 1529 | n++; |
| 1530 | break; |
| 1531 | case 'i': |
| 1532 | case 'd': |
| 1533 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 1534 | width, precision, *f); |
| 1535 | if (longflag) |
| 1536 | numprinted = sprintf(numberresult, fmt, |
| 1537 | va_arg(count, long)); |
| 1538 | #ifdef HAVE_LONG_LONG |
| 1539 | else if (longlongflag) |
| 1540 | numprinted = sprintf(numberresult, fmt, |
| 1541 | va_arg(count, PY_LONG_LONG)); |
| 1542 | #endif |
| 1543 | else if (size_tflag) |
| 1544 | numprinted = sprintf(numberresult, fmt, |
| 1545 | va_arg(count, Py_ssize_t)); |
| 1546 | else |
| 1547 | numprinted = sprintf(numberresult, fmt, |
| 1548 | va_arg(count, int)); |
| 1549 | n += numprinted; |
| 1550 | /* advance by +1 to skip over the '\0' */ |
| 1551 | numberresult += (numprinted + 1); |
| 1552 | assert(*(numberresult - 1) == '\0'); |
| 1553 | assert(*(numberresult - 2) != '\0'); |
| 1554 | assert(numprinted >= 0); |
| 1555 | assert(numberresult <= numberresults + numbersize); |
| 1556 | break; |
| 1557 | case 'u': |
| 1558 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 1559 | width, precision, 'u'); |
| 1560 | if (longflag) |
| 1561 | numprinted = sprintf(numberresult, fmt, |
| 1562 | va_arg(count, unsigned long)); |
| 1563 | #ifdef HAVE_LONG_LONG |
| 1564 | else if (longlongflag) |
| 1565 | numprinted = sprintf(numberresult, fmt, |
| 1566 | va_arg(count, unsigned PY_LONG_LONG)); |
| 1567 | #endif |
| 1568 | else if (size_tflag) |
| 1569 | numprinted = sprintf(numberresult, fmt, |
| 1570 | va_arg(count, size_t)); |
| 1571 | else |
| 1572 | numprinted = sprintf(numberresult, fmt, |
| 1573 | va_arg(count, unsigned int)); |
| 1574 | n += numprinted; |
| 1575 | numberresult += (numprinted + 1); |
| 1576 | assert(*(numberresult - 1) == '\0'); |
| 1577 | assert(*(numberresult - 2) != '\0'); |
| 1578 | assert(numprinted >= 0); |
| 1579 | assert(numberresult <= numberresults + numbersize); |
| 1580 | break; |
| 1581 | case 'x': |
| 1582 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
| 1583 | numprinted = sprintf(numberresult, fmt, va_arg(count, int)); |
| 1584 | n += numprinted; |
| 1585 | numberresult += (numprinted + 1); |
| 1586 | assert(*(numberresult - 1) == '\0'); |
| 1587 | assert(*(numberresult - 2) != '\0'); |
| 1588 | assert(numprinted >= 0); |
| 1589 | assert(numberresult <= numberresults + numbersize); |
| 1590 | break; |
| 1591 | case 'p': |
| 1592 | numprinted = sprintf(numberresult, "%p", va_arg(count, void*)); |
| 1593 | /* %p is ill-defined: ensure leading 0x. */ |
| 1594 | if (numberresult[1] == 'X') |
| 1595 | numberresult[1] = 'x'; |
| 1596 | else if (numberresult[1] != 'x') { |
| 1597 | memmove(numberresult + 2, numberresult, |
| 1598 | strlen(numberresult) + 1); |
| 1599 | numberresult[0] = '0'; |
| 1600 | numberresult[1] = 'x'; |
| 1601 | numprinted += 2; |
| 1602 | } |
| 1603 | n += numprinted; |
| 1604 | numberresult += (numprinted + 1); |
| 1605 | assert(*(numberresult - 1) == '\0'); |
| 1606 | assert(*(numberresult - 2) != '\0'); |
| 1607 | assert(numprinted >= 0); |
| 1608 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1609 | break; |
| 1610 | case 's': |
| 1611 | { |
| 1612 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 1613 | const char *s = va_arg(count, const char*); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1614 | PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); |
| 1615 | if (!str) |
| 1616 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1617 | /* since PyUnicode_DecodeUTF8 returns already flexible |
| 1618 | unicode objects, there is no need to call ready on them */ |
| 1619 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
| 1620 | maxchar = PY_MAX(maxchar, argmaxchar); |
| 1621 | n += PyUnicode_GET_LENGTH(str); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1622 | /* Remember the str and switch to the next slot */ |
| 1623 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1624 | break; |
| 1625 | } |
| 1626 | case 'U': |
| 1627 | { |
| 1628 | PyObject *obj = va_arg(count, PyObject *); |
| 1629 | assert(obj && PyUnicode_Check(obj)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1630 | if (PyUnicode_READY(obj) == -1) |
| 1631 | goto fail; |
| 1632 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
| 1633 | maxchar = PY_MAX(maxchar, argmaxchar); |
| 1634 | n += PyUnicode_GET_LENGTH(obj); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1635 | break; |
| 1636 | } |
| 1637 | case 'V': |
| 1638 | { |
| 1639 | PyObject *obj = va_arg(count, PyObject *); |
| 1640 | const char *str = va_arg(count, const char *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1641 | PyObject *str_obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1642 | assert(obj || str); |
| 1643 | assert(!obj || PyUnicode_Check(obj)); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1644 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1645 | if (PyUnicode_READY(obj) == -1) |
| 1646 | goto fail; |
| 1647 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
| 1648 | maxchar = PY_MAX(maxchar, argmaxchar); |
| 1649 | n += PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1650 | *callresult++ = NULL; |
| 1651 | } |
| 1652 | else { |
| 1653 | str_obj = PyUnicode_DecodeUTF8(str, strlen(str), "replace"); |
| 1654 | if (!str_obj) |
| 1655 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1656 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj); |
| 1657 | maxchar = PY_MAX(maxchar, argmaxchar); |
| 1658 | n += PyUnicode_GET_LENGTH(str_obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1659 | *callresult++ = str_obj; |
| 1660 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1661 | break; |
| 1662 | } |
| 1663 | case 'S': |
| 1664 | { |
| 1665 | PyObject *obj = va_arg(count, PyObject *); |
| 1666 | PyObject *str; |
| 1667 | assert(obj); |
| 1668 | str = PyObject_Str(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1669 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1670 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1671 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
| 1672 | maxchar = PY_MAX(maxchar, argmaxchar); |
| 1673 | n += PyUnicode_GET_LENGTH(str); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1674 | /* Remember the str and switch to the next slot */ |
| 1675 | *callresult++ = str; |
| 1676 | break; |
| 1677 | } |
| 1678 | case 'R': |
| 1679 | { |
| 1680 | PyObject *obj = va_arg(count, PyObject *); |
| 1681 | PyObject *repr; |
| 1682 | assert(obj); |
| 1683 | repr = PyObject_Repr(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1684 | if (!repr || PyUnicode_READY(repr) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1685 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1686 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(repr); |
| 1687 | maxchar = PY_MAX(maxchar, argmaxchar); |
| 1688 | n += PyUnicode_GET_LENGTH(repr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1689 | /* Remember the repr and switch to the next slot */ |
| 1690 | *callresult++ = repr; |
| 1691 | break; |
| 1692 | } |
| 1693 | case 'A': |
| 1694 | { |
| 1695 | PyObject *obj = va_arg(count, PyObject *); |
| 1696 | PyObject *ascii; |
| 1697 | assert(obj); |
| 1698 | ascii = PyObject_ASCII(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1699 | if (!ascii || PyUnicode_READY(ascii) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1700 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1701 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(ascii); |
| 1702 | maxchar = PY_MAX(maxchar, argmaxchar); |
| 1703 | n += PyUnicode_GET_LENGTH(ascii); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1704 | /* Remember the repr and switch to the next slot */ |
| 1705 | *callresult++ = ascii; |
| 1706 | break; |
| 1707 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1708 | default: |
| 1709 | /* if we stumble upon an unknown |
| 1710 | formatting code, copy the rest of |
| 1711 | the format string to the output |
| 1712 | string. (we cannot just skip the |
| 1713 | code, since there's no way to know |
| 1714 | what's in the argument list) */ |
| 1715 | n += strlen(p); |
| 1716 | goto expand; |
| 1717 | } |
| 1718 | } else |
| 1719 | n++; |
| 1720 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1721 | expand: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1722 | /* step 4: fill the buffer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1723 | /* Since we've analyzed how much space we need, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1724 | we don't have to resize the string. |
| 1725 | There can be no errors beyond this point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1726 | string = (PyUnicodeObject *)PyUnicode_New(n, maxchar); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1727 | if (!string) |
| 1728 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1729 | kind = PyUnicode_KIND(string); |
| 1730 | data = PyUnicode_DATA(string); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1731 | callresult = callresults; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1732 | numberresult = numberresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1733 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1734 | for (i = 0, f = format; *f; f++) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1735 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1736 | const char* p; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1737 | |
| 1738 | p = f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1739 | f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL); |
| 1740 | /* checking for == because the last argument could be a empty |
| 1741 | string, which causes i to point to end, the assert at the end of |
| 1742 | the loop */ |
| 1743 | assert(i <= PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1744 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1745 | switch (*f) { |
| 1746 | case 'c': |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 1747 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1748 | const int ordinal = va_arg(vargs, int); |
| 1749 | PyUnicode_WRITE(kind, data, i++, ordinal); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1750 | break; |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 1751 | } |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 1752 | case 'i': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1753 | case 'd': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1754 | case 'u': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1755 | case 'x': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1756 | case 'p': |
| 1757 | /* unused, since we already have the result */ |
| 1758 | if (*f == 'p') |
| 1759 | (void) va_arg(vargs, void *); |
| 1760 | else |
| 1761 | (void) va_arg(vargs, int); |
| 1762 | /* extract the result from numberresults and append. */ |
| 1763 | for (; *numberresult; ++i, ++numberresult) |
| 1764 | PyUnicode_WRITE(kind, data, i, *numberresult); |
| 1765 | /* skip over the separating '\0' */ |
| 1766 | assert(*numberresult == '\0'); |
| 1767 | numberresult++; |
| 1768 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1769 | break; |
| 1770 | case 's': |
| 1771 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1772 | /* unused, since we already have the result */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1773 | Py_ssize_t size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1774 | (void) va_arg(vargs, char *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1775 | size = PyUnicode_GET_LENGTH(*callresult); |
| 1776 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
| 1777 | PyUnicode_CopyCharacters((PyObject*)string, i, |
| 1778 | *callresult, 0, |
| 1779 | size); |
| 1780 | i += size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1781 | /* We're done with the unicode()/repr() => forget it */ |
| 1782 | Py_DECREF(*callresult); |
| 1783 | /* switch to next unicode()/repr() result */ |
| 1784 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1785 | break; |
| 1786 | } |
| 1787 | case 'U': |
| 1788 | { |
| 1789 | PyObject *obj = va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1790 | Py_ssize_t size; |
| 1791 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
| 1792 | size = PyUnicode_GET_LENGTH(obj); |
| 1793 | PyUnicode_CopyCharacters((PyObject*)string, i, |
| 1794 | obj, 0, |
| 1795 | size); |
| 1796 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1797 | break; |
| 1798 | } |
| 1799 | case 'V': |
| 1800 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1801 | Py_ssize_t size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1802 | PyObject *obj = va_arg(vargs, PyObject *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1803 | va_arg(vargs, const char *); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1804 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1805 | size = PyUnicode_GET_LENGTH(obj); |
| 1806 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
| 1807 | PyUnicode_CopyCharacters((PyObject*)string, i, |
| 1808 | obj, 0, |
| 1809 | size); |
| 1810 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1811 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1812 | size = PyUnicode_GET_LENGTH(*callresult); |
| 1813 | assert(PyUnicode_KIND(*callresult) <= |
| 1814 | PyUnicode_KIND(string)); |
| 1815 | PyUnicode_CopyCharacters((PyObject*)string, i, |
| 1816 | *callresult, |
| 1817 | 0, size); |
| 1818 | i += size; |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1819 | Py_DECREF(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1820 | } |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1821 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1822 | break; |
| 1823 | } |
| 1824 | case 'S': |
| 1825 | case 'R': |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 1826 | case 'A': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1827 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1828 | /* unused, since we already have the result */ |
| 1829 | (void) va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1830 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
| 1831 | PyUnicode_CopyCharacters((PyObject*)string, i, |
| 1832 | *callresult, 0, |
| 1833 | PyUnicode_GET_LENGTH(*callresult)); |
| 1834 | i += PyUnicode_GET_LENGTH(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1835 | /* We're done with the unicode()/repr() => forget it */ |
| 1836 | Py_DECREF(*callresult); |
| 1837 | /* switch to next unicode()/repr() result */ |
| 1838 | ++callresult; |
| 1839 | break; |
| 1840 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1841 | case '%': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1842 | PyUnicode_WRITE(kind, data, i++, '%'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1843 | break; |
| 1844 | default: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1845 | for (; *p; ++p, ++i) |
| 1846 | PyUnicode_WRITE(kind, data, i, *p); |
| 1847 | assert(i == PyUnicode_GET_LENGTH(string)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1848 | goto end; |
| 1849 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 1850 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1851 | else { |
| 1852 | assert(i < PyUnicode_GET_LENGTH(string)); |
| 1853 | PyUnicode_WRITE(kind, data, i++, *f); |
| 1854 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1855 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1856 | assert(i == PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1857 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1858 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1859 | if (callresults) |
| 1860 | PyObject_Free(callresults); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1861 | if (numberresults) |
| 1862 | PyObject_Free(numberresults); |
| 1863 | return (PyObject *)string; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1864 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1865 | if (callresults) { |
| 1866 | PyObject **callresult2 = callresults; |
| 1867 | while (callresult2 < callresult) { |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1868 | Py_XDECREF(*callresult2); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1869 | ++callresult2; |
| 1870 | } |
| 1871 | PyObject_Free(callresults); |
| 1872 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1873 | if (numberresults) |
| 1874 | PyObject_Free(numberresults); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1875 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1876 | } |
| 1877 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1878 | PyObject * |
| 1879 | PyUnicode_FromFormat(const char *format, ...) |
| 1880 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1881 | PyObject* ret; |
| 1882 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1883 | |
| 1884 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1885 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1886 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1887 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1888 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1889 | ret = PyUnicode_FromFormatV(format, vargs); |
| 1890 | va_end(vargs); |
| 1891 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1892 | } |
| 1893 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1894 | #ifdef HAVE_WCHAR_H |
| 1895 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1896 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 1897 | convert a Unicode object to a wide character string. |
| 1898 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 1899 | - 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] | 1900 | character) required to convert the unicode object. Ignore size argument. |
| 1901 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 1902 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1903 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 1904 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1905 | static Py_ssize_t |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1906 | unicode_aswidechar(PyUnicodeObject *unicode, |
| 1907 | wchar_t *w, |
| 1908 | Py_ssize_t size) |
| 1909 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1910 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1911 | const wchar_t *wstr; |
| 1912 | |
| 1913 | wstr = PyUnicode_AsUnicodeAndSize((PyObject *)unicode, &res); |
| 1914 | if (wstr == NULL) |
| 1915 | return -1; |
| 1916 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1917 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1918 | if (size > res) |
| 1919 | size = res + 1; |
| 1920 | else |
| 1921 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1922 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1923 | return res; |
| 1924 | } |
| 1925 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1926 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1927 | } |
| 1928 | |
| 1929 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 1930 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1931 | wchar_t *w, |
| 1932 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1933 | { |
| 1934 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1935 | PyErr_BadInternalCall(); |
| 1936 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1937 | } |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 1938 | return unicode_aswidechar((PyUnicodeObject*)unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1939 | } |
| 1940 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1941 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 1942 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1943 | Py_ssize_t *size) |
| 1944 | { |
| 1945 | wchar_t* buffer; |
| 1946 | Py_ssize_t buflen; |
| 1947 | |
| 1948 | if (unicode == NULL) { |
| 1949 | PyErr_BadInternalCall(); |
| 1950 | return NULL; |
| 1951 | } |
| 1952 | |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 1953 | buflen = unicode_aswidechar((PyUnicodeObject *)unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1954 | if (buflen == -1) |
| 1955 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1956 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1957 | PyErr_NoMemory(); |
| 1958 | return NULL; |
| 1959 | } |
| 1960 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1961 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 1962 | if (buffer == NULL) { |
| 1963 | PyErr_NoMemory(); |
| 1964 | return NULL; |
| 1965 | } |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 1966 | buflen = unicode_aswidechar((PyUnicodeObject *)unicode, buffer, buflen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1967 | if (buflen == -1) |
| 1968 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1969 | if (size != NULL) |
| 1970 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1971 | return buffer; |
| 1972 | } |
| 1973 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1974 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1975 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1976 | PyObject * |
| 1977 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1978 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1979 | PyObject *v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1980 | if (ordinal < 0 || ordinal > 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1981 | PyErr_SetString(PyExc_ValueError, |
| 1982 | "chr() arg not in range(0x110000)"); |
| 1983 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1984 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1985 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1986 | if (ordinal < 256) |
| 1987 | return get_latin1_char(ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1988 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1989 | v = PyUnicode_New(1, ordinal); |
| 1990 | if (v == NULL) |
| 1991 | return NULL; |
| 1992 | PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal); |
| 1993 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1994 | } |
| 1995 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1996 | PyObject * |
| 1997 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1998 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1999 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2000 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2001 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2002 | Py_INCREF(obj); |
| 2003 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2004 | } |
| 2005 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2006 | /* For a Unicode subtype that's not a Unicode object, |
| 2007 | return a true Unicode object with the same data. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2008 | if (PyUnicode_READY(obj) == -1) |
| 2009 | return NULL; |
| 2010 | return substring((PyUnicodeObject *)obj, 0, PyUnicode_GET_LENGTH(obj)); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2011 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2012 | PyErr_Format(PyExc_TypeError, |
| 2013 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2014 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2015 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2016 | } |
| 2017 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2018 | PyObject * |
| 2019 | PyUnicode_FromEncodedObject(register PyObject *obj, |
| 2020 | const char *encoding, |
| 2021 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2022 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2023 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2024 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2025 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2026 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2027 | PyErr_BadInternalCall(); |
| 2028 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2029 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2030 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2031 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2032 | if (PyBytes_Check(obj)) { |
| 2033 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2034 | Py_INCREF(unicode_empty); |
| 2035 | v = (PyObject *) unicode_empty; |
| 2036 | } |
| 2037 | else { |
| 2038 | v = PyUnicode_Decode( |
| 2039 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 2040 | encoding, errors); |
| 2041 | } |
| 2042 | return v; |
| 2043 | } |
| 2044 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2045 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2046 | PyErr_SetString(PyExc_TypeError, |
| 2047 | "decoding str is not supported"); |
| 2048 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2049 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2050 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2051 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 2052 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 2053 | PyErr_Format(PyExc_TypeError, |
| 2054 | "coercing to str: need bytes, bytearray " |
| 2055 | "or buffer-like object, %.80s found", |
| 2056 | Py_TYPE(obj)->tp_name); |
| 2057 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2058 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2059 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2060 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2061 | Py_INCREF(unicode_empty); |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2062 | v = (PyObject *) unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2063 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2064 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2065 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2066 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2067 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2068 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2069 | } |
| 2070 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2071 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2072 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 2073 | 1 on success. */ |
| 2074 | static int |
| 2075 | normalize_encoding(const char *encoding, |
| 2076 | char *lower, |
| 2077 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2078 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2079 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2080 | char *l; |
| 2081 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2082 | |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2083 | e = encoding; |
| 2084 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2085 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2086 | while (*e) { |
| 2087 | if (l == l_end) |
| 2088 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2089 | if (Py_ISUPPER(*e)) { |
| 2090 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2091 | } |
| 2092 | else if (*e == '_') { |
| 2093 | *l++ = '-'; |
| 2094 | e++; |
| 2095 | } |
| 2096 | else { |
| 2097 | *l++ = *e++; |
| 2098 | } |
| 2099 | } |
| 2100 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2101 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2102 | } |
| 2103 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2104 | PyObject * |
| 2105 | PyUnicode_Decode(const char *s, |
| 2106 | Py_ssize_t size, |
| 2107 | const char *encoding, |
| 2108 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2109 | { |
| 2110 | PyObject *buffer = NULL, *unicode; |
| 2111 | Py_buffer info; |
| 2112 | char lower[11]; /* Enough for any encoding shortcut */ |
| 2113 | |
| 2114 | if (encoding == NULL) |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2115 | return PyUnicode_DecodeUTF8(s, size, errors); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2116 | |
| 2117 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2118 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2119 | if ((strcmp(lower, "utf-8") == 0) || |
| 2120 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2121 | return PyUnicode_DecodeUTF8(s, size, errors); |
| 2122 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2123 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2124 | (strcmp(lower, "iso-8859-1") == 0)) |
| 2125 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2126 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2127 | else if (strcmp(lower, "mbcs") == 0) |
| 2128 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2129 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2130 | else if (strcmp(lower, "ascii") == 0) |
| 2131 | return PyUnicode_DecodeASCII(s, size, errors); |
| 2132 | else if (strcmp(lower, "utf-16") == 0) |
| 2133 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 2134 | else if (strcmp(lower, "utf-32") == 0) |
| 2135 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 2136 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2137 | |
| 2138 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 2139 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 2140 | 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] | 2141 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 2142 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2143 | if (buffer == NULL) |
| 2144 | goto onError; |
| 2145 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 2146 | if (unicode == NULL) |
| 2147 | goto onError; |
| 2148 | if (!PyUnicode_Check(unicode)) { |
| 2149 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2150 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2151 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2152 | Py_DECREF(unicode); |
| 2153 | goto onError; |
| 2154 | } |
| 2155 | Py_DECREF(buffer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2156 | if (PyUnicode_READY(unicode)) { |
| 2157 | Py_DECREF(unicode); |
| 2158 | return NULL; |
| 2159 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2160 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2161 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2162 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2163 | Py_XDECREF(buffer); |
| 2164 | return NULL; |
| 2165 | } |
| 2166 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2167 | PyObject * |
| 2168 | PyUnicode_AsDecodedObject(PyObject *unicode, |
| 2169 | const char *encoding, |
| 2170 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2171 | { |
| 2172 | PyObject *v; |
| 2173 | |
| 2174 | if (!PyUnicode_Check(unicode)) { |
| 2175 | PyErr_BadArgument(); |
| 2176 | goto onError; |
| 2177 | } |
| 2178 | |
| 2179 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2180 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2181 | |
| 2182 | /* Decode via the codec registry */ |
| 2183 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2184 | if (v == NULL) |
| 2185 | goto onError; |
| 2186 | return v; |
| 2187 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2188 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2189 | return NULL; |
| 2190 | } |
| 2191 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2192 | PyObject * |
| 2193 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
| 2194 | const char *encoding, |
| 2195 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2196 | { |
| 2197 | PyObject *v; |
| 2198 | |
| 2199 | if (!PyUnicode_Check(unicode)) { |
| 2200 | PyErr_BadArgument(); |
| 2201 | goto onError; |
| 2202 | } |
| 2203 | |
| 2204 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2205 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2206 | |
| 2207 | /* Decode via the codec registry */ |
| 2208 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2209 | if (v == NULL) |
| 2210 | goto onError; |
| 2211 | if (!PyUnicode_Check(v)) { |
| 2212 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2213 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2214 | Py_TYPE(v)->tp_name); |
| 2215 | Py_DECREF(v); |
| 2216 | goto onError; |
| 2217 | } |
| 2218 | return v; |
| 2219 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2220 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2221 | return NULL; |
| 2222 | } |
| 2223 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2224 | PyObject * |
| 2225 | PyUnicode_Encode(const Py_UNICODE *s, |
| 2226 | Py_ssize_t size, |
| 2227 | const char *encoding, |
| 2228 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2229 | { |
| 2230 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2231 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2232 | unicode = PyUnicode_FromUnicode(s, size); |
| 2233 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2234 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2235 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 2236 | Py_DECREF(unicode); |
| 2237 | return v; |
| 2238 | } |
| 2239 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2240 | PyObject * |
| 2241 | PyUnicode_AsEncodedObject(PyObject *unicode, |
| 2242 | const char *encoding, |
| 2243 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2244 | { |
| 2245 | PyObject *v; |
| 2246 | |
| 2247 | if (!PyUnicode_Check(unicode)) { |
| 2248 | PyErr_BadArgument(); |
| 2249 | goto onError; |
| 2250 | } |
| 2251 | |
| 2252 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2253 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2254 | |
| 2255 | /* Encode via the codec registry */ |
| 2256 | v = PyCodec_Encode(unicode, encoding, errors); |
| 2257 | if (v == NULL) |
| 2258 | goto onError; |
| 2259 | return v; |
| 2260 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2261 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2262 | return NULL; |
| 2263 | } |
| 2264 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2265 | PyObject * |
| 2266 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2267 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2268 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2269 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 2270 | PyUnicode_GET_SIZE(unicode), |
| 2271 | NULL); |
| 2272 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2273 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2274 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 2275 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 2276 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 2277 | cannot use it to encode and decode filenames before it is loaded. Load |
| 2278 | the Python codec requires to encode at least its own filename. Use the C |
| 2279 | version of the locale codec until the codec registry is initialized and |
| 2280 | the Python codec is loaded. |
| 2281 | |
| 2282 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 2283 | cannot only rely on it: check also interp->fscodec_initialized for |
| 2284 | subinterpreters. */ |
| 2285 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2286 | return PyUnicode_AsEncodedString(unicode, |
| 2287 | Py_FileSystemDefaultEncoding, |
| 2288 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 2289 | } |
| 2290 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2291 | /* locale encoding with surrogateescape */ |
| 2292 | wchar_t *wchar; |
| 2293 | char *bytes; |
| 2294 | PyObject *bytes_obj; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 2295 | size_t error_pos; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2296 | |
| 2297 | wchar = PyUnicode_AsWideCharString(unicode, NULL); |
| 2298 | if (wchar == NULL) |
| 2299 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 2300 | bytes = _Py_wchar2char(wchar, &error_pos); |
| 2301 | if (bytes == NULL) { |
| 2302 | if (error_pos != (size_t)-1) { |
| 2303 | char *errmsg = strerror(errno); |
| 2304 | PyObject *exc = NULL; |
| 2305 | if (errmsg == NULL) |
| 2306 | errmsg = "Py_wchar2char() failed"; |
| 2307 | raise_encode_exception(&exc, |
| 2308 | "filesystemencoding", |
| 2309 | PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), |
| 2310 | error_pos, error_pos+1, |
| 2311 | errmsg); |
| 2312 | Py_XDECREF(exc); |
| 2313 | } |
| 2314 | else |
| 2315 | PyErr_NoMemory(); |
| 2316 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2317 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 2318 | } |
| 2319 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2320 | |
| 2321 | bytes_obj = PyBytes_FromString(bytes); |
| 2322 | PyMem_Free(bytes); |
| 2323 | return bytes_obj; |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 2324 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2325 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2326 | } |
| 2327 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2328 | PyObject * |
| 2329 | PyUnicode_AsEncodedString(PyObject *unicode, |
| 2330 | const char *encoding, |
| 2331 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2332 | { |
| 2333 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2334 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2335 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2336 | if (!PyUnicode_Check(unicode)) { |
| 2337 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2338 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2339 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2340 | |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2341 | if (encoding == NULL) { |
| 2342 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2343 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2344 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2345 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2346 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2347 | |
| 2348 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2349 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2350 | if ((strcmp(lower, "utf-8") == 0) || |
| 2351 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 2352 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2353 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2354 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2355 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2356 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 2357 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2358 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2359 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2360 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2361 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2362 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2363 | else if (strcmp(lower, "mbcs") == 0) |
| 2364 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 2365 | PyUnicode_GET_SIZE(unicode), |
| 2366 | errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2367 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2368 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2369 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2370 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2371 | |
| 2372 | /* Encode via the codec registry */ |
| 2373 | v = PyCodec_Encode(unicode, encoding, errors); |
| 2374 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2375 | return NULL; |
| 2376 | |
| 2377 | /* The normal path */ |
| 2378 | if (PyBytes_Check(v)) |
| 2379 | return v; |
| 2380 | |
| 2381 | /* 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] | 2382 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2383 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2384 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2385 | |
| 2386 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 2387 | "encoder %s returned bytearray instead of bytes", |
| 2388 | encoding); |
| 2389 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2390 | Py_DECREF(v); |
| 2391 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2392 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2393 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2394 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 2395 | Py_DECREF(v); |
| 2396 | return b; |
| 2397 | } |
| 2398 | |
| 2399 | PyErr_Format(PyExc_TypeError, |
| 2400 | "encoder did not return a bytes object (type=%.400s)", |
| 2401 | Py_TYPE(v)->tp_name); |
| 2402 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2403 | return NULL; |
| 2404 | } |
| 2405 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2406 | PyObject * |
| 2407 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
| 2408 | const char *encoding, |
| 2409 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2410 | { |
| 2411 | PyObject *v; |
| 2412 | |
| 2413 | if (!PyUnicode_Check(unicode)) { |
| 2414 | PyErr_BadArgument(); |
| 2415 | goto onError; |
| 2416 | } |
| 2417 | |
| 2418 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2419 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2420 | |
| 2421 | /* Encode via the codec registry */ |
| 2422 | v = PyCodec_Encode(unicode, encoding, errors); |
| 2423 | if (v == NULL) |
| 2424 | goto onError; |
| 2425 | if (!PyUnicode_Check(v)) { |
| 2426 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2427 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2428 | Py_TYPE(v)->tp_name); |
| 2429 | Py_DECREF(v); |
| 2430 | goto onError; |
| 2431 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2432 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2433 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2434 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2435 | return NULL; |
| 2436 | } |
| 2437 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2438 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 2439 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2440 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 2441 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 2442 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2443 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 2444 | PyObject* |
| 2445 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 2446 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2447 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2448 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 2449 | #elif defined(__APPLE__) |
| 2450 | return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); |
| 2451 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 2452 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 2453 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 2454 | cannot use it to encode and decode filenames before it is loaded. Load |
| 2455 | the Python codec requires to encode at least its own filename. Use the C |
| 2456 | version of the locale codec until the codec registry is initialized and |
| 2457 | the Python codec is loaded. |
| 2458 | |
| 2459 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 2460 | cannot only rely on it: check also interp->fscodec_initialized for |
| 2461 | subinterpreters. */ |
| 2462 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2463 | return PyUnicode_Decode(s, size, |
| 2464 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 2465 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2466 | } |
| 2467 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2468 | /* locale encoding with surrogateescape */ |
| 2469 | wchar_t *wchar; |
| 2470 | PyObject *unicode; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 2471 | size_t len; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2472 | |
| 2473 | if (s[size] != '\0' || size != strlen(s)) { |
| 2474 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 2475 | return NULL; |
| 2476 | } |
| 2477 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 2478 | wchar = _Py_char2wchar(s, &len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2479 | if (wchar == NULL) |
Victor Stinner | d5af0a5 | 2010-11-08 23:34:29 +0000 | [diff] [blame] | 2480 | return PyErr_NoMemory(); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2481 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 2482 | unicode = PyUnicode_FromWideChar(wchar, len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2483 | PyMem_Free(wchar); |
| 2484 | return unicode; |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2485 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2486 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2487 | } |
| 2488 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2489 | |
| 2490 | int |
| 2491 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 2492 | { |
| 2493 | PyObject *output = NULL; |
| 2494 | Py_ssize_t size; |
| 2495 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 2496 | if (arg == NULL) { |
| 2497 | Py_DECREF(*(PyObject**)addr); |
| 2498 | return 1; |
| 2499 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2500 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2501 | output = arg; |
| 2502 | Py_INCREF(output); |
| 2503 | } |
| 2504 | else { |
| 2505 | arg = PyUnicode_FromObject(arg); |
| 2506 | if (!arg) |
| 2507 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2508 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2509 | Py_DECREF(arg); |
| 2510 | if (!output) |
| 2511 | return 0; |
| 2512 | if (!PyBytes_Check(output)) { |
| 2513 | Py_DECREF(output); |
| 2514 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 2515 | return 0; |
| 2516 | } |
| 2517 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 2518 | size = PyBytes_GET_SIZE(output); |
| 2519 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2520 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 2521 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2522 | Py_DECREF(output); |
| 2523 | return 0; |
| 2524 | } |
| 2525 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 2526 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2527 | } |
| 2528 | |
| 2529 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 2530 | int |
| 2531 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 2532 | { |
| 2533 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 2534 | if (arg == NULL) { |
| 2535 | Py_DECREF(*(PyObject**)addr); |
| 2536 | return 1; |
| 2537 | } |
| 2538 | if (PyUnicode_Check(arg)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2539 | if (PyUnicode_READY(arg)) |
| 2540 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 2541 | output = arg; |
| 2542 | Py_INCREF(output); |
| 2543 | } |
| 2544 | else { |
| 2545 | arg = PyBytes_FromObject(arg); |
| 2546 | if (!arg) |
| 2547 | return 0; |
| 2548 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 2549 | PyBytes_GET_SIZE(arg)); |
| 2550 | Py_DECREF(arg); |
| 2551 | if (!output) |
| 2552 | return 0; |
| 2553 | if (!PyUnicode_Check(output)) { |
| 2554 | Py_DECREF(output); |
| 2555 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 2556 | return 0; |
| 2557 | } |
| 2558 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2559 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
| 2560 | PyUnicode_GET_LENGTH(output), 0, 1)) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 2561 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 2562 | Py_DECREF(output); |
| 2563 | return 0; |
| 2564 | } |
| 2565 | *(PyObject**)addr = output; |
| 2566 | return Py_CLEANUP_SUPPORTED; |
| 2567 | } |
| 2568 | |
| 2569 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2570 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2571 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2572 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 2573 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2574 | PyUnicodeObject *u = (PyUnicodeObject *)unicode; |
| 2575 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 2576 | if (!PyUnicode_Check(unicode)) { |
| 2577 | PyErr_BadArgument(); |
| 2578 | return NULL; |
| 2579 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2580 | if (PyUnicode_READY(u) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2581 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2582 | |
| 2583 | if (_PyUnicode_UTF8(unicode) == NULL) { |
| 2584 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 2585 | if (bytes == NULL) |
| 2586 | return NULL; |
| 2587 | u->_base.utf8 = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 2588 | if (u->_base.utf8 == NULL) { |
| 2589 | Py_DECREF(bytes); |
| 2590 | return NULL; |
| 2591 | } |
| 2592 | u->_base.utf8_length = PyBytes_GET_SIZE(bytes); |
| 2593 | Py_MEMCPY(u->_base.utf8, PyBytes_AS_STRING(bytes), u->_base.utf8_length + 1); |
| 2594 | Py_DECREF(bytes); |
| 2595 | } |
| 2596 | |
| 2597 | if (psize) |
| 2598 | *psize = _PyUnicode_UTF8_LENGTH(unicode); |
| 2599 | return _PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 2600 | } |
| 2601 | |
| 2602 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2603 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 2604 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2605 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 2606 | } |
| 2607 | |
| 2608 | #ifdef Py_DEBUG |
| 2609 | int unicode_as_unicode_calls = 0; |
| 2610 | #endif |
| 2611 | |
| 2612 | |
| 2613 | Py_UNICODE * |
| 2614 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 2615 | { |
| 2616 | PyUnicodeObject *u; |
| 2617 | const unsigned char *one_byte; |
| 2618 | #if SIZEOF_WCHAR_T == 4 |
| 2619 | const Py_UCS2 *two_bytes; |
| 2620 | #else |
| 2621 | const Py_UCS4 *four_bytes; |
| 2622 | const Py_UCS4 *ucs4_end; |
| 2623 | Py_ssize_t num_surrogates; |
| 2624 | #endif |
| 2625 | wchar_t *w; |
| 2626 | wchar_t *wchar_end; |
| 2627 | |
| 2628 | if (!PyUnicode_Check(unicode)) { |
| 2629 | PyErr_BadArgument(); |
| 2630 | return NULL; |
| 2631 | } |
| 2632 | u = (PyUnicodeObject*)unicode; |
| 2633 | if (_PyUnicode_WSTR(u) == NULL) { |
| 2634 | /* Non-ASCII compact unicode object */ |
| 2635 | assert(_PyUnicode_KIND(u) != 0); |
| 2636 | assert(PyUnicode_IS_READY(u)); |
| 2637 | |
| 2638 | #ifdef Py_DEBUG |
| 2639 | ++unicode_as_unicode_calls; |
| 2640 | #endif |
| 2641 | |
| 2642 | if (PyUnicode_KIND(u) == PyUnicode_4BYTE_KIND) { |
| 2643 | #if SIZEOF_WCHAR_T == 2 |
| 2644 | four_bytes = PyUnicode_4BYTE_DATA(u); |
| 2645 | ucs4_end = four_bytes + _PyUnicode_LENGTH(u); |
| 2646 | num_surrogates = 0; |
| 2647 | |
| 2648 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 2649 | if (*four_bytes > 0xFFFF) |
| 2650 | ++num_surrogates; |
| 2651 | } |
| 2652 | |
| 2653 | _PyUnicode_WSTR(u) = (wchar_t *) PyObject_MALLOC( |
| 2654 | sizeof(wchar_t) * (_PyUnicode_LENGTH(u) + 1 + num_surrogates)); |
| 2655 | if (!_PyUnicode_WSTR(u)) { |
| 2656 | PyErr_NoMemory(); |
| 2657 | return NULL; |
| 2658 | } |
| 2659 | _PyUnicode_WSTR_LENGTH(u) = _PyUnicode_LENGTH(u) + num_surrogates; |
| 2660 | |
| 2661 | w = _PyUnicode_WSTR(u); |
| 2662 | wchar_end = w + _PyUnicode_WSTR_LENGTH(u); |
| 2663 | four_bytes = PyUnicode_4BYTE_DATA(u); |
| 2664 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 2665 | if (*four_bytes > 0xFFFF) { |
| 2666 | /* encode surrogate pair in this case */ |
| 2667 | *w++ = 0xD800 | ((*four_bytes - 0x10000) >> 10); |
| 2668 | *w = 0xDC00 | ((*four_bytes - 0x10000) & 0x3FF); |
| 2669 | } |
| 2670 | else |
| 2671 | *w = *four_bytes; |
| 2672 | |
| 2673 | if (w > wchar_end) { |
| 2674 | assert(0 && "Miscalculated string end"); |
| 2675 | } |
| 2676 | } |
| 2677 | *w = 0; |
| 2678 | #else |
| 2679 | /* sizeof(wchar_t) == 4 */ |
| 2680 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 2681 | "should share memory already."); |
| 2682 | return NULL; |
| 2683 | #endif |
| 2684 | } |
| 2685 | else { |
| 2686 | _PyUnicode_WSTR(u) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 2687 | (_PyUnicode_LENGTH(u) + 1)); |
| 2688 | if (!_PyUnicode_WSTR(u)) { |
| 2689 | PyErr_NoMemory(); |
| 2690 | return NULL; |
| 2691 | } |
| 2692 | if (!PyUnicode_IS_COMPACT_ASCII(u)) |
| 2693 | _PyUnicode_WSTR_LENGTH(u) = _PyUnicode_LENGTH(u); |
| 2694 | w = _PyUnicode_WSTR(u); |
| 2695 | wchar_end = w + _PyUnicode_LENGTH(u); |
| 2696 | |
| 2697 | if (PyUnicode_KIND(u) == PyUnicode_1BYTE_KIND) { |
| 2698 | one_byte = PyUnicode_1BYTE_DATA(u); |
| 2699 | for (; w < wchar_end; ++one_byte, ++w) |
| 2700 | *w = *one_byte; |
| 2701 | /* null-terminate the wstr */ |
| 2702 | *w = 0; |
| 2703 | } |
| 2704 | else if (PyUnicode_KIND(u) == PyUnicode_2BYTE_KIND) { |
| 2705 | #if SIZEOF_WCHAR_T == 4 |
| 2706 | two_bytes = PyUnicode_2BYTE_DATA(u); |
| 2707 | for (; w < wchar_end; ++two_bytes, ++w) |
| 2708 | *w = *two_bytes; |
| 2709 | /* null-terminate the wstr */ |
| 2710 | *w = 0; |
| 2711 | #else |
| 2712 | /* sizeof(wchar_t) == 2 */ |
| 2713 | PyObject_FREE(_PyUnicode_WSTR(u)); |
| 2714 | _PyUnicode_WSTR(u) = NULL; |
| 2715 | Py_FatalError("Impossible unicode object state, wstr " |
| 2716 | "and str should share memory already."); |
| 2717 | return NULL; |
| 2718 | #endif |
| 2719 | } |
| 2720 | else { |
| 2721 | assert(0 && "This should never happen."); |
| 2722 | } |
| 2723 | } |
| 2724 | } |
| 2725 | if (size != NULL) |
| 2726 | *size = PyUnicode_WSTR_LENGTH(u); |
| 2727 | return _PyUnicode_WSTR(u); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2728 | } |
| 2729 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2730 | Py_UNICODE * |
| 2731 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2732 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2733 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2734 | } |
| 2735 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2736 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2737 | Py_ssize_t |
| 2738 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2739 | { |
| 2740 | if (!PyUnicode_Check(unicode)) { |
| 2741 | PyErr_BadArgument(); |
| 2742 | goto onError; |
| 2743 | } |
| 2744 | return PyUnicode_GET_SIZE(unicode); |
| 2745 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2746 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2747 | return -1; |
| 2748 | } |
| 2749 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2750 | Py_ssize_t |
| 2751 | PyUnicode_GetLength(PyObject *unicode) |
| 2752 | { |
| 2753 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) != -1) { |
| 2754 | PyErr_BadArgument(); |
| 2755 | return -1; |
| 2756 | } |
| 2757 | |
| 2758 | return PyUnicode_GET_LENGTH(unicode); |
| 2759 | } |
| 2760 | |
| 2761 | Py_UCS4 |
| 2762 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 2763 | { |
| 2764 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) != -1) { |
| 2765 | return PyErr_BadArgument(); |
| 2766 | return (Py_UCS4)-1; |
| 2767 | } |
| 2768 | return PyUnicode_READ_CHAR(unicode, index); |
| 2769 | } |
| 2770 | |
| 2771 | int |
| 2772 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 2773 | { |
| 2774 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
| 2775 | return PyErr_BadArgument(); |
| 2776 | return -1; |
| 2777 | } |
| 2778 | |
| 2779 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 2780 | index, ch); |
| 2781 | return 0; |
| 2782 | } |
| 2783 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2784 | const char * |
| 2785 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2786 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 2787 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2788 | } |
| 2789 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 2790 | /* create or adjust a UnicodeDecodeError */ |
| 2791 | static void |
| 2792 | make_decode_exception(PyObject **exceptionObject, |
| 2793 | const char *encoding, |
| 2794 | const char *input, Py_ssize_t length, |
| 2795 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 2796 | const char *reason) |
| 2797 | { |
| 2798 | if (*exceptionObject == NULL) { |
| 2799 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 2800 | encoding, input, length, startpos, endpos, reason); |
| 2801 | } |
| 2802 | else { |
| 2803 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 2804 | goto onError; |
| 2805 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 2806 | goto onError; |
| 2807 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 2808 | goto onError; |
| 2809 | } |
| 2810 | return; |
| 2811 | |
| 2812 | onError: |
| 2813 | Py_DECREF(*exceptionObject); |
| 2814 | *exceptionObject = NULL; |
| 2815 | } |
| 2816 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2817 | /* error handling callback helper: |
| 2818 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 2819 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2820 | and adjust various state variables. |
| 2821 | return 0 on success, -1 on error |
| 2822 | */ |
| 2823 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2824 | static int |
| 2825 | unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
| 2826 | const char *encoding, const char *reason, |
| 2827 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 2828 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 2829 | PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2830 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2831 | 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] | 2832 | |
| 2833 | PyObject *restuple = NULL; |
| 2834 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2835 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2836 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2837 | Py_ssize_t requiredsize; |
| 2838 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2839 | const Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2840 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2841 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2842 | int res = -1; |
| 2843 | |
| 2844 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2845 | *errorHandler = PyCodec_LookupError(errors); |
| 2846 | if (*errorHandler == NULL) |
| 2847 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2848 | } |
| 2849 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 2850 | make_decode_exception(exceptionObject, |
| 2851 | encoding, |
| 2852 | *input, *inend - *input, |
| 2853 | *startinpos, *endinpos, |
| 2854 | reason); |
| 2855 | if (*exceptionObject == NULL) |
| 2856 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2857 | |
| 2858 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 2859 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2860 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2861 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 2862 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2863 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2864 | } |
| 2865 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2866 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2867 | |
| 2868 | /* Copy back the bytes variables, which might have been modified by the |
| 2869 | callback */ |
| 2870 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 2871 | if (!inputobj) |
| 2872 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2873 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2874 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2875 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2876 | *input = PyBytes_AS_STRING(inputobj); |
| 2877 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2878 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 2879 | /* we can DECREF safely, as the exception has another reference, |
| 2880 | so the object won't go away. */ |
| 2881 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2882 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2883 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2884 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 2885 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2886 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 2887 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 2888 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2889 | |
| 2890 | /* need more space? (at least enough for what we |
| 2891 | have+the replacement+the rest of the string (starting |
| 2892 | at the new input position), so we won't have to check space |
| 2893 | when there are no errors in the rest of the string) */ |
| 2894 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 2895 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 2896 | requiredsize = *outpos + repsize + insize-newpos; |
| 2897 | if (requiredsize > outsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2898 | if (requiredsize<2*outsize) |
| 2899 | requiredsize = 2*outsize; |
| 2900 | if (_PyUnicode_Resize(output, requiredsize) < 0) |
| 2901 | goto onError; |
| 2902 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2903 | } |
| 2904 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2905 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2906 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 2907 | *outptr += repsize; |
| 2908 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2909 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2910 | /* we made it! */ |
| 2911 | res = 0; |
| 2912 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2913 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2914 | Py_XDECREF(restuple); |
| 2915 | return res; |
| 2916 | } |
| 2917 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2918 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 2919 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2920 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 2921 | |
| 2922 | /* Three simple macros defining base-64. */ |
| 2923 | |
| 2924 | /* Is c a base-64 character? */ |
| 2925 | |
| 2926 | #define IS_BASE64(c) \ |
| 2927 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 2928 | ((c) >= 'a' && (c) <= 'z') || \ |
| 2929 | ((c) >= '0' && (c) <= '9') || \ |
| 2930 | (c) == '+' || (c) == '/') |
| 2931 | |
| 2932 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 2933 | |
| 2934 | #define FROM_BASE64(c) \ |
| 2935 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 2936 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 2937 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 2938 | (c) == '+' ? 62 : 63) |
| 2939 | |
| 2940 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 2941 | |
| 2942 | #define TO_BASE64(n) \ |
| 2943 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 2944 | |
| 2945 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 2946 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 2947 | * byte not decoding to itself is the + which begins a base64 |
| 2948 | * string. */ |
| 2949 | |
| 2950 | #define DECODE_DIRECT(c) \ |
| 2951 | ((c) <= 127 && (c) != '+') |
| 2952 | |
| 2953 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 2954 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 2955 | * the above). See RFC2152. This array identifies these different |
| 2956 | * sets: |
| 2957 | * 0 : "Set D" |
| 2958 | * alphanumeric and '(),-./:? |
| 2959 | * 1 : "Set O" |
| 2960 | * !"#$%&*;<=>@[]^_`{|} |
| 2961 | * 2 : "whitespace" |
| 2962 | * ht nl cr sp |
| 2963 | * 3 : special (must be base64 encoded) |
| 2964 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 2965 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2966 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2967 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2968 | char utf7_category[128] = { |
| 2969 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 2970 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 2971 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 2972 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 2973 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 2974 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 2975 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 2976 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 2977 | /* @ A B C D E F G H I J K L M N O */ |
| 2978 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2979 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 2980 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 2981 | /* ` a b c d e f g h i j k l m n o */ |
| 2982 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2983 | /* p q r s t u v w x y z { | } ~ del */ |
| 2984 | 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] | 2985 | }; |
| 2986 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2987 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 2988 | * answer depends on whether we are encoding set O as itself, and also |
| 2989 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 2990 | * clear that the answers to these questions vary between |
| 2991 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 2992 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2993 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 2994 | ((c) < 128 && (c) > 0 && \ |
| 2995 | ((utf7_category[(c)] == 0) || \ |
| 2996 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 2997 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2998 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2999 | PyObject * |
| 3000 | PyUnicode_DecodeUTF7(const char *s, |
| 3001 | Py_ssize_t size, |
| 3002 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3003 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3004 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 3005 | } |
| 3006 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3007 | /* The decoder. The only state we preserve is our read position, |
| 3008 | * i.e. how many characters we have consumed. So if we end in the |
| 3009 | * middle of a shift sequence we have to back off the read position |
| 3010 | * and the output to the beginning of the sequence, otherwise we lose |
| 3011 | * all the shift state (seen bits, number of bits seen, high |
| 3012 | * surrogate). */ |
| 3013 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3014 | PyObject * |
| 3015 | PyUnicode_DecodeUTF7Stateful(const char *s, |
| 3016 | Py_ssize_t size, |
| 3017 | const char *errors, |
| 3018 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3019 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3020 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3021 | Py_ssize_t startinpos; |
| 3022 | Py_ssize_t endinpos; |
| 3023 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3024 | const char *e; |
| 3025 | PyUnicodeObject *unicode; |
| 3026 | Py_UNICODE *p; |
| 3027 | const char *errmsg = ""; |
| 3028 | int inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3029 | Py_UNICODE *shiftOutStart; |
| 3030 | unsigned int base64bits = 0; |
| 3031 | unsigned long base64buffer = 0; |
| 3032 | Py_UNICODE surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3033 | PyObject *errorHandler = NULL; |
| 3034 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3035 | |
| 3036 | unicode = _PyUnicode_New(size); |
| 3037 | if (!unicode) |
| 3038 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3039 | if (size == 0) { |
| 3040 | if (consumed) |
| 3041 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3042 | return (PyObject *)unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3043 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3044 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3045 | p = PyUnicode_AS_UNICODE(unicode); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3046 | shiftOutStart = p; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3047 | e = s + size; |
| 3048 | |
| 3049 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3050 | Py_UNICODE ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3051 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 3052 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3053 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3054 | if (inShift) { /* in a base-64 section */ |
| 3055 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 3056 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 3057 | base64bits += 6; |
| 3058 | s++; |
| 3059 | if (base64bits >= 16) { |
| 3060 | /* we have enough bits for a UTF-16 value */ |
| 3061 | Py_UNICODE outCh = (Py_UNICODE) |
| 3062 | (base64buffer >> (base64bits-16)); |
| 3063 | base64bits -= 16; |
| 3064 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 3065 | if (surrogate) { |
| 3066 | /* expecting a second surrogate */ |
| 3067 | if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 3068 | #ifdef Py_UNICODE_WIDE |
| 3069 | *p++ = (((surrogate & 0x3FF)<<10) |
| 3070 | | (outCh & 0x3FF)) + 0x10000; |
| 3071 | #else |
| 3072 | *p++ = surrogate; |
| 3073 | *p++ = outCh; |
| 3074 | #endif |
| 3075 | surrogate = 0; |
| 3076 | } |
| 3077 | else { |
| 3078 | surrogate = 0; |
| 3079 | errmsg = "second surrogate missing"; |
| 3080 | goto utf7Error; |
| 3081 | } |
| 3082 | } |
| 3083 | else if (outCh >= 0xD800 && outCh <= 0xDBFF) { |
| 3084 | /* first surrogate */ |
| 3085 | surrogate = outCh; |
| 3086 | } |
| 3087 | else if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 3088 | errmsg = "unexpected second surrogate"; |
| 3089 | goto utf7Error; |
| 3090 | } |
| 3091 | else { |
| 3092 | *p++ = outCh; |
| 3093 | } |
| 3094 | } |
| 3095 | } |
| 3096 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3097 | inShift = 0; |
| 3098 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3099 | if (surrogate) { |
| 3100 | errmsg = "second surrogate missing at end of shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3101 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3102 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3103 | if (base64bits > 0) { /* left-over bits */ |
| 3104 | if (base64bits >= 6) { |
| 3105 | /* We've seen at least one base-64 character */ |
| 3106 | errmsg = "partial character in shift sequence"; |
| 3107 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3108 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3109 | else { |
| 3110 | /* Some bits remain; they should be zero */ |
| 3111 | if (base64buffer != 0) { |
| 3112 | errmsg = "non-zero padding bits in shift sequence"; |
| 3113 | goto utf7Error; |
| 3114 | } |
| 3115 | } |
| 3116 | } |
| 3117 | if (ch != '-') { |
| 3118 | /* '-' is absorbed; other terminating |
| 3119 | characters are preserved */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3120 | *p++ = ch; |
| 3121 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3122 | } |
| 3123 | } |
| 3124 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3125 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3126 | s++; /* consume '+' */ |
| 3127 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3128 | s++; |
| 3129 | *p++ = '+'; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3130 | } |
| 3131 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3132 | inShift = 1; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3133 | shiftOutStart = p; |
| 3134 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3135 | } |
| 3136 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3137 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3138 | *p++ = ch; |
| 3139 | s++; |
| 3140 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3141 | else { |
| 3142 | startinpos = s-starts; |
| 3143 | s++; |
| 3144 | errmsg = "unexpected special character"; |
| 3145 | goto utf7Error; |
| 3146 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3147 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3148 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3149 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 3150 | endinpos = s-starts; |
| 3151 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3152 | errors, &errorHandler, |
| 3153 | "utf7", errmsg, |
| 3154 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 3155 | &unicode, &outpos, &p)) |
| 3156 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3157 | } |
| 3158 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3159 | /* end of string */ |
| 3160 | |
| 3161 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 3162 | /* if we're in an inconsistent state, that's an error */ |
| 3163 | if (surrogate || |
| 3164 | (base64bits >= 6) || |
| 3165 | (base64bits > 0 && base64buffer != 0)) { |
| 3166 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 3167 | endinpos = size; |
| 3168 | if (unicode_decode_call_errorhandler( |
| 3169 | errors, &errorHandler, |
| 3170 | "utf7", "unterminated shift sequence", |
| 3171 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 3172 | &unicode, &outpos, &p)) |
| 3173 | goto onError; |
| 3174 | if (s < e) |
| 3175 | goto restart; |
| 3176 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3177 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3178 | |
| 3179 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3180 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3181 | if (inShift) { |
| 3182 | p = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3183 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3184 | } |
| 3185 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3186 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3187 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3188 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3189 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 3190 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3191 | goto onError; |
| 3192 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3193 | Py_XDECREF(errorHandler); |
| 3194 | Py_XDECREF(exc); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3195 | if (PyUnicode_READY(unicode) == -1) { |
| 3196 | Py_DECREF(unicode); |
| 3197 | return NULL; |
| 3198 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3199 | return (PyObject *)unicode; |
| 3200 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3201 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3202 | Py_XDECREF(errorHandler); |
| 3203 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3204 | Py_DECREF(unicode); |
| 3205 | return NULL; |
| 3206 | } |
| 3207 | |
| 3208 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3209 | PyObject * |
| 3210 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 3211 | Py_ssize_t size, |
| 3212 | int base64SetO, |
| 3213 | int base64WhiteSpace, |
| 3214 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3215 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3216 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3217 | /* It might be possible to tighten this worst case */ |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 3218 | Py_ssize_t allocated = 8 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3219 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3220 | Py_ssize_t i = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3221 | unsigned int base64bits = 0; |
| 3222 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3223 | char * out; |
| 3224 | char * start; |
| 3225 | |
| 3226 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3227 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3228 | |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 3229 | if (allocated / 8 != size) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3230 | return PyErr_NoMemory(); |
| 3231 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3232 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3233 | if (v == NULL) |
| 3234 | return NULL; |
| 3235 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3236 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3237 | for (;i < size; ++i) { |
| 3238 | Py_UNICODE ch = s[i]; |
| 3239 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3240 | if (inShift) { |
| 3241 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 3242 | /* shifting out */ |
| 3243 | if (base64bits) { /* output remaining bits */ |
| 3244 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 3245 | base64buffer = 0; |
| 3246 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3247 | } |
| 3248 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3249 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 3250 | so no '-' is required, except if the character is itself a '-' */ |
| 3251 | if (IS_BASE64(ch) || ch == '-') { |
| 3252 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3253 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3254 | *out++ = (char) ch; |
| 3255 | } |
| 3256 | else { |
| 3257 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3258 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3259 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3260 | else { /* not in a shift sequence */ |
| 3261 | if (ch == '+') { |
| 3262 | *out++ = '+'; |
| 3263 | *out++ = '-'; |
| 3264 | } |
| 3265 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 3266 | *out++ = (char) ch; |
| 3267 | } |
| 3268 | else { |
| 3269 | *out++ = '+'; |
| 3270 | inShift = 1; |
| 3271 | goto encode_char; |
| 3272 | } |
| 3273 | } |
| 3274 | continue; |
| 3275 | encode_char: |
| 3276 | #ifdef Py_UNICODE_WIDE |
| 3277 | if (ch >= 0x10000) { |
| 3278 | /* code first surrogate */ |
| 3279 | base64bits += 16; |
| 3280 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 3281 | while (base64bits >= 6) { |
| 3282 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 3283 | base64bits -= 6; |
| 3284 | } |
| 3285 | /* prepare second surrogate */ |
| 3286 | ch = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 3287 | } |
| 3288 | #endif |
| 3289 | base64bits += 16; |
| 3290 | base64buffer = (base64buffer << 16) | ch; |
| 3291 | while (base64bits >= 6) { |
| 3292 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 3293 | base64bits -= 6; |
| 3294 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 3295 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3296 | if (base64bits) |
| 3297 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 3298 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3299 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3300 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 3301 | return NULL; |
| 3302 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3303 | } |
| 3304 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3305 | #undef IS_BASE64 |
| 3306 | #undef FROM_BASE64 |
| 3307 | #undef TO_BASE64 |
| 3308 | #undef DECODE_DIRECT |
| 3309 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3310 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3311 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 3312 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3313 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3314 | char utf8_code_length[256] = { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3315 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 3316 | illegal prefix. See RFC 3629 for details */ |
| 3317 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 3318 | 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] | 3319 | 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] | 3320 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 3321 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 3322 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 3323 | 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] | 3324 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 3325 | 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] | 3326 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3327 | 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] | 3328 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 3329 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 3330 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 3331 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 3332 | 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] | 3333 | }; |
| 3334 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3335 | PyObject * |
| 3336 | PyUnicode_DecodeUTF8(const char *s, |
| 3337 | Py_ssize_t size, |
| 3338 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3339 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3340 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 3341 | } |
| 3342 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3343 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 3344 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 3345 | |
| 3346 | /* Mask to quickly check whether a C 'long' contains a |
| 3347 | non-ASCII, UTF8-encoded char. */ |
| 3348 | #if (SIZEOF_LONG == 8) |
| 3349 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 3350 | #elif (SIZEOF_LONG == 4) |
| 3351 | # define ASCII_CHAR_MASK 0x80808080L |
| 3352 | #else |
| 3353 | # error C 'long' size should be either 4 or 8! |
| 3354 | #endif |
| 3355 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3356 | /* Scans a UTF-8 string and returns the maximum character to be expected, |
| 3357 | the size of the decoded unicode string and if any major errors were |
| 3358 | encountered. |
| 3359 | |
| 3360 | This function does check basic UTF-8 sanity, it does however NOT CHECK |
| 3361 | if the string contains surrogates, and if all continuation bytes are |
| 3362 | within the correct ranges, these checks are performed in |
| 3363 | PyUnicode_DecodeUTF8Stateful. |
| 3364 | |
| 3365 | If it sets has_errors to 1, it means the value of unicode_size and max_char |
| 3366 | will be bogus and you should not rely on useful information in them. |
| 3367 | */ |
| 3368 | static Py_UCS4 |
| 3369 | utf8_max_char_size_and_has_errors(const char *s, Py_ssize_t string_size, |
| 3370 | Py_ssize_t *unicode_size, Py_ssize_t* consumed, |
| 3371 | int *has_errors) |
| 3372 | { |
| 3373 | Py_ssize_t n; |
| 3374 | Py_ssize_t char_count = 0; |
| 3375 | Py_UCS4 max_char = 127, new_max; |
| 3376 | Py_UCS4 upper_bound; |
| 3377 | const unsigned char *p = (const unsigned char *)s; |
| 3378 | const unsigned char *end = p + string_size; |
| 3379 | const unsigned char *aligned_end = (const unsigned char *) ((size_t) end & ~LONG_PTR_MASK); |
| 3380 | int err = 0; |
| 3381 | |
| 3382 | for (; p < end && !err; ++p, ++char_count) { |
| 3383 | /* Only check value if it's not a ASCII char... */ |
| 3384 | if (*p < 0x80) { |
| 3385 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 3386 | an explanation. */ |
| 3387 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 3388 | /* Help register allocation */ |
| 3389 | register const unsigned char *_p = p; |
| 3390 | while (_p < aligned_end) { |
| 3391 | unsigned long value = *(unsigned long *) _p; |
| 3392 | if (value & ASCII_CHAR_MASK) |
| 3393 | break; |
| 3394 | _p += SIZEOF_LONG; |
| 3395 | char_count += SIZEOF_LONG; |
| 3396 | } |
| 3397 | p = _p; |
| 3398 | if (p == end) |
| 3399 | break; |
| 3400 | } |
| 3401 | } |
| 3402 | if (*p >= 0x80) { |
| 3403 | n = utf8_code_length[*p]; |
| 3404 | new_max = max_char; |
| 3405 | switch (n) { |
| 3406 | /* invalid start byte */ |
| 3407 | case 0: |
| 3408 | err = 1; |
| 3409 | break; |
| 3410 | case 2: |
| 3411 | /* Code points between 0x00FF and 0x07FF inclusive. |
| 3412 | Approximate the upper bound of the code point, |
| 3413 | if this flips over 255 we can be sure it will be more |
| 3414 | than 255 and the string will need 2 bytes per code coint, |
| 3415 | if it stays under or equal to 255, we can be sure 1 byte |
| 3416 | is enough. |
| 3417 | ((*p & 0b00011111) << 6) | 0b00111111 */ |
| 3418 | upper_bound = ((*p & 0x1F) << 6) | 0x3F; |
| 3419 | if (max_char < upper_bound) |
| 3420 | new_max = upper_bound; |
| 3421 | /* Ensure we track at least that we left ASCII space. */ |
| 3422 | if (new_max < 128) |
| 3423 | new_max = 128; |
| 3424 | break; |
| 3425 | case 3: |
| 3426 | /* Between 0x0FFF and 0xFFFF inclusive, so values are |
| 3427 | always > 255 and <= 65535 and will always need 2 bytes. */ |
| 3428 | if (max_char < 65535) |
| 3429 | new_max = 65535; |
| 3430 | break; |
| 3431 | case 4: |
| 3432 | /* Code point will be above 0xFFFF for sure in this case. */ |
| 3433 | new_max = 65537; |
| 3434 | break; |
| 3435 | /* Internal error, this should be caught by the first if */ |
| 3436 | case 1: |
| 3437 | default: |
| 3438 | assert(0 && "Impossible case in utf8_max_char_and_size"); |
| 3439 | err = 1; |
| 3440 | } |
| 3441 | /* Instead of number of overall bytes for this code point, |
| 3442 | n containts the number of following bytes: */ |
| 3443 | --n; |
| 3444 | /* Check if the follow up chars are all valid continuation bytes */ |
| 3445 | if (n >= 1) { |
| 3446 | const unsigned char *cont; |
| 3447 | if ((p + n) >= end) { |
| 3448 | if (consumed == 0) |
| 3449 | /* incomplete data, non-incremental decoding */ |
| 3450 | err = 1; |
| 3451 | break; |
| 3452 | } |
| 3453 | for (cont = p + 1; cont < (p + n); ++cont) { |
| 3454 | if ((*cont & 0xc0) != 0x80) { |
| 3455 | err = 1; |
| 3456 | break; |
| 3457 | } |
| 3458 | } |
| 3459 | p += n; |
| 3460 | } |
| 3461 | else |
| 3462 | err = 1; |
| 3463 | max_char = new_max; |
| 3464 | } |
| 3465 | } |
| 3466 | |
| 3467 | if (unicode_size) |
| 3468 | *unicode_size = char_count; |
| 3469 | if (has_errors) |
| 3470 | *has_errors = err; |
| 3471 | return max_char; |
| 3472 | } |
| 3473 | |
| 3474 | /* Similar to PyUnicode_WRITE but can also write into wstr field |
| 3475 | of the legacy unicode representation */ |
| 3476 | #define WRITE_FLEXIBLE_OR_WSTR(kind, buf, index, value) \ |
| 3477 | do { \ |
| 3478 | const int k_ = (kind); \ |
| 3479 | if (k_ == PyUnicode_WCHAR_KIND) \ |
| 3480 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \ |
| 3481 | else if (k_ == PyUnicode_1BYTE_KIND) \ |
| 3482 | ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \ |
| 3483 | else if (k_ == PyUnicode_2BYTE_KIND) \ |
| 3484 | ((Py_UCS2 *)(buf))[(index)] = (Py_UCS2)(value); \ |
| 3485 | else \ |
| 3486 | ((Py_UCS4 *)(buf))[(index)] = (Py_UCS4)(value); \ |
| 3487 | } while (0) |
| 3488 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3489 | PyObject * |
| 3490 | PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3491 | Py_ssize_t size, |
| 3492 | const char *errors, |
| 3493 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3494 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3495 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3496 | int n; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3497 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3498 | Py_ssize_t startinpos; |
| 3499 | Py_ssize_t endinpos; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3500 | const char *e, *aligned_end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3501 | PyUnicodeObject *unicode; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 3502 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3503 | PyObject *errorHandler = NULL; |
| 3504 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3505 | Py_UCS4 maxchar = 0; |
| 3506 | Py_ssize_t unicode_size; |
| 3507 | Py_ssize_t i; |
| 3508 | int kind; |
| 3509 | void *data; |
| 3510 | int has_errors; |
| 3511 | Py_UNICODE *error_outptr; |
| 3512 | #if SIZEOF_WCHAR_T == 2 |
| 3513 | Py_ssize_t wchar_offset = 0; |
| 3514 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3515 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3516 | if (size == 0) { |
| 3517 | if (consumed) |
| 3518 | *consumed = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3519 | return (PyObject *)PyUnicode_New(0, 0); |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3520 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3521 | maxchar = utf8_max_char_size_and_has_errors(s, size, &unicode_size, |
| 3522 | consumed, &has_errors); |
| 3523 | if (has_errors) { |
| 3524 | unicode = _PyUnicode_New(size); |
| 3525 | if (!unicode) |
| 3526 | return NULL; |
| 3527 | kind = PyUnicode_WCHAR_KIND; |
| 3528 | data = PyUnicode_AS_UNICODE(unicode); |
| 3529 | assert(data != NULL); |
| 3530 | } |
| 3531 | else { |
| 3532 | unicode = (PyUnicodeObject *)PyUnicode_New(unicode_size, maxchar); |
| 3533 | if (!unicode) |
| 3534 | return NULL; |
| 3535 | /* When the string is ASCII only, just use memcpy and return. |
| 3536 | unicode_size may be != size if there is an incomplete UTF-8 |
| 3537 | sequence at the end of the ASCII block. */ |
| 3538 | if (maxchar < 128 && size == unicode_size) { |
| 3539 | Py_MEMCPY(PyUnicode_1BYTE_DATA(unicode), s, unicode_size); |
| 3540 | return (PyObject *)unicode; |
| 3541 | } |
| 3542 | kind = PyUnicode_KIND(unicode); |
| 3543 | data = PyUnicode_DATA(unicode); |
| 3544 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3545 | /* Unpack UTF-8 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3546 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3547 | e = s + size; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3548 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3549 | |
| 3550 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 3551 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3552 | |
| 3553 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3554 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 3555 | input will consist of an overwhelming majority of ASCII |
| 3556 | characters, we try to optimize for this case by checking |
| 3557 | as many characters as a C 'long' can contain. |
| 3558 | First, check if we can do an aligned read, as most CPUs have |
| 3559 | a penalty for unaligned reads. |
| 3560 | */ |
| 3561 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 3562 | /* Help register allocation */ |
| 3563 | register const char *_s = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3564 | register Py_ssize_t _i = i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3565 | while (_s < aligned_end) { |
| 3566 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 3567 | and do a fast unrolled copy if it only contains ASCII |
| 3568 | characters. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3569 | unsigned long value = *(unsigned long *) _s; |
| 3570 | if (value & ASCII_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3571 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3572 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+0, _s[0]); |
| 3573 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+1, _s[1]); |
| 3574 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+2, _s[2]); |
| 3575 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+3, _s[3]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3576 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3577 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+4, _s[4]); |
| 3578 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+5, _s[5]); |
| 3579 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+6, _s[6]); |
| 3580 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+7, _s[7]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3581 | #endif |
| 3582 | _s += SIZEOF_LONG; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3583 | _i += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3584 | } |
| 3585 | s = _s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3586 | i = _i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3587 | if (s == e) |
| 3588 | break; |
| 3589 | ch = (unsigned char)*s; |
| 3590 | } |
| 3591 | } |
| 3592 | |
| 3593 | if (ch < 0x80) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3594 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3595 | s++; |
| 3596 | continue; |
| 3597 | } |
| 3598 | |
| 3599 | n = utf8_code_length[ch]; |
| 3600 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 3601 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3602 | if (consumed) |
| 3603 | break; |
| 3604 | else { |
| 3605 | errmsg = "unexpected end of data"; |
| 3606 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3607 | endinpos = startinpos+1; |
| 3608 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 3609 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3610 | goto utf8Error; |
| 3611 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3612 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3613 | |
| 3614 | switch (n) { |
| 3615 | |
| 3616 | case 0: |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3617 | errmsg = "invalid start byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3618 | startinpos = s-starts; |
| 3619 | endinpos = startinpos+1; |
| 3620 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3621 | |
| 3622 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 3623 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3624 | startinpos = s-starts; |
| 3625 | endinpos = startinpos+1; |
| 3626 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3627 | |
| 3628 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 3629 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3630 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3631 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3632 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3633 | goto utf8Error; |
| 3634 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3635 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3636 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3637 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3638 | break; |
| 3639 | |
| 3640 | case 3: |
Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 3641 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 3642 | will result in surrogates in range d800-dfff. Surrogates are |
| 3643 | not valid UTF-8 so they are rejected. |
| 3644 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 3645 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3646 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3647 | (s[2] & 0xc0) != 0x80 || |
| 3648 | ((unsigned char)s[0] == 0xE0 && |
| 3649 | (unsigned char)s[1] < 0xA0) || |
| 3650 | ((unsigned char)s[0] == 0xED && |
| 3651 | (unsigned char)s[1] > 0x9F)) { |
| 3652 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3653 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3654 | endinpos = startinpos + 1; |
| 3655 | |
| 3656 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 3657 | continuation byte is s[2], so increment endinpos by 1, |
| 3658 | if not, s[1] is invalid and endinpos doesn't need to |
| 3659 | be incremented. */ |
| 3660 | if ((s[1] & 0xC0) == 0x80) |
| 3661 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3662 | goto utf8Error; |
| 3663 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3664 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3665 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3666 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 3667 | break; |
| 3668 | |
| 3669 | case 4: |
| 3670 | if ((s[1] & 0xc0) != 0x80 || |
| 3671 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3672 | (s[3] & 0xc0) != 0x80 || |
| 3673 | ((unsigned char)s[0] == 0xF0 && |
| 3674 | (unsigned char)s[1] < 0x90) || |
| 3675 | ((unsigned char)s[0] == 0xF4 && |
| 3676 | (unsigned char)s[1] > 0x8F)) { |
| 3677 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3678 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3679 | endinpos = startinpos + 1; |
| 3680 | if ((s[1] & 0xC0) == 0x80) { |
| 3681 | endinpos++; |
| 3682 | if ((s[2] & 0xC0) == 0x80) |
| 3683 | endinpos++; |
| 3684 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3685 | goto utf8Error; |
| 3686 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 3687 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3688 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 3689 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 3690 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3691 | /* If the string is flexible or we have native UCS-4, write |
| 3692 | directly.. */ |
| 3693 | if (sizeof(Py_UNICODE) > 2 || kind != PyUnicode_WCHAR_KIND) |
| 3694 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3695 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3696 | else { |
| 3697 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3698 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3699 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 3700 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3701 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3702 | /* high surrogate = top 10 bits added to D800 */ |
| 3703 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, |
| 3704 | (Py_UNICODE)(0xD800 + (ch >> 10))); |
| 3705 | |
| 3706 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 3707 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, |
| 3708 | (Py_UNICODE)(0xDC00 + (ch & 0x03FF))); |
| 3709 | } |
| 3710 | #if SIZEOF_WCHAR_T == 2 |
| 3711 | wchar_offset++; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3712 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3713 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3714 | } |
| 3715 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3716 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3717 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3718 | utf8Error: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3719 | /* If this is not yet a resizable string, make it one.. */ |
| 3720 | if (kind != PyUnicode_WCHAR_KIND) { |
| 3721 | const Py_UNICODE *u; |
| 3722 | PyUnicodeObject *new_unicode = _PyUnicode_New(size); |
| 3723 | if (!new_unicode) |
| 3724 | goto onError; |
| 3725 | u = PyUnicode_AsUnicode((PyObject *)unicode); |
| 3726 | if (!u) |
| 3727 | goto onError; |
| 3728 | #if SIZEOF_WCHAR_T == 2 |
| 3729 | i += wchar_offset; |
| 3730 | #endif |
| 3731 | Py_UNICODE_COPY(PyUnicode_AS_UNICODE(new_unicode), u, i); |
| 3732 | Py_DECREF(unicode); |
| 3733 | unicode = new_unicode; |
| 3734 | kind = 0; |
| 3735 | data = PyUnicode_AS_UNICODE(new_unicode); |
| 3736 | assert(data != NULL); |
| 3737 | } |
| 3738 | error_outptr = PyUnicode_AS_UNICODE(unicode) + i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3739 | if (unicode_decode_call_errorhandler( |
| 3740 | errors, &errorHandler, |
| 3741 | "utf8", errmsg, |
| 3742 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3743 | &unicode, &i, &error_outptr)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3744 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3745 | /* Update data because unicode_decode_call_errorhandler might have |
| 3746 | re-created or resized the unicode object. */ |
| 3747 | data = PyUnicode_AS_UNICODE(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3748 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3749 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3750 | /* Ensure the unicode_size calculation above was correct: */ |
| 3751 | assert(kind == PyUnicode_WCHAR_KIND || i == unicode_size); |
| 3752 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3753 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3754 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3755 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3756 | /* Adjust length and ready string when it contained errors and |
| 3757 | is of the old resizable kind. */ |
| 3758 | if (kind == PyUnicode_WCHAR_KIND) { |
| 3759 | if (_PyUnicode_Resize(&unicode, i) < 0 || |
| 3760 | PyUnicode_READY(unicode) == -1) |
| 3761 | goto onError; |
| 3762 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3763 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3764 | Py_XDECREF(errorHandler); |
| 3765 | Py_XDECREF(exc); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3766 | if (PyUnicode_READY(unicode) == -1) { |
| 3767 | Py_DECREF(unicode); |
| 3768 | return NULL; |
| 3769 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3770 | return (PyObject *)unicode; |
| 3771 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3772 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3773 | Py_XDECREF(errorHandler); |
| 3774 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3775 | Py_DECREF(unicode); |
| 3776 | return NULL; |
| 3777 | } |
| 3778 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3779 | #undef WRITE_FLEXIBLE_OR_WSTR |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3780 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 3781 | #ifdef __APPLE__ |
| 3782 | |
| 3783 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
| 3784 | used to decode the command line arguments on Mac OS X. */ |
| 3785 | |
| 3786 | wchar_t* |
| 3787 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 3788 | { |
| 3789 | int n; |
| 3790 | const char *e; |
| 3791 | wchar_t *unicode, *p; |
| 3792 | |
| 3793 | /* Note: size will always be longer than the resulting Unicode |
| 3794 | character count */ |
| 3795 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) { |
| 3796 | PyErr_NoMemory(); |
| 3797 | return NULL; |
| 3798 | } |
| 3799 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 3800 | if (!unicode) |
| 3801 | return NULL; |
| 3802 | |
| 3803 | /* Unpack UTF-8 encoded data */ |
| 3804 | p = unicode; |
| 3805 | e = s + size; |
| 3806 | while (s < e) { |
| 3807 | Py_UCS4 ch = (unsigned char)*s; |
| 3808 | |
| 3809 | if (ch < 0x80) { |
| 3810 | *p++ = (wchar_t)ch; |
| 3811 | s++; |
| 3812 | continue; |
| 3813 | } |
| 3814 | |
| 3815 | n = utf8_code_length[ch]; |
| 3816 | if (s + n > e) { |
| 3817 | goto surrogateescape; |
| 3818 | } |
| 3819 | |
| 3820 | switch (n) { |
| 3821 | case 0: |
| 3822 | case 1: |
| 3823 | goto surrogateescape; |
| 3824 | |
| 3825 | case 2: |
| 3826 | if ((s[1] & 0xc0) != 0x80) |
| 3827 | goto surrogateescape; |
| 3828 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
| 3829 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 3830 | *p++ = (wchar_t)ch; |
| 3831 | break; |
| 3832 | |
| 3833 | case 3: |
| 3834 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 3835 | will result in surrogates in range d800-dfff. Surrogates are |
| 3836 | not valid UTF-8 so they are rejected. |
| 3837 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 3838 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
| 3839 | if ((s[1] & 0xc0) != 0x80 || |
| 3840 | (s[2] & 0xc0) != 0x80 || |
| 3841 | ((unsigned char)s[0] == 0xE0 && |
| 3842 | (unsigned char)s[1] < 0xA0) || |
| 3843 | ((unsigned char)s[0] == 0xED && |
| 3844 | (unsigned char)s[1] > 0x9F)) { |
| 3845 | |
| 3846 | goto surrogateescape; |
| 3847 | } |
| 3848 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
| 3849 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3850 | *p++ = (wchar_t)ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 3851 | break; |
| 3852 | |
| 3853 | case 4: |
| 3854 | if ((s[1] & 0xc0) != 0x80 || |
| 3855 | (s[2] & 0xc0) != 0x80 || |
| 3856 | (s[3] & 0xc0) != 0x80 || |
| 3857 | ((unsigned char)s[0] == 0xF0 && |
| 3858 | (unsigned char)s[1] < 0x90) || |
| 3859 | ((unsigned char)s[0] == 0xF4 && |
| 3860 | (unsigned char)s[1] > 0x8F)) { |
| 3861 | goto surrogateescape; |
| 3862 | } |
| 3863 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 3864 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 3865 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 3866 | |
| 3867 | #if SIZEOF_WCHAR_T == 4 |
| 3868 | *p++ = (wchar_t)ch; |
| 3869 | #else |
| 3870 | /* compute and append the two surrogates: */ |
| 3871 | |
| 3872 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 3873 | ch -= 0x10000; |
| 3874 | |
| 3875 | /* high surrogate = top 10 bits added to D800 */ |
| 3876 | *p++ = (wchar_t)(0xD800 + (ch >> 10)); |
| 3877 | |
| 3878 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 3879 | *p++ = (wchar_t)(0xDC00 + (ch & 0x03FF)); |
| 3880 | #endif |
| 3881 | break; |
| 3882 | } |
| 3883 | s += n; |
| 3884 | continue; |
| 3885 | |
| 3886 | surrogateescape: |
| 3887 | *p++ = 0xDC00 + ch; |
| 3888 | s++; |
| 3889 | } |
| 3890 | *p = L'\0'; |
| 3891 | return unicode; |
| 3892 | } |
| 3893 | |
| 3894 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3895 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3896 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 3897 | |
| 3898 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 3899 | and allocate exactly as much space needed at the end. Else allocate the |
| 3900 | maximum possible needed (4 result bytes per Unicode character), and return |
| 3901 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 3902 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 3903 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3904 | _PyUnicode_AsUTF8String(PyObject *obj, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3905 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 3906 | #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] | 3907 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3908 | Py_ssize_t i; /* index into s of next input byte */ |
| 3909 | PyObject *result; /* result string object */ |
| 3910 | char *p; /* next free byte in output buffer */ |
| 3911 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 3912 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 3913 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 3914 | PyObject *errorHandler = NULL; |
| 3915 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3916 | int kind; |
| 3917 | void *data; |
| 3918 | Py_ssize_t size; |
| 3919 | PyUnicodeObject *unicode = (PyUnicodeObject *)obj; |
| 3920 | #if SIZEOF_WCHAR_T == 2 |
| 3921 | Py_ssize_t wchar_offset = 0; |
| 3922 | #endif |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 3923 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3924 | if (!PyUnicode_Check(unicode)) { |
| 3925 | PyErr_BadArgument(); |
| 3926 | return NULL; |
| 3927 | } |
| 3928 | |
| 3929 | if (PyUnicode_READY(unicode) == -1) |
| 3930 | return NULL; |
| 3931 | |
| 3932 | if (_PyUnicode_UTF8(unicode)) |
| 3933 | return PyBytes_FromStringAndSize(_PyUnicode_UTF8(unicode), |
| 3934 | _PyUnicode_UTF8_LENGTH(unicode)); |
| 3935 | |
| 3936 | kind = PyUnicode_KIND(unicode); |
| 3937 | data = PyUnicode_DATA(unicode); |
| 3938 | size = PyUnicode_GET_LENGTH(unicode); |
| 3939 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 3940 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3941 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 3942 | if (size <= MAX_SHORT_UNICHARS) { |
| 3943 | /* Write into the stack buffer; nallocated can't overflow. |
| 3944 | * At the end, we'll allocate exactly as much heap space as it |
| 3945 | * turns out we need. |
| 3946 | */ |
| 3947 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3948 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 3949 | p = stackbuf; |
| 3950 | } |
| 3951 | else { |
| 3952 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 3953 | nallocated = size * 4; |
| 3954 | if (nallocated / 4 != size) /* overflow! */ |
| 3955 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3956 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3957 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 3958 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3959 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 3960 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 3961 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 3962 | for (i = 0; i < size;) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3963 | Py_UCS4 ch = PyUnicode_READ(kind, data, i++); |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 3964 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 3965 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 3966 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3967 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 3968 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3969 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 3970 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 3971 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 3972 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 3973 | } else if (0xD800 <= ch && ch <= 0xDFFF) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3974 | Py_ssize_t newpos; |
| 3975 | PyObject *rep; |
| 3976 | Py_ssize_t repsize, k, startpos; |
| 3977 | startpos = i-1; |
| 3978 | #if SIZEOF_WCHAR_T == 2 |
| 3979 | startpos += wchar_offset; |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 3980 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3981 | rep = unicode_encode_call_errorhandler( |
| 3982 | errors, &errorHandler, "utf-8", "surrogates not allowed", |
| 3983 | PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), |
| 3984 | &exc, startpos, startpos+1, &newpos); |
| 3985 | if (!rep) |
| 3986 | goto error; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 3987 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3988 | if (PyBytes_Check(rep)) |
| 3989 | repsize = PyBytes_GET_SIZE(rep); |
| 3990 | else |
| 3991 | repsize = PyUnicode_GET_SIZE(rep); |
| 3992 | |
| 3993 | if (repsize > 4) { |
| 3994 | Py_ssize_t offset; |
| 3995 | |
| 3996 | if (result == NULL) |
| 3997 | offset = p - stackbuf; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 3998 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3999 | offset = p - PyBytes_AS_STRING(result); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4000 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4001 | if (nallocated > PY_SSIZE_T_MAX - repsize + 4) { |
| 4002 | /* integer overflow */ |
| 4003 | PyErr_NoMemory(); |
| 4004 | goto error; |
| 4005 | } |
| 4006 | nallocated += repsize - 4; |
| 4007 | if (result != NULL) { |
| 4008 | if (_PyBytes_Resize(&result, nallocated) < 0) |
| 4009 | goto error; |
| 4010 | } else { |
| 4011 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4012 | if (result == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4013 | goto error; |
| 4014 | Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset); |
| 4015 | } |
| 4016 | p = PyBytes_AS_STRING(result) + offset; |
| 4017 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4018 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4019 | if (PyBytes_Check(rep)) { |
| 4020 | char *prep = PyBytes_AS_STRING(rep); |
| 4021 | for(k = repsize; k > 0; k--) |
| 4022 | *p++ = *prep++; |
| 4023 | } else /* rep is unicode */ { |
| 4024 | const Py_UNICODE *prep = PyUnicode_AS_UNICODE(rep); |
| 4025 | Py_UNICODE c; |
| 4026 | |
| 4027 | for(k=0; k<repsize; k++) { |
| 4028 | c = prep[k]; |
| 4029 | if (0x80 <= c) { |
| 4030 | raise_encode_exception(&exc, "utf-8", |
| 4031 | PyUnicode_AS_UNICODE(unicode), |
| 4032 | size, i-1, i, |
| 4033 | "surrogates not allowed"); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4034 | goto error; |
| 4035 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4036 | *p++ = (char)prep[k]; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4037 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4038 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4039 | Py_DECREF(rep); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4040 | } else if (ch < 0x10000) { |
| 4041 | *p++ = (char)(0xe0 | (ch >> 12)); |
| 4042 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4043 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 4044 | } else /* ch >= 0x10000 */ { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4045 | /* Encode UCS4 Unicode ordinals */ |
| 4046 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 4047 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 4048 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4049 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4050 | #if SIZEOF_WCHAR_T == 2 |
| 4051 | wchar_offset++; |
| 4052 | #endif |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4053 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4054 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 4055 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4056 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4057 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4058 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4059 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4060 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4061 | } |
| 4062 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 4063 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4064 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4065 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4066 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4067 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4068 | |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4069 | Py_XDECREF(errorHandler); |
| 4070 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4071 | return result; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4072 | error: |
| 4073 | Py_XDECREF(errorHandler); |
| 4074 | Py_XDECREF(exc); |
| 4075 | Py_XDECREF(result); |
| 4076 | return NULL; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4077 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4078 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4079 | } |
| 4080 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4081 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4082 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 4083 | Py_ssize_t size, |
| 4084 | const char *errors) |
| 4085 | { |
| 4086 | PyObject *v, *unicode; |
| 4087 | |
| 4088 | unicode = PyUnicode_FromUnicode(s, size); |
| 4089 | if (unicode == NULL) |
| 4090 | return NULL; |
| 4091 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 4092 | Py_DECREF(unicode); |
| 4093 | return v; |
| 4094 | } |
| 4095 | |
| 4096 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4097 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4098 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4099 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4100 | } |
| 4101 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4102 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 4103 | |
| 4104 | PyObject * |
| 4105 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4106 | Py_ssize_t size, |
| 4107 | const char *errors, |
| 4108 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4109 | { |
| 4110 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 4111 | } |
| 4112 | |
| 4113 | PyObject * |
| 4114 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4115 | Py_ssize_t size, |
| 4116 | const char *errors, |
| 4117 | int *byteorder, |
| 4118 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4119 | { |
| 4120 | const char *starts = s; |
| 4121 | Py_ssize_t startinpos; |
| 4122 | Py_ssize_t endinpos; |
| 4123 | Py_ssize_t outpos; |
| 4124 | PyUnicodeObject *unicode; |
| 4125 | Py_UNICODE *p; |
| 4126 | #ifndef Py_UNICODE_WIDE |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4127 | int pairs = 0; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4128 | const unsigned char *qq; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4129 | #else |
| 4130 | const int pairs = 0; |
| 4131 | #endif |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4132 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4133 | int bo = 0; /* assume native ordering by default */ |
| 4134 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4135 | /* Offsets from q for retrieving bytes in the right order. */ |
| 4136 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4137 | int iorder[] = {0, 1, 2, 3}; |
| 4138 | #else |
| 4139 | int iorder[] = {3, 2, 1, 0}; |
| 4140 | #endif |
| 4141 | PyObject *errorHandler = NULL; |
| 4142 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4143 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4144 | q = (unsigned char *)s; |
| 4145 | e = q + size; |
| 4146 | |
| 4147 | if (byteorder) |
| 4148 | bo = *byteorder; |
| 4149 | |
| 4150 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4151 | byte order setting accordingly. In native mode, the leading BOM |
| 4152 | mark is skipped, in all other modes, it is copied to the output |
| 4153 | stream as-is (giving a ZWNBSP character). */ |
| 4154 | if (bo == 0) { |
| 4155 | if (size >= 4) { |
| 4156 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4157 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4158 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4159 | if (bom == 0x0000FEFF) { |
| 4160 | q += 4; |
| 4161 | bo = -1; |
| 4162 | } |
| 4163 | else if (bom == 0xFFFE0000) { |
| 4164 | q += 4; |
| 4165 | bo = 1; |
| 4166 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4167 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4168 | if (bom == 0x0000FEFF) { |
| 4169 | q += 4; |
| 4170 | bo = 1; |
| 4171 | } |
| 4172 | else if (bom == 0xFFFE0000) { |
| 4173 | q += 4; |
| 4174 | bo = -1; |
| 4175 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4176 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4177 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4178 | } |
| 4179 | |
| 4180 | if (bo == -1) { |
| 4181 | /* force LE */ |
| 4182 | iorder[0] = 0; |
| 4183 | iorder[1] = 1; |
| 4184 | iorder[2] = 2; |
| 4185 | iorder[3] = 3; |
| 4186 | } |
| 4187 | else if (bo == 1) { |
| 4188 | /* force BE */ |
| 4189 | iorder[0] = 3; |
| 4190 | iorder[1] = 2; |
| 4191 | iorder[2] = 1; |
| 4192 | iorder[3] = 0; |
| 4193 | } |
| 4194 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4195 | /* On narrow builds we split characters outside the BMP into two |
| 4196 | codepoints => count how much extra space we need. */ |
| 4197 | #ifndef Py_UNICODE_WIDE |
| 4198 | for (qq = q; qq < e; qq += 4) |
| 4199 | if (qq[iorder[2]] != 0 || qq[iorder[3]] != 0) |
| 4200 | pairs++; |
| 4201 | #endif |
| 4202 | |
| 4203 | /* This might be one to much, because of a BOM */ |
| 4204 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 4205 | if (!unicode) |
| 4206 | return NULL; |
| 4207 | if (size == 0) |
| 4208 | return (PyObject *)unicode; |
| 4209 | |
| 4210 | /* Unpack UTF-32 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4211 | p = PyUnicode_AS_UNICODE(unicode); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4212 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4213 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4214 | Py_UCS4 ch; |
| 4215 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 4216 | if (e-q<4) { |
| 4217 | if (consumed) |
| 4218 | break; |
| 4219 | errmsg = "truncated data"; |
| 4220 | startinpos = ((const char *)q)-starts; |
| 4221 | endinpos = ((const char *)e)-starts; |
| 4222 | goto utf32Error; |
| 4223 | /* The remaining input chars are ignored if the callback |
| 4224 | chooses to skip the input */ |
| 4225 | } |
| 4226 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 4227 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4228 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4229 | if (ch >= 0x110000) |
| 4230 | { |
| 4231 | errmsg = "codepoint not in range(0x110000)"; |
| 4232 | startinpos = ((const char *)q)-starts; |
| 4233 | endinpos = startinpos+4; |
| 4234 | goto utf32Error; |
| 4235 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4236 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4237 | if (ch >= 0x10000) |
| 4238 | { |
| 4239 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 4240 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 4241 | } |
| 4242 | else |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4243 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4244 | *p++ = ch; |
| 4245 | q += 4; |
| 4246 | continue; |
| 4247 | utf32Error: |
| 4248 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 4249 | if (unicode_decode_call_errorhandler( |
| 4250 | errors, &errorHandler, |
| 4251 | "utf32", errmsg, |
| 4252 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
| 4253 | &unicode, &outpos, &p)) |
| 4254 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4255 | } |
| 4256 | |
| 4257 | if (byteorder) |
| 4258 | *byteorder = bo; |
| 4259 | |
| 4260 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4261 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4262 | |
| 4263 | /* Adjust length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4264 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4265 | goto onError; |
| 4266 | |
| 4267 | Py_XDECREF(errorHandler); |
| 4268 | Py_XDECREF(exc); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4269 | if (PyUnicode_READY(unicode) == -1) { |
| 4270 | Py_DECREF(unicode); |
| 4271 | return NULL; |
| 4272 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4273 | return (PyObject *)unicode; |
| 4274 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4275 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4276 | Py_DECREF(unicode); |
| 4277 | Py_XDECREF(errorHandler); |
| 4278 | Py_XDECREF(exc); |
| 4279 | return NULL; |
| 4280 | } |
| 4281 | |
| 4282 | PyObject * |
| 4283 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4284 | Py_ssize_t size, |
| 4285 | const char *errors, |
| 4286 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4287 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4288 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4289 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4290 | Py_ssize_t nsize, bytesize; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4291 | #ifndef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4292 | Py_ssize_t i, pairs; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4293 | #else |
| 4294 | const int pairs = 0; |
| 4295 | #endif |
| 4296 | /* Offsets from p for storing byte pairs in the right order. */ |
| 4297 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4298 | int iorder[] = {0, 1, 2, 3}; |
| 4299 | #else |
| 4300 | int iorder[] = {3, 2, 1, 0}; |
| 4301 | #endif |
| 4302 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4303 | #define STORECHAR(CH) \ |
| 4304 | do { \ |
| 4305 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 4306 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 4307 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 4308 | p[iorder[0]] = (CH) & 0xff; \ |
| 4309 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4310 | } while(0) |
| 4311 | |
| 4312 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 4313 | so we need less space. */ |
| 4314 | #ifndef Py_UNICODE_WIDE |
| 4315 | for (i = pairs = 0; i < size-1; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4316 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 4317 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 4318 | pairs++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4319 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4320 | nsize = (size - pairs + (byteorder == 0)); |
| 4321 | bytesize = nsize * 4; |
| 4322 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4323 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4324 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4325 | if (v == NULL) |
| 4326 | return NULL; |
| 4327 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4328 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4329 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4330 | STORECHAR(0xFEFF); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4331 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4332 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4333 | |
| 4334 | if (byteorder == -1) { |
| 4335 | /* force LE */ |
| 4336 | iorder[0] = 0; |
| 4337 | iorder[1] = 1; |
| 4338 | iorder[2] = 2; |
| 4339 | iorder[3] = 3; |
| 4340 | } |
| 4341 | else if (byteorder == 1) { |
| 4342 | /* force BE */ |
| 4343 | iorder[0] = 3; |
| 4344 | iorder[1] = 2; |
| 4345 | iorder[2] = 1; |
| 4346 | iorder[3] = 0; |
| 4347 | } |
| 4348 | |
| 4349 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4350 | Py_UCS4 ch = *s++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4351 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4352 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 4353 | Py_UCS4 ch2 = *s; |
| 4354 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 4355 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 4356 | s++; |
| 4357 | size--; |
| 4358 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4359 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4360 | #endif |
| 4361 | STORECHAR(ch); |
| 4362 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4363 | |
| 4364 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4365 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4366 | #undef STORECHAR |
| 4367 | } |
| 4368 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4369 | PyObject * |
| 4370 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4371 | { |
| 4372 | if (!PyUnicode_Check(unicode)) { |
| 4373 | PyErr_BadArgument(); |
| 4374 | return NULL; |
| 4375 | } |
| 4376 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4377 | PyUnicode_GET_SIZE(unicode), |
| 4378 | NULL, |
| 4379 | 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4380 | } |
| 4381 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4382 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 4383 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4384 | PyObject * |
| 4385 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4386 | Py_ssize_t size, |
| 4387 | const char *errors, |
| 4388 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4389 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4390 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 4391 | } |
| 4392 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4393 | /* Two masks for fast checking of whether a C 'long' may contain |
| 4394 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 4395 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 4396 | rare in most input. |
| 4397 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 4398 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4399 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4400 | #if (SIZEOF_LONG == 8) |
| 4401 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 4402 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 4403 | #elif (SIZEOF_LONG == 4) |
| 4404 | # define FAST_CHAR_MASK 0x80008000L |
| 4405 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 4406 | #else |
| 4407 | # error C 'long' size should be either 4 or 8! |
| 4408 | #endif |
| 4409 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4410 | PyObject * |
| 4411 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4412 | Py_ssize_t size, |
| 4413 | const char *errors, |
| 4414 | int *byteorder, |
| 4415 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4416 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4417 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4418 | Py_ssize_t startinpos; |
| 4419 | Py_ssize_t endinpos; |
| 4420 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4421 | PyUnicodeObject *unicode; |
| 4422 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4423 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4424 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4425 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4426 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4427 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 4428 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4429 | int ihi = 1, ilo = 0; |
| 4430 | #else |
| 4431 | int ihi = 0, ilo = 1; |
| 4432 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4433 | PyObject *errorHandler = NULL; |
| 4434 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4435 | |
| 4436 | /* Note: size will always be longer than the resulting Unicode |
| 4437 | character count */ |
| 4438 | unicode = _PyUnicode_New(size); |
| 4439 | if (!unicode) |
| 4440 | return NULL; |
| 4441 | if (size == 0) |
| 4442 | return (PyObject *)unicode; |
| 4443 | |
| 4444 | /* Unpack UTF-16 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4445 | p = PyUnicode_AS_UNICODE(unicode); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4446 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4447 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4448 | |
| 4449 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4450 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4451 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 4452 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4453 | byte order setting accordingly. In native mode, the leading BOM |
| 4454 | mark is skipped, in all other modes, it is copied to the output |
| 4455 | stream as-is (giving a ZWNBSP character). */ |
| 4456 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4457 | if (size >= 2) { |
| 4458 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 4459 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4460 | if (bom == 0xFEFF) { |
| 4461 | q += 2; |
| 4462 | bo = -1; |
| 4463 | } |
| 4464 | else if (bom == 0xFFFE) { |
| 4465 | q += 2; |
| 4466 | bo = 1; |
| 4467 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4468 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4469 | if (bom == 0xFEFF) { |
| 4470 | q += 2; |
| 4471 | bo = 1; |
| 4472 | } |
| 4473 | else if (bom == 0xFFFE) { |
| 4474 | q += 2; |
| 4475 | bo = -1; |
| 4476 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 4477 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4478 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 4479 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4480 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4481 | if (bo == -1) { |
| 4482 | /* force LE */ |
| 4483 | ihi = 1; |
| 4484 | ilo = 0; |
| 4485 | } |
| 4486 | else if (bo == 1) { |
| 4487 | /* force BE */ |
| 4488 | ihi = 0; |
| 4489 | ilo = 1; |
| 4490 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4491 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4492 | native_ordering = ilo < ihi; |
| 4493 | #else |
| 4494 | native_ordering = ilo > ihi; |
| 4495 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4496 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4497 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4498 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4499 | Py_UNICODE ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4500 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 4501 | reads are more expensive, better to defer to another iteration. */ |
| 4502 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 4503 | /* Fast path for runs of non-surrogate chars. */ |
| 4504 | register const unsigned char *_q = q; |
| 4505 | Py_UNICODE *_p = p; |
| 4506 | if (native_ordering) { |
| 4507 | /* Native ordering is simple: as long as the input cannot |
| 4508 | possibly contain a surrogate char, do an unrolled copy |
| 4509 | of several 16-bit code points to the target object. |
| 4510 | The non-surrogate check is done on several input bytes |
| 4511 | at a time (as many as a C 'long' can contain). */ |
| 4512 | while (_q < aligned_end) { |
| 4513 | unsigned long data = * (unsigned long *) _q; |
| 4514 | if (data & FAST_CHAR_MASK) |
| 4515 | break; |
| 4516 | _p[0] = ((unsigned short *) _q)[0]; |
| 4517 | _p[1] = ((unsigned short *) _q)[1]; |
| 4518 | #if (SIZEOF_LONG == 8) |
| 4519 | _p[2] = ((unsigned short *) _q)[2]; |
| 4520 | _p[3] = ((unsigned short *) _q)[3]; |
| 4521 | #endif |
| 4522 | _q += SIZEOF_LONG; |
| 4523 | _p += SIZEOF_LONG / 2; |
| 4524 | } |
| 4525 | } |
| 4526 | else { |
| 4527 | /* Byteswapped ordering is similar, but we must decompose |
| 4528 | the copy bytewise, and take care of zero'ing out the |
| 4529 | upper bytes if the target object is in 32-bit units |
| 4530 | (that is, in UCS-4 builds). */ |
| 4531 | while (_q < aligned_end) { |
| 4532 | unsigned long data = * (unsigned long *) _q; |
| 4533 | if (data & SWAPPED_FAST_CHAR_MASK) |
| 4534 | break; |
| 4535 | /* Zero upper bytes in UCS-4 builds */ |
| 4536 | #if (Py_UNICODE_SIZE > 2) |
| 4537 | _p[0] = 0; |
| 4538 | _p[1] = 0; |
| 4539 | #if (SIZEOF_LONG == 8) |
| 4540 | _p[2] = 0; |
| 4541 | _p[3] = 0; |
| 4542 | #endif |
| 4543 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 4544 | /* Issue #4916; UCS-4 builds on big endian machines must |
| 4545 | fill the two last bytes of each 4-byte unit. */ |
| 4546 | #if (!defined(BYTEORDER_IS_LITTLE_ENDIAN) && Py_UNICODE_SIZE > 2) |
| 4547 | # define OFF 2 |
| 4548 | #else |
| 4549 | # define OFF 0 |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4550 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 4551 | ((unsigned char *) _p)[OFF + 1] = _q[0]; |
| 4552 | ((unsigned char *) _p)[OFF + 0] = _q[1]; |
| 4553 | ((unsigned char *) _p)[OFF + 1 + Py_UNICODE_SIZE] = _q[2]; |
| 4554 | ((unsigned char *) _p)[OFF + 0 + Py_UNICODE_SIZE] = _q[3]; |
| 4555 | #if (SIZEOF_LONG == 8) |
| 4556 | ((unsigned char *) _p)[OFF + 1 + 2 * Py_UNICODE_SIZE] = _q[4]; |
| 4557 | ((unsigned char *) _p)[OFF + 0 + 2 * Py_UNICODE_SIZE] = _q[5]; |
| 4558 | ((unsigned char *) _p)[OFF + 1 + 3 * Py_UNICODE_SIZE] = _q[6]; |
| 4559 | ((unsigned char *) _p)[OFF + 0 + 3 * Py_UNICODE_SIZE] = _q[7]; |
| 4560 | #endif |
| 4561 | #undef OFF |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4562 | _q += SIZEOF_LONG; |
| 4563 | _p += SIZEOF_LONG / 2; |
| 4564 | } |
| 4565 | } |
| 4566 | p = _p; |
| 4567 | q = _q; |
| 4568 | if (q >= e) |
| 4569 | break; |
| 4570 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4571 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4572 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4573 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4574 | |
| 4575 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 4576 | *p++ = ch; |
| 4577 | continue; |
| 4578 | } |
| 4579 | |
| 4580 | /* UTF-16 code pair: */ |
| 4581 | if (q > e) { |
| 4582 | errmsg = "unexpected end of data"; |
| 4583 | startinpos = (((const char *)q) - 2) - starts; |
| 4584 | endinpos = ((const char *)e) + 1 - starts; |
| 4585 | goto utf16Error; |
| 4586 | } |
| 4587 | if (0xD800 <= ch && ch <= 0xDBFF) { |
| 4588 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 4589 | q += 2; |
| 4590 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 4591 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4592 | *p++ = ch; |
| 4593 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4594 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4595 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4596 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4597 | continue; |
| 4598 | } |
| 4599 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4600 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4601 | startinpos = (((const char *)q)-4)-starts; |
| 4602 | endinpos = startinpos+2; |
| 4603 | goto utf16Error; |
| 4604 | } |
| 4605 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4606 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4607 | errmsg = "illegal encoding"; |
| 4608 | startinpos = (((const char *)q)-2)-starts; |
| 4609 | endinpos = startinpos+2; |
| 4610 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4611 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4612 | utf16Error: |
| 4613 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 4614 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4615 | errors, |
| 4616 | &errorHandler, |
| 4617 | "utf16", errmsg, |
| 4618 | &starts, |
| 4619 | (const char **)&e, |
| 4620 | &startinpos, |
| 4621 | &endinpos, |
| 4622 | &exc, |
| 4623 | (const char **)&q, |
| 4624 | &unicode, |
| 4625 | &outpos, |
| 4626 | &p)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4627 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4628 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4629 | /* remaining byte at the end? (size should be even) */ |
| 4630 | if (e == q) { |
| 4631 | if (!consumed) { |
| 4632 | errmsg = "truncated data"; |
| 4633 | startinpos = ((const char *)q) - starts; |
| 4634 | endinpos = ((const char *)e) + 1 - starts; |
| 4635 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 4636 | if (unicode_decode_call_errorhandler( |
| 4637 | errors, |
| 4638 | &errorHandler, |
| 4639 | "utf16", errmsg, |
| 4640 | &starts, |
| 4641 | (const char **)&e, |
| 4642 | &startinpos, |
| 4643 | &endinpos, |
| 4644 | &exc, |
| 4645 | (const char **)&q, |
| 4646 | &unicode, |
| 4647 | &outpos, |
| 4648 | &p)) |
| 4649 | goto onError; |
| 4650 | /* The remaining input chars are ignored if the callback |
| 4651 | chooses to skip the input */ |
| 4652 | } |
| 4653 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4654 | |
| 4655 | if (byteorder) |
| 4656 | *byteorder = bo; |
| 4657 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4658 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4659 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4660 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4661 | /* Adjust length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4662 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4663 | goto onError; |
| 4664 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4665 | Py_XDECREF(errorHandler); |
| 4666 | Py_XDECREF(exc); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4667 | if (PyUnicode_READY(unicode) == -1) { |
| 4668 | Py_DECREF(unicode); |
| 4669 | return NULL; |
| 4670 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4671 | return (PyObject *)unicode; |
| 4672 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4673 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4674 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4675 | Py_XDECREF(errorHandler); |
| 4676 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4677 | return NULL; |
| 4678 | } |
| 4679 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4680 | #undef FAST_CHAR_MASK |
| 4681 | #undef SWAPPED_FAST_CHAR_MASK |
| 4682 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4683 | PyObject * |
| 4684 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4685 | Py_ssize_t size, |
| 4686 | const char *errors, |
| 4687 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4688 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4689 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4690 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4691 | Py_ssize_t nsize, bytesize; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4692 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4693 | Py_ssize_t i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4694 | #else |
| 4695 | const int pairs = 0; |
| 4696 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4697 | /* Offsets from p for storing byte pairs in the right order. */ |
| 4698 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4699 | int ihi = 1, ilo = 0; |
| 4700 | #else |
| 4701 | int ihi = 0, ilo = 1; |
| 4702 | #endif |
| 4703 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4704 | #define STORECHAR(CH) \ |
| 4705 | do { \ |
| 4706 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 4707 | p[ilo] = (CH) & 0xff; \ |
| 4708 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4709 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4710 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4711 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4712 | for (i = pairs = 0; i < size; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4713 | if (s[i] >= 0x10000) |
| 4714 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4715 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4716 | /* 2 * (size + pairs + (byteorder == 0)) */ |
| 4717 | if (size > PY_SSIZE_T_MAX || |
| 4718 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4719 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4720 | nsize = size + pairs + (byteorder == 0); |
| 4721 | bytesize = nsize * 2; |
| 4722 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4723 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4724 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4725 | if (v == NULL) |
| 4726 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4727 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4728 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4729 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4730 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 4731 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4732 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4733 | |
| 4734 | if (byteorder == -1) { |
| 4735 | /* force LE */ |
| 4736 | ihi = 1; |
| 4737 | ilo = 0; |
| 4738 | } |
| 4739 | else if (byteorder == 1) { |
| 4740 | /* force BE */ |
| 4741 | ihi = 0; |
| 4742 | ilo = 1; |
| 4743 | } |
| 4744 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4745 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4746 | Py_UNICODE ch = *s++; |
| 4747 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4748 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4749 | if (ch >= 0x10000) { |
| 4750 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 4751 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 4752 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4753 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4754 | STORECHAR(ch); |
| 4755 | if (ch2) |
| 4756 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4757 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4758 | |
| 4759 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4760 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4761 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4762 | } |
| 4763 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4764 | PyObject * |
| 4765 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4766 | { |
| 4767 | if (!PyUnicode_Check(unicode)) { |
| 4768 | PyErr_BadArgument(); |
| 4769 | return NULL; |
| 4770 | } |
| 4771 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4772 | PyUnicode_GET_SIZE(unicode), |
| 4773 | NULL, |
| 4774 | 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4775 | } |
| 4776 | |
| 4777 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 4778 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4779 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 4780 | if all the escapes in the string make it still a valid ASCII string. |
| 4781 | Returns -1 if any escapes were found which cause the string to |
| 4782 | pop out of ASCII range. Otherwise returns the length of the |
| 4783 | required buffer to hold the string. |
| 4784 | */ |
| 4785 | Py_ssize_t |
| 4786 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 4787 | { |
| 4788 | const unsigned char *p = (const unsigned char *)s; |
| 4789 | const unsigned char *end = p + size; |
| 4790 | Py_ssize_t length = 0; |
| 4791 | |
| 4792 | if (size < 0) |
| 4793 | return -1; |
| 4794 | |
| 4795 | for (; p < end; ++p) { |
| 4796 | if (*p > 127) { |
| 4797 | /* Non-ASCII */ |
| 4798 | return -1; |
| 4799 | } |
| 4800 | else if (*p != '\\') { |
| 4801 | /* Normal character */ |
| 4802 | ++length; |
| 4803 | } |
| 4804 | else { |
| 4805 | /* Backslash-escape, check next char */ |
| 4806 | ++p; |
| 4807 | /* Escape sequence reaches till end of string or |
| 4808 | non-ASCII follow-up. */ |
| 4809 | if (p >= end || *p > 127) |
| 4810 | return -1; |
| 4811 | switch (*p) { |
| 4812 | case '\n': |
| 4813 | /* backslash + \n result in zero characters */ |
| 4814 | break; |
| 4815 | case '\\': case '\'': case '\"': |
| 4816 | case 'b': case 'f': case 't': |
| 4817 | case 'n': case 'r': case 'v': case 'a': |
| 4818 | ++length; |
| 4819 | break; |
| 4820 | case '0': case '1': case '2': case '3': |
| 4821 | case '4': case '5': case '6': case '7': |
| 4822 | case 'x': case 'u': case 'U': case 'N': |
| 4823 | /* these do not guarantee ASCII characters */ |
| 4824 | return -1; |
| 4825 | default: |
| 4826 | /* count the backslash + the other character */ |
| 4827 | length += 2; |
| 4828 | } |
| 4829 | } |
| 4830 | } |
| 4831 | return length; |
| 4832 | } |
| 4833 | |
| 4834 | /* Similar to PyUnicode_WRITE but either write into wstr field |
| 4835 | or treat string as ASCII. */ |
| 4836 | #define WRITE_ASCII_OR_WSTR(kind, buf, index, value) \ |
| 4837 | do { \ |
| 4838 | if ((kind) != PyUnicode_WCHAR_KIND) \ |
| 4839 | ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \ |
| 4840 | else \ |
| 4841 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \ |
| 4842 | } while (0) |
| 4843 | |
| 4844 | #define WRITE_WSTR(buf, index, value) \ |
| 4845 | assert(kind == PyUnicode_WCHAR_KIND), \ |
| 4846 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value) |
| 4847 | |
| 4848 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 4849 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 4850 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4851 | PyObject * |
| 4852 | PyUnicode_DecodeUnicodeEscape(const char *s, |
| 4853 | Py_ssize_t size, |
| 4854 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4855 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4856 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4857 | Py_ssize_t startinpos; |
| 4858 | Py_ssize_t endinpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4859 | int j; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4860 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4861 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4862 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 4863 | char* message; |
| 4864 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4865 | PyObject *errorHandler = NULL; |
| 4866 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4867 | Py_ssize_t ascii_length; |
| 4868 | Py_ssize_t i; |
| 4869 | int kind; |
| 4870 | void *data; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 4871 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4872 | ascii_length = length_of_escaped_ascii_string(s, size); |
| 4873 | |
| 4874 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 4875 | either the string is pure ASCII with named escapes like \n, etc. |
| 4876 | and we determined it's exact size (common case) |
| 4877 | or it contains \x, \u, ... escape sequences. then we create a |
| 4878 | legacy wchar string and resize it at the end of this function. */ |
| 4879 | if (ascii_length >= 0) { |
| 4880 | v = (PyUnicodeObject *)PyUnicode_New(ascii_length, 127); |
| 4881 | if (!v) |
| 4882 | goto onError; |
| 4883 | assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND); |
| 4884 | kind = PyUnicode_1BYTE_KIND; |
| 4885 | data = PyUnicode_DATA(v); |
| 4886 | } |
| 4887 | else { |
| 4888 | /* Escaped strings will always be longer than the resulting |
| 4889 | Unicode string, so we start with size here and then reduce the |
| 4890 | length after conversion to the true value. |
| 4891 | (but if the error callback returns a long replacement string |
| 4892 | we'll have to allocate more space) */ |
| 4893 | v = _PyUnicode_New(size); |
| 4894 | if (!v) |
| 4895 | goto onError; |
| 4896 | kind = PyUnicode_WCHAR_KIND; |
| 4897 | data = PyUnicode_AS_UNICODE(v); |
| 4898 | } |
| 4899 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4900 | if (size == 0) |
| 4901 | return (PyObject *)v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4902 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4903 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 4904 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4905 | while (s < end) { |
| 4906 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 4907 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4908 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4909 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4910 | if (kind == PyUnicode_WCHAR_KIND) { |
| 4911 | assert(i < _PyUnicode_WSTR_LENGTH(v)); |
| 4912 | } |
| 4913 | else { |
| 4914 | /* The only case in which i == ascii_length is a backslash |
| 4915 | followed by a newline. */ |
| 4916 | assert(i <= ascii_length); |
| 4917 | } |
| 4918 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4919 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 4920 | if (*s != '\\') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4921 | WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char) *s++); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4922 | continue; |
| 4923 | } |
| 4924 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4925 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4926 | /* \ - Escapes */ |
| 4927 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 4928 | c = *s++; |
| 4929 | if (s > end) |
| 4930 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4931 | |
| 4932 | if (kind == PyUnicode_WCHAR_KIND) { |
| 4933 | assert(i < _PyUnicode_WSTR_LENGTH(v)); |
| 4934 | } |
| 4935 | else { |
| 4936 | /* The only case in which i == ascii_length is a backslash |
| 4937 | followed by a newline. */ |
| 4938 | assert(i < ascii_length || (i == ascii_length && c == '\n')); |
| 4939 | } |
| 4940 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 4941 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4942 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4943 | /* \x escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4944 | case '\n': break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4945 | case '\\': WRITE_ASCII_OR_WSTR(kind, data, i++, '\\'); break; |
| 4946 | case '\'': WRITE_ASCII_OR_WSTR(kind, data, i++, '\''); break; |
| 4947 | case '\"': WRITE_ASCII_OR_WSTR(kind, data, i++, '\"'); break; |
| 4948 | case 'b': WRITE_ASCII_OR_WSTR(kind, data, i++, '\b'); break; |
| 4949 | /* FF */ |
| 4950 | case 'f': WRITE_ASCII_OR_WSTR(kind, data, i++, '\014'); break; |
| 4951 | case 't': WRITE_ASCII_OR_WSTR(kind, data, i++, '\t'); break; |
| 4952 | case 'n': WRITE_ASCII_OR_WSTR(kind, data, i++, '\n'); break; |
| 4953 | case 'r': WRITE_ASCII_OR_WSTR(kind, data, i++, '\r'); break; |
| 4954 | /* VT */ |
| 4955 | case 'v': WRITE_ASCII_OR_WSTR(kind, data, i++, '\013'); break; |
| 4956 | /* BEL, not classic C */ |
| 4957 | case 'a': WRITE_ASCII_OR_WSTR(kind, data, i++, '\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4958 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4959 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4960 | case '0': case '1': case '2': case '3': |
| 4961 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 4962 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 4963 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 4964 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 4965 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 4966 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4967 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4968 | WRITE_WSTR(data, i++, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4969 | break; |
| 4970 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4971 | /* hex escapes */ |
| 4972 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4973 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 4974 | digits = 2; |
| 4975 | message = "truncated \\xXX escape"; |
| 4976 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4977 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4978 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4979 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 4980 | digits = 4; |
| 4981 | message = "truncated \\uXXXX escape"; |
| 4982 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4983 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4984 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 4985 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 4986 | digits = 8; |
| 4987 | message = "truncated \\UXXXXXXXX escape"; |
| 4988 | hexescape: |
| 4989 | chr = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4990 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4991 | if (s+digits>end) { |
| 4992 | endinpos = size; |
| 4993 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4994 | errors, &errorHandler, |
| 4995 | "unicodeescape", "end of string in escape sequence", |
| 4996 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4997 | &v, &i, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4998 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4999 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5000 | goto nextByte; |
| 5001 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5002 | for (j = 0; j < digits; ++j) { |
| 5003 | c = (unsigned char) s[j]; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5004 | if (!Py_ISXDIGIT(c)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5005 | endinpos = (s+j+1)-starts; |
| 5006 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5007 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5008 | errors, &errorHandler, |
| 5009 | "unicodeescape", message, |
| 5010 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5011 | &v, &i, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5012 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5013 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5014 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5015 | } |
| 5016 | chr = (chr<<4) & ~0xF; |
| 5017 | if (c >= '0' && c <= '9') |
| 5018 | chr += c - '0'; |
| 5019 | else if (c >= 'a' && c <= 'f') |
| 5020 | chr += 10 + c - 'a'; |
| 5021 | else |
| 5022 | chr += 10 + c - 'A'; |
| 5023 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5024 | s += j; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5025 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5026 | /* _decoding_error will have already written into the |
| 5027 | target buffer. */ |
| 5028 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5029 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5030 | /* when we get here, chr is a 32-bit unicode character */ |
| 5031 | if (chr <= 0xffff) |
| 5032 | /* UCS-2 character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5033 | WRITE_WSTR(data, i++, chr); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5034 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5035 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5036 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 5037 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5038 | WRITE_WSTR(data, i++, chr); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5039 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5040 | chr -= 0x10000L; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5041 | WRITE_WSTR(data, i++, 0xD800 + (Py_UNICODE) (chr >> 10)); |
| 5042 | WRITE_WSTR(data, i++, 0xDC00 + (Py_UNICODE) (chr & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5043 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5044 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5045 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5046 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5047 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5048 | errors, &errorHandler, |
| 5049 | "unicodeescape", "illegal Unicode character", |
| 5050 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5051 | &v, &i, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5052 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5053 | data = PyUnicode_AS_UNICODE(v); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5054 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5055 | break; |
| 5056 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5057 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5058 | case 'N': |
| 5059 | message = "malformed \\N character escape"; |
| 5060 | if (ucnhash_CAPI == NULL) { |
| 5061 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5062 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5063 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5064 | if (ucnhash_CAPI == NULL) |
| 5065 | goto ucnhashError; |
| 5066 | } |
| 5067 | if (*s == '{') { |
| 5068 | const char *start = s+1; |
| 5069 | /* look for the closing brace */ |
| 5070 | while (*s != '}' && s < end) |
| 5071 | s++; |
| 5072 | if (s > start && s < end && *s == '}') { |
| 5073 | /* found a name. look it up in the unicode database */ |
| 5074 | message = "unknown Unicode character name"; |
| 5075 | s++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5076 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
| 5077 | &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5078 | goto store; |
| 5079 | } |
| 5080 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5081 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5082 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5083 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5084 | errors, &errorHandler, |
| 5085 | "unicodeescape", message, |
| 5086 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5087 | &v, &i, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5088 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5089 | data = PyUnicode_AS_UNICODE(v); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5090 | break; |
| 5091 | |
| 5092 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5093 | if (s > end) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5094 | assert(kind == PyUnicode_WCHAR_KIND); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5095 | message = "\\ at end of string"; |
| 5096 | s--; |
| 5097 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5098 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5099 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5100 | errors, &errorHandler, |
| 5101 | "unicodeescape", message, |
| 5102 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5103 | &v, &i, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5104 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5105 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5106 | } |
| 5107 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5108 | WRITE_ASCII_OR_WSTR(kind, data, i++, '\\'); |
| 5109 | WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char)s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5110 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5111 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5112 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5113 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5114 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5115 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5116 | /* Ensure the length prediction worked in case of ASCII strings */ |
| 5117 | assert(kind == PyUnicode_WCHAR_KIND || i == ascii_length); |
| 5118 | |
| 5119 | if (kind == PyUnicode_WCHAR_KIND && (_PyUnicode_Resize(&v, i) < 0 || |
| 5120 | PyUnicode_READY(v) == -1)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5121 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5122 | Py_XDECREF(errorHandler); |
| 5123 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5124 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5125 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5126 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5127 | PyErr_SetString( |
| 5128 | PyExc_UnicodeError, |
| 5129 | "\\N escapes not supported (can't load unicodedata module)" |
| 5130 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5131 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5132 | Py_XDECREF(errorHandler); |
| 5133 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5134 | return NULL; |
| 5135 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5136 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5137 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5138 | Py_XDECREF(errorHandler); |
| 5139 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5140 | return NULL; |
| 5141 | } |
| 5142 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5143 | #undef WRITE_ASCII_OR_WSTR |
| 5144 | #undef WRITE_WSTR |
| 5145 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5146 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5147 | |
| 5148 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5149 | appropriate. |
| 5150 | |
| 5151 | */ |
| 5152 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5153 | static const char *hexdigits = "0123456789abcdef"; |
| 5154 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5155 | PyObject * |
| 5156 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 5157 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5158 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5159 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5160 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5161 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5162 | #ifdef Py_UNICODE_WIDE |
| 5163 | const Py_ssize_t expandsize = 10; |
| 5164 | #else |
| 5165 | const Py_ssize_t expandsize = 6; |
| 5166 | #endif |
| 5167 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5168 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 5169 | better to choose a different scheme. Perhaps scan the |
| 5170 | first N-chars of the string and allocate based on that size. |
| 5171 | */ |
| 5172 | /* Initial allocation is based on the longest-possible unichr |
| 5173 | escape. |
| 5174 | |
| 5175 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 5176 | unichr, so in this case it's the longest unichr escape. In |
| 5177 | narrow (UTF-16) builds this is five chars per source unichr |
| 5178 | since there are two unichrs in the surrogate pair, so in narrow |
| 5179 | (UTF-16) builds it's not the longest unichr escape. |
| 5180 | |
| 5181 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 5182 | so in the narrow (UTF-16) build case it's the longest unichr |
| 5183 | escape. |
| 5184 | */ |
| 5185 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5186 | if (size == 0) |
| 5187 | return PyBytes_FromStringAndSize(NULL, 0); |
| 5188 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5189 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5190 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5191 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5192 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5193 | 2 |
| 5194 | + expandsize*size |
| 5195 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5196 | if (repr == NULL) |
| 5197 | return NULL; |
| 5198 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5199 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5200 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5201 | while (size-- > 0) { |
| 5202 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5203 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5204 | /* Escape backslashes */ |
| 5205 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5206 | *p++ = '\\'; |
| 5207 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5208 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5209 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5210 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 5211 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5212 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 5213 | else if (ch >= 0x10000) { |
| 5214 | *p++ = '\\'; |
| 5215 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5216 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 5217 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 5218 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 5219 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 5220 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 5221 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 5222 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 5223 | *p++ = hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5224 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5225 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5226 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5227 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 5228 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 5229 | Py_UNICODE ch2; |
| 5230 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5231 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5232 | ch2 = *s++; |
| 5233 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 5234 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5235 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 5236 | *p++ = '\\'; |
| 5237 | *p++ = 'U'; |
| 5238 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 5239 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 5240 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 5241 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 5242 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 5243 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 5244 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 5245 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 5246 | continue; |
| 5247 | } |
| 5248 | /* Fall through: isolated surrogates are copied as-is */ |
| 5249 | s--; |
| 5250 | size++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5251 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5252 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5253 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5254 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5255 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5256 | *p++ = '\\'; |
| 5257 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5258 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 5259 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 5260 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 5261 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5262 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5263 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5264 | /* Map special whitespace to '\t', \n', '\r' */ |
| 5265 | else if (ch == '\t') { |
| 5266 | *p++ = '\\'; |
| 5267 | *p++ = 't'; |
| 5268 | } |
| 5269 | else if (ch == '\n') { |
| 5270 | *p++ = '\\'; |
| 5271 | *p++ = 'n'; |
| 5272 | } |
| 5273 | else if (ch == '\r') { |
| 5274 | *p++ = '\\'; |
| 5275 | *p++ = 'r'; |
| 5276 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5277 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5278 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 5279 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5280 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5281 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5282 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 5283 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5284 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5285 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5286 | /* Copy everything else as-is */ |
| 5287 | else |
| 5288 | *p++ = (char) ch; |
| 5289 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5290 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5291 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 5292 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 5293 | return NULL; |
| 5294 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5295 | } |
| 5296 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5297 | PyObject * |
| 5298 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5299 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 5300 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5301 | if (!PyUnicode_Check(unicode)) { |
| 5302 | PyErr_BadArgument(); |
| 5303 | return NULL; |
| 5304 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5305 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 5306 | PyUnicode_GET_SIZE(unicode)); |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 5307 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5308 | } |
| 5309 | |
| 5310 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 5311 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5312 | PyObject * |
| 5313 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
| 5314 | Py_ssize_t size, |
| 5315 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5316 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5317 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5318 | Py_ssize_t startinpos; |
| 5319 | Py_ssize_t endinpos; |
| 5320 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5321 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5322 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5323 | const char *end; |
| 5324 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5325 | PyObject *errorHandler = NULL; |
| 5326 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5327 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5328 | /* Escaped strings will always be longer than the resulting |
| 5329 | 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] | 5330 | length after conversion to the true value. (But decoding error |
| 5331 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5332 | v = _PyUnicode_New(size); |
| 5333 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5334 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5335 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5336 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5337 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5338 | end = s + size; |
| 5339 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5340 | unsigned char c; |
| 5341 | Py_UCS4 x; |
| 5342 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5343 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5344 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5345 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5346 | if (*s != '\\') { |
| 5347 | *p++ = (unsigned char)*s++; |
| 5348 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5349 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5350 | startinpos = s-starts; |
| 5351 | |
| 5352 | /* \u-escapes are only interpreted iff the number of leading |
| 5353 | backslashes if odd */ |
| 5354 | bs = s; |
| 5355 | for (;s < end;) { |
| 5356 | if (*s != '\\') |
| 5357 | break; |
| 5358 | *p++ = (unsigned char)*s++; |
| 5359 | } |
| 5360 | if (((s - bs) & 1) == 0 || |
| 5361 | s >= end || |
| 5362 | (*s != 'u' && *s != 'U')) { |
| 5363 | continue; |
| 5364 | } |
| 5365 | p--; |
| 5366 | count = *s=='u' ? 4 : 8; |
| 5367 | s++; |
| 5368 | |
| 5369 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
| 5370 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 5371 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 5372 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5373 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5374 | endinpos = s-starts; |
| 5375 | if (unicode_decode_call_errorhandler( |
| 5376 | errors, &errorHandler, |
| 5377 | "rawunicodeescape", "truncated \\uXXXX", |
| 5378 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 5379 | &v, &outpos, &p)) |
| 5380 | goto onError; |
| 5381 | goto nextByte; |
| 5382 | } |
| 5383 | x = (x<<4) & ~0xF; |
| 5384 | if (c >= '0' && c <= '9') |
| 5385 | x += c - '0'; |
| 5386 | else if (c >= 'a' && c <= 'f') |
| 5387 | x += 10 + c - 'a'; |
| 5388 | else |
| 5389 | x += 10 + c - 'A'; |
| 5390 | } |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5391 | if (x <= 0xffff) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5392 | /* UCS-2 character */ |
| 5393 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5394 | else if (x <= 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5395 | /* UCS-4 character. Either store directly, or as |
| 5396 | surrogate pair. */ |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5397 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5398 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5399 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5400 | x -= 0x10000L; |
| 5401 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 5402 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5403 | #endif |
| 5404 | } else { |
| 5405 | endinpos = s-starts; |
| 5406 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5407 | if (unicode_decode_call_errorhandler( |
| 5408 | errors, &errorHandler, |
| 5409 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5410 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 5411 | &v, &outpos, &p)) |
| 5412 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5413 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5414 | nextByte: |
| 5415 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5416 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5417 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5418 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5419 | Py_XDECREF(errorHandler); |
| 5420 | Py_XDECREF(exc); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5421 | if (PyUnicode_READY(v) == -1) { |
| 5422 | Py_DECREF(v); |
| 5423 | return NULL; |
| 5424 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5425 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5426 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5427 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5428 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5429 | Py_XDECREF(errorHandler); |
| 5430 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5431 | return NULL; |
| 5432 | } |
| 5433 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5434 | PyObject * |
| 5435 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 5436 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5437 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5438 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5439 | char *p; |
| 5440 | char *q; |
| 5441 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5442 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5443 | const Py_ssize_t expandsize = 10; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5444 | #else |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5445 | const Py_ssize_t expandsize = 6; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5446 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5447 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5448 | if (size > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5449 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5450 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5451 | repr = PyBytes_FromStringAndSize(NULL, expandsize * size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5452 | if (repr == NULL) |
| 5453 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 5454 | if (size == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5455 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5456 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5457 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5458 | while (size-- > 0) { |
| 5459 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5460 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5461 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 5462 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5463 | *p++ = '\\'; |
| 5464 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 5465 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 5466 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 5467 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 5468 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 5469 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 5470 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 5471 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 5472 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5473 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5474 | else |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5475 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5476 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 5477 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 5478 | Py_UNICODE ch2; |
| 5479 | Py_UCS4 ucs; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5480 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5481 | ch2 = *s++; |
| 5482 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 5483 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5484 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 5485 | *p++ = '\\'; |
| 5486 | *p++ = 'U'; |
| 5487 | *p++ = hexdigits[(ucs >> 28) & 0xf]; |
| 5488 | *p++ = hexdigits[(ucs >> 24) & 0xf]; |
| 5489 | *p++ = hexdigits[(ucs >> 20) & 0xf]; |
| 5490 | *p++ = hexdigits[(ucs >> 16) & 0xf]; |
| 5491 | *p++ = hexdigits[(ucs >> 12) & 0xf]; |
| 5492 | *p++ = hexdigits[(ucs >> 8) & 0xf]; |
| 5493 | *p++ = hexdigits[(ucs >> 4) & 0xf]; |
| 5494 | *p++ = hexdigits[ucs & 0xf]; |
| 5495 | continue; |
| 5496 | } |
| 5497 | /* Fall through: isolated surrogates are copied as-is */ |
| 5498 | s--; |
| 5499 | size++; |
| 5500 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5501 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5502 | /* Map 16-bit characters to '\uxxxx' */ |
| 5503 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5504 | *p++ = '\\'; |
| 5505 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 5506 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 5507 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 5508 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 5509 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5510 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5511 | /* Copy everything else as-is */ |
| 5512 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5513 | *p++ = (char) ch; |
| 5514 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5515 | size = p - q; |
| 5516 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5517 | assert(size > 0); |
| 5518 | if (_PyBytes_Resize(&repr, size) < 0) |
| 5519 | return NULL; |
| 5520 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5521 | } |
| 5522 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5523 | PyObject * |
| 5524 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5525 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 5526 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5527 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 5528 | PyErr_BadArgument(); |
| 5529 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5530 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 5531 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 5532 | PyUnicode_GET_SIZE(unicode)); |
| 5533 | |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 5534 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5535 | } |
| 5536 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5537 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 5538 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5539 | PyObject * |
| 5540 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
| 5541 | Py_ssize_t size, |
| 5542 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5543 | { |
| 5544 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5545 | Py_ssize_t startinpos; |
| 5546 | Py_ssize_t endinpos; |
| 5547 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5548 | PyUnicodeObject *v; |
| 5549 | Py_UNICODE *p; |
| 5550 | const char *end; |
| 5551 | const char *reason; |
| 5552 | PyObject *errorHandler = NULL; |
| 5553 | PyObject *exc = NULL; |
| 5554 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 5555 | #ifdef Py_UNICODE_WIDE |
| 5556 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 5557 | #endif |
| 5558 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5559 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5560 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 5561 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5562 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5563 | /* Intentionally PyUnicode_GET_SIZE instead of PyUnicode_GET_LENGTH |
| 5564 | as string was created with the old API. */ |
| 5565 | if (PyUnicode_GET_SIZE(v) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5566 | return (PyObject *)v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5567 | p = PyUnicode_AS_UNICODE(v); |
| 5568 | end = s + size; |
| 5569 | |
| 5570 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5571 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5572 | /* We have to sanity check the raw data, otherwise doom looms for |
| 5573 | some malformed UCS-4 data. */ |
| 5574 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5575 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5576 | *p > unimax || *p < 0 || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5577 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5578 | end-s < Py_UNICODE_SIZE |
| 5579 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5580 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5581 | startinpos = s - starts; |
| 5582 | if (end-s < Py_UNICODE_SIZE) { |
| 5583 | endinpos = end-starts; |
| 5584 | reason = "truncated input"; |
| 5585 | } |
| 5586 | else { |
| 5587 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 5588 | reason = "illegal code point (> 0x10FFFF)"; |
| 5589 | } |
| 5590 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 5591 | if (unicode_decode_call_errorhandler( |
| 5592 | errors, &errorHandler, |
| 5593 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 5594 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 5595 | &v, &outpos, &p)) { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5596 | goto onError; |
| 5597 | } |
| 5598 | } |
| 5599 | else { |
| 5600 | p++; |
| 5601 | s += Py_UNICODE_SIZE; |
| 5602 | } |
| 5603 | } |
| 5604 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5605 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5606 | goto onError; |
| 5607 | Py_XDECREF(errorHandler); |
| 5608 | Py_XDECREF(exc); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5609 | if (PyUnicode_READY(v) == -1) { |
| 5610 | Py_DECREF(v); |
| 5611 | return NULL; |
| 5612 | } |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5613 | return (PyObject *)v; |
| 5614 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5615 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5616 | Py_XDECREF(v); |
| 5617 | Py_XDECREF(errorHandler); |
| 5618 | Py_XDECREF(exc); |
| 5619 | return NULL; |
| 5620 | } |
| 5621 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5622 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 5623 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5624 | PyObject * |
| 5625 | PyUnicode_DecodeLatin1(const char *s, |
| 5626 | Py_ssize_t size, |
| 5627 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5628 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5629 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5630 | return PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5631 | } |
| 5632 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5633 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5634 | static void |
| 5635 | make_encode_exception(PyObject **exceptionObject, |
| 5636 | const char *encoding, |
| 5637 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5638 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5639 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5640 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5641 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5642 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 5643 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5644 | } |
| 5645 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5646 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 5647 | goto onError; |
| 5648 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 5649 | goto onError; |
| 5650 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 5651 | goto onError; |
| 5652 | return; |
| 5653 | onError: |
| 5654 | Py_DECREF(*exceptionObject); |
| 5655 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5656 | } |
| 5657 | } |
| 5658 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5659 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5660 | static void |
| 5661 | raise_encode_exception(PyObject **exceptionObject, |
| 5662 | const char *encoding, |
| 5663 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5664 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5665 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5666 | { |
| 5667 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5668 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5669 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5670 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5671 | } |
| 5672 | |
| 5673 | /* error handling callback helper: |
| 5674 | build arguments, call the callback and check the arguments, |
| 5675 | put the result into newpos and return the replacement string, which |
| 5676 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5677 | static PyObject * |
| 5678 | unicode_encode_call_errorhandler(const char *errors, |
| 5679 | PyObject **errorHandler, |
| 5680 | const char *encoding, const char *reason, |
| 5681 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 5682 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5683 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5684 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5685 | 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] | 5686 | |
| 5687 | PyObject *restuple; |
| 5688 | PyObject *resunicode; |
| 5689 | |
| 5690 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5691 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5692 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5693 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5694 | } |
| 5695 | |
| 5696 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5697 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5698 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5699 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5700 | |
| 5701 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5702 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5703 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5704 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5705 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5706 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5707 | Py_DECREF(restuple); |
| 5708 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5709 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5710 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5711 | &resunicode, newpos)) { |
| 5712 | Py_DECREF(restuple); |
| 5713 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5714 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5715 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 5716 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 5717 | Py_DECREF(restuple); |
| 5718 | return NULL; |
| 5719 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5720 | if (*newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5721 | *newpos = size+*newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5722 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5723 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 5724 | Py_DECREF(restuple); |
| 5725 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5726 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5727 | Py_INCREF(resunicode); |
| 5728 | Py_DECREF(restuple); |
| 5729 | return resunicode; |
| 5730 | } |
| 5731 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5732 | static PyObject * |
| 5733 | unicode_encode_ucs1(const Py_UNICODE *p, |
| 5734 | Py_ssize_t size, |
| 5735 | const char *errors, |
| 5736 | int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5737 | { |
| 5738 | /* output object */ |
| 5739 | PyObject *res; |
| 5740 | /* pointers to the beginning and end+1 of input */ |
| 5741 | const Py_UNICODE *startp = p; |
| 5742 | const Py_UNICODE *endp = p + size; |
| 5743 | /* pointer to the beginning of the unencodable characters */ |
| 5744 | /* const Py_UNICODE *badp = NULL; */ |
| 5745 | /* pointer into the output */ |
| 5746 | char *str; |
| 5747 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5748 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5749 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 5750 | 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] | 5751 | PyObject *errorHandler = NULL; |
| 5752 | PyObject *exc = NULL; |
| 5753 | /* the following variable is used for caching string comparisons |
| 5754 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 5755 | int known_errorHandler = -1; |
| 5756 | |
| 5757 | /* allocate enough for a simple encoding without |
| 5758 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5759 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5760 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5761 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5762 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5763 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5764 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5765 | ressize = size; |
| 5766 | |
| 5767 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5768 | Py_UNICODE c = *p; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5769 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5770 | /* can we encode this? */ |
| 5771 | if (c<limit) { |
| 5772 | /* no overflow check, because we know that the space is enough */ |
| 5773 | *str++ = (char)c; |
| 5774 | ++p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5775 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5776 | else { |
| 5777 | Py_ssize_t unicodepos = p-startp; |
| 5778 | Py_ssize_t requiredsize; |
| 5779 | PyObject *repunicode; |
| 5780 | Py_ssize_t repsize; |
| 5781 | Py_ssize_t newpos; |
| 5782 | Py_ssize_t respos; |
| 5783 | Py_UNICODE *uni2; |
| 5784 | /* startpos for collecting unencodable chars */ |
| 5785 | const Py_UNICODE *collstart = p; |
| 5786 | const Py_UNICODE *collend = p; |
| 5787 | /* find all unecodable characters */ |
| 5788 | while ((collend < endp) && ((*collend)>=limit)) |
| 5789 | ++collend; |
| 5790 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 5791 | if (known_errorHandler==-1) { |
| 5792 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5793 | known_errorHandler = 1; |
| 5794 | else if (!strcmp(errors, "replace")) |
| 5795 | known_errorHandler = 2; |
| 5796 | else if (!strcmp(errors, "ignore")) |
| 5797 | known_errorHandler = 3; |
| 5798 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5799 | known_errorHandler = 4; |
| 5800 | else |
| 5801 | known_errorHandler = 0; |
| 5802 | } |
| 5803 | switch (known_errorHandler) { |
| 5804 | case 1: /* strict */ |
| 5805 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 5806 | goto onError; |
| 5807 | case 2: /* replace */ |
| 5808 | while (collstart++<collend) |
| 5809 | *str++ = '?'; /* fall through */ |
| 5810 | case 3: /* ignore */ |
| 5811 | p = collend; |
| 5812 | break; |
| 5813 | case 4: /* xmlcharrefreplace */ |
| 5814 | respos = str - PyBytes_AS_STRING(res); |
| 5815 | /* determine replacement size (temporarily (mis)uses p) */ |
| 5816 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 5817 | if (*p<10) |
| 5818 | repsize += 2+1+1; |
| 5819 | else if (*p<100) |
| 5820 | repsize += 2+2+1; |
| 5821 | else if (*p<1000) |
| 5822 | repsize += 2+3+1; |
| 5823 | else if (*p<10000) |
| 5824 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 5825 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5826 | else |
| 5827 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 5828 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5829 | else if (*p<100000) |
| 5830 | repsize += 2+5+1; |
| 5831 | else if (*p<1000000) |
| 5832 | repsize += 2+6+1; |
| 5833 | else |
| 5834 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5835 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5836 | } |
| 5837 | requiredsize = respos+repsize+(endp-collend); |
| 5838 | if (requiredsize > ressize) { |
| 5839 | if (requiredsize<2*ressize) |
| 5840 | requiredsize = 2*ressize; |
| 5841 | if (_PyBytes_Resize(&res, requiredsize)) |
| 5842 | goto onError; |
| 5843 | str = PyBytes_AS_STRING(res) + respos; |
| 5844 | ressize = requiredsize; |
| 5845 | } |
| 5846 | /* generate replacement (temporarily (mis)uses p) */ |
| 5847 | for (p = collstart; p < collend; ++p) { |
| 5848 | str += sprintf(str, "&#%d;", (int)*p); |
| 5849 | } |
| 5850 | p = collend; |
| 5851 | break; |
| 5852 | default: |
| 5853 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 5854 | encoding, reason, startp, size, &exc, |
| 5855 | collstart-startp, collend-startp, &newpos); |
| 5856 | if (repunicode == NULL) |
| 5857 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5858 | if (PyBytes_Check(repunicode)) { |
| 5859 | /* Directly copy bytes result to output. */ |
| 5860 | repsize = PyBytes_Size(repunicode); |
| 5861 | if (repsize > 1) { |
| 5862 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 5863 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5864 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 5865 | Py_DECREF(repunicode); |
| 5866 | goto onError; |
| 5867 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 5868 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5869 | ressize += repsize-1; |
| 5870 | } |
| 5871 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 5872 | str += repsize; |
| 5873 | p = startp + newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5874 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5875 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5876 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5877 | /* need more space? (at least enough for what we |
| 5878 | have+the replacement+the rest of the string, so |
| 5879 | we won't have to check space for encodable characters) */ |
| 5880 | respos = str - PyBytes_AS_STRING(res); |
| 5881 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 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 | Py_DECREF(repunicode); |
| 5888 | goto onError; |
| 5889 | } |
| 5890 | str = PyBytes_AS_STRING(res) + respos; |
| 5891 | ressize = requiredsize; |
| 5892 | } |
| 5893 | /* check if there is anything unencodable in the replacement |
| 5894 | and copy it to the output */ |
| 5895 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 5896 | c = *uni2; |
| 5897 | if (c >= limit) { |
| 5898 | raise_encode_exception(&exc, encoding, startp, size, |
| 5899 | unicodepos, unicodepos+1, reason); |
| 5900 | Py_DECREF(repunicode); |
| 5901 | goto onError; |
| 5902 | } |
| 5903 | *str = (char)c; |
| 5904 | } |
| 5905 | p = startp + newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5906 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5907 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5908 | } |
| 5909 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5910 | /* Resize if we allocated to much */ |
| 5911 | size = str - PyBytes_AS_STRING(res); |
| 5912 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 5913 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5914 | if (_PyBytes_Resize(&res, size) < 0) |
| 5915 | goto onError; |
| 5916 | } |
| 5917 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5918 | Py_XDECREF(errorHandler); |
| 5919 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5920 | return res; |
| 5921 | |
| 5922 | onError: |
| 5923 | Py_XDECREF(res); |
| 5924 | Py_XDECREF(errorHandler); |
| 5925 | Py_XDECREF(exc); |
| 5926 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5927 | } |
| 5928 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5929 | PyObject * |
| 5930 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
| 5931 | Py_ssize_t size, |
| 5932 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5933 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5934 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5935 | } |
| 5936 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5937 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5938 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5939 | { |
| 5940 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5941 | PyErr_BadArgument(); |
| 5942 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5943 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5944 | if (PyUnicode_READY(unicode) == -1) |
| 5945 | return NULL; |
| 5946 | /* Fast path: if it is a one-byte string, construct |
| 5947 | bytes object directly. */ |
| 5948 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 5949 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 5950 | PyUnicode_GET_LENGTH(unicode)); |
| 5951 | /* Non-Latin-1 characters present. Defer to above function to |
| 5952 | raise the exception. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5953 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5954 | PyUnicode_GET_SIZE(unicode), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5955 | errors); |
| 5956 | } |
| 5957 | |
| 5958 | PyObject* |
| 5959 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 5960 | { |
| 5961 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5962 | } |
| 5963 | |
| 5964 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 5965 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5966 | PyObject * |
| 5967 | PyUnicode_DecodeASCII(const char *s, |
| 5968 | Py_ssize_t size, |
| 5969 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5970 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5971 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5972 | PyUnicodeObject *v; |
| 5973 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5974 | Py_ssize_t startinpos; |
| 5975 | Py_ssize_t endinpos; |
| 5976 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5977 | const char *e; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5978 | unsigned char* d; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5979 | PyObject *errorHandler = NULL; |
| 5980 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5981 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5982 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5983 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5984 | if (size == 1 && *(unsigned char*)s < 128) |
| 5985 | return PyUnicode_FromOrdinal(*(unsigned char*)s); |
| 5986 | |
| 5987 | /* Fast path. Assume the input actually *is* ASCII, and allocate |
| 5988 | a single-block Unicode object with that assumption. If there is |
| 5989 | an error, drop the object and start over. */ |
| 5990 | v = (PyUnicodeObject*)PyUnicode_New(size, 127); |
| 5991 | if (v == NULL) |
| 5992 | goto onError; |
| 5993 | d = PyUnicode_1BYTE_DATA(v); |
| 5994 | for (i = 0; i < size; i++) { |
| 5995 | unsigned char ch = ((unsigned char*)s)[i]; |
| 5996 | if (ch < 128) |
| 5997 | d[i] = ch; |
| 5998 | else |
| 5999 | break; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6000 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6001 | if (i == size) |
| 6002 | return (PyObject*)v; |
| 6003 | Py_DECREF(v); /* start over */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6004 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6005 | v = _PyUnicode_New(size); |
| 6006 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6007 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6008 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6009 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6010 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6011 | e = s + size; |
| 6012 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6013 | register unsigned char c = (unsigned char)*s; |
| 6014 | if (c < 128) { |
| 6015 | *p++ = c; |
| 6016 | ++s; |
| 6017 | } |
| 6018 | else { |
| 6019 | startinpos = s-starts; |
| 6020 | endinpos = startinpos + 1; |
| 6021 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
| 6022 | if (unicode_decode_call_errorhandler( |
| 6023 | errors, &errorHandler, |
| 6024 | "ascii", "ordinal not in range(128)", |
| 6025 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 6026 | &v, &outpos, &p)) |
| 6027 | goto onError; |
| 6028 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6029 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6030 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6031 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 6032 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6033 | Py_XDECREF(errorHandler); |
| 6034 | Py_XDECREF(exc); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6035 | if (PyUnicode_READY(v) == -1) { |
| 6036 | Py_DECREF(v); |
| 6037 | return NULL; |
| 6038 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6039 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6040 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6041 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6042 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6043 | Py_XDECREF(errorHandler); |
| 6044 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6045 | return NULL; |
| 6046 | } |
| 6047 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6048 | PyObject * |
| 6049 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6050 | Py_ssize_t size, |
| 6051 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6052 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6053 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6054 | } |
| 6055 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6056 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6057 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6058 | { |
| 6059 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6060 | PyErr_BadArgument(); |
| 6061 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6062 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6063 | if (PyUnicode_READY(unicode) == -1) |
| 6064 | return NULL; |
| 6065 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6066 | directly. Else defer to above function to raise the exception. */ |
| 6067 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 6068 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6069 | PyUnicode_GET_LENGTH(unicode)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6070 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6071 | PyUnicode_GET_SIZE(unicode), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6072 | errors); |
| 6073 | } |
| 6074 | |
| 6075 | PyObject * |
| 6076 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6077 | { |
| 6078 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6079 | } |
| 6080 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6081 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6082 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6083 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6084 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6085 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6086 | #define NEED_RETRY |
| 6087 | #endif |
| 6088 | |
| 6089 | /* XXX This code is limited to "true" double-byte encodings, as |
| 6090 | a) it assumes an incomplete character consists of a single byte, and |
| 6091 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6092 | encodings, see IsDBCSLeadByteEx documentation. */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6093 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6094 | static int |
| 6095 | is_dbcs_lead_byte(const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6096 | { |
| 6097 | const char *curr = s + offset; |
| 6098 | |
| 6099 | if (IsDBCSLeadByte(*curr)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6100 | const char *prev = CharPrev(s, curr); |
| 6101 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6102 | } |
| 6103 | return 0; |
| 6104 | } |
| 6105 | |
| 6106 | /* |
| 6107 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 6108 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 6109 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6110 | static int |
| 6111 | decode_mbcs(PyUnicodeObject **v, |
| 6112 | const char *s, /* MBCS string */ |
| 6113 | int size, /* sizeof MBCS string */ |
| 6114 | int final, |
| 6115 | const char *errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6116 | { |
| 6117 | Py_UNICODE *p; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6118 | Py_ssize_t n; |
| 6119 | DWORD usize; |
| 6120 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6121 | |
| 6122 | assert(size >= 0); |
| 6123 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6124 | /* check and handle 'errors' arg */ |
| 6125 | if (errors==NULL || strcmp(errors, "strict")==0) |
| 6126 | flags = MB_ERR_INVALID_CHARS; |
| 6127 | else if (strcmp(errors, "ignore")==0) |
| 6128 | flags = 0; |
| 6129 | else { |
| 6130 | PyErr_Format(PyExc_ValueError, |
| 6131 | "mbcs encoding does not support errors='%s'", |
| 6132 | errors); |
| 6133 | return -1; |
| 6134 | } |
| 6135 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6136 | /* Skip trailing lead-byte unless 'final' is set */ |
| 6137 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6138 | --size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6139 | |
| 6140 | /* First get the size of the result */ |
| 6141 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6142 | usize = MultiByteToWideChar(CP_ACP, flags, s, size, NULL, 0); |
| 6143 | if (usize==0) |
| 6144 | goto mbcs_decode_error; |
| 6145 | } else |
| 6146 | usize = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6147 | |
| 6148 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6149 | /* Create unicode object */ |
| 6150 | *v = _PyUnicode_New(usize); |
| 6151 | if (*v == NULL) |
| 6152 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6153 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6154 | } |
| 6155 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6156 | /* Extend unicode object */ |
| 6157 | n = PyUnicode_GET_SIZE(*v); |
| 6158 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 6159 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6160 | } |
| 6161 | |
| 6162 | /* Do the conversion */ |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6163 | if (usize > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6164 | p = PyUnicode_AS_UNICODE(*v) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6165 | if (0 == MultiByteToWideChar(CP_ACP, flags, s, size, p, usize)) { |
| 6166 | goto mbcs_decode_error; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6167 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6168 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6169 | return size; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6170 | |
| 6171 | mbcs_decode_error: |
| 6172 | /* If the last error was ERROR_NO_UNICODE_TRANSLATION, then |
| 6173 | we raise a UnicodeDecodeError - else it is a 'generic' |
| 6174 | windows error |
| 6175 | */ |
| 6176 | if (GetLastError()==ERROR_NO_UNICODE_TRANSLATION) { |
| 6177 | /* Ideally, we should get reason from FormatMessage - this |
| 6178 | is the Windows 2000 English version of the message |
| 6179 | */ |
| 6180 | PyObject *exc = NULL; |
| 6181 | const char *reason = "No mapping for the Unicode character exists " |
| 6182 | "in the target multi-byte code page."; |
| 6183 | make_decode_exception(&exc, "mbcs", s, size, 0, 0, reason); |
| 6184 | if (exc != NULL) { |
| 6185 | PyCodec_StrictErrors(exc); |
| 6186 | Py_DECREF(exc); |
| 6187 | } |
| 6188 | } else { |
| 6189 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 6190 | } |
| 6191 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6192 | } |
| 6193 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6194 | PyObject * |
| 6195 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 6196 | Py_ssize_t size, |
| 6197 | const char *errors, |
| 6198 | Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6199 | { |
| 6200 | PyUnicodeObject *v = NULL; |
| 6201 | int done; |
| 6202 | |
| 6203 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6204 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6205 | |
| 6206 | #ifdef NEED_RETRY |
| 6207 | retry: |
| 6208 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6209 | done = decode_mbcs(&v, s, INT_MAX, 0, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6210 | else |
| 6211 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6212 | done = decode_mbcs(&v, s, (int)size, !consumed, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6213 | |
| 6214 | if (done < 0) { |
| 6215 | Py_XDECREF(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6216 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6217 | } |
| 6218 | |
| 6219 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6220 | *consumed += done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6221 | |
| 6222 | #ifdef NEED_RETRY |
| 6223 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6224 | s += done; |
| 6225 | size -= done; |
| 6226 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6227 | } |
| 6228 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6229 | if (PyUnicode_READY(v) == -1) { |
| 6230 | Py_DECREF(v); |
| 6231 | return NULL; |
| 6232 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6233 | return (PyObject *)v; |
| 6234 | } |
| 6235 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6236 | PyObject * |
| 6237 | PyUnicode_DecodeMBCS(const char *s, |
| 6238 | Py_ssize_t size, |
| 6239 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6240 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6241 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 6242 | } |
| 6243 | |
| 6244 | /* |
| 6245 | * Convert unicode into string object (MBCS). |
| 6246 | * Returns 0 if succeed, -1 otherwise. |
| 6247 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6248 | static int |
| 6249 | encode_mbcs(PyObject **repr, |
| 6250 | const Py_UNICODE *p, /* unicode */ |
| 6251 | int size, /* size of unicode */ |
| 6252 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6253 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6254 | BOOL usedDefaultChar = FALSE; |
| 6255 | BOOL *pusedDefaultChar; |
| 6256 | int mbcssize; |
| 6257 | Py_ssize_t n; |
| 6258 | PyObject *exc = NULL; |
| 6259 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6260 | |
| 6261 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6262 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6263 | /* check and handle 'errors' arg */ |
| 6264 | if (errors==NULL || strcmp(errors, "strict")==0) { |
| 6265 | flags = WC_NO_BEST_FIT_CHARS; |
| 6266 | pusedDefaultChar = &usedDefaultChar; |
| 6267 | } else if (strcmp(errors, "replace")==0) { |
| 6268 | flags = 0; |
| 6269 | pusedDefaultChar = NULL; |
| 6270 | } else { |
| 6271 | PyErr_Format(PyExc_ValueError, |
| 6272 | "mbcs encoding does not support errors='%s'", |
| 6273 | errors); |
| 6274 | return -1; |
| 6275 | } |
| 6276 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6277 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6278 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6279 | mbcssize = WideCharToMultiByte(CP_ACP, flags, p, size, NULL, 0, |
| 6280 | NULL, pusedDefaultChar); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6281 | if (mbcssize == 0) { |
| 6282 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 6283 | return -1; |
| 6284 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6285 | /* If we used a default char, then we failed! */ |
| 6286 | if (pusedDefaultChar && *pusedDefaultChar) |
| 6287 | goto mbcs_encode_error; |
| 6288 | } else { |
| 6289 | mbcssize = 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6290 | } |
| 6291 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6292 | if (*repr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6293 | /* Create string object */ |
| 6294 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); |
| 6295 | if (*repr == NULL) |
| 6296 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6297 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6298 | } |
| 6299 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6300 | /* Extend string object */ |
| 6301 | n = PyBytes_Size(*repr); |
| 6302 | if (_PyBytes_Resize(repr, n + mbcssize) < 0) |
| 6303 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6304 | } |
| 6305 | |
| 6306 | /* Do the conversion */ |
| 6307 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6308 | char *s = PyBytes_AS_STRING(*repr) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6309 | if (0 == WideCharToMultiByte(CP_ACP, flags, p, size, s, mbcssize, |
| 6310 | NULL, pusedDefaultChar)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6311 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 6312 | return -1; |
| 6313 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6314 | if (pusedDefaultChar && *pusedDefaultChar) |
| 6315 | goto mbcs_encode_error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6316 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6317 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6318 | |
| 6319 | mbcs_encode_error: |
| 6320 | raise_encode_exception(&exc, "mbcs", p, size, 0, 0, "invalid character"); |
| 6321 | Py_XDECREF(exc); |
| 6322 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6323 | } |
| 6324 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6325 | PyObject * |
| 6326 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 6327 | Py_ssize_t size, |
| 6328 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6329 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6330 | PyObject *repr = NULL; |
| 6331 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 6332 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6333 | #ifdef NEED_RETRY |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6334 | retry: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6335 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6336 | ret = encode_mbcs(&repr, p, INT_MAX, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6337 | else |
| 6338 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6339 | ret = encode_mbcs(&repr, p, (int)size, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6340 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6341 | if (ret < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6342 | Py_XDECREF(repr); |
| 6343 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6344 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6345 | |
| 6346 | #ifdef NEED_RETRY |
| 6347 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6348 | p += INT_MAX; |
| 6349 | size -= INT_MAX; |
| 6350 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6351 | } |
| 6352 | #endif |
| 6353 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6354 | return repr; |
| 6355 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6356 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6357 | PyObject * |
| 6358 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 6359 | { |
| 6360 | if (!PyUnicode_Check(unicode)) { |
| 6361 | PyErr_BadArgument(); |
| 6362 | return NULL; |
| 6363 | } |
| 6364 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6365 | PyUnicode_GET_SIZE(unicode), |
| 6366 | NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 6367 | } |
| 6368 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6369 | #undef NEED_RETRY |
| 6370 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6371 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6372 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6373 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 6374 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6375 | PyObject * |
| 6376 | PyUnicode_DecodeCharmap(const char *s, |
| 6377 | Py_ssize_t size, |
| 6378 | PyObject *mapping, |
| 6379 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6380 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6381 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6382 | Py_ssize_t startinpos; |
| 6383 | Py_ssize_t endinpos; |
| 6384 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6385 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6386 | PyUnicodeObject *v; |
| 6387 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6388 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6389 | PyObject *errorHandler = NULL; |
| 6390 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 6391 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6392 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6393 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6394 | /* Default to Latin-1 */ |
| 6395 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6396 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6397 | |
| 6398 | v = _PyUnicode_New(size); |
| 6399 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6400 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6401 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6402 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6403 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6404 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 6405 | if (PyUnicode_CheckExact(mapping)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6406 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 6407 | maplen = PyUnicode_GET_SIZE(mapping); |
| 6408 | while (s < e) { |
| 6409 | unsigned char ch = *s; |
| 6410 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6411 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6412 | if (ch < maplen) |
| 6413 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6414 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6415 | if (x == 0xfffe) { |
| 6416 | /* undefined mapping */ |
| 6417 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 6418 | startinpos = s-starts; |
| 6419 | endinpos = startinpos+1; |
| 6420 | if (unicode_decode_call_errorhandler( |
| 6421 | errors, &errorHandler, |
| 6422 | "charmap", "character maps to <undefined>", |
| 6423 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 6424 | &v, &outpos, &p)) { |
| 6425 | goto onError; |
| 6426 | } |
| 6427 | continue; |
| 6428 | } |
| 6429 | *p++ = x; |
| 6430 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6431 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 6432 | } |
| 6433 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6434 | while (s < e) { |
| 6435 | unsigned char ch = *s; |
| 6436 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 6437 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6438 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 6439 | w = PyLong_FromLong((long)ch); |
| 6440 | if (w == NULL) |
| 6441 | goto onError; |
| 6442 | x = PyObject_GetItem(mapping, w); |
| 6443 | Py_DECREF(w); |
| 6444 | if (x == NULL) { |
| 6445 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 6446 | /* No mapping found means: mapping is undefined. */ |
| 6447 | PyErr_Clear(); |
| 6448 | x = Py_None; |
| 6449 | Py_INCREF(x); |
| 6450 | } else |
| 6451 | goto onError; |
| 6452 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6453 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6454 | /* Apply mapping */ |
| 6455 | if (PyLong_Check(x)) { |
| 6456 | long value = PyLong_AS_LONG(x); |
| 6457 | if (value < 0 || value > 65535) { |
| 6458 | PyErr_SetString(PyExc_TypeError, |
| 6459 | "character mapping must be in range(65536)"); |
| 6460 | Py_DECREF(x); |
| 6461 | goto onError; |
| 6462 | } |
| 6463 | *p++ = (Py_UNICODE)value; |
| 6464 | } |
| 6465 | else if (x == Py_None) { |
| 6466 | /* undefined mapping */ |
| 6467 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 6468 | startinpos = s-starts; |
| 6469 | endinpos = startinpos+1; |
| 6470 | if (unicode_decode_call_errorhandler( |
| 6471 | errors, &errorHandler, |
| 6472 | "charmap", "character maps to <undefined>", |
| 6473 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 6474 | &v, &outpos, &p)) { |
| 6475 | Py_DECREF(x); |
| 6476 | goto onError; |
| 6477 | } |
| 6478 | Py_DECREF(x); |
| 6479 | continue; |
| 6480 | } |
| 6481 | else if (PyUnicode_Check(x)) { |
| 6482 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6483 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6484 | if (targetsize == 1) |
| 6485 | /* 1-1 mapping */ |
| 6486 | *p++ = *PyUnicode_AS_UNICODE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6487 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6488 | else if (targetsize > 1) { |
| 6489 | /* 1-n mapping */ |
| 6490 | if (targetsize > extrachars) { |
| 6491 | /* resize first */ |
| 6492 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 6493 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 6494 | (targetsize << 2); |
| 6495 | extrachars += needed; |
| 6496 | /* XXX overflow detection missing */ |
| 6497 | if (_PyUnicode_Resize(&v, |
| 6498 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 6499 | Py_DECREF(x); |
| 6500 | goto onError; |
| 6501 | } |
| 6502 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 6503 | } |
| 6504 | Py_UNICODE_COPY(p, |
| 6505 | PyUnicode_AS_UNICODE(x), |
| 6506 | targetsize); |
| 6507 | p += targetsize; |
| 6508 | extrachars -= targetsize; |
| 6509 | } |
| 6510 | /* 1-0 mapping: skip the character */ |
| 6511 | } |
| 6512 | else { |
| 6513 | /* wrong return value */ |
| 6514 | PyErr_SetString(PyExc_TypeError, |
| 6515 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6516 | Py_DECREF(x); |
| 6517 | goto onError; |
| 6518 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6519 | Py_DECREF(x); |
| 6520 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6521 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6522 | } |
| 6523 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6524 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 6525 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6526 | Py_XDECREF(errorHandler); |
| 6527 | Py_XDECREF(exc); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6528 | if (PyUnicode_READY(v) == -1) { |
| 6529 | Py_DECREF(v); |
| 6530 | return NULL; |
| 6531 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6532 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6533 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6534 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6535 | Py_XDECREF(errorHandler); |
| 6536 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6537 | Py_XDECREF(v); |
| 6538 | return NULL; |
| 6539 | } |
| 6540 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6541 | /* Charmap encoding: the lookup table */ |
| 6542 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6543 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6544 | PyObject_HEAD |
| 6545 | unsigned char level1[32]; |
| 6546 | int count2, count3; |
| 6547 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6548 | }; |
| 6549 | |
| 6550 | static PyObject* |
| 6551 | encoding_map_size(PyObject *obj, PyObject* args) |
| 6552 | { |
| 6553 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6554 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6555 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6556 | } |
| 6557 | |
| 6558 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6559 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6560 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 6561 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6562 | }; |
| 6563 | |
| 6564 | static void |
| 6565 | encoding_map_dealloc(PyObject* o) |
| 6566 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6567 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6568 | } |
| 6569 | |
| 6570 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6571 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6572 | "EncodingMap", /*tp_name*/ |
| 6573 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 6574 | 0, /*tp_itemsize*/ |
| 6575 | /* methods */ |
| 6576 | encoding_map_dealloc, /*tp_dealloc*/ |
| 6577 | 0, /*tp_print*/ |
| 6578 | 0, /*tp_getattr*/ |
| 6579 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 6580 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6581 | 0, /*tp_repr*/ |
| 6582 | 0, /*tp_as_number*/ |
| 6583 | 0, /*tp_as_sequence*/ |
| 6584 | 0, /*tp_as_mapping*/ |
| 6585 | 0, /*tp_hash*/ |
| 6586 | 0, /*tp_call*/ |
| 6587 | 0, /*tp_str*/ |
| 6588 | 0, /*tp_getattro*/ |
| 6589 | 0, /*tp_setattro*/ |
| 6590 | 0, /*tp_as_buffer*/ |
| 6591 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 6592 | 0, /*tp_doc*/ |
| 6593 | 0, /*tp_traverse*/ |
| 6594 | 0, /*tp_clear*/ |
| 6595 | 0, /*tp_richcompare*/ |
| 6596 | 0, /*tp_weaklistoffset*/ |
| 6597 | 0, /*tp_iter*/ |
| 6598 | 0, /*tp_iternext*/ |
| 6599 | encoding_map_methods, /*tp_methods*/ |
| 6600 | 0, /*tp_members*/ |
| 6601 | 0, /*tp_getset*/ |
| 6602 | 0, /*tp_base*/ |
| 6603 | 0, /*tp_dict*/ |
| 6604 | 0, /*tp_descr_get*/ |
| 6605 | 0, /*tp_descr_set*/ |
| 6606 | 0, /*tp_dictoffset*/ |
| 6607 | 0, /*tp_init*/ |
| 6608 | 0, /*tp_alloc*/ |
| 6609 | 0, /*tp_new*/ |
| 6610 | 0, /*tp_free*/ |
| 6611 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6612 | }; |
| 6613 | |
| 6614 | PyObject* |
| 6615 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 6616 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6617 | PyObject *result; |
| 6618 | struct encoding_map *mresult; |
| 6619 | int i; |
| 6620 | int need_dict = 0; |
| 6621 | unsigned char level1[32]; |
| 6622 | unsigned char level2[512]; |
| 6623 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 6624 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6625 | int kind; |
| 6626 | void *data; |
| 6627 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6628 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6629 | if (!PyUnicode_Check(string) || PyUnicode_GET_LENGTH(string) != 256) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6630 | PyErr_BadArgument(); |
| 6631 | return NULL; |
| 6632 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6633 | kind = PyUnicode_KIND(string); |
| 6634 | data = PyUnicode_DATA(string); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6635 | memset(level1, 0xFF, sizeof level1); |
| 6636 | memset(level2, 0xFF, sizeof level2); |
| 6637 | |
| 6638 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 6639 | or if there are non-BMP characters, we need to use |
| 6640 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6641 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6642 | need_dict = 1; |
| 6643 | for (i = 1; i < 256; i++) { |
| 6644 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6645 | ch = PyUnicode_READ(kind, data, i); |
| 6646 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6647 | need_dict = 1; |
| 6648 | break; |
| 6649 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6650 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6651 | /* unmapped character */ |
| 6652 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6653 | l1 = ch >> 11; |
| 6654 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6655 | if (level1[l1] == 0xFF) |
| 6656 | level1[l1] = count2++; |
| 6657 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6658 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6659 | } |
| 6660 | |
| 6661 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 6662 | need_dict = 1; |
| 6663 | |
| 6664 | if (need_dict) { |
| 6665 | PyObject *result = PyDict_New(); |
| 6666 | PyObject *key, *value; |
| 6667 | if (!result) |
| 6668 | return NULL; |
| 6669 | for (i = 0; i < 256; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6670 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6671 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6672 | if (!key || !value) |
| 6673 | goto failed1; |
| 6674 | if (PyDict_SetItem(result, key, value) == -1) |
| 6675 | goto failed1; |
| 6676 | Py_DECREF(key); |
| 6677 | Py_DECREF(value); |
| 6678 | } |
| 6679 | return result; |
| 6680 | failed1: |
| 6681 | Py_XDECREF(key); |
| 6682 | Py_XDECREF(value); |
| 6683 | Py_DECREF(result); |
| 6684 | return NULL; |
| 6685 | } |
| 6686 | |
| 6687 | /* Create a three-level trie */ |
| 6688 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 6689 | 16*count2 + 128*count3 - 1); |
| 6690 | if (!result) |
| 6691 | return PyErr_NoMemory(); |
| 6692 | PyObject_Init(result, &EncodingMapType); |
| 6693 | mresult = (struct encoding_map*)result; |
| 6694 | mresult->count2 = count2; |
| 6695 | mresult->count3 = count3; |
| 6696 | mlevel1 = mresult->level1; |
| 6697 | mlevel2 = mresult->level23; |
| 6698 | mlevel3 = mresult->level23 + 16*count2; |
| 6699 | memcpy(mlevel1, level1, 32); |
| 6700 | memset(mlevel2, 0xFF, 16*count2); |
| 6701 | memset(mlevel3, 0, 128*count3); |
| 6702 | count3 = 0; |
| 6703 | for (i = 1; i < 256; i++) { |
| 6704 | int o1, o2, o3, i2, i3; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6705 | if (PyUnicode_READ(kind, data, i) == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6706 | /* unmapped character */ |
| 6707 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6708 | o1 = PyUnicode_READ(kind, data, i)>>11; |
| 6709 | o2 = (PyUnicode_READ(kind, data, i)>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6710 | i2 = 16*mlevel1[o1] + o2; |
| 6711 | if (mlevel2[i2] == 0xFF) |
| 6712 | mlevel2[i2] = count3++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6713 | o3 = PyUnicode_READ(kind, data, i) & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6714 | i3 = 128*mlevel2[i2] + o3; |
| 6715 | mlevel3[i3] = i; |
| 6716 | } |
| 6717 | return result; |
| 6718 | } |
| 6719 | |
| 6720 | static int |
| 6721 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 6722 | { |
| 6723 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 6724 | int l1 = c>>11; |
| 6725 | int l2 = (c>>7) & 0xF; |
| 6726 | int l3 = c & 0x7F; |
| 6727 | int i; |
| 6728 | |
| 6729 | #ifdef Py_UNICODE_WIDE |
| 6730 | if (c > 0xFFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6731 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6732 | } |
| 6733 | #endif |
| 6734 | if (c == 0) |
| 6735 | return 0; |
| 6736 | /* level 1*/ |
| 6737 | i = map->level1[l1]; |
| 6738 | if (i == 0xFF) { |
| 6739 | return -1; |
| 6740 | } |
| 6741 | /* level 2*/ |
| 6742 | i = map->level23[16*i+l2]; |
| 6743 | if (i == 0xFF) { |
| 6744 | return -1; |
| 6745 | } |
| 6746 | /* level 3 */ |
| 6747 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 6748 | if (i == 0) { |
| 6749 | return -1; |
| 6750 | } |
| 6751 | return i; |
| 6752 | } |
| 6753 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6754 | /* Lookup the character ch in the mapping. If the character |
| 6755 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 6756 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6757 | static PyObject * |
| 6758 | charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6759 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6760 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6761 | PyObject *x; |
| 6762 | |
| 6763 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6764 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6765 | x = PyObject_GetItem(mapping, w); |
| 6766 | Py_DECREF(w); |
| 6767 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6768 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 6769 | /* No mapping found means: mapping is undefined. */ |
| 6770 | PyErr_Clear(); |
| 6771 | x = Py_None; |
| 6772 | Py_INCREF(x); |
| 6773 | return x; |
| 6774 | } else |
| 6775 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6776 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 6777 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6778 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6779 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6780 | long value = PyLong_AS_LONG(x); |
| 6781 | if (value < 0 || value > 255) { |
| 6782 | PyErr_SetString(PyExc_TypeError, |
| 6783 | "character mapping must be in range(256)"); |
| 6784 | Py_DECREF(x); |
| 6785 | return NULL; |
| 6786 | } |
| 6787 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6788 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6789 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6790 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6791 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6792 | /* wrong return value */ |
| 6793 | PyErr_Format(PyExc_TypeError, |
| 6794 | "character mapping must return integer, bytes or None, not %.400s", |
| 6795 | x->ob_type->tp_name); |
| 6796 | Py_DECREF(x); |
| 6797 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6798 | } |
| 6799 | } |
| 6800 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6801 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6802 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6803 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6804 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 6805 | /* exponentially overallocate to minimize reallocations */ |
| 6806 | if (requiredsize < 2*outsize) |
| 6807 | requiredsize = 2*outsize; |
| 6808 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 6809 | return -1; |
| 6810 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6811 | } |
| 6812 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6813 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6814 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6815 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6816 | /* 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] | 6817 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6818 | space is available. Return a new reference to the object that |
| 6819 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 6820 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 6821 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6822 | static charmapencode_result |
| 6823 | charmapencode_output(Py_UNICODE c, PyObject *mapping, |
| 6824 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6825 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6826 | PyObject *rep; |
| 6827 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6828 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6829 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 6830 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6831 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6832 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6833 | if (res == -1) |
| 6834 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6835 | if (outsize<requiredsize) |
| 6836 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 6837 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6838 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6839 | outstart[(*outpos)++] = (char)res; |
| 6840 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6841 | } |
| 6842 | |
| 6843 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6844 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6845 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6846 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6847 | Py_DECREF(rep); |
| 6848 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6849 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6850 | if (PyLong_Check(rep)) { |
| 6851 | Py_ssize_t requiredsize = *outpos+1; |
| 6852 | if (outsize<requiredsize) |
| 6853 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 6854 | Py_DECREF(rep); |
| 6855 | return enc_EXCEPTION; |
| 6856 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6857 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6858 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6859 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6860 | else { |
| 6861 | const char *repchars = PyBytes_AS_STRING(rep); |
| 6862 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 6863 | Py_ssize_t requiredsize = *outpos+repsize; |
| 6864 | if (outsize<requiredsize) |
| 6865 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 6866 | Py_DECREF(rep); |
| 6867 | return enc_EXCEPTION; |
| 6868 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6869 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6870 | memcpy(outstart + *outpos, repchars, repsize); |
| 6871 | *outpos += repsize; |
| 6872 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6873 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6874 | Py_DECREF(rep); |
| 6875 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6876 | } |
| 6877 | |
| 6878 | /* handle an error in PyUnicode_EncodeCharmap |
| 6879 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6880 | static int |
| 6881 | charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6882 | 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] | 6883 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 6884 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6885 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6886 | { |
| 6887 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6888 | Py_ssize_t repsize; |
| 6889 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6890 | Py_UNICODE *uni2; |
| 6891 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6892 | Py_ssize_t collstartpos = *inpos; |
| 6893 | Py_ssize_t collendpos = *inpos+1; |
| 6894 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6895 | char *encoding = "charmap"; |
| 6896 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6897 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6898 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6899 | /* find all unencodable characters */ |
| 6900 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6901 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 6902 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6903 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 6904 | if (res != -1) |
| 6905 | break; |
| 6906 | ++collendpos; |
| 6907 | continue; |
| 6908 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6909 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6910 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 6911 | if (rep==NULL) |
| 6912 | return -1; |
| 6913 | else if (rep!=Py_None) { |
| 6914 | Py_DECREF(rep); |
| 6915 | break; |
| 6916 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6917 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6918 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6919 | } |
| 6920 | /* cache callback name lookup |
| 6921 | * (if not done yet, i.e. it's the first error) */ |
| 6922 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6923 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6924 | *known_errorHandler = 1; |
| 6925 | else if (!strcmp(errors, "replace")) |
| 6926 | *known_errorHandler = 2; |
| 6927 | else if (!strcmp(errors, "ignore")) |
| 6928 | *known_errorHandler = 3; |
| 6929 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6930 | *known_errorHandler = 4; |
| 6931 | else |
| 6932 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6933 | } |
| 6934 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6935 | case 1: /* strict */ |
| 6936 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 6937 | return -1; |
| 6938 | case 2: /* replace */ |
| 6939 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6940 | x = charmapencode_output('?', mapping, res, respos); |
| 6941 | if (x==enc_EXCEPTION) { |
| 6942 | return -1; |
| 6943 | } |
| 6944 | else if (x==enc_FAILED) { |
| 6945 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 6946 | return -1; |
| 6947 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6948 | } |
| 6949 | /* fall through */ |
| 6950 | case 3: /* ignore */ |
| 6951 | *inpos = collendpos; |
| 6952 | break; |
| 6953 | case 4: /* xmlcharrefreplace */ |
| 6954 | /* generate replacement (temporarily (mis)uses p) */ |
| 6955 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6956 | char buffer[2+29+1+1]; |
| 6957 | char *cp; |
| 6958 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 6959 | for (cp = buffer; *cp; ++cp) { |
| 6960 | x = charmapencode_output(*cp, mapping, res, respos); |
| 6961 | if (x==enc_EXCEPTION) |
| 6962 | return -1; |
| 6963 | else if (x==enc_FAILED) { |
| 6964 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 6965 | return -1; |
| 6966 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6967 | } |
| 6968 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6969 | *inpos = collendpos; |
| 6970 | break; |
| 6971 | default: |
| 6972 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6973 | encoding, reason, p, size, exceptionObject, |
| 6974 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6975 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6976 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6977 | if (PyBytes_Check(repunicode)) { |
| 6978 | /* Directly copy bytes result to output. */ |
| 6979 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 6980 | Py_ssize_t requiredsize; |
| 6981 | repsize = PyBytes_Size(repunicode); |
| 6982 | requiredsize = *respos + repsize; |
| 6983 | if (requiredsize > outsize) |
| 6984 | /* Make room for all additional bytes. */ |
| 6985 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 6986 | Py_DECREF(repunicode); |
| 6987 | return -1; |
| 6988 | } |
| 6989 | memcpy(PyBytes_AsString(*res) + *respos, |
| 6990 | PyBytes_AsString(repunicode), repsize); |
| 6991 | *respos += repsize; |
| 6992 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6993 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6994 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6995 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6996 | /* generate replacement */ |
| 6997 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 6998 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6999 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 7000 | if (x==enc_EXCEPTION) { |
| 7001 | return -1; |
| 7002 | } |
| 7003 | else if (x==enc_FAILED) { |
| 7004 | Py_DECREF(repunicode); |
| 7005 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7006 | return -1; |
| 7007 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7008 | } |
| 7009 | *inpos = newpos; |
| 7010 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7011 | } |
| 7012 | return 0; |
| 7013 | } |
| 7014 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7015 | PyObject * |
| 7016 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 7017 | Py_ssize_t size, |
| 7018 | PyObject *mapping, |
| 7019 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7020 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7021 | /* output object */ |
| 7022 | PyObject *res = NULL; |
| 7023 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7024 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7025 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7026 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7027 | PyObject *errorHandler = NULL; |
| 7028 | PyObject *exc = NULL; |
| 7029 | /* the following variable is used for caching string comparisons |
| 7030 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 7031 | * 3=ignore, 4=xmlcharrefreplace */ |
| 7032 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7033 | |
| 7034 | /* Default to Latin-1 */ |
| 7035 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7036 | return PyUnicode_EncodeLatin1(p, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7037 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7038 | /* allocate enough for a simple encoding without |
| 7039 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7040 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7041 | if (res == NULL) |
| 7042 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 7043 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7044 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7045 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7046 | while (inpos<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7047 | /* try to encode it */ |
| 7048 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 7049 | if (x==enc_EXCEPTION) /* error */ |
| 7050 | goto onError; |
| 7051 | if (x==enc_FAILED) { /* unencodable character */ |
| 7052 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 7053 | &exc, |
| 7054 | &known_errorHandler, &errorHandler, errors, |
| 7055 | &res, &respos)) { |
| 7056 | goto onError; |
| 7057 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7058 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7059 | else |
| 7060 | /* done with this character => adjust input position */ |
| 7061 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7062 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7063 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7064 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7065 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 7066 | if (_PyBytes_Resize(&res, respos) < 0) |
| 7067 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7068 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7069 | Py_XDECREF(exc); |
| 7070 | Py_XDECREF(errorHandler); |
| 7071 | return res; |
| 7072 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7073 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7074 | Py_XDECREF(res); |
| 7075 | Py_XDECREF(exc); |
| 7076 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7077 | return NULL; |
| 7078 | } |
| 7079 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7080 | PyObject * |
| 7081 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 7082 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7083 | { |
| 7084 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7085 | PyErr_BadArgument(); |
| 7086 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7087 | } |
| 7088 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7089 | PyUnicode_GET_SIZE(unicode), |
| 7090 | mapping, |
| 7091 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7092 | } |
| 7093 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7094 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7095 | static void |
| 7096 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7097 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7098 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7099 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7100 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7101 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7102 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 7103 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7104 | } |
| 7105 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7106 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 7107 | goto onError; |
| 7108 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 7109 | goto onError; |
| 7110 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 7111 | goto onError; |
| 7112 | return; |
| 7113 | onError: |
| 7114 | Py_DECREF(*exceptionObject); |
| 7115 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7116 | } |
| 7117 | } |
| 7118 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7119 | /* raises a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7120 | static void |
| 7121 | raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7122 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7123 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7124 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7125 | { |
| 7126 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7127 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7128 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7129 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7130 | } |
| 7131 | |
| 7132 | /* error handling callback helper: |
| 7133 | build arguments, call the callback and check the arguments, |
| 7134 | put the result into newpos and return the replacement string, which |
| 7135 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7136 | static PyObject * |
| 7137 | unicode_translate_call_errorhandler(const char *errors, |
| 7138 | PyObject **errorHandler, |
| 7139 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7140 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7141 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7142 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7143 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7144 | 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] | 7145 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7146 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7147 | PyObject *restuple; |
| 7148 | PyObject *resunicode; |
| 7149 | |
| 7150 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7151 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7152 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7153 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7154 | } |
| 7155 | |
| 7156 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7157 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7158 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7159 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7160 | |
| 7161 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7162 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7163 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7164 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7165 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 7166 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7167 | Py_DECREF(restuple); |
| 7168 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7169 | } |
| 7170 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7171 | &resunicode, &i_newpos)) { |
| 7172 | Py_DECREF(restuple); |
| 7173 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7174 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7175 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7176 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7177 | else |
| 7178 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7179 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7180 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 7181 | Py_DECREF(restuple); |
| 7182 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 7183 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7184 | Py_INCREF(resunicode); |
| 7185 | Py_DECREF(restuple); |
| 7186 | return resunicode; |
| 7187 | } |
| 7188 | |
| 7189 | /* Lookup the character ch in the mapping and put the result in result, |
| 7190 | which must be decrefed by the caller. |
| 7191 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7192 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7193 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7194 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7195 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7196 | PyObject *x; |
| 7197 | |
| 7198 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7199 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7200 | x = PyObject_GetItem(mapping, w); |
| 7201 | Py_DECREF(w); |
| 7202 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7203 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7204 | /* No mapping found means: use 1:1 mapping. */ |
| 7205 | PyErr_Clear(); |
| 7206 | *result = NULL; |
| 7207 | return 0; |
| 7208 | } else |
| 7209 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7210 | } |
| 7211 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7212 | *result = x; |
| 7213 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7214 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7215 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7216 | long value = PyLong_AS_LONG(x); |
| 7217 | long max = PyUnicode_GetMax(); |
| 7218 | if (value < 0 || value > max) { |
| 7219 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 7220 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7221 | Py_DECREF(x); |
| 7222 | return -1; |
| 7223 | } |
| 7224 | *result = x; |
| 7225 | return 0; |
| 7226 | } |
| 7227 | else if (PyUnicode_Check(x)) { |
| 7228 | *result = x; |
| 7229 | return 0; |
| 7230 | } |
| 7231 | else { |
| 7232 | /* wrong return value */ |
| 7233 | PyErr_SetString(PyExc_TypeError, |
| 7234 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7235 | Py_DECREF(x); |
| 7236 | return -1; |
| 7237 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7238 | } |
| 7239 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7240 | if not reallocate and adjust various state variables. |
| 7241 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7242 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7243 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7244 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7245 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7246 | Py_ssize_t oldsize = *psize; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 7247 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7248 | /* exponentially overallocate to minimize reallocations */ |
| 7249 | if (requiredsize < 2 * oldsize) |
| 7250 | requiredsize = 2 * oldsize; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7251 | *outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 7252 | if (*outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7253 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7254 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7255 | } |
| 7256 | return 0; |
| 7257 | } |
| 7258 | /* lookup the character, put the result in the output string and adjust |
| 7259 | various state variables. Return a new reference to the object that |
| 7260 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 7261 | undefined (in which case no character was written). |
| 7262 | The called must decref result. |
| 7263 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7264 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7265 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 7266 | PyObject *mapping, Py_UCS4 **output, |
| 7267 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7268 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7269 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7270 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 7271 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7272 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7273 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7274 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7275 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7276 | } |
| 7277 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7278 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7279 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7280 | /* 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] | 7281 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7282 | } |
| 7283 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7284 | Py_ssize_t repsize; |
| 7285 | if (PyUnicode_READY(*res) == -1) |
| 7286 | return -1; |
| 7287 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7288 | if (repsize==1) { |
| 7289 | /* 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] | 7290 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7291 | } |
| 7292 | else if (repsize!=0) { |
| 7293 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7294 | Py_ssize_t requiredsize = *opos + |
| 7295 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7296 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7297 | Py_ssize_t i; |
| 7298 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7299 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7300 | for(i = 0; i < repsize; i++) |
| 7301 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7302 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7303 | } |
| 7304 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7305 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7306 | return 0; |
| 7307 | } |
| 7308 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7309 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7310 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 7311 | PyObject *mapping, |
| 7312 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7313 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7314 | /* input object */ |
| 7315 | char *idata; |
| 7316 | Py_ssize_t size, i; |
| 7317 | int kind; |
| 7318 | /* output buffer */ |
| 7319 | Py_UCS4 *output = NULL; |
| 7320 | Py_ssize_t osize; |
| 7321 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7322 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7323 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7324 | char *reason = "character maps to <undefined>"; |
| 7325 | PyObject *errorHandler = NULL; |
| 7326 | PyObject *exc = NULL; |
| 7327 | /* the following variable is used for caching string comparisons |
| 7328 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 7329 | * 3=ignore, 4=xmlcharrefreplace */ |
| 7330 | int known_errorHandler = -1; |
| 7331 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7332 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7333 | PyErr_BadArgument(); |
| 7334 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7335 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7336 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7337 | if (PyUnicode_READY(input) == -1) |
| 7338 | return NULL; |
| 7339 | idata = (char*)PyUnicode_DATA(input); |
| 7340 | kind = PyUnicode_KIND(input); |
| 7341 | size = PyUnicode_GET_LENGTH(input); |
| 7342 | i = 0; |
| 7343 | |
| 7344 | if (size == 0) { |
| 7345 | Py_INCREF(input); |
| 7346 | return input; |
| 7347 | } |
| 7348 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7349 | /* allocate enough for a simple 1:1 translation without |
| 7350 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7351 | osize = size; |
| 7352 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 7353 | opos = 0; |
| 7354 | if (output == NULL) { |
| 7355 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7356 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7357 | } |
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 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7360 | /* try to encode it */ |
| 7361 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7362 | if (charmaptranslate_output(input, i, mapping, |
| 7363 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7364 | Py_XDECREF(x); |
| 7365 | goto onError; |
| 7366 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7367 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7368 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7369 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7370 | else { /* untranslatable character */ |
| 7371 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 7372 | Py_ssize_t repsize; |
| 7373 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7374 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7375 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7376 | Py_ssize_t collstart = i; |
| 7377 | Py_ssize_t collend = i+1; |
| 7378 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7379 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7380 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7381 | while (collend < size) { |
| 7382 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7383 | goto onError; |
| 7384 | Py_XDECREF(x); |
| 7385 | if (x!=Py_None) |
| 7386 | break; |
| 7387 | ++collend; |
| 7388 | } |
| 7389 | /* cache callback name lookup |
| 7390 | * (if not done yet, i.e. it's the first error) */ |
| 7391 | if (known_errorHandler==-1) { |
| 7392 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 7393 | known_errorHandler = 1; |
| 7394 | else if (!strcmp(errors, "replace")) |
| 7395 | known_errorHandler = 2; |
| 7396 | else if (!strcmp(errors, "ignore")) |
| 7397 | known_errorHandler = 3; |
| 7398 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 7399 | known_errorHandler = 4; |
| 7400 | else |
| 7401 | known_errorHandler = 0; |
| 7402 | } |
| 7403 | switch (known_errorHandler) { |
| 7404 | case 1: /* strict */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7405 | raise_translate_exception(&exc, input, collstart, |
| 7406 | collend, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7407 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7408 | case 2: /* replace */ |
| 7409 | /* 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] | 7410 | for (coll = collstart; coll<collend; coll++) |
| 7411 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7412 | /* fall through */ |
| 7413 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7414 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7415 | break; |
| 7416 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7417 | /* generate replacement (temporarily (mis)uses i) */ |
| 7418 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7419 | char buffer[2+29+1+1]; |
| 7420 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7421 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 7422 | if (charmaptranslate_makespace(&output, &osize, |
| 7423 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7424 | goto onError; |
| 7425 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7426 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7427 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7428 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7429 | break; |
| 7430 | default: |
| 7431 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7432 | reason, input, &exc, |
| 7433 | collstart, collend, &newpos); |
| 7434 | if (repunicode == NULL || PyUnicode_READY(repunicode) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7435 | goto onError; |
| 7436 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7437 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 7438 | if (charmaptranslate_makespace(&output, &osize, |
| 7439 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7440 | Py_DECREF(repunicode); |
| 7441 | goto onError; |
| 7442 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7443 | for (uni2 = 0; repsize-->0; ++uni2) |
| 7444 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 7445 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7446 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7447 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7448 | } |
| 7449 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7450 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 7451 | if (!res) |
| 7452 | goto onError; |
| 7453 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7454 | Py_XDECREF(exc); |
| 7455 | Py_XDECREF(errorHandler); |
| 7456 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7457 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7458 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7459 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7460 | Py_XDECREF(exc); |
| 7461 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7462 | return NULL; |
| 7463 | } |
| 7464 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7465 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 7466 | PyObject * |
| 7467 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 7468 | Py_ssize_t size, |
| 7469 | PyObject *mapping, |
| 7470 | const char *errors) |
| 7471 | { |
| 7472 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 7473 | if (!unicode) |
| 7474 | return NULL; |
| 7475 | return _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 7476 | } |
| 7477 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7478 | PyObject * |
| 7479 | PyUnicode_Translate(PyObject *str, |
| 7480 | PyObject *mapping, |
| 7481 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7482 | { |
| 7483 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7484 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7485 | str = PyUnicode_FromObject(str); |
| 7486 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7487 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7488 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7489 | Py_DECREF(str); |
| 7490 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7491 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7492 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7493 | Py_XDECREF(str); |
| 7494 | return NULL; |
| 7495 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7496 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7497 | static Py_UCS4 |
| 7498 | fix_decimal_and_space_to_ascii(PyUnicodeObject *self) |
| 7499 | { |
| 7500 | /* No need to call PyUnicode_READY(self) because this function is only |
| 7501 | called as a callback from fixup() which does it already. */ |
| 7502 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 7503 | const int kind = PyUnicode_KIND(self); |
| 7504 | void *data = PyUnicode_DATA(self); |
| 7505 | Py_UCS4 maxchar = 0, ch, fixed; |
| 7506 | Py_ssize_t i; |
| 7507 | |
| 7508 | for (i = 0; i < len; ++i) { |
| 7509 | ch = PyUnicode_READ(kind, data, i); |
| 7510 | fixed = 0; |
| 7511 | if (ch > 127) { |
| 7512 | if (Py_UNICODE_ISSPACE(ch)) |
| 7513 | fixed = ' '; |
| 7514 | else { |
| 7515 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 7516 | if (decimal >= 0) |
| 7517 | fixed = '0' + decimal; |
| 7518 | } |
| 7519 | if (fixed != 0) { |
| 7520 | if (fixed > maxchar) |
| 7521 | maxchar = fixed; |
| 7522 | PyUnicode_WRITE(kind, data, i, fixed); |
| 7523 | } |
| 7524 | else if (ch > maxchar) |
| 7525 | maxchar = ch; |
| 7526 | } |
| 7527 | else if (ch > maxchar) |
| 7528 | maxchar = ch; |
| 7529 | } |
| 7530 | |
| 7531 | return maxchar; |
| 7532 | } |
| 7533 | |
| 7534 | PyObject * |
| 7535 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 7536 | { |
| 7537 | if (!PyUnicode_Check(unicode)) { |
| 7538 | PyErr_BadInternalCall(); |
| 7539 | return NULL; |
| 7540 | } |
| 7541 | if (PyUnicode_READY(unicode) == -1) |
| 7542 | return NULL; |
| 7543 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 7544 | /* If the string is already ASCII, just return the same string */ |
| 7545 | Py_INCREF(unicode); |
| 7546 | return unicode; |
| 7547 | } |
| 7548 | return fixup((PyUnicodeObject *)unicode, fix_decimal_and_space_to_ascii); |
| 7549 | } |
| 7550 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 7551 | PyObject * |
| 7552 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 7553 | Py_ssize_t length) |
| 7554 | { |
| 7555 | PyObject *result; |
| 7556 | Py_UNICODE *p; /* write pointer into result */ |
| 7557 | Py_ssize_t i; |
| 7558 | /* Copy to a new string */ |
| 7559 | result = (PyObject *)_PyUnicode_New(length); |
| 7560 | Py_UNICODE_COPY(PyUnicode_AS_UNICODE(result), s, length); |
| 7561 | if (result == NULL) |
| 7562 | return result; |
| 7563 | p = PyUnicode_AS_UNICODE(result); |
| 7564 | /* Iterate over code points */ |
| 7565 | for (i = 0; i < length; i++) { |
| 7566 | Py_UNICODE ch =s[i]; |
| 7567 | if (ch > 127) { |
| 7568 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 7569 | if (decimal >= 0) |
| 7570 | p[i] = '0' + decimal; |
| 7571 | } |
| 7572 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7573 | if (PyUnicode_READY((PyUnicodeObject*)result) == -1) { |
| 7574 | Py_DECREF(result); |
| 7575 | return NULL; |
| 7576 | } |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 7577 | return result; |
| 7578 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 7579 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 7580 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7581 | int |
| 7582 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 7583 | Py_ssize_t length, |
| 7584 | char *output, |
| 7585 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 7586 | { |
| 7587 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7588 | PyObject *errorHandler = NULL; |
| 7589 | PyObject *exc = NULL; |
| 7590 | const char *encoding = "decimal"; |
| 7591 | const char *reason = "invalid decimal Unicode string"; |
| 7592 | /* the following variable is used for caching string comparisons |
| 7593 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 7594 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 7595 | |
| 7596 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7597 | PyErr_BadArgument(); |
| 7598 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 7599 | } |
| 7600 | |
| 7601 | p = s; |
| 7602 | end = s + length; |
| 7603 | while (p < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7604 | register Py_UNICODE ch = *p; |
| 7605 | int decimal; |
| 7606 | PyObject *repunicode; |
| 7607 | Py_ssize_t repsize; |
| 7608 | Py_ssize_t newpos; |
| 7609 | Py_UNICODE *uni2; |
| 7610 | Py_UNICODE *collstart; |
| 7611 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7612 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7613 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7614 | *output++ = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7615 | ++p; |
| 7616 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7617 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7618 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 7619 | if (decimal >= 0) { |
| 7620 | *output++ = '0' + decimal; |
| 7621 | ++p; |
| 7622 | continue; |
| 7623 | } |
| 7624 | if (0 < ch && ch < 256) { |
| 7625 | *output++ = (char)ch; |
| 7626 | ++p; |
| 7627 | continue; |
| 7628 | } |
| 7629 | /* All other characters are considered unencodable */ |
| 7630 | collstart = p; |
| 7631 | collend = p+1; |
| 7632 | while (collend < end) { |
| 7633 | if ((0 < *collend && *collend < 256) || |
| 7634 | !Py_UNICODE_ISSPACE(*collend) || |
| 7635 | Py_UNICODE_TODECIMAL(*collend)) |
| 7636 | break; |
| 7637 | } |
| 7638 | /* cache callback name lookup |
| 7639 | * (if not done yet, i.e. it's the first error) */ |
| 7640 | if (known_errorHandler==-1) { |
| 7641 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 7642 | known_errorHandler = 1; |
| 7643 | else if (!strcmp(errors, "replace")) |
| 7644 | known_errorHandler = 2; |
| 7645 | else if (!strcmp(errors, "ignore")) |
| 7646 | known_errorHandler = 3; |
| 7647 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 7648 | known_errorHandler = 4; |
| 7649 | else |
| 7650 | known_errorHandler = 0; |
| 7651 | } |
| 7652 | switch (known_errorHandler) { |
| 7653 | case 1: /* strict */ |
| 7654 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 7655 | goto onError; |
| 7656 | case 2: /* replace */ |
| 7657 | for (p = collstart; p < collend; ++p) |
| 7658 | *output++ = '?'; |
| 7659 | /* fall through */ |
| 7660 | case 3: /* ignore */ |
| 7661 | p = collend; |
| 7662 | break; |
| 7663 | case 4: /* xmlcharrefreplace */ |
| 7664 | /* generate replacement (temporarily (mis)uses p) */ |
| 7665 | for (p = collstart; p < collend; ++p) |
| 7666 | output += sprintf(output, "&#%d;", (int)*p); |
| 7667 | p = collend; |
| 7668 | break; |
| 7669 | default: |
| 7670 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 7671 | encoding, reason, s, length, &exc, |
| 7672 | collstart-s, collend-s, &newpos); |
| 7673 | if (repunicode == NULL) |
| 7674 | goto onError; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7675 | if (!PyUnicode_Check(repunicode)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7676 | /* Byte results not supported, since they have no decimal property. */ |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7677 | PyErr_SetString(PyExc_TypeError, "error handler should return unicode"); |
| 7678 | Py_DECREF(repunicode); |
| 7679 | goto onError; |
| 7680 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7681 | /* generate replacement */ |
| 7682 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 7683 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 7684 | Py_UNICODE ch = *uni2; |
| 7685 | if (Py_UNICODE_ISSPACE(ch)) |
| 7686 | *output++ = ' '; |
| 7687 | else { |
| 7688 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 7689 | if (decimal >= 0) |
| 7690 | *output++ = '0' + decimal; |
| 7691 | else if (0 < ch && ch < 256) |
| 7692 | *output++ = (char)ch; |
| 7693 | else { |
| 7694 | Py_DECREF(repunicode); |
| 7695 | raise_encode_exception(&exc, encoding, |
| 7696 | s, length, collstart-s, collend-s, reason); |
| 7697 | goto onError; |
| 7698 | } |
| 7699 | } |
| 7700 | } |
| 7701 | p = s + newpos; |
| 7702 | Py_DECREF(repunicode); |
| 7703 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 7704 | } |
| 7705 | /* 0-terminate the output string */ |
| 7706 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7707 | Py_XDECREF(exc); |
| 7708 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 7709 | return 0; |
| 7710 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7711 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7712 | Py_XDECREF(exc); |
| 7713 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 7714 | return -1; |
| 7715 | } |
| 7716 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7717 | /* --- Helpers ------------------------------------------------------------ */ |
| 7718 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7719 | #include "stringlib/ucs1lib.h" |
| 7720 | #include "stringlib/fastsearch.h" |
| 7721 | #include "stringlib/partition.h" |
| 7722 | #include "stringlib/split.h" |
| 7723 | #include "stringlib/count.h" |
| 7724 | #include "stringlib/find.h" |
| 7725 | #include "stringlib/localeutil.h" |
| 7726 | #include "stringlib/undef.h" |
| 7727 | |
| 7728 | #include "stringlib/ucs2lib.h" |
| 7729 | #include "stringlib/fastsearch.h" |
| 7730 | #include "stringlib/partition.h" |
| 7731 | #include "stringlib/split.h" |
| 7732 | #include "stringlib/count.h" |
| 7733 | #include "stringlib/find.h" |
| 7734 | #include "stringlib/localeutil.h" |
| 7735 | #include "stringlib/undef.h" |
| 7736 | |
| 7737 | #include "stringlib/ucs4lib.h" |
| 7738 | #include "stringlib/fastsearch.h" |
| 7739 | #include "stringlib/partition.h" |
| 7740 | #include "stringlib/split.h" |
| 7741 | #include "stringlib/count.h" |
| 7742 | #include "stringlib/find.h" |
| 7743 | #include "stringlib/localeutil.h" |
| 7744 | #include "stringlib/undef.h" |
| 7745 | |
| 7746 | static Py_ssize_t |
| 7747 | any_find_slice(Py_ssize_t Py_LOCAL_CALLBACK(ucs1)(const Py_UCS1*, Py_ssize_t, |
| 7748 | const Py_UCS1*, Py_ssize_t, |
| 7749 | Py_ssize_t, Py_ssize_t), |
| 7750 | Py_ssize_t Py_LOCAL_CALLBACK(ucs2)(const Py_UCS2*, Py_ssize_t, |
| 7751 | const Py_UCS2*, Py_ssize_t, |
| 7752 | Py_ssize_t, Py_ssize_t), |
| 7753 | Py_ssize_t Py_LOCAL_CALLBACK(ucs4)(const Py_UCS4*, Py_ssize_t, |
| 7754 | const Py_UCS4*, Py_ssize_t, |
| 7755 | Py_ssize_t, Py_ssize_t), |
| 7756 | PyObject* s1, PyObject* s2, |
| 7757 | Py_ssize_t start, |
| 7758 | Py_ssize_t end) |
| 7759 | { |
| 7760 | int kind1, kind2, kind; |
| 7761 | void *buf1, *buf2; |
| 7762 | Py_ssize_t len1, len2, result; |
| 7763 | |
| 7764 | kind1 = PyUnicode_KIND(s1); |
| 7765 | kind2 = PyUnicode_KIND(s2); |
| 7766 | kind = kind1 > kind2 ? kind1 : kind2; |
| 7767 | buf1 = PyUnicode_DATA(s1); |
| 7768 | buf2 = PyUnicode_DATA(s2); |
| 7769 | if (kind1 != kind) |
| 7770 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 7771 | if (!buf1) |
| 7772 | return -2; |
| 7773 | if (kind2 != kind) |
| 7774 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 7775 | if (!buf2) { |
| 7776 | if (kind1 != kind) PyMem_Free(buf1); |
| 7777 | return -2; |
| 7778 | } |
| 7779 | len1 = PyUnicode_GET_LENGTH(s1); |
| 7780 | len2 = PyUnicode_GET_LENGTH(s2); |
| 7781 | |
| 7782 | switch(kind) { |
| 7783 | case PyUnicode_1BYTE_KIND: |
| 7784 | result = ucs1(buf1, len1, buf2, len2, start, end); |
| 7785 | break; |
| 7786 | case PyUnicode_2BYTE_KIND: |
| 7787 | result = ucs2(buf1, len1, buf2, len2, start, end); |
| 7788 | break; |
| 7789 | case PyUnicode_4BYTE_KIND: |
| 7790 | result = ucs4(buf1, len1, buf2, len2, start, end); |
| 7791 | break; |
| 7792 | default: |
| 7793 | assert(0); result = -2; |
| 7794 | } |
| 7795 | |
| 7796 | if (kind1 != kind) |
| 7797 | PyMem_Free(buf1); |
| 7798 | if (kind2 != kind) |
| 7799 | PyMem_Free(buf2); |
| 7800 | |
| 7801 | return result; |
| 7802 | } |
| 7803 | |
| 7804 | Py_ssize_t |
| 7805 | _PyUnicode_InsertThousandsGrouping(int kind, void *data, |
| 7806 | Py_ssize_t n_buffer, |
| 7807 | void *digits, Py_ssize_t n_digits, |
| 7808 | Py_ssize_t min_width, |
| 7809 | const char *grouping, |
| 7810 | const char *thousands_sep) |
| 7811 | { |
| 7812 | switch(kind) { |
| 7813 | case PyUnicode_1BYTE_KIND: |
| 7814 | return _PyUnicode_ucs1_InsertThousandsGrouping( |
| 7815 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 7816 | min_width, grouping, thousands_sep); |
| 7817 | case PyUnicode_2BYTE_KIND: |
| 7818 | return _PyUnicode_ucs2_InsertThousandsGrouping( |
| 7819 | (Py_UCS2*)data, n_buffer, (Py_UCS2*)digits, n_digits, |
| 7820 | min_width, grouping, thousands_sep); |
| 7821 | case PyUnicode_4BYTE_KIND: |
| 7822 | return _PyUnicode_ucs4_InsertThousandsGrouping( |
| 7823 | (Py_UCS4*)data, n_buffer, (Py_UCS4*)digits, n_digits, |
| 7824 | min_width, grouping, thousands_sep); |
| 7825 | } |
| 7826 | assert(0); |
| 7827 | return -1; |
| 7828 | } |
| 7829 | |
| 7830 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 7831 | #include "stringlib/unicodedefs.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7832 | #include "stringlib/fastsearch.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 7833 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7834 | #include "stringlib/count.h" |
| 7835 | #include "stringlib/find.h" |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 7836 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7837 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 7838 | #define ADJUST_INDICES(start, end, len) \ |
| 7839 | if (end > len) \ |
| 7840 | end = len; \ |
| 7841 | else if (end < 0) { \ |
| 7842 | end += len; \ |
| 7843 | if (end < 0) \ |
| 7844 | end = 0; \ |
| 7845 | } \ |
| 7846 | if (start < 0) { \ |
| 7847 | start += len; \ |
| 7848 | if (start < 0) \ |
| 7849 | start = 0; \ |
| 7850 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7851 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7852 | Py_ssize_t |
| 7853 | PyUnicode_Count(PyObject *str, |
| 7854 | PyObject *substr, |
| 7855 | Py_ssize_t start, |
| 7856 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7857 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7858 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7859 | PyUnicodeObject* str_obj; |
| 7860 | PyUnicodeObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7861 | int kind1, kind2, kind; |
| 7862 | void *buf1 = NULL, *buf2 = NULL; |
| 7863 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7864 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7865 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7866 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7867 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7868 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7869 | if (!sub_obj || PyUnicode_READY(str_obj) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7870 | Py_DECREF(str_obj); |
| 7871 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7872 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7873 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7874 | kind1 = PyUnicode_KIND(str_obj); |
| 7875 | kind2 = PyUnicode_KIND(sub_obj); |
| 7876 | kind = kind1 > kind2 ? kind1 : kind2; |
| 7877 | buf1 = PyUnicode_DATA(str_obj); |
| 7878 | if (kind1 != kind) |
| 7879 | buf1 = _PyUnicode_AsKind((PyObject*)str_obj, kind); |
| 7880 | if (!buf1) |
| 7881 | goto onError; |
| 7882 | buf2 = PyUnicode_DATA(sub_obj); |
| 7883 | if (kind2 != kind) |
| 7884 | buf2 = _PyUnicode_AsKind((PyObject*)sub_obj, kind); |
| 7885 | if (!buf2) |
| 7886 | goto onError; |
| 7887 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 7888 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 7889 | |
| 7890 | ADJUST_INDICES(start, end, len1); |
| 7891 | switch(kind) { |
| 7892 | case PyUnicode_1BYTE_KIND: |
| 7893 | result = ucs1lib_count( |
| 7894 | ((Py_UCS1*)buf1) + start, end - start, |
| 7895 | buf2, len2, PY_SSIZE_T_MAX |
| 7896 | ); |
| 7897 | break; |
| 7898 | case PyUnicode_2BYTE_KIND: |
| 7899 | result = ucs2lib_count( |
| 7900 | ((Py_UCS2*)buf1) + start, end - start, |
| 7901 | buf2, len2, PY_SSIZE_T_MAX |
| 7902 | ); |
| 7903 | break; |
| 7904 | case PyUnicode_4BYTE_KIND: |
| 7905 | result = ucs4lib_count( |
| 7906 | ((Py_UCS4*)buf1) + start, end - start, |
| 7907 | buf2, len2, PY_SSIZE_T_MAX |
| 7908 | ); |
| 7909 | break; |
| 7910 | default: |
| 7911 | assert(0); result = 0; |
| 7912 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7913 | |
| 7914 | Py_DECREF(sub_obj); |
| 7915 | Py_DECREF(str_obj); |
| 7916 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7917 | if (kind1 != kind) |
| 7918 | PyMem_Free(buf1); |
| 7919 | if (kind2 != kind) |
| 7920 | PyMem_Free(buf2); |
| 7921 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7922 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7923 | onError: |
| 7924 | Py_DECREF(sub_obj); |
| 7925 | Py_DECREF(str_obj); |
| 7926 | if (kind1 != kind && buf1) |
| 7927 | PyMem_Free(buf1); |
| 7928 | if (kind2 != kind && buf2) |
| 7929 | PyMem_Free(buf2); |
| 7930 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7931 | } |
| 7932 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7933 | Py_ssize_t |
| 7934 | PyUnicode_Find(PyObject *str, |
| 7935 | PyObject *sub, |
| 7936 | Py_ssize_t start, |
| 7937 | Py_ssize_t end, |
| 7938 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7939 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7940 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7941 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7942 | str = PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7943 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7944 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7945 | sub = PyUnicode_FromObject(sub); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7946 | if (!sub || PyUnicode_READY(sub) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7947 | Py_DECREF(str); |
| 7948 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7949 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7950 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7951 | if (direction > 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7952 | result = any_find_slice( |
| 7953 | ucs1lib_find_slice, ucs2lib_find_slice, ucs4lib_find_slice, |
| 7954 | str, sub, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7955 | ); |
| 7956 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7957 | result = any_find_slice( |
| 7958 | ucs1lib_rfind_slice, ucs2lib_rfind_slice, ucs4lib_rfind_slice, |
| 7959 | str, sub, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7960 | ); |
| 7961 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7962 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7963 | Py_DECREF(sub); |
| 7964 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7965 | return result; |
| 7966 | } |
| 7967 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7968 | Py_ssize_t |
| 7969 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 7970 | Py_ssize_t start, Py_ssize_t end, |
| 7971 | int direction) |
| 7972 | { |
| 7973 | char *result; |
| 7974 | int kind; |
| 7975 | if (PyUnicode_READY(str) == -1) |
| 7976 | return -2; |
| 7977 | if (end > PyUnicode_GET_LENGTH(str)) |
| 7978 | end = PyUnicode_GET_LENGTH(str); |
| 7979 | kind = PyUnicode_KIND(str); |
| 7980 | result = findchar(PyUnicode_1BYTE_DATA(str) |
| 7981 | + PyUnicode_KIND_SIZE(kind, start), |
| 7982 | kind, |
| 7983 | end-start, ch, direction); |
| 7984 | if (!result) |
| 7985 | return -1; |
| 7986 | return (result-(char*)PyUnicode_DATA(str)) >> (kind-1); |
| 7987 | } |
| 7988 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7989 | static int |
| 7990 | tailmatch(PyUnicodeObject *self, |
| 7991 | PyUnicodeObject *substring, |
| 7992 | Py_ssize_t start, |
| 7993 | Py_ssize_t end, |
| 7994 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7995 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7996 | int kind_self; |
| 7997 | int kind_sub; |
| 7998 | void *data_self; |
| 7999 | void *data_sub; |
| 8000 | Py_ssize_t offset; |
| 8001 | Py_ssize_t i; |
| 8002 | Py_ssize_t end_sub; |
| 8003 | |
| 8004 | if (PyUnicode_READY(self) == -1 || |
| 8005 | PyUnicode_READY(substring) == -1) |
| 8006 | return 0; |
| 8007 | |
| 8008 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8009 | return 1; |
| 8010 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8011 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 8012 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8013 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8014 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8015 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8016 | kind_self = PyUnicode_KIND(self); |
| 8017 | data_self = PyUnicode_DATA(self); |
| 8018 | kind_sub = PyUnicode_KIND(substring); |
| 8019 | data_sub = PyUnicode_DATA(substring); |
| 8020 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 8021 | |
| 8022 | if (direction > 0) |
| 8023 | offset = end; |
| 8024 | else |
| 8025 | offset = start; |
| 8026 | |
| 8027 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 8028 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 8029 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 8030 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 8031 | /* If both are of the same kind, memcmp is sufficient */ |
| 8032 | if (kind_self == kind_sub) { |
| 8033 | return ! memcmp((char *)data_self + |
| 8034 | (offset * PyUnicode_CHARACTER_SIZE(substring)), |
| 8035 | data_sub, |
| 8036 | PyUnicode_GET_LENGTH(substring) * |
| 8037 | PyUnicode_CHARACTER_SIZE(substring)); |
| 8038 | } |
| 8039 | /* otherwise we have to compare each character by first accesing it */ |
| 8040 | else { |
| 8041 | /* We do not need to compare 0 and len(substring)-1 because |
| 8042 | the if statement above ensured already that they are equal |
| 8043 | when we end up here. */ |
| 8044 | // TODO: honor direction and do a forward or backwards search |
| 8045 | for (i = 1; i < end_sub; ++i) { |
| 8046 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 8047 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 8048 | return 0; |
| 8049 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8050 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8051 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8052 | } |
| 8053 | |
| 8054 | return 0; |
| 8055 | } |
| 8056 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8057 | Py_ssize_t |
| 8058 | PyUnicode_Tailmatch(PyObject *str, |
| 8059 | PyObject *substr, |
| 8060 | Py_ssize_t start, |
| 8061 | Py_ssize_t end, |
| 8062 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8063 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8064 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8065 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8066 | str = PyUnicode_FromObject(str); |
| 8067 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8068 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8069 | substr = PyUnicode_FromObject(substr); |
| 8070 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8071 | Py_DECREF(str); |
| 8072 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8073 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8074 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8075 | result = tailmatch((PyUnicodeObject *)str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8076 | (PyUnicodeObject *)substr, |
| 8077 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8078 | Py_DECREF(str); |
| 8079 | Py_DECREF(substr); |
| 8080 | return result; |
| 8081 | } |
| 8082 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8083 | /* Apply fixfct filter to the Unicode object self and return a |
| 8084 | reference to the modified object */ |
| 8085 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8086 | static PyObject * |
| 8087 | fixup(PyUnicodeObject *self, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8088 | Py_UCS4 (*fixfct)(PyUnicodeObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8089 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8090 | PyObject *u; |
| 8091 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8092 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8093 | if (PyUnicode_READY(self) == -1) |
| 8094 | return NULL; |
| 8095 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(self); |
| 8096 | u = PyUnicode_New(PyUnicode_GET_LENGTH(self), |
| 8097 | maxchar_old); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8098 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8099 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8100 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8101 | Py_MEMCPY(PyUnicode_1BYTE_DATA(u), PyUnicode_1BYTE_DATA(self), |
| 8102 | PyUnicode_GET_LENGTH(u) * PyUnicode_CHARACTER_SIZE(u)); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8103 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8104 | /* fix functions return the new maximum character in a string, |
| 8105 | if the kind of the resulting unicode object does not change, |
| 8106 | everything is fine. Otherwise we need to change the string kind |
| 8107 | and re-run the fix function. */ |
| 8108 | maxchar_new = fixfct((PyUnicodeObject*)u); |
| 8109 | if (maxchar_new == 0) |
| 8110 | /* do nothing, keep maxchar_new at 0 which means no changes. */; |
| 8111 | else if (maxchar_new <= 127) |
| 8112 | maxchar_new = 127; |
| 8113 | else if (maxchar_new <= 255) |
| 8114 | maxchar_new = 255; |
| 8115 | else if (maxchar_new <= 65535) |
| 8116 | maxchar_new = 65535; |
| 8117 | else |
| 8118 | maxchar_new = 1114111; /* 0x10ffff */ |
| 8119 | |
| 8120 | if (!maxchar_new && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8121 | /* fixfct should return TRUE if it modified the buffer. If |
| 8122 | FALSE, return a reference to the original buffer instead |
| 8123 | (to save space, not time) */ |
| 8124 | Py_INCREF(self); |
| 8125 | Py_DECREF(u); |
| 8126 | return (PyObject*) self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8127 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8128 | else if (maxchar_new == maxchar_old) { |
| 8129 | return u; |
| 8130 | } |
| 8131 | else { |
| 8132 | /* In case the maximum character changed, we need to |
| 8133 | convert the string to the new category. */ |
| 8134 | PyObject *v = PyUnicode_New( |
| 8135 | PyUnicode_GET_LENGTH(self), maxchar_new); |
| 8136 | if (v == NULL) { |
| 8137 | Py_DECREF(u); |
| 8138 | return NULL; |
| 8139 | } |
| 8140 | if (maxchar_new > maxchar_old) { |
| 8141 | /* If the maxchar increased so that the kind changed, not all |
| 8142 | characters are representable anymore and we need to fix the |
| 8143 | string again. This only happens in very few cases. */ |
| 8144 | PyUnicode_CopyCharacters(v, 0, (PyObject*)self, 0, PyUnicode_GET_LENGTH(self)); |
| 8145 | maxchar_old = fixfct((PyUnicodeObject*)v); |
| 8146 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
| 8147 | } |
| 8148 | else |
| 8149 | PyUnicode_CopyCharacters(v, 0, u, 0, PyUnicode_GET_LENGTH(self)); |
| 8150 | |
| 8151 | Py_DECREF(u); |
| 8152 | return v; |
| 8153 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8154 | } |
| 8155 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8156 | static Py_UCS4 |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8157 | fixupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8158 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8159 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8160 | called as a callback from fixup() which does it already. */ |
| 8161 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8162 | const int kind = PyUnicode_KIND(self); |
| 8163 | void *data = PyUnicode_DATA(self); |
| 8164 | int touched = 0; |
| 8165 | Py_UCS4 maxchar = 0; |
| 8166 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8167 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8168 | for (i = 0; i < len; ++i) { |
| 8169 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8170 | const Py_UCS4 up = Py_UNICODE_TOUPPER(ch); |
| 8171 | if (up != ch) { |
| 8172 | if (up > maxchar) |
| 8173 | maxchar = up; |
| 8174 | PyUnicode_WRITE(kind, data, i, up); |
| 8175 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8176 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8177 | else if (ch > maxchar) |
| 8178 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8179 | } |
| 8180 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8181 | if (touched) |
| 8182 | return maxchar; |
| 8183 | else |
| 8184 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8185 | } |
| 8186 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8187 | static Py_UCS4 |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8188 | fixlower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8189 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8190 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8191 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8192 | const int kind = PyUnicode_KIND(self); |
| 8193 | void *data = PyUnicode_DATA(self); |
| 8194 | int touched = 0; |
| 8195 | Py_UCS4 maxchar = 0; |
| 8196 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8197 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8198 | for(i = 0; i < len; ++i) { |
| 8199 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8200 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 8201 | if (lo != ch) { |
| 8202 | if (lo > maxchar) |
| 8203 | maxchar = lo; |
| 8204 | PyUnicode_WRITE(kind, data, i, lo); |
| 8205 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8206 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8207 | else if (ch > maxchar) |
| 8208 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8209 | } |
| 8210 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8211 | if (touched) |
| 8212 | return maxchar; |
| 8213 | else |
| 8214 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8215 | } |
| 8216 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8217 | static Py_UCS4 |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8218 | fixswapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8219 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8220 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8221 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8222 | const int kind = PyUnicode_KIND(self); |
| 8223 | void *data = PyUnicode_DATA(self); |
| 8224 | int touched = 0; |
| 8225 | Py_UCS4 maxchar = 0; |
| 8226 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8227 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8228 | for(i = 0; i < len; ++i) { |
| 8229 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8230 | Py_UCS4 nu = 0; |
| 8231 | |
| 8232 | if (Py_UNICODE_ISUPPER(ch)) |
| 8233 | nu = Py_UNICODE_TOLOWER(ch); |
| 8234 | else if (Py_UNICODE_ISLOWER(ch)) |
| 8235 | nu = Py_UNICODE_TOUPPER(ch); |
| 8236 | |
| 8237 | if (nu != 0) { |
| 8238 | if (nu > maxchar) |
| 8239 | maxchar = nu; |
| 8240 | PyUnicode_WRITE(kind, data, i, nu); |
| 8241 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8242 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8243 | else if (ch > maxchar) |
| 8244 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8245 | } |
| 8246 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8247 | if (touched) |
| 8248 | return maxchar; |
| 8249 | else |
| 8250 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8251 | } |
| 8252 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8253 | static Py_UCS4 |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8254 | fixcapitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8255 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8256 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8257 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8258 | const int kind = PyUnicode_KIND(self); |
| 8259 | void *data = PyUnicode_DATA(self); |
| 8260 | int touched = 0; |
| 8261 | Py_UCS4 maxchar = 0; |
| 8262 | Py_ssize_t i = 0; |
| 8263 | Py_UCS4 ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8264 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 8265 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8266 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8267 | |
| 8268 | ch = PyUnicode_READ(kind, data, i); |
| 8269 | if (!Py_UNICODE_ISUPPER(ch)) { |
| 8270 | maxchar = Py_UNICODE_TOUPPER(ch); |
| 8271 | PyUnicode_WRITE(kind, data, i, maxchar); |
| 8272 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8273 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8274 | ++i; |
| 8275 | for(; i < len; ++i) { |
| 8276 | ch = PyUnicode_READ(kind, data, i); |
| 8277 | if (!Py_UNICODE_ISLOWER(ch)) { |
| 8278 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 8279 | if (lo > maxchar) |
| 8280 | maxchar = lo; |
| 8281 | PyUnicode_WRITE(kind, data, i, lo); |
| 8282 | touched = 1; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 8283 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8284 | else if (ch > maxchar) |
| 8285 | maxchar = ch; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 8286 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8287 | |
| 8288 | if (touched) |
| 8289 | return maxchar; |
| 8290 | else |
| 8291 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8292 | } |
| 8293 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8294 | static Py_UCS4 |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8295 | fixtitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8296 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8297 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8298 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8299 | const int kind = PyUnicode_KIND(self); |
| 8300 | void *data = PyUnicode_DATA(self); |
| 8301 | Py_UCS4 maxchar = 0; |
| 8302 | Py_ssize_t i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8303 | int previous_is_cased; |
| 8304 | |
| 8305 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8306 | if (len == 1) { |
| 8307 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8308 | const Py_UCS4 ti = Py_UNICODE_TOTITLE(ch); |
| 8309 | if (ti != ch) { |
| 8310 | PyUnicode_WRITE(kind, data, i, ti); |
| 8311 | return ti; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8312 | } |
| 8313 | else |
| 8314 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8315 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8316 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8317 | for(; i < len; ++i) { |
| 8318 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8319 | Py_UCS4 nu; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8320 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8321 | if (previous_is_cased) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8322 | nu = Py_UNICODE_TOLOWER(ch); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8323 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8324 | nu = Py_UNICODE_TOTITLE(ch); |
| 8325 | |
| 8326 | if (nu > maxchar) |
| 8327 | maxchar = nu; |
| 8328 | PyUnicode_WRITE(kind, data, i, nu); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8329 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8330 | if (Py_UNICODE_ISLOWER(ch) || |
| 8331 | Py_UNICODE_ISUPPER(ch) || |
| 8332 | Py_UNICODE_ISTITLE(ch)) |
| 8333 | previous_is_cased = 1; |
| 8334 | else |
| 8335 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8336 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8337 | return maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8338 | } |
| 8339 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8340 | PyObject * |
| 8341 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8342 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8343 | PyObject *sep = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8344 | Py_ssize_t seplen = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8345 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8346 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8347 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 8348 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8349 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8350 | Py_ssize_t sz, i, res_offset; |
| 8351 | Py_UCS4 maxchar = 0; |
| 8352 | Py_UCS4 item_maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8353 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8354 | fseq = PySequence_Fast(seq, ""); |
| 8355 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8356 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8357 | } |
| 8358 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8359 | /* NOTE: the following code can't call back into Python code, |
| 8360 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 8361 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8362 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8363 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 8364 | /* If empty sequence, return u"". */ |
| 8365 | if (seqlen == 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8366 | res = PyUnicode_New(0, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8367 | goto Done; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8368 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8369 | items = PySequence_Fast_ITEMS(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8370 | /* If singleton sequence with an exact Unicode, return that. */ |
| 8371 | if (seqlen == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8372 | item = items[0]; |
| 8373 | if (PyUnicode_CheckExact(item)) { |
| 8374 | Py_INCREF(item); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8375 | res = item; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8376 | goto Done; |
| 8377 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8378 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8379 | else { |
| 8380 | /* Set up sep and seplen */ |
| 8381 | if (separator == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8382 | /* fall back to a blank space separator */ |
| 8383 | sep = PyUnicode_FromOrdinal(' '); |
| 8384 | if (!sep || PyUnicode_READY(sep) == -1) |
| 8385 | goto onError; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8386 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8387 | else { |
| 8388 | if (!PyUnicode_Check(separator)) { |
| 8389 | PyErr_Format(PyExc_TypeError, |
| 8390 | "separator: expected str instance," |
| 8391 | " %.80s found", |
| 8392 | Py_TYPE(separator)->tp_name); |
| 8393 | goto onError; |
| 8394 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8395 | if (PyUnicode_READY(separator) == -1) |
| 8396 | goto onError; |
| 8397 | sep = separator; |
| 8398 | seplen = PyUnicode_GET_LENGTH(separator); |
| 8399 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 8400 | /* inc refcount to keep this code path symetric with the |
| 8401 | above case of a blank separator */ |
| 8402 | Py_INCREF(sep); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8403 | } |
| 8404 | } |
| 8405 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8406 | /* There are at least two things to join, or else we have a subclass |
| 8407 | * of str in the sequence. |
| 8408 | * Do a pre-pass to figure out the total amount of space we'll |
| 8409 | * need (sz), and see whether all argument are strings. |
| 8410 | */ |
| 8411 | sz = 0; |
| 8412 | for (i = 0; i < seqlen; i++) { |
| 8413 | const Py_ssize_t old_sz = sz; |
| 8414 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8415 | if (!PyUnicode_Check(item)) { |
| 8416 | PyErr_Format(PyExc_TypeError, |
| 8417 | "sequence item %zd: expected str instance," |
| 8418 | " %.80s found", |
| 8419 | i, Py_TYPE(item)->tp_name); |
| 8420 | goto onError; |
| 8421 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8422 | if (PyUnicode_READY(item) == -1) |
| 8423 | goto onError; |
| 8424 | sz += PyUnicode_GET_LENGTH(item); |
| 8425 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
| 8426 | if (item_maxchar > maxchar) |
| 8427 | maxchar = item_maxchar; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8428 | if (i != 0) |
| 8429 | sz += seplen; |
| 8430 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 8431 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8432 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8433 | goto onError; |
| 8434 | } |
| 8435 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8436 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8437 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8438 | if (res == NULL) |
| 8439 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 8440 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8441 | /* Catenate everything. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8442 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8443 | Py_ssize_t itemlen; |
| 8444 | item = items[i]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8445 | itemlen = PyUnicode_GET_LENGTH(item); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8446 | /* Copy item, and maybe the separator. */ |
| 8447 | if (i) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8448 | PyUnicode_CopyCharacters(res, res_offset, |
| 8449 | sep, 0, seplen); |
| 8450 | res_offset += seplen; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8451 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8452 | PyUnicode_CopyCharacters(res, res_offset, |
| 8453 | item, 0, itemlen); |
| 8454 | res_offset += itemlen; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8455 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8456 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8457 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8458 | Done: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8459 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8460 | Py_XDECREF(sep); |
| 8461 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8462 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8463 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8464 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8465 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8466 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8467 | return NULL; |
| 8468 | } |
| 8469 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8470 | #define FILL(kind, data, value, start, length) \ |
| 8471 | do { \ |
| 8472 | Py_ssize_t i_ = 0; \ |
| 8473 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 8474 | switch ((kind)) { \ |
| 8475 | case PyUnicode_1BYTE_KIND: { \ |
| 8476 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
| 8477 | memset(to_, (unsigned char)value, length); \ |
| 8478 | break; \ |
| 8479 | } \ |
| 8480 | case PyUnicode_2BYTE_KIND: { \ |
| 8481 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 8482 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 8483 | break; \ |
| 8484 | } \ |
| 8485 | default: { \ |
| 8486 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 8487 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 8488 | break; \ |
| 8489 | } \ |
| 8490 | } \ |
| 8491 | } while (0) |
| 8492 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8493 | static PyUnicodeObject * |
| 8494 | pad(PyUnicodeObject *self, |
| 8495 | Py_ssize_t left, |
| 8496 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8497 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8498 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8499 | PyObject *u; |
| 8500 | Py_UCS4 maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8501 | |
| 8502 | if (left < 0) |
| 8503 | left = 0; |
| 8504 | if (right < 0) |
| 8505 | right = 0; |
| 8506 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 8507 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8508 | Py_INCREF(self); |
| 8509 | return self; |
| 8510 | } |
| 8511 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8512 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 8513 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 8514 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 8515 | return NULL; |
| 8516 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8517 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 8518 | if (fill > maxchar) |
| 8519 | maxchar = fill; |
| 8520 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8521 | if (u) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8522 | int kind = PyUnicode_KIND(u); |
| 8523 | void *data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8524 | if (left) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8525 | FILL(kind, data, fill, 0, left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8526 | if (right) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8527 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
| 8528 | PyUnicode_CopyCharacters(u, left, (PyObject*)self, 0, _PyUnicode_LENGTH(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8529 | } |
| 8530 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8531 | return (PyUnicodeObject*)u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8532 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8533 | #undef FILL |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8534 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8535 | PyObject * |
| 8536 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8537 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8538 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8539 | |
| 8540 | string = PyUnicode_FromObject(string); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8541 | if (string == NULL || PyUnicode_READY(string) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8542 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8543 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8544 | switch(PyUnicode_KIND(string)) { |
| 8545 | case PyUnicode_1BYTE_KIND: |
| 8546 | list = ucs1lib_splitlines( |
| 8547 | (PyObject*) string, PyUnicode_1BYTE_DATA(string), |
| 8548 | PyUnicode_GET_LENGTH(string), keepends); |
| 8549 | break; |
| 8550 | case PyUnicode_2BYTE_KIND: |
| 8551 | list = ucs2lib_splitlines( |
| 8552 | (PyObject*) string, PyUnicode_2BYTE_DATA(string), |
| 8553 | PyUnicode_GET_LENGTH(string), keepends); |
| 8554 | break; |
| 8555 | case PyUnicode_4BYTE_KIND: |
| 8556 | list = ucs4lib_splitlines( |
| 8557 | (PyObject*) string, PyUnicode_4BYTE_DATA(string), |
| 8558 | PyUnicode_GET_LENGTH(string), keepends); |
| 8559 | break; |
| 8560 | default: |
| 8561 | assert(0); |
| 8562 | list = 0; |
| 8563 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8564 | Py_DECREF(string); |
| 8565 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8566 | } |
| 8567 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8568 | static PyObject * |
| 8569 | split(PyUnicodeObject *self, |
| 8570 | PyUnicodeObject *substring, |
| 8571 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8572 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8573 | int kind1, kind2, kind; |
| 8574 | void *buf1, *buf2; |
| 8575 | Py_ssize_t len1, len2; |
| 8576 | PyObject* out; |
| 8577 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8578 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8579 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8580 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8581 | if (PyUnicode_READY(self) == -1) |
| 8582 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8583 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8584 | if (substring == NULL) |
| 8585 | switch(PyUnicode_KIND(self)) { |
| 8586 | case PyUnicode_1BYTE_KIND: |
| 8587 | return ucs1lib_split_whitespace( |
| 8588 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 8589 | PyUnicode_GET_LENGTH(self), maxcount |
| 8590 | ); |
| 8591 | case PyUnicode_2BYTE_KIND: |
| 8592 | return ucs2lib_split_whitespace( |
| 8593 | (PyObject*) self, PyUnicode_2BYTE_DATA(self), |
| 8594 | PyUnicode_GET_LENGTH(self), maxcount |
| 8595 | ); |
| 8596 | case PyUnicode_4BYTE_KIND: |
| 8597 | return ucs4lib_split_whitespace( |
| 8598 | (PyObject*) self, PyUnicode_4BYTE_DATA(self), |
| 8599 | PyUnicode_GET_LENGTH(self), maxcount |
| 8600 | ); |
| 8601 | default: |
| 8602 | assert(0); |
| 8603 | return NULL; |
| 8604 | } |
| 8605 | |
| 8606 | if (PyUnicode_READY(substring) == -1) |
| 8607 | return NULL; |
| 8608 | |
| 8609 | kind1 = PyUnicode_KIND(self); |
| 8610 | kind2 = PyUnicode_KIND(substring); |
| 8611 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8612 | buf1 = PyUnicode_DATA(self); |
| 8613 | buf2 = PyUnicode_DATA(substring); |
| 8614 | if (kind1 != kind) |
| 8615 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 8616 | if (!buf1) |
| 8617 | return NULL; |
| 8618 | if (kind2 != kind) |
| 8619 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 8620 | if (!buf2) { |
| 8621 | if (kind1 != kind) PyMem_Free(buf1); |
| 8622 | return NULL; |
| 8623 | } |
| 8624 | len1 = PyUnicode_GET_LENGTH(self); |
| 8625 | len2 = PyUnicode_GET_LENGTH(substring); |
| 8626 | |
| 8627 | switch(kind) { |
| 8628 | case PyUnicode_1BYTE_KIND: |
| 8629 | out = ucs1lib_split( |
| 8630 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 8631 | break; |
| 8632 | case PyUnicode_2BYTE_KIND: |
| 8633 | out = ucs2lib_split( |
| 8634 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 8635 | break; |
| 8636 | case PyUnicode_4BYTE_KIND: |
| 8637 | out = ucs4lib_split( |
| 8638 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 8639 | break; |
| 8640 | default: |
| 8641 | out = NULL; |
| 8642 | } |
| 8643 | if (kind1 != kind) |
| 8644 | PyMem_Free(buf1); |
| 8645 | if (kind2 != kind) |
| 8646 | PyMem_Free(buf2); |
| 8647 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8648 | } |
| 8649 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8650 | static PyObject * |
| 8651 | rsplit(PyUnicodeObject *self, |
| 8652 | PyUnicodeObject *substring, |
| 8653 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8654 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8655 | int kind1, kind2, kind; |
| 8656 | void *buf1, *buf2; |
| 8657 | Py_ssize_t len1, len2; |
| 8658 | PyObject* out; |
| 8659 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8660 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8661 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8662 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8663 | if (PyUnicode_READY(self) == -1) |
| 8664 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8665 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8666 | if (substring == NULL) |
| 8667 | switch(PyUnicode_KIND(self)) { |
| 8668 | case PyUnicode_1BYTE_KIND: |
| 8669 | return ucs1lib_rsplit_whitespace( |
| 8670 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 8671 | PyUnicode_GET_LENGTH(self), maxcount |
| 8672 | ); |
| 8673 | case PyUnicode_2BYTE_KIND: |
| 8674 | return ucs2lib_rsplit_whitespace( |
| 8675 | (PyObject*) self, PyUnicode_2BYTE_DATA(self), |
| 8676 | PyUnicode_GET_LENGTH(self), maxcount |
| 8677 | ); |
| 8678 | case PyUnicode_4BYTE_KIND: |
| 8679 | return ucs4lib_rsplit_whitespace( |
| 8680 | (PyObject*) self, PyUnicode_4BYTE_DATA(self), |
| 8681 | PyUnicode_GET_LENGTH(self), maxcount |
| 8682 | ); |
| 8683 | default: |
| 8684 | assert(0); |
| 8685 | return NULL; |
| 8686 | } |
| 8687 | |
| 8688 | if (PyUnicode_READY(substring) == -1) |
| 8689 | return NULL; |
| 8690 | |
| 8691 | kind1 = PyUnicode_KIND(self); |
| 8692 | kind2 = PyUnicode_KIND(substring); |
| 8693 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8694 | buf1 = PyUnicode_DATA(self); |
| 8695 | buf2 = PyUnicode_DATA(substring); |
| 8696 | if (kind1 != kind) |
| 8697 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 8698 | if (!buf1) |
| 8699 | return NULL; |
| 8700 | if (kind2 != kind) |
| 8701 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 8702 | if (!buf2) { |
| 8703 | if (kind1 != kind) PyMem_Free(buf1); |
| 8704 | return NULL; |
| 8705 | } |
| 8706 | len1 = PyUnicode_GET_LENGTH(self); |
| 8707 | len2 = PyUnicode_GET_LENGTH(substring); |
| 8708 | |
| 8709 | switch(kind) { |
| 8710 | case PyUnicode_1BYTE_KIND: |
| 8711 | out = ucs1lib_rsplit( |
| 8712 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 8713 | break; |
| 8714 | case PyUnicode_2BYTE_KIND: |
| 8715 | out = ucs2lib_rsplit( |
| 8716 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 8717 | break; |
| 8718 | case PyUnicode_4BYTE_KIND: |
| 8719 | out = ucs4lib_rsplit( |
| 8720 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 8721 | break; |
| 8722 | default: |
| 8723 | out = NULL; |
| 8724 | } |
| 8725 | if (kind1 != kind) |
| 8726 | PyMem_Free(buf1); |
| 8727 | if (kind2 != kind) |
| 8728 | PyMem_Free(buf2); |
| 8729 | return out; |
| 8730 | } |
| 8731 | |
| 8732 | static Py_ssize_t |
| 8733 | anylib_find(int kind, void *buf1, Py_ssize_t len1, |
| 8734 | void *buf2, Py_ssize_t len2, Py_ssize_t offset) |
| 8735 | { |
| 8736 | switch(kind) { |
| 8737 | case PyUnicode_1BYTE_KIND: |
| 8738 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
| 8739 | case PyUnicode_2BYTE_KIND: |
| 8740 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 8741 | case PyUnicode_4BYTE_KIND: |
| 8742 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 8743 | } |
| 8744 | assert(0); |
| 8745 | return -1; |
| 8746 | } |
| 8747 | |
| 8748 | static Py_ssize_t |
| 8749 | anylib_count(int kind, void* sbuf, Py_ssize_t slen, |
| 8750 | void *buf1, Py_ssize_t len1, Py_ssize_t maxcount) |
| 8751 | { |
| 8752 | switch(kind) { |
| 8753 | case PyUnicode_1BYTE_KIND: |
| 8754 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
| 8755 | case PyUnicode_2BYTE_KIND: |
| 8756 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 8757 | case PyUnicode_4BYTE_KIND: |
| 8758 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 8759 | } |
| 8760 | assert(0); |
| 8761 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8762 | } |
| 8763 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8764 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8765 | replace(PyObject *self, PyObject *str1, |
| 8766 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8767 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8768 | PyObject *u; |
| 8769 | char *sbuf = PyUnicode_DATA(self); |
| 8770 | char *buf1 = PyUnicode_DATA(str1); |
| 8771 | char *buf2 = PyUnicode_DATA(str2); |
| 8772 | int srelease = 0, release1 = 0, release2 = 0; |
| 8773 | int skind = PyUnicode_KIND(self); |
| 8774 | int kind1 = PyUnicode_KIND(str1); |
| 8775 | int kind2 = PyUnicode_KIND(str2); |
| 8776 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 8777 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 8778 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8779 | |
| 8780 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8781 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8782 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8783 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8784 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8785 | if (skind < kind1) |
| 8786 | /* substring too wide to be present */ |
| 8787 | goto nothing; |
| 8788 | |
| 8789 | if (len1 == len2) { |
Antoine Pitrou | cbfdee3 | 2010-01-13 08:58:08 +0000 | [diff] [blame] | 8790 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8791 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8792 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8793 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8794 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8795 | /* replace characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8796 | Py_UCS4 u1, u2, maxchar; |
| 8797 | int mayshrink, rkind; |
| 8798 | u1 = PyUnicode_READ_CHAR(str1, 0); |
| 8799 | if (!findchar(sbuf, PyUnicode_KIND(self), |
| 8800 | slen, u1, 1)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8801 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8802 | u2 = PyUnicode_READ_CHAR(str2, 0); |
| 8803 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 8804 | /* Replacing u1 with u2 may cause a maxchar reduction in the |
| 8805 | result string. */ |
| 8806 | mayshrink = maxchar > 127; |
| 8807 | if (u2 > maxchar) { |
| 8808 | maxchar = u2; |
| 8809 | mayshrink = 0; |
| 8810 | } |
| 8811 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8812 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8813 | goto error; |
| 8814 | PyUnicode_CopyCharacters(u, 0, |
| 8815 | (PyObject*)self, 0, slen); |
| 8816 | rkind = PyUnicode_KIND(u); |
| 8817 | for (i = 0; i < PyUnicode_GET_LENGTH(u); i++) |
| 8818 | if (PyUnicode_READ(rkind, PyUnicode_DATA(u), i) == u1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8819 | if (--maxcount < 0) |
| 8820 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8821 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), i, u2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8822 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8823 | if (mayshrink) { |
| 8824 | PyObject *tmp = u; |
| 8825 | u = PyUnicode_FromKindAndData(rkind, PyUnicode_DATA(tmp), |
| 8826 | PyUnicode_GET_LENGTH(tmp)); |
| 8827 | Py_DECREF(tmp); |
| 8828 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8829 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8830 | int rkind = skind; |
| 8831 | char *res; |
| 8832 | if (kind1 < rkind) { |
| 8833 | /* widen substring */ |
| 8834 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 8835 | if (!buf1) goto error; |
| 8836 | release1 = 1; |
| 8837 | } |
| 8838 | i = anylib_find(rkind, sbuf, slen, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8839 | if (i < 0) |
| 8840 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8841 | if (rkind > kind2) { |
| 8842 | /* widen replacement */ |
| 8843 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 8844 | if (!buf2) goto error; |
| 8845 | release2 = 1; |
| 8846 | } |
| 8847 | else if (rkind < kind2) { |
| 8848 | /* widen self and buf1 */ |
| 8849 | rkind = kind2; |
| 8850 | if (release1) PyMem_Free(buf1); |
| 8851 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 8852 | if (!sbuf) goto error; |
| 8853 | srelease = 1; |
| 8854 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 8855 | if (!buf1) goto error; |
| 8856 | release1 = 1; |
| 8857 | } |
| 8858 | res = PyMem_Malloc(PyUnicode_KIND_SIZE(rkind, slen)); |
| 8859 | if (!res) { |
| 8860 | PyErr_NoMemory(); |
| 8861 | goto error; |
| 8862 | } |
| 8863 | memcpy(res, sbuf, PyUnicode_KIND_SIZE(rkind, slen)); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8864 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8865 | memcpy(res + PyUnicode_KIND_SIZE(rkind, i), |
| 8866 | buf2, |
| 8867 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 8868 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8869 | |
| 8870 | while ( --maxcount > 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8871 | i = anylib_find(rkind, sbuf+PyUnicode_KIND_SIZE(rkind, i), |
| 8872 | slen-i, |
| 8873 | buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8874 | if (i == -1) |
| 8875 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8876 | memcpy(res + PyUnicode_KIND_SIZE(rkind, i), |
| 8877 | buf2, |
| 8878 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 8879 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8880 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8881 | |
| 8882 | u = PyUnicode_FromKindAndData(rkind, res, slen); |
| 8883 | PyMem_Free(res); |
| 8884 | if (!u) goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8885 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8886 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8887 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8888 | Py_ssize_t n, i, j, ires; |
| 8889 | Py_ssize_t product, new_size; |
| 8890 | int rkind = skind; |
| 8891 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8892 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8893 | if (kind1 < rkind) { |
| 8894 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 8895 | if (!buf1) goto error; |
| 8896 | release1 = 1; |
| 8897 | } |
| 8898 | n = anylib_count(rkind, sbuf, slen, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8899 | if (n == 0) |
| 8900 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8901 | if (kind2 < rkind) { |
| 8902 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 8903 | if (!buf2) goto error; |
| 8904 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8905 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8906 | else if (kind2 > rkind) { |
| 8907 | rkind = kind2; |
| 8908 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 8909 | if (!sbuf) goto error; |
| 8910 | srelease = 1; |
| 8911 | if (release1) PyMem_Free(buf1); |
| 8912 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 8913 | if (!buf1) goto error; |
| 8914 | release1 = 1; |
| 8915 | } |
| 8916 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 8917 | PyUnicode_GET_LENGTH(str1))); */ |
| 8918 | product = n * (len2-len1); |
| 8919 | if ((product / (len2-len1)) != n) { |
| 8920 | PyErr_SetString(PyExc_OverflowError, |
| 8921 | "replace string is too long"); |
| 8922 | goto error; |
| 8923 | } |
| 8924 | new_size = slen + product; |
| 8925 | if (new_size < 0 || new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
| 8926 | PyErr_SetString(PyExc_OverflowError, |
| 8927 | "replace string is too long"); |
| 8928 | goto error; |
| 8929 | } |
| 8930 | res = PyMem_Malloc(PyUnicode_KIND_SIZE(rkind, new_size)); |
| 8931 | if (!res) |
| 8932 | goto error; |
| 8933 | ires = i = 0; |
| 8934 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8935 | while (n-- > 0) { |
| 8936 | /* look for next match */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8937 | j = anylib_find(rkind, |
| 8938 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 8939 | slen-i, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8940 | if (j == -1) |
| 8941 | break; |
| 8942 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8943 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8944 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 8945 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 8946 | PyUnicode_KIND_SIZE(rkind, j-i)); |
| 8947 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8948 | } |
| 8949 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8950 | if (len2 > 0) { |
| 8951 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 8952 | buf2, |
| 8953 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 8954 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8955 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8956 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8957 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8958 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8959 | /* copy tail [i:] */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8960 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 8961 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 8962 | PyUnicode_KIND_SIZE(rkind, slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8963 | } else { |
| 8964 | /* interleave */ |
| 8965 | while (n > 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8966 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 8967 | buf2, |
| 8968 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 8969 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8970 | if (--n <= 0) |
| 8971 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8972 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 8973 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 8974 | PyUnicode_KIND_SIZE(rkind, 1)); |
| 8975 | ires++; |
| 8976 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8977 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8978 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 8979 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 8980 | PyUnicode_KIND_SIZE(rkind, slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8981 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8982 | u = PyUnicode_FromKindAndData(rkind, res, new_size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8983 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8984 | if (srelease) |
| 8985 | PyMem_FREE(sbuf); |
| 8986 | if (release1) |
| 8987 | PyMem_FREE(buf1); |
| 8988 | if (release2) |
| 8989 | PyMem_FREE(buf2); |
| 8990 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8991 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8992 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8993 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8994 | if (srelease) |
| 8995 | PyMem_FREE(sbuf); |
| 8996 | if (release1) |
| 8997 | PyMem_FREE(buf1); |
| 8998 | if (release2) |
| 8999 | PyMem_FREE(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9000 | if (PyUnicode_CheckExact(self)) { |
| 9001 | Py_INCREF(self); |
| 9002 | return (PyObject *) self; |
| 9003 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9004 | return PyUnicode_FromKindAndData(PyUnicode_KIND(self), |
| 9005 | PyUnicode_DATA(self), |
| 9006 | PyUnicode_GET_LENGTH(self)); |
| 9007 | error: |
| 9008 | if (srelease && sbuf) |
| 9009 | PyMem_FREE(sbuf); |
| 9010 | if (release1 && buf1) |
| 9011 | PyMem_FREE(buf1); |
| 9012 | if (release2 && buf2) |
| 9013 | PyMem_FREE(buf2); |
| 9014 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9015 | } |
| 9016 | |
| 9017 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 9018 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9019 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9020 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9021 | \n\ |
| 9022 | 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] | 9023 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9024 | |
| 9025 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9026 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9027 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9028 | return fixup(self, fixtitle); |
| 9029 | } |
| 9030 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9031 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9032 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9033 | \n\ |
| 9034 | 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] | 9035 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9036 | |
| 9037 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9038 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9039 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9040 | return fixup(self, fixcapitalize); |
| 9041 | } |
| 9042 | |
| 9043 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9044 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9045 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9046 | \n\ |
| 9047 | 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] | 9048 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9049 | |
| 9050 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9051 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9052 | { |
| 9053 | PyObject *list; |
| 9054 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9055 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9056 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9057 | /* Split into words */ |
| 9058 | list = split(self, NULL, -1); |
| 9059 | if (!list) |
| 9060 | return NULL; |
| 9061 | |
| 9062 | /* Capitalize each word */ |
| 9063 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 9064 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9065 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9066 | if (item == NULL) |
| 9067 | goto onError; |
| 9068 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 9069 | PyList_SET_ITEM(list, i, item); |
| 9070 | } |
| 9071 | |
| 9072 | /* Join the words to form a new string */ |
| 9073 | item = PyUnicode_Join(NULL, list); |
| 9074 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9075 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9076 | Py_DECREF(list); |
| 9077 | return (PyObject *)item; |
| 9078 | } |
| 9079 | #endif |
| 9080 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9081 | /* Argument converter. Coerces to a single unicode character */ |
| 9082 | |
| 9083 | static int |
| 9084 | convert_uc(PyObject *obj, void *addr) |
| 9085 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9086 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9087 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9088 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9089 | uniobj = PyUnicode_FromObject(obj); |
| 9090 | if (uniobj == NULL) { |
| 9091 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9092 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9093 | return 0; |
| 9094 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9095 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9096 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9097 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9098 | Py_DECREF(uniobj); |
| 9099 | return 0; |
| 9100 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9101 | if (PyUnicode_READY(uniobj)) { |
| 9102 | Py_DECREF(uniobj); |
| 9103 | return 0; |
| 9104 | } |
| 9105 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9106 | Py_DECREF(uniobj); |
| 9107 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9108 | } |
| 9109 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9110 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9111 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9112 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 9113 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9114 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9115 | |
| 9116 | static PyObject * |
| 9117 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 9118 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9119 | Py_ssize_t marg, left; |
| 9120 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9121 | Py_UCS4 fillchar = ' '; |
| 9122 | |
| 9123 | if (PyUnicode_READY(self) == -1) |
| 9124 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9125 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 9126 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9127 | return NULL; |
| 9128 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9129 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9130 | Py_INCREF(self); |
| 9131 | return (PyObject*) self; |
| 9132 | } |
| 9133 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9134 | marg = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9135 | left = marg / 2 + (marg & width & 1); |
| 9136 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9137 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9138 | } |
| 9139 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9140 | #if 0 |
| 9141 | |
| 9142 | /* This code should go into some future Unicode collation support |
| 9143 | module. The basic comparison should compare ordinals on a naive |
Georg Brandl | c6c3178 | 2009-06-08 13:41:29 +0000 | [diff] [blame] | 9144 | basis (this is what Java does and thus Jython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9145 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9146 | /* speedy UTF-16 code point order comparison */ |
| 9147 | /* gleaned from: */ |
| 9148 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 9149 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 9150 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9151 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9152 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9153 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 9154 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 9155 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9156 | }; |
| 9157 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9158 | static int |
| 9159 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 9160 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9161 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9162 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9163 | Py_UNICODE *s1 = str1->str; |
| 9164 | Py_UNICODE *s2 = str2->str; |
| 9165 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9166 | len1 = str1->_base._base.length; |
| 9167 | len2 = str2->_base._base.length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9168 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9169 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9170 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9171 | |
| 9172 | c1 = *s1++; |
| 9173 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 9174 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9175 | if (c1 > (1<<11) * 26) |
| 9176 | c1 += utf16Fixup[c1>>11]; |
| 9177 | if (c2 > (1<<11) * 26) |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9178 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9179 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 9180 | |
| 9181 | if (c1 != c2) |
| 9182 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9183 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9184 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9185 | } |
| 9186 | |
| 9187 | return (len1 < len2) ? -1 : (len1 != len2); |
| 9188 | } |
| 9189 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9190 | #else |
| 9191 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9192 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 9193 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9194 | static int |
| 9195 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 9196 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9197 | int kind1, kind2; |
| 9198 | void *data1, *data2; |
| 9199 | Py_ssize_t len1, len2, i; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9200 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9201 | kind1 = PyUnicode_KIND(str1); |
| 9202 | kind2 = PyUnicode_KIND(str2); |
| 9203 | data1 = PyUnicode_DATA(str1); |
| 9204 | data2 = PyUnicode_DATA(str2); |
| 9205 | len1 = PyUnicode_GET_LENGTH(str1); |
| 9206 | len2 = PyUnicode_GET_LENGTH(str2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9207 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9208 | for (i = 0; i < len1 && i < len2; ++i) { |
| 9209 | Py_UCS4 c1, c2; |
| 9210 | c1 = PyUnicode_READ(kind1, data1, i); |
| 9211 | c2 = PyUnicode_READ(kind2, data2, i); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 9212 | |
| 9213 | if (c1 != c2) |
| 9214 | return (c1 < c2) ? -1 : 1; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9215 | } |
| 9216 | |
| 9217 | return (len1 < len2) ? -1 : (len1 != len2); |
| 9218 | } |
| 9219 | |
| 9220 | #endif |
| 9221 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9222 | int |
| 9223 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9224 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9225 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 9226 | if (PyUnicode_READY(left) == -1 || |
| 9227 | PyUnicode_READY(right) == -1) |
| 9228 | return -1; |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 9229 | return unicode_compare((PyUnicodeObject *)left, |
| 9230 | (PyUnicodeObject *)right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9231 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 9232 | PyErr_Format(PyExc_TypeError, |
| 9233 | "Can't compare %.100s and %.100s", |
| 9234 | left->ob_type->tp_name, |
| 9235 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9236 | return -1; |
| 9237 | } |
| 9238 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9239 | int |
| 9240 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 9241 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9242 | Py_ssize_t i; |
| 9243 | int kind; |
| 9244 | void *data; |
| 9245 | Py_UCS4 chr; |
| 9246 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9247 | assert(PyUnicode_Check(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9248 | if (PyUnicode_READY(uni) == -1) |
| 9249 | return -1; |
| 9250 | kind = PyUnicode_KIND(uni); |
| 9251 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9252 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9253 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 9254 | if (chr != str[i]) |
| 9255 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 9256 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 9257 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9258 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9259 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9260 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9261 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9262 | return 0; |
| 9263 | } |
| 9264 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9265 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9266 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9267 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9268 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9269 | PyObject * |
| 9270 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9271 | { |
| 9272 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9273 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9274 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 9275 | PyObject *v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9276 | if (PyUnicode_READY(left) == -1 || |
| 9277 | PyUnicode_READY(right) == -1) |
| 9278 | return NULL; |
| 9279 | if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) || |
| 9280 | PyUnicode_KIND(left) != PyUnicode_KIND(right)) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9281 | if (op == Py_EQ) { |
| 9282 | Py_INCREF(Py_False); |
| 9283 | return Py_False; |
| 9284 | } |
| 9285 | if (op == Py_NE) { |
| 9286 | Py_INCREF(Py_True); |
| 9287 | return Py_True; |
| 9288 | } |
| 9289 | } |
| 9290 | if (left == right) |
| 9291 | result = 0; |
| 9292 | else |
| 9293 | result = unicode_compare((PyUnicodeObject *)left, |
| 9294 | (PyUnicodeObject *)right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9295 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9296 | /* Convert the return value to a Boolean */ |
| 9297 | switch (op) { |
| 9298 | case Py_EQ: |
| 9299 | v = TEST_COND(result == 0); |
| 9300 | break; |
| 9301 | case Py_NE: |
| 9302 | v = TEST_COND(result != 0); |
| 9303 | break; |
| 9304 | case Py_LE: |
| 9305 | v = TEST_COND(result <= 0); |
| 9306 | break; |
| 9307 | case Py_GE: |
| 9308 | v = TEST_COND(result >= 0); |
| 9309 | break; |
| 9310 | case Py_LT: |
| 9311 | v = TEST_COND(result == -1); |
| 9312 | break; |
| 9313 | case Py_GT: |
| 9314 | v = TEST_COND(result == 1); |
| 9315 | break; |
| 9316 | default: |
| 9317 | PyErr_BadArgument(); |
| 9318 | return NULL; |
| 9319 | } |
| 9320 | Py_INCREF(v); |
| 9321 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9322 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9323 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 9324 | Py_RETURN_NOTIMPLEMENTED; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9325 | } |
| 9326 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9327 | int |
| 9328 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9329 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9330 | PyObject *str, *sub; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9331 | int kind1, kind2, kind; |
| 9332 | void *buf1, *buf2; |
| 9333 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9334 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9335 | |
| 9336 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9337 | sub = PyUnicode_FromObject(element); |
| 9338 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9339 | PyErr_Format(PyExc_TypeError, |
| 9340 | "'in <string>' requires string as left operand, not %s", |
| 9341 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9342 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9343 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9344 | if (PyUnicode_READY(sub) == -1) |
| 9345 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9346 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9347 | str = PyUnicode_FromObject(container); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9348 | if (!str || PyUnicode_READY(container) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9349 | Py_DECREF(sub); |
| 9350 | return -1; |
| 9351 | } |
| 9352 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9353 | kind1 = PyUnicode_KIND(str); |
| 9354 | kind2 = PyUnicode_KIND(sub); |
| 9355 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9356 | buf1 = PyUnicode_DATA(str); |
| 9357 | buf2 = PyUnicode_DATA(sub); |
| 9358 | if (kind1 != kind) |
| 9359 | buf1 = _PyUnicode_AsKind((PyObject*)str, kind); |
| 9360 | if (!buf1) { |
| 9361 | Py_DECREF(sub); |
| 9362 | return -1; |
| 9363 | } |
| 9364 | if (kind2 != kind) |
| 9365 | buf2 = _PyUnicode_AsKind((PyObject*)sub, kind); |
| 9366 | if (!buf2) { |
| 9367 | Py_DECREF(sub); |
| 9368 | if (kind1 != kind) PyMem_Free(buf1); |
| 9369 | return -1; |
| 9370 | } |
| 9371 | len1 = PyUnicode_GET_LENGTH(str); |
| 9372 | len2 = PyUnicode_GET_LENGTH(sub); |
| 9373 | |
| 9374 | switch(kind) { |
| 9375 | case PyUnicode_1BYTE_KIND: |
| 9376 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 9377 | break; |
| 9378 | case PyUnicode_2BYTE_KIND: |
| 9379 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 9380 | break; |
| 9381 | case PyUnicode_4BYTE_KIND: |
| 9382 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 9383 | break; |
| 9384 | default: |
| 9385 | result = -1; |
| 9386 | assert(0); |
| 9387 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9388 | |
| 9389 | Py_DECREF(str); |
| 9390 | Py_DECREF(sub); |
| 9391 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9392 | if (kind1 != kind) |
| 9393 | PyMem_Free(buf1); |
| 9394 | if (kind2 != kind) |
| 9395 | PyMem_Free(buf2); |
| 9396 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9397 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9398 | } |
| 9399 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9400 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 9401 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9402 | PyObject * |
| 9403 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9404 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9405 | PyObject *u = NULL, *v = NULL, *w; |
| 9406 | Py_UCS4 maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9407 | |
| 9408 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9409 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9410 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9411 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9412 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9413 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9414 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9415 | |
| 9416 | /* Shortcuts */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9417 | if (v == (PyObject*)unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9418 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9419 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9420 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9421 | if (u == (PyObject*)unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9422 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9423 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9424 | } |
| 9425 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9426 | if (PyUnicode_READY(u) == -1 || PyUnicode_READY(v) == -1) |
| 9427 | goto onError; |
| 9428 | |
| 9429 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
| 9430 | if (PyUnicode_MAX_CHAR_VALUE(v) > maxchar) |
| 9431 | maxchar = PyUnicode_MAX_CHAR_VALUE(v); |
| 9432 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9433 | /* Concat the two Unicode strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9434 | w = PyUnicode_New( |
| 9435 | PyUnicode_GET_LENGTH(u) + PyUnicode_GET_LENGTH(v), |
| 9436 | maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9437 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9438 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9439 | PyUnicode_CopyCharacters(w, 0, u, 0, PyUnicode_GET_LENGTH(u)); |
| 9440 | PyUnicode_CopyCharacters(w, PyUnicode_GET_LENGTH(u), v, 0, |
| 9441 | PyUnicode_GET_LENGTH(v)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9442 | Py_DECREF(u); |
| 9443 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9444 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9445 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9446 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9447 | Py_XDECREF(u); |
| 9448 | Py_XDECREF(v); |
| 9449 | return NULL; |
| 9450 | } |
| 9451 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 9452 | void |
| 9453 | PyUnicode_Append(PyObject **pleft, PyObject *right) |
| 9454 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9455 | PyObject *new; |
| 9456 | if (*pleft == NULL) |
| 9457 | return; |
| 9458 | if (right == NULL || !PyUnicode_Check(*pleft)) { |
| 9459 | Py_DECREF(*pleft); |
| 9460 | *pleft = NULL; |
| 9461 | return; |
| 9462 | } |
| 9463 | new = PyUnicode_Concat(*pleft, right); |
| 9464 | Py_DECREF(*pleft); |
| 9465 | *pleft = new; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 9466 | } |
| 9467 | |
| 9468 | void |
| 9469 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 9470 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9471 | PyUnicode_Append(pleft, right); |
| 9472 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 9473 | } |
| 9474 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9475 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9476 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9477 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9478 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 9479 | 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] | 9480 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9481 | |
| 9482 | static PyObject * |
| 9483 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 9484 | { |
| 9485 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9486 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9487 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9488 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9489 | int kind1, kind2, kind; |
| 9490 | void *buf1, *buf2; |
| 9491 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9492 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 9493 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 9494 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9495 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9496 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9497 | kind1 = PyUnicode_KIND(self); |
| 9498 | kind2 = PyUnicode_KIND(substring); |
| 9499 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9500 | buf1 = PyUnicode_DATA(self); |
| 9501 | buf2 = PyUnicode_DATA(substring); |
| 9502 | if (kind1 != kind) |
| 9503 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 9504 | if (!buf1) { |
| 9505 | Py_DECREF(substring); |
| 9506 | return NULL; |
| 9507 | } |
| 9508 | if (kind2 != kind) |
| 9509 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 9510 | if (!buf2) { |
| 9511 | Py_DECREF(substring); |
| 9512 | if (kind1 != kind) PyMem_Free(buf1); |
| 9513 | return NULL; |
| 9514 | } |
| 9515 | len1 = PyUnicode_GET_LENGTH(self); |
| 9516 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9517 | |
| 9518 | ADJUST_INDICES(start, end, len1); |
| 9519 | switch(kind) { |
| 9520 | case PyUnicode_1BYTE_KIND: |
| 9521 | iresult = ucs1lib_count( |
| 9522 | ((Py_UCS1*)buf1) + start, end - start, |
| 9523 | buf2, len2, PY_SSIZE_T_MAX |
| 9524 | ); |
| 9525 | break; |
| 9526 | case PyUnicode_2BYTE_KIND: |
| 9527 | iresult = ucs2lib_count( |
| 9528 | ((Py_UCS2*)buf1) + start, end - start, |
| 9529 | buf2, len2, PY_SSIZE_T_MAX |
| 9530 | ); |
| 9531 | break; |
| 9532 | case PyUnicode_4BYTE_KIND: |
| 9533 | iresult = ucs4lib_count( |
| 9534 | ((Py_UCS4*)buf1) + start, end - start, |
| 9535 | buf2, len2, PY_SSIZE_T_MAX |
| 9536 | ); |
| 9537 | break; |
| 9538 | default: |
| 9539 | assert(0); iresult = 0; |
| 9540 | } |
| 9541 | |
| 9542 | result = PyLong_FromSsize_t(iresult); |
| 9543 | |
| 9544 | if (kind1 != kind) |
| 9545 | PyMem_Free(buf1); |
| 9546 | if (kind2 != kind) |
| 9547 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9548 | |
| 9549 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9550 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9551 | return result; |
| 9552 | } |
| 9553 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9554 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 9555 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9556 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 9557 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 9558 | 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] | 9559 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 9560 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 9561 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 9562 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9563 | |
| 9564 | static PyObject * |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 9565 | unicode_encode(PyUnicodeObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9566 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 9567 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9568 | char *encoding = NULL; |
| 9569 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 9570 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 9571 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 9572 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9573 | return NULL; |
Georg Brandl | 3b9406b | 2010-12-03 07:54:09 +0000 | [diff] [blame] | 9574 | return PyUnicode_AsEncodedString((PyObject *)self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 9575 | } |
| 9576 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9577 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9578 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9579 | \n\ |
| 9580 | 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] | 9581 | 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] | 9582 | |
| 9583 | static PyObject* |
| 9584 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 9585 | { |
| 9586 | Py_UNICODE *e; |
| 9587 | Py_UNICODE *p; |
| 9588 | Py_UNICODE *q; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9589 | Py_UNICODE *qe; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9590 | Py_ssize_t i, j, incr, wstr_length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9591 | PyUnicodeObject *u; |
| 9592 | int tabsize = 8; |
| 9593 | |
| 9594 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9595 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9596 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9597 | if (PyUnicode_AsUnicodeAndSize((PyObject *)self, &wstr_length) == NULL) |
| 9598 | return NULL; |
| 9599 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 9600 | /* First pass: determine size of output string */ |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9601 | i = 0; /* chars up to and including most recent \n or \r */ |
| 9602 | 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] | 9603 | e = _PyUnicode_WSTR(self) + wstr_length; /* end of input */ |
| 9604 | for (p = _PyUnicode_WSTR(self); p < e; p++) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9605 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9606 | if (tabsize > 0) { |
| 9607 | incr = tabsize - (j % tabsize); /* cannot overflow */ |
| 9608 | if (j > PY_SSIZE_T_MAX - incr) |
| 9609 | goto overflow1; |
| 9610 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9611 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9612 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9613 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9614 | if (j > PY_SSIZE_T_MAX - 1) |
| 9615 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9616 | j++; |
| 9617 | if (*p == '\n' || *p == '\r') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9618 | if (i > PY_SSIZE_T_MAX - j) |
| 9619 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9620 | i += j; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9621 | j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9622 | } |
| 9623 | } |
| 9624 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9625 | if (i > PY_SSIZE_T_MAX - j) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9626 | goto overflow1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 9627 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9628 | /* Second pass: create output string and fill it */ |
| 9629 | u = _PyUnicode_New(i + j); |
| 9630 | if (!u) |
| 9631 | return NULL; |
| 9632 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9633 | j = 0; /* same as in first pass */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9634 | q = _PyUnicode_WSTR(u); /* next output char */ |
| 9635 | qe = _PyUnicode_WSTR(u) + PyUnicode_GET_SIZE(u); /* end of output */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9636 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9637 | for (p = _PyUnicode_WSTR(self); p < e; p++) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9638 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9639 | if (tabsize > 0) { |
| 9640 | i = tabsize - (j % tabsize); |
| 9641 | j += i; |
| 9642 | while (i--) { |
| 9643 | if (q >= qe) |
| 9644 | goto overflow2; |
| 9645 | *q++ = ' '; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9646 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9647 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9648 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9649 | else { |
| 9650 | if (q >= qe) |
| 9651 | goto overflow2; |
| 9652 | *q++ = *p; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9653 | j++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9654 | if (*p == '\n' || *p == '\r') |
| 9655 | j = 0; |
| 9656 | } |
| 9657 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9658 | if (PyUnicode_READY(u) == -1) { |
| 9659 | Py_DECREF(u); |
| 9660 | return NULL; |
| 9661 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9662 | return (PyObject*) u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9663 | |
| 9664 | overflow2: |
| 9665 | Py_DECREF(u); |
| 9666 | overflow1: |
| 9667 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 9668 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9669 | } |
| 9670 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9671 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9672 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9673 | \n\ |
| 9674 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 9675 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9676 | arguments start and end are interpreted as in slice notation.\n\ |
| 9677 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9678 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9679 | |
| 9680 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9681 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9682 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 9683 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 9684 | Py_ssize_t start; |
| 9685 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9686 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9687 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 9688 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 9689 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9690 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9691 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9692 | if (PyUnicode_READY(self) == -1) |
| 9693 | return NULL; |
| 9694 | if (PyUnicode_READY(substring) == -1) |
| 9695 | return NULL; |
| 9696 | |
| 9697 | result = any_find_slice( |
| 9698 | ucs1lib_find_slice, ucs2lib_find_slice, ucs4lib_find_slice, |
| 9699 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9700 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9701 | |
| 9702 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9703 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9704 | if (result == -2) |
| 9705 | return NULL; |
| 9706 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9707 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9708 | } |
| 9709 | |
| 9710 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9711 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9712 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9713 | Py_UCS4 ch; |
| 9714 | |
| 9715 | if (PyUnicode_READY(self) == -1) |
| 9716 | return NULL; |
| 9717 | if (index < 0 || index >= _PyUnicode_LENGTH(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9718 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9719 | return NULL; |
| 9720 | } |
| 9721 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9722 | ch = PyUnicode_READ(PyUnicode_KIND(self), PyUnicode_DATA(self), index); |
| 9723 | return PyUnicode_FromOrdinal(ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9724 | } |
| 9725 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 9726 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 9727 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 9728 | static Py_hash_t |
Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 9729 | unicode_hash(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9730 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 9731 | Py_ssize_t len; |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 9732 | Py_uhash_t x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 9733 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9734 | if (_PyUnicode_HASH(self) != -1) |
| 9735 | return _PyUnicode_HASH(self); |
| 9736 | if (PyUnicode_READY(self) == -1) |
| 9737 | return -1; |
| 9738 | len = PyUnicode_GET_LENGTH(self); |
| 9739 | |
| 9740 | /* The hash function as a macro, gets expanded three times below. */ |
| 9741 | #define HASH(P) \ |
| 9742 | x = (Py_uhash_t)*P << 7; \ |
| 9743 | while (--len >= 0) \ |
| 9744 | x = (1000003*x) ^ (Py_uhash_t)*P++; |
| 9745 | |
| 9746 | switch (PyUnicode_KIND(self)) { |
| 9747 | case PyUnicode_1BYTE_KIND: { |
| 9748 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 9749 | HASH(c); |
| 9750 | break; |
| 9751 | } |
| 9752 | case PyUnicode_2BYTE_KIND: { |
| 9753 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 9754 | HASH(s); |
| 9755 | break; |
| 9756 | } |
| 9757 | default: { |
| 9758 | Py_UCS4 *l; |
| 9759 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 9760 | "Impossible switch case in unicode_hash"); |
| 9761 | l = PyUnicode_4BYTE_DATA(self); |
| 9762 | HASH(l); |
| 9763 | break; |
| 9764 | } |
| 9765 | } |
| 9766 | x ^= (Py_uhash_t)PyUnicode_GET_LENGTH(self); |
| 9767 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 9768 | if (x == -1) |
| 9769 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9770 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 9771 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9772 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9773 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9774 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9775 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9776 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9777 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9778 | 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] | 9779 | |
| 9780 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9781 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9782 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9783 | Py_ssize_t result; |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 9784 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 9785 | Py_ssize_t start; |
| 9786 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9787 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 9788 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 9789 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9790 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9791 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9792 | if (PyUnicode_READY(self) == -1) |
| 9793 | return NULL; |
| 9794 | if (PyUnicode_READY(substring) == -1) |
| 9795 | return NULL; |
| 9796 | |
| 9797 | result = any_find_slice( |
| 9798 | ucs1lib_find_slice, ucs2lib_find_slice, ucs4lib_find_slice, |
| 9799 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9800 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9801 | |
| 9802 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9803 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9804 | if (result == -2) |
| 9805 | return NULL; |
| 9806 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9807 | if (result < 0) { |
| 9808 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 9809 | return NULL; |
| 9810 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9811 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9812 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9813 | } |
| 9814 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9815 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9816 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9817 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 9818 | 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] | 9819 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9820 | |
| 9821 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9822 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9823 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9824 | Py_ssize_t i, length; |
| 9825 | int kind; |
| 9826 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9827 | int cased; |
| 9828 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9829 | if (PyUnicode_READY(self) == -1) |
| 9830 | return NULL; |
| 9831 | length = PyUnicode_GET_LENGTH(self); |
| 9832 | kind = PyUnicode_KIND(self); |
| 9833 | data = PyUnicode_DATA(self); |
| 9834 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9835 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9836 | if (length == 1) |
| 9837 | return PyBool_FromLong( |
| 9838 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9839 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 9840 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9841 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9842 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 9843 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9844 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9845 | for (i = 0; i < length; i++) { |
| 9846 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9847 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9848 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 9849 | return PyBool_FromLong(0); |
| 9850 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 9851 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9852 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 9853 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9854 | } |
| 9855 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9856 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9857 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9858 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 9859 | 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] | 9860 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9861 | |
| 9862 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9863 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9864 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9865 | Py_ssize_t i, length; |
| 9866 | int kind; |
| 9867 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9868 | int cased; |
| 9869 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9870 | if (PyUnicode_READY(self) == -1) |
| 9871 | return NULL; |
| 9872 | length = PyUnicode_GET_LENGTH(self); |
| 9873 | kind = PyUnicode_KIND(self); |
| 9874 | data = PyUnicode_DATA(self); |
| 9875 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9876 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9877 | if (length == 1) |
| 9878 | return PyBool_FromLong( |
| 9879 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9880 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 9881 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9882 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9883 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 9884 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9885 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9886 | for (i = 0; i < length; i++) { |
| 9887 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9888 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9889 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 9890 | return PyBool_FromLong(0); |
| 9891 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 9892 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9893 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 9894 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9895 | } |
| 9896 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9897 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9898 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9899 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 9900 | Return True if S is a titlecased string and there is at least one\n\ |
| 9901 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 9902 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 9903 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9904 | |
| 9905 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9906 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9907 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9908 | Py_ssize_t i, length; |
| 9909 | int kind; |
| 9910 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9911 | int cased, previous_is_cased; |
| 9912 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9913 | if (PyUnicode_READY(self) == -1) |
| 9914 | return NULL; |
| 9915 | length = PyUnicode_GET_LENGTH(self); |
| 9916 | kind = PyUnicode_KIND(self); |
| 9917 | data = PyUnicode_DATA(self); |
| 9918 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9919 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9920 | if (length == 1) { |
| 9921 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 9922 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 9923 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 9924 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9925 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 9926 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9927 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9928 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 9929 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9930 | cased = 0; |
| 9931 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9932 | for (i = 0; i < length; i++) { |
| 9933 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9934 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9935 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 9936 | if (previous_is_cased) |
| 9937 | return PyBool_FromLong(0); |
| 9938 | previous_is_cased = 1; |
| 9939 | cased = 1; |
| 9940 | } |
| 9941 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 9942 | if (!previous_is_cased) |
| 9943 | return PyBool_FromLong(0); |
| 9944 | previous_is_cased = 1; |
| 9945 | cased = 1; |
| 9946 | } |
| 9947 | else |
| 9948 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9949 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 9950 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9951 | } |
| 9952 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9953 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9954 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9955 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 9956 | Return True if all characters in S are whitespace\n\ |
| 9957 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9958 | |
| 9959 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9960 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9961 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9962 | Py_ssize_t i, length; |
| 9963 | int kind; |
| 9964 | void *data; |
| 9965 | |
| 9966 | if (PyUnicode_READY(self) == -1) |
| 9967 | return NULL; |
| 9968 | length = PyUnicode_GET_LENGTH(self); |
| 9969 | kind = PyUnicode_KIND(self); |
| 9970 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9971 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9972 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9973 | if (length == 1) |
| 9974 | return PyBool_FromLong( |
| 9975 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9976 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 9977 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9978 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9979 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 9980 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9981 | for (i = 0; i < length; i++) { |
| 9982 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 9983 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9984 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9985 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 9986 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9987 | } |
| 9988 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9989 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9990 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 9991 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 9992 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9993 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 9994 | |
| 9995 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9996 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 9997 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9998 | Py_ssize_t i, length; |
| 9999 | int kind; |
| 10000 | void *data; |
| 10001 | |
| 10002 | if (PyUnicode_READY(self) == -1) |
| 10003 | return NULL; |
| 10004 | length = PyUnicode_GET_LENGTH(self); |
| 10005 | kind = PyUnicode_KIND(self); |
| 10006 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10007 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10008 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10009 | if (length == 1) |
| 10010 | return PyBool_FromLong( |
| 10011 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10012 | |
| 10013 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10014 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10015 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10016 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10017 | for (i = 0; i < length; i++) { |
| 10018 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10019 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10020 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10021 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10022 | } |
| 10023 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10024 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10025 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10026 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10027 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10028 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10029 | |
| 10030 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10031 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10032 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10033 | int kind; |
| 10034 | void *data; |
| 10035 | Py_ssize_t len, i; |
| 10036 | |
| 10037 | if (PyUnicode_READY(self) == -1) |
| 10038 | return NULL; |
| 10039 | |
| 10040 | kind = PyUnicode_KIND(self); |
| 10041 | data = PyUnicode_DATA(self); |
| 10042 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10043 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +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 (len == 1) { |
| 10046 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 10047 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 10048 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10049 | |
| 10050 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10051 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10052 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10053 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10054 | for (i = 0; i < len; i++) { |
| 10055 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 10056 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10057 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10058 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10059 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10060 | } |
| 10061 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10062 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10063 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10064 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10065 | 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] | 10066 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10067 | |
| 10068 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10069 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10070 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10071 | Py_ssize_t i, length; |
| 10072 | int kind; |
| 10073 | void *data; |
| 10074 | |
| 10075 | if (PyUnicode_READY(self) == -1) |
| 10076 | return NULL; |
| 10077 | length = PyUnicode_GET_LENGTH(self); |
| 10078 | kind = PyUnicode_KIND(self); |
| 10079 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10080 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10081 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10082 | if (length == 1) |
| 10083 | return PyBool_FromLong( |
| 10084 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10085 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10086 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10087 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10088 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10089 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10090 | for (i = 0; i < length; i++) { |
| 10091 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10092 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10093 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10094 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10095 | } |
| 10096 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10097 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10098 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10099 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10100 | Return True if all characters in S are digits\n\ |
| 10101 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10102 | |
| 10103 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10104 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10105 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10106 | Py_ssize_t i, length; |
| 10107 | int kind; |
| 10108 | void *data; |
| 10109 | |
| 10110 | if (PyUnicode_READY(self) == -1) |
| 10111 | return NULL; |
| 10112 | length = PyUnicode_GET_LENGTH(self); |
| 10113 | kind = PyUnicode_KIND(self); |
| 10114 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10115 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +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 (length == 1) { |
| 10118 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 10119 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 10120 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10121 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10122 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10123 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10124 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10125 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10126 | for (i = 0; i < length; i++) { |
| 10127 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10128 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10129 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10130 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10131 | } |
| 10132 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10133 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10134 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10135 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10136 | 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] | 10137 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10138 | |
| 10139 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10140 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10141 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10142 | Py_ssize_t i, length; |
| 10143 | int kind; |
| 10144 | void *data; |
| 10145 | |
| 10146 | if (PyUnicode_READY(self) == -1) |
| 10147 | return NULL; |
| 10148 | length = PyUnicode_GET_LENGTH(self); |
| 10149 | kind = PyUnicode_KIND(self); |
| 10150 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10151 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10152 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10153 | if (length == 1) |
| 10154 | return PyBool_FromLong( |
| 10155 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10156 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10157 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10158 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10159 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10160 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10161 | for (i = 0; i < length; i++) { |
| 10162 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10163 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10164 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10165 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10166 | } |
| 10167 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10168 | int |
| 10169 | PyUnicode_IsIdentifier(PyObject *self) |
| 10170 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10171 | int kind; |
| 10172 | void *data; |
| 10173 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 10174 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10175 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10176 | if (PyUnicode_READY(self) == -1) { |
| 10177 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10178 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10179 | } |
| 10180 | |
| 10181 | /* Special case for empty strings */ |
| 10182 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 10183 | return 0; |
| 10184 | kind = PyUnicode_KIND(self); |
| 10185 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10186 | |
| 10187 | /* PEP 3131 says that the first character must be in |
| 10188 | XID_Start and subsequent characters in XID_Continue, |
| 10189 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10190 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10191 | letters, digits, underscore). However, given the current |
| 10192 | definition of XID_Start and XID_Continue, it is sufficient |
| 10193 | to check just for these, except that _ must be allowed |
| 10194 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10195 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 10196 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10197 | return 0; |
| 10198 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10199 | for (i = 0; i < PyUnicode_GET_LENGTH(self); i++) |
| 10200 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10201 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10202 | return 1; |
| 10203 | } |
| 10204 | |
| 10205 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10206 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10207 | \n\ |
| 10208 | Return True if S is a valid identifier according\n\ |
| 10209 | to the language definition."); |
| 10210 | |
| 10211 | static PyObject* |
| 10212 | unicode_isidentifier(PyObject *self) |
| 10213 | { |
| 10214 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 10215 | } |
| 10216 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10217 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10218 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10219 | \n\ |
| 10220 | Return True if all characters in S are considered\n\ |
| 10221 | printable in repr() or S is empty, False otherwise."); |
| 10222 | |
| 10223 | static PyObject* |
| 10224 | unicode_isprintable(PyObject *self) |
| 10225 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10226 | Py_ssize_t i, length; |
| 10227 | int kind; |
| 10228 | void *data; |
| 10229 | |
| 10230 | if (PyUnicode_READY(self) == -1) |
| 10231 | return NULL; |
| 10232 | length = PyUnicode_GET_LENGTH(self); |
| 10233 | kind = PyUnicode_KIND(self); |
| 10234 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10235 | |
| 10236 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10237 | if (length == 1) |
| 10238 | return PyBool_FromLong( |
| 10239 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10240 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10241 | for (i = 0; i < length; i++) { |
| 10242 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10243 | Py_RETURN_FALSE; |
| 10244 | } |
| 10245 | } |
| 10246 | Py_RETURN_TRUE; |
| 10247 | } |
| 10248 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10249 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 10250 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10251 | \n\ |
| 10252 | 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] | 10253 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10254 | |
| 10255 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10256 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10257 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10258 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10259 | } |
| 10260 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10261 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10262 | unicode_length(PyUnicodeObject *self) |
| 10263 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10264 | if (PyUnicode_READY(self) == -1) |
| 10265 | return -1; |
| 10266 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10267 | } |
| 10268 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10269 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10270 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10271 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 10272 | 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] | 10273 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10274 | |
| 10275 | static PyObject * |
| 10276 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 10277 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10278 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10279 | Py_UCS4 fillchar = ' '; |
| 10280 | |
| 10281 | if (PyUnicode_READY(self) == -1) |
| 10282 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10283 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10284 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10285 | return NULL; |
| 10286 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10287 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10288 | Py_INCREF(self); |
| 10289 | return (PyObject*) self; |
| 10290 | } |
| 10291 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10292 | return (PyObject*) pad(self, 0, width - _PyUnicode_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10293 | } |
| 10294 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10295 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10296 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10297 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10298 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10299 | |
| 10300 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10301 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10302 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10303 | return fixup(self, fixlower); |
| 10304 | } |
| 10305 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10306 | #define LEFTSTRIP 0 |
| 10307 | #define RIGHTSTRIP 1 |
| 10308 | #define BOTHSTRIP 2 |
| 10309 | |
| 10310 | /* Arrays indexed by above */ |
| 10311 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 10312 | |
| 10313 | #define STRIPNAME(i) (stripformat[i]+3) |
| 10314 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10315 | /* externally visible for str.strip(unicode) */ |
| 10316 | PyObject * |
| 10317 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 10318 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10319 | void *data; |
| 10320 | int kind; |
| 10321 | Py_ssize_t i, j, len; |
| 10322 | BLOOM_MASK sepmask; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10323 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10324 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 10325 | return NULL; |
| 10326 | |
| 10327 | kind = PyUnicode_KIND(self); |
| 10328 | data = PyUnicode_DATA(self); |
| 10329 | len = PyUnicode_GET_LENGTH(self); |
| 10330 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 10331 | PyUnicode_DATA(sepobj), |
| 10332 | PyUnicode_GET_LENGTH(sepobj)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10333 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10334 | i = 0; |
| 10335 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10336 | while (i < len && |
| 10337 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10338 | i++; |
| 10339 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10340 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10341 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10342 | j = len; |
| 10343 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10344 | do { |
| 10345 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10346 | } while (j >= i && |
| 10347 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10348 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10349 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10350 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10351 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10352 | Py_INCREF(self); |
| 10353 | return (PyObject*)self; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10354 | } |
| 10355 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10356 | return PyUnicode_Substring((PyObject*)self, i, j); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10357 | } |
| 10358 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10359 | /* Assumes an already ready self string. */ |
| 10360 | |
| 10361 | static PyObject * |
| 10362 | substring(PyUnicodeObject *self, Py_ssize_t start, Py_ssize_t len) |
| 10363 | { |
| 10364 | const int kind = PyUnicode_KIND(self); |
| 10365 | void *data = PyUnicode_DATA(self); |
| 10366 | Py_UCS4 maxchar = 0; |
| 10367 | Py_ssize_t i; |
| 10368 | PyObject *unicode; |
| 10369 | |
| 10370 | if (start < 0 || len < 0 || (start + len) > PyUnicode_GET_LENGTH(self)) { |
| 10371 | PyErr_BadInternalCall(); |
| 10372 | return NULL; |
| 10373 | } |
| 10374 | |
| 10375 | if (len == PyUnicode_GET_LENGTH(self) && PyUnicode_CheckExact(self)) { |
| 10376 | Py_INCREF(self); |
| 10377 | return (PyObject*)self; |
| 10378 | } |
| 10379 | |
| 10380 | for (i = 0; i < len; ++i) { |
| 10381 | const Py_UCS4 ch = PyUnicode_READ(kind, data, start + i); |
| 10382 | if (ch > maxchar) |
| 10383 | maxchar = ch; |
| 10384 | } |
| 10385 | |
| 10386 | unicode = PyUnicode_New(len, maxchar); |
| 10387 | if (unicode == NULL) |
| 10388 | return NULL; |
| 10389 | PyUnicode_CopyCharacters(unicode, 0, |
| 10390 | (PyObject*)self, start, len); |
| 10391 | return unicode; |
| 10392 | } |
| 10393 | |
| 10394 | PyObject* |
| 10395 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 10396 | { |
| 10397 | unsigned char *data; |
| 10398 | int kind; |
| 10399 | |
| 10400 | if (start == 0 && end == PyUnicode_GET_LENGTH(self) |
| 10401 | && PyUnicode_CheckExact(self)) |
| 10402 | { |
| 10403 | Py_INCREF(self); |
| 10404 | return (PyObject *)self; |
| 10405 | } |
| 10406 | |
| 10407 | if ((end - start) == 1) |
| 10408 | return unicode_getitem((PyUnicodeObject*)self, start); |
| 10409 | |
| 10410 | if (PyUnicode_READY(self) == -1) |
| 10411 | return NULL; |
| 10412 | kind = PyUnicode_KIND(self); |
| 10413 | data = PyUnicode_1BYTE_DATA(self); |
| 10414 | return PyUnicode_FromKindAndData(kind, data + PyUnicode_KIND_SIZE(kind, start), |
| 10415 | end-start); |
| 10416 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10417 | |
| 10418 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10419 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10420 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10421 | int kind; |
| 10422 | void *data; |
| 10423 | Py_ssize_t len, i, j; |
| 10424 | |
| 10425 | if (PyUnicode_READY(self) == -1) |
| 10426 | return NULL; |
| 10427 | |
| 10428 | kind = PyUnicode_KIND(self); |
| 10429 | data = PyUnicode_DATA(self); |
| 10430 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10431 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10432 | i = 0; |
| 10433 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10434 | while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10435 | i++; |
| 10436 | } |
| 10437 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10438 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10439 | j = len; |
| 10440 | if (striptype != LEFTSTRIP) { |
| 10441 | do { |
| 10442 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10443 | } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10444 | j++; |
| 10445 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10446 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10447 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 10448 | Py_INCREF(self); |
| 10449 | return (PyObject*)self; |
| 10450 | } |
| 10451 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10452 | return substring(self, i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10453 | } |
| 10454 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10455 | |
| 10456 | static PyObject * |
| 10457 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 10458 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10459 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10460 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10461 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 10462 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10463 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10464 | if (sep != NULL && sep != Py_None) { |
| 10465 | if (PyUnicode_Check(sep)) |
| 10466 | return _PyUnicode_XStrip(self, striptype, sep); |
| 10467 | else { |
| 10468 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10469 | "%s arg must be None or str", |
| 10470 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10471 | return NULL; |
| 10472 | } |
| 10473 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10474 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10475 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10476 | } |
| 10477 | |
| 10478 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10479 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10480 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10481 | \n\ |
| 10482 | Return a copy of the string S with leading and trailing\n\ |
| 10483 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10484 | 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] | 10485 | |
| 10486 | static PyObject * |
| 10487 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 10488 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10489 | if (PyTuple_GET_SIZE(args) == 0) |
| 10490 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 10491 | else |
| 10492 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10493 | } |
| 10494 | |
| 10495 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10496 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10497 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10498 | \n\ |
| 10499 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10500 | 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] | 10501 | |
| 10502 | static PyObject * |
| 10503 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 10504 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10505 | if (PyTuple_GET_SIZE(args) == 0) |
| 10506 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 10507 | else |
| 10508 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10509 | } |
| 10510 | |
| 10511 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10512 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10513 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10514 | \n\ |
| 10515 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10516 | 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] | 10517 | |
| 10518 | static PyObject * |
| 10519 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 10520 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10521 | if (PyTuple_GET_SIZE(args) == 0) |
| 10522 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 10523 | else |
| 10524 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10525 | } |
| 10526 | |
| 10527 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10528 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10529 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10530 | { |
| 10531 | PyUnicodeObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10532 | Py_ssize_t nchars, n; |
| 10533 | size_t nbytes, char_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10534 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 10535 | if (len < 1) { |
| 10536 | Py_INCREF(unicode_empty); |
| 10537 | return (PyObject *)unicode_empty; |
| 10538 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10539 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 10540 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10541 | /* no repeat, return original string */ |
| 10542 | Py_INCREF(str); |
| 10543 | return (PyObject*) str; |
| 10544 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 10545 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10546 | if (PyUnicode_READY(str) == -1) |
| 10547 | return NULL; |
| 10548 | |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 10549 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 10550 | * needed doesn't overflow size_t |
| 10551 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10552 | nchars = len * PyUnicode_GET_LENGTH(str); |
| 10553 | if (nchars / len != PyUnicode_GET_LENGTH(str)) { |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 10554 | PyErr_SetString(PyExc_OverflowError, |
| 10555 | "repeated string is too long"); |
| 10556 | return NULL; |
| 10557 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10558 | char_size = PyUnicode_CHARACTER_SIZE(str); |
| 10559 | nbytes = (nchars + 1) * char_size; |
| 10560 | if (nbytes / char_size != (size_t)(nchars + 1)) { |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 10561 | PyErr_SetString(PyExc_OverflowError, |
| 10562 | "repeated string is too long"); |
| 10563 | return NULL; |
| 10564 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10565 | u = (PyUnicodeObject *)PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10566 | if (!u) |
| 10567 | return NULL; |
| 10568 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10569 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 10570 | const int kind = PyUnicode_KIND(str); |
| 10571 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
| 10572 | void *to = PyUnicode_DATA(u); |
| 10573 | for (n = 0; n < len; ++n) |
| 10574 | PyUnicode_WRITE(kind, to, n, fill_char); |
| 10575 | } |
| 10576 | else { |
| 10577 | /* number of characters copied this far */ |
| 10578 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
| 10579 | const Py_ssize_t char_size = PyUnicode_CHARACTER_SIZE(str); |
| 10580 | char *to = (char *) PyUnicode_DATA(u); |
| 10581 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 10582 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10583 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10584 | n = (done <= nchars-done) ? done : nchars-done; |
| 10585 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10586 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10587 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10588 | } |
| 10589 | |
| 10590 | return (PyObject*) u; |
| 10591 | } |
| 10592 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10593 | PyObject * |
| 10594 | PyUnicode_Replace(PyObject *obj, |
| 10595 | PyObject *subobj, |
| 10596 | PyObject *replobj, |
| 10597 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10598 | { |
| 10599 | PyObject *self; |
| 10600 | PyObject *str1; |
| 10601 | PyObject *str2; |
| 10602 | PyObject *result; |
| 10603 | |
| 10604 | self = PyUnicode_FromObject(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10605 | if (self == NULL || PyUnicode_READY(obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10606 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10607 | str1 = PyUnicode_FromObject(subobj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10608 | if (str1 == NULL || PyUnicode_READY(obj) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10609 | Py_DECREF(self); |
| 10610 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10611 | } |
| 10612 | str2 = PyUnicode_FromObject(replobj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10613 | if (str2 == NULL || PyUnicode_READY(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10614 | Py_DECREF(self); |
| 10615 | Py_DECREF(str1); |
| 10616 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10617 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10618 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10619 | Py_DECREF(self); |
| 10620 | Py_DECREF(str1); |
| 10621 | Py_DECREF(str2); |
| 10622 | return result; |
| 10623 | } |
| 10624 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10625 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 10626 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10627 | \n\ |
| 10628 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 10629 | old replaced by new. If the optional argument count is\n\ |
| 10630 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10631 | |
| 10632 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10633 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10634 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10635 | PyObject *str1; |
| 10636 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10637 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10638 | PyObject *result; |
| 10639 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10640 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10641 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10642 | if (!PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10643 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10644 | str1 = PyUnicode_FromObject(str1); |
| 10645 | if (str1 == NULL || PyUnicode_READY(str1) == -1) |
| 10646 | return NULL; |
| 10647 | str2 = PyUnicode_FromObject(str2); |
| 10648 | if (str2 == NULL || PyUnicode_READY(str1) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10649 | Py_DECREF(str1); |
| 10650 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 10651 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10652 | |
| 10653 | result = replace(self, str1, str2, maxcount); |
| 10654 | |
| 10655 | Py_DECREF(str1); |
| 10656 | Py_DECREF(str2); |
| 10657 | return result; |
| 10658 | } |
| 10659 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10660 | static PyObject * |
| 10661 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10662 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10663 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10664 | Py_ssize_t isize; |
| 10665 | Py_ssize_t osize, squote, dquote, i, o; |
| 10666 | Py_UCS4 max, quote; |
| 10667 | int ikind, okind; |
| 10668 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10669 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10670 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10671 | return NULL; |
| 10672 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10673 | isize = PyUnicode_GET_LENGTH(unicode); |
| 10674 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10675 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10676 | /* Compute length of output, quote characters, and |
| 10677 | maximum character */ |
| 10678 | osize = 2; /* quotes */ |
| 10679 | max = 127; |
| 10680 | squote = dquote = 0; |
| 10681 | ikind = PyUnicode_KIND(unicode); |
| 10682 | for (i = 0; i < isize; i++) { |
| 10683 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 10684 | switch (ch) { |
| 10685 | case '\'': squote++; osize++; break; |
| 10686 | case '"': dquote++; osize++; break; |
| 10687 | case '\\': case '\t': case '\r': case '\n': |
| 10688 | osize += 2; break; |
| 10689 | default: |
| 10690 | /* Fast-path ASCII */ |
| 10691 | if (ch < ' ' || ch == 0x7f) |
| 10692 | osize += 4; /* \xHH */ |
| 10693 | else if (ch < 0x7f) |
| 10694 | osize++; |
| 10695 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 10696 | osize++; |
| 10697 | max = ch > max ? ch : max; |
| 10698 | } |
| 10699 | else if (ch < 0x100) |
| 10700 | osize += 4; /* \xHH */ |
| 10701 | else if (ch < 0x10000) |
| 10702 | osize += 6; /* \uHHHH */ |
| 10703 | else |
| 10704 | osize += 10; /* \uHHHHHHHH */ |
| 10705 | } |
| 10706 | } |
| 10707 | |
| 10708 | quote = '\''; |
| 10709 | if (squote) { |
| 10710 | if (dquote) |
| 10711 | /* Both squote and dquote present. Use squote, |
| 10712 | and escape them */ |
| 10713 | osize += squote; |
| 10714 | else |
| 10715 | quote = '"'; |
| 10716 | } |
| 10717 | |
| 10718 | repr = PyUnicode_New(osize, max); |
| 10719 | if (repr == NULL) |
| 10720 | return NULL; |
| 10721 | okind = PyUnicode_KIND(repr); |
| 10722 | odata = PyUnicode_DATA(repr); |
| 10723 | |
| 10724 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 10725 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
| 10726 | |
| 10727 | for (i = 0, o = 1; i < isize; i++) { |
| 10728 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10729 | |
| 10730 | /* Escape quotes and backslashes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10731 | if ((ch == quote) || (ch == '\\')) { |
| 10732 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 10733 | PyUnicode_WRITE(okind, odata, o++, ch); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10734 | continue; |
| 10735 | } |
| 10736 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10737 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10738 | if (ch == '\t') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10739 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 10740 | PyUnicode_WRITE(okind, odata, o++, 't'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10741 | } |
| 10742 | else if (ch == '\n') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10743 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 10744 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10745 | } |
| 10746 | else if (ch == '\r') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10747 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 10748 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10749 | } |
| 10750 | |
| 10751 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10752 | else if (ch < ' ' || ch == 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10753 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 10754 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 10755 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0x000F]); |
| 10756 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0x000F]); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10757 | } |
| 10758 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10759 | /* Copy ASCII characters as-is */ |
| 10760 | else if (ch < 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10761 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10762 | } |
| 10763 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10764 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10765 | else { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10766 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10767 | (categories Z* and C* except ASCII space) |
| 10768 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10769 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10770 | /* Map 8-bit characters to '\xhh' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10771 | if (ch <= 0xff) { |
| 10772 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 10773 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 10774 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0x000F]); |
| 10775 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0x000F]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10776 | } |
| 10777 | /* Map 21-bit characters to '\U00xxxxxx' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10778 | else if (ch >= 0x10000) { |
| 10779 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 10780 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
| 10781 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 28) & 0xF]); |
| 10782 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 24) & 0xF]); |
| 10783 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 20) & 0xF]); |
| 10784 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 16) & 0xF]); |
| 10785 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 12) & 0xF]); |
| 10786 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 8) & 0xF]); |
| 10787 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0xF]); |
| 10788 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10789 | } |
| 10790 | /* Map 16-bit characters to '\uxxxx' */ |
| 10791 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10792 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 10793 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
| 10794 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 12) & 0xF]); |
| 10795 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 8) & 0xF]); |
| 10796 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0xF]); |
| 10797 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10798 | } |
| 10799 | } |
| 10800 | /* Copy characters as-is */ |
| 10801 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10802 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10803 | } |
| 10804 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10805 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10806 | /* Closing quote already added at the beginning */ |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10807 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10808 | } |
| 10809 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10810 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10811 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10812 | \n\ |
| 10813 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 10814 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10815 | arguments start and end are interpreted as in slice notation.\n\ |
| 10816 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10817 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10818 | |
| 10819 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10820 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10821 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10822 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10823 | Py_ssize_t start; |
| 10824 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10825 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10826 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10827 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 10828 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10829 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10830 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10831 | if (PyUnicode_READY(self) == -1) |
| 10832 | return NULL; |
| 10833 | if (PyUnicode_READY(substring) == -1) |
| 10834 | return NULL; |
| 10835 | |
| 10836 | result = any_find_slice( |
| 10837 | ucs1lib_rfind_slice, ucs2lib_rfind_slice, ucs4lib_rfind_slice, |
| 10838 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10839 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10840 | |
| 10841 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10842 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10843 | if (result == -2) |
| 10844 | return NULL; |
| 10845 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10846 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10847 | } |
| 10848 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10849 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10850 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10851 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10852 | 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] | 10853 | |
| 10854 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10855 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10856 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10857 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10858 | Py_ssize_t start; |
| 10859 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10860 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10861 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10862 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 10863 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10864 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10865 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10866 | if (PyUnicode_READY(self) == -1) |
| 10867 | return NULL; |
| 10868 | if (PyUnicode_READY(substring) == -1) |
| 10869 | return NULL; |
| 10870 | |
| 10871 | result = any_find_slice( |
| 10872 | ucs1lib_rfind_slice, ucs2lib_rfind_slice, ucs4lib_rfind_slice, |
| 10873 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10874 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10875 | |
| 10876 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10877 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10878 | if (result == -2) |
| 10879 | return NULL; |
| 10880 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10881 | if (result < 0) { |
| 10882 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 10883 | return NULL; |
| 10884 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10885 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10886 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10887 | } |
| 10888 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10889 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10890 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10891 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 10892 | 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] | 10893 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10894 | |
| 10895 | static PyObject * |
| 10896 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 10897 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10898 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10899 | Py_UCS4 fillchar = ' '; |
| 10900 | |
| 10901 | if (PyUnicode_READY(self) == -1) |
| 10902 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10903 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10904 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10905 | return NULL; |
| 10906 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10907 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10908 | Py_INCREF(self); |
| 10909 | return (PyObject*) self; |
| 10910 | } |
| 10911 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10912 | return (PyObject*) pad(self, width - _PyUnicode_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10913 | } |
| 10914 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10915 | PyObject * |
| 10916 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10917 | { |
| 10918 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10919 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10920 | s = PyUnicode_FromObject(s); |
| 10921 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10922 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10923 | if (sep != NULL) { |
| 10924 | sep = PyUnicode_FromObject(sep); |
| 10925 | if (sep == NULL) { |
| 10926 | Py_DECREF(s); |
| 10927 | return NULL; |
| 10928 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10929 | } |
| 10930 | |
| 10931 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 10932 | |
| 10933 | Py_DECREF(s); |
| 10934 | Py_XDECREF(sep); |
| 10935 | return result; |
| 10936 | } |
| 10937 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10938 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10939 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10940 | \n\ |
| 10941 | Return a list of the words in S, using sep as the\n\ |
| 10942 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 10943 | 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] | 10944 | whitespace string is a separator and empty strings are\n\ |
| 10945 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10946 | |
| 10947 | static PyObject* |
| 10948 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 10949 | { |
| 10950 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10951 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10952 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10953 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10954 | return NULL; |
| 10955 | |
| 10956 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10957 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10958 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10959 | return split(self, (PyUnicodeObject *)substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10960 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10961 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10962 | } |
| 10963 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10964 | PyObject * |
| 10965 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 10966 | { |
| 10967 | PyObject* str_obj; |
| 10968 | PyObject* sep_obj; |
| 10969 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10970 | int kind1, kind2, kind; |
| 10971 | void *buf1 = NULL, *buf2 = NULL; |
| 10972 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10973 | |
| 10974 | str_obj = PyUnicode_FromObject(str_in); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10975 | if (!str_obj || PyUnicode_READY(str_in) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10976 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10977 | sep_obj = PyUnicode_FromObject(sep_in); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10978 | if (!sep_obj || PyUnicode_READY(sep_obj) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10979 | Py_DECREF(str_obj); |
| 10980 | return NULL; |
| 10981 | } |
| 10982 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10983 | kind1 = PyUnicode_KIND(str_in); |
| 10984 | kind2 = PyUnicode_KIND(sep_obj); |
| 10985 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10986 | buf1 = PyUnicode_DATA(str_in); |
| 10987 | if (kind1 != kind) |
| 10988 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 10989 | if (!buf1) |
| 10990 | goto onError; |
| 10991 | buf2 = PyUnicode_DATA(sep_obj); |
| 10992 | if (kind2 != kind) |
| 10993 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 10994 | if (!buf2) |
| 10995 | goto onError; |
| 10996 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 10997 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 10998 | |
| 10999 | switch(PyUnicode_KIND(str_in)) { |
| 11000 | case PyUnicode_1BYTE_KIND: |
| 11001 | out = ucs1lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11002 | break; |
| 11003 | case PyUnicode_2BYTE_KIND: |
| 11004 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11005 | break; |
| 11006 | case PyUnicode_4BYTE_KIND: |
| 11007 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11008 | break; |
| 11009 | default: |
| 11010 | assert(0); |
| 11011 | out = 0; |
| 11012 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11013 | |
| 11014 | Py_DECREF(sep_obj); |
| 11015 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11016 | if (kind1 != kind) |
| 11017 | PyMem_Free(buf1); |
| 11018 | if (kind2 != kind) |
| 11019 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11020 | |
| 11021 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11022 | onError: |
| 11023 | Py_DECREF(sep_obj); |
| 11024 | Py_DECREF(str_obj); |
| 11025 | if (kind1 != kind && buf1) |
| 11026 | PyMem_Free(buf1); |
| 11027 | if (kind2 != kind && buf2) |
| 11028 | PyMem_Free(buf2); |
| 11029 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11030 | } |
| 11031 | |
| 11032 | |
| 11033 | PyObject * |
| 11034 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 11035 | { |
| 11036 | PyObject* str_obj; |
| 11037 | PyObject* sep_obj; |
| 11038 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11039 | int kind1, kind2, kind; |
| 11040 | void *buf1 = NULL, *buf2 = NULL; |
| 11041 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11042 | |
| 11043 | str_obj = PyUnicode_FromObject(str_in); |
| 11044 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11045 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11046 | sep_obj = PyUnicode_FromObject(sep_in); |
| 11047 | if (!sep_obj) { |
| 11048 | Py_DECREF(str_obj); |
| 11049 | return NULL; |
| 11050 | } |
| 11051 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11052 | kind1 = PyUnicode_KIND(str_in); |
| 11053 | kind2 = PyUnicode_KIND(sep_obj); |
| 11054 | kind = PY_MAX(kind1, kind2); |
| 11055 | buf1 = PyUnicode_DATA(str_in); |
| 11056 | if (kind1 != kind) |
| 11057 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 11058 | if (!buf1) |
| 11059 | goto onError; |
| 11060 | buf2 = PyUnicode_DATA(sep_obj); |
| 11061 | if (kind2 != kind) |
| 11062 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 11063 | if (!buf2) |
| 11064 | goto onError; |
| 11065 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 11066 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 11067 | |
| 11068 | switch(PyUnicode_KIND(str_in)) { |
| 11069 | case PyUnicode_1BYTE_KIND: |
| 11070 | out = ucs1lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11071 | break; |
| 11072 | case PyUnicode_2BYTE_KIND: |
| 11073 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11074 | break; |
| 11075 | case PyUnicode_4BYTE_KIND: |
| 11076 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11077 | break; |
| 11078 | default: |
| 11079 | assert(0); |
| 11080 | out = 0; |
| 11081 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11082 | |
| 11083 | Py_DECREF(sep_obj); |
| 11084 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11085 | if (kind1 != kind) |
| 11086 | PyMem_Free(buf1); |
| 11087 | if (kind2 != kind) |
| 11088 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11089 | |
| 11090 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11091 | onError: |
| 11092 | Py_DECREF(sep_obj); |
| 11093 | Py_DECREF(str_obj); |
| 11094 | if (kind1 != kind && buf1) |
| 11095 | PyMem_Free(buf1); |
| 11096 | if (kind2 != kind && buf2) |
| 11097 | PyMem_Free(buf2); |
| 11098 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11099 | } |
| 11100 | |
| 11101 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11102 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11103 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 11104 | 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] | 11105 | 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] | 11106 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11107 | |
| 11108 | static PyObject* |
| 11109 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
| 11110 | { |
| 11111 | return PyUnicode_Partition((PyObject *)self, separator); |
| 11112 | } |
| 11113 | |
| 11114 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 11115 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11116 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 11117 | 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] | 11118 | 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] | 11119 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11120 | |
| 11121 | static PyObject* |
| 11122 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 11123 | { |
| 11124 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 11125 | } |
| 11126 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11127 | PyObject * |
| 11128 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11129 | { |
| 11130 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11131 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11132 | s = PyUnicode_FromObject(s); |
| 11133 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11134 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11135 | if (sep != NULL) { |
| 11136 | sep = PyUnicode_FromObject(sep); |
| 11137 | if (sep == NULL) { |
| 11138 | Py_DECREF(s); |
| 11139 | return NULL; |
| 11140 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11141 | } |
| 11142 | |
| 11143 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 11144 | |
| 11145 | Py_DECREF(s); |
| 11146 | Py_XDECREF(sep); |
| 11147 | return result; |
| 11148 | } |
| 11149 | |
| 11150 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11151 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11152 | \n\ |
| 11153 | Return a list of the words in S, using sep as the\n\ |
| 11154 | delimiter string, starting at the end of the string and\n\ |
| 11155 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 11156 | splits are done. If sep is not specified, any whitespace string\n\ |
| 11157 | is a separator."); |
| 11158 | |
| 11159 | static PyObject* |
| 11160 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 11161 | { |
| 11162 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11163 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11164 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11165 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11166 | return NULL; |
| 11167 | |
| 11168 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11169 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11170 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11171 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11172 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11173 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11174 | } |
| 11175 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11176 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11177 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11178 | \n\ |
| 11179 | 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] | 11180 | 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] | 11181 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11182 | |
| 11183 | static PyObject* |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 11184 | unicode_splitlines(PyUnicodeObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11185 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 11186 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 11187 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11188 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 11189 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 11190 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11191 | return NULL; |
| 11192 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 11193 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11194 | } |
| 11195 | |
| 11196 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 11197 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11198 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 11199 | if (PyUnicode_CheckExact(self)) { |
| 11200 | Py_INCREF(self); |
| 11201 | return self; |
| 11202 | } else |
| 11203 | /* Subtype -- return genuine unicode string with the same value. */ |
| 11204 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(self), |
| 11205 | PyUnicode_GET_SIZE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11206 | } |
| 11207 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11208 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11209 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11210 | \n\ |
| 11211 | 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] | 11212 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11213 | |
| 11214 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11215 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11216 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11217 | return fixup(self, fixswapcase); |
| 11218 | } |
| 11219 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11220 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11221 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11222 | \n\ |
| 11223 | Return a translation table usable for str.translate().\n\ |
| 11224 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 11225 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11226 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11227 | If there are two arguments, they must be strings of equal length, and\n\ |
| 11228 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 11229 | character at the same position in y. If there is a third argument, it\n\ |
| 11230 | must be a string, whose characters will be mapped to None in the result."); |
| 11231 | |
| 11232 | static PyObject* |
| 11233 | unicode_maketrans(PyUnicodeObject *null, PyObject *args) |
| 11234 | { |
| 11235 | PyObject *x, *y = NULL, *z = NULL; |
| 11236 | PyObject *new = NULL, *key, *value; |
| 11237 | Py_ssize_t i = 0; |
| 11238 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11239 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11240 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 11241 | return NULL; |
| 11242 | new = PyDict_New(); |
| 11243 | if (!new) |
| 11244 | return NULL; |
| 11245 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11246 | int x_kind, y_kind, z_kind; |
| 11247 | void *x_data, *y_data, *z_data; |
| 11248 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11249 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11250 | if (!PyUnicode_Check(x)) { |
| 11251 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 11252 | "be a string if there is a second argument"); |
| 11253 | goto err; |
| 11254 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11255 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11256 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 11257 | "arguments must have equal length"); |
| 11258 | goto err; |
| 11259 | } |
| 11260 | /* 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] | 11261 | x_kind = PyUnicode_KIND(x); |
| 11262 | y_kind = PyUnicode_KIND(y); |
| 11263 | x_data = PyUnicode_DATA(x); |
| 11264 | y_data = PyUnicode_DATA(y); |
| 11265 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 11266 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
| 11267 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11268 | if (!key || !value) |
| 11269 | goto err; |
| 11270 | res = PyDict_SetItem(new, key, value); |
| 11271 | Py_DECREF(key); |
| 11272 | Py_DECREF(value); |
| 11273 | if (res < 0) |
| 11274 | goto err; |
| 11275 | } |
| 11276 | /* create entries for deleting chars in z */ |
| 11277 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11278 | z_kind = PyUnicode_KIND(z); |
| 11279 | z_data = PyUnicode_DATA(z); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11280 | for (i = 0; i < PyUnicode_GET_SIZE(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11281 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11282 | if (!key) |
| 11283 | goto err; |
| 11284 | res = PyDict_SetItem(new, key, Py_None); |
| 11285 | Py_DECREF(key); |
| 11286 | if (res < 0) |
| 11287 | goto err; |
| 11288 | } |
| 11289 | } |
| 11290 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11291 | int kind; |
| 11292 | void *data; |
| 11293 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11294 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 11295 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11296 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 11297 | "to maketrans it must be a dict"); |
| 11298 | goto err; |
| 11299 | } |
| 11300 | /* copy entries into the new dict, converting string keys to int keys */ |
| 11301 | while (PyDict_Next(x, &i, &key, &value)) { |
| 11302 | if (PyUnicode_Check(key)) { |
| 11303 | /* convert string keys to integer keys */ |
| 11304 | PyObject *newkey; |
| 11305 | if (PyUnicode_GET_SIZE(key) != 1) { |
| 11306 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 11307 | "table must be of length 1"); |
| 11308 | goto err; |
| 11309 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11310 | kind = PyUnicode_KIND(key); |
| 11311 | data = PyUnicode_DATA(key); |
| 11312 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11313 | if (!newkey) |
| 11314 | goto err; |
| 11315 | res = PyDict_SetItem(new, newkey, value); |
| 11316 | Py_DECREF(newkey); |
| 11317 | if (res < 0) |
| 11318 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11319 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11320 | /* just keep integer keys */ |
| 11321 | if (PyDict_SetItem(new, key, value) < 0) |
| 11322 | goto err; |
| 11323 | } else { |
| 11324 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 11325 | "be strings or integers"); |
| 11326 | goto err; |
| 11327 | } |
| 11328 | } |
| 11329 | } |
| 11330 | return new; |
| 11331 | err: |
| 11332 | Py_DECREF(new); |
| 11333 | return NULL; |
| 11334 | } |
| 11335 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11336 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11337 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11338 | \n\ |
| 11339 | Return a copy of the string S, where all characters have been mapped\n\ |
| 11340 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11341 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 11342 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 11343 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11344 | |
| 11345 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11346 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11347 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11348 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11349 | } |
| 11350 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11351 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11352 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11353 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11354 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11355 | |
| 11356 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11357 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11358 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11359 | return fixup(self, fixupper); |
| 11360 | } |
| 11361 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11362 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11363 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11364 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 11365 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 11366 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11367 | |
| 11368 | static PyObject * |
| 11369 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 11370 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11371 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11372 | PyUnicodeObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11373 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11374 | int kind; |
| 11375 | void *data; |
| 11376 | Py_UCS4 chr; |
| 11377 | |
| 11378 | if (PyUnicode_READY(self) == -1) |
| 11379 | return NULL; |
| 11380 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11381 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11382 | return NULL; |
| 11383 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11384 | if (PyUnicode_GET_LENGTH(self) >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 11385 | if (PyUnicode_CheckExact(self)) { |
| 11386 | Py_INCREF(self); |
| 11387 | return (PyObject*) self; |
| 11388 | } |
| 11389 | else |
| 11390 | return PyUnicode_FromUnicode( |
| 11391 | PyUnicode_AS_UNICODE(self), |
| 11392 | PyUnicode_GET_SIZE(self) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11393 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11394 | } |
| 11395 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11396 | fill = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11397 | |
| 11398 | u = pad(self, fill, 0, '0'); |
| 11399 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 11400 | if (u == NULL) |
| 11401 | return NULL; |
| 11402 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11403 | kind = PyUnicode_KIND(u); |
| 11404 | data = PyUnicode_DATA(u); |
| 11405 | chr = PyUnicode_READ(kind, data, fill); |
| 11406 | |
| 11407 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11408 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11409 | PyUnicode_WRITE(kind, data, 0, chr); |
| 11410 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11411 | } |
| 11412 | |
| 11413 | return (PyObject*) u; |
| 11414 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11415 | |
| 11416 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 11417 | static PyObject * |
| 11418 | unicode__decimal2ascii(PyObject *self) |
| 11419 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11420 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 11421 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11422 | #endif |
| 11423 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11424 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11425 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11426 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 11427 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 11428 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11429 | With optional end, stop comparing S at that position.\n\ |
| 11430 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11431 | |
| 11432 | static PyObject * |
| 11433 | unicode_startswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11434 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11435 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11436 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11437 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11438 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11439 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11440 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11441 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11442 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11443 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11444 | if (PyTuple_Check(subobj)) { |
| 11445 | Py_ssize_t i; |
| 11446 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 11447 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11448 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11449 | if (substring == NULL) |
| 11450 | return NULL; |
| 11451 | result = tailmatch(self, substring, start, end, -1); |
| 11452 | Py_DECREF(substring); |
| 11453 | if (result) { |
| 11454 | Py_RETURN_TRUE; |
| 11455 | } |
| 11456 | } |
| 11457 | /* nothing matched */ |
| 11458 | Py_RETURN_FALSE; |
| 11459 | } |
| 11460 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 11461 | if (substring == NULL) { |
| 11462 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 11463 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 11464 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11465 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 11466 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11467 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11468 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11469 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11470 | } |
| 11471 | |
| 11472 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11473 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11474 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11475 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 11476 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 11477 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11478 | With optional end, stop comparing S at that position.\n\ |
| 11479 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11480 | |
| 11481 | static PyObject * |
| 11482 | unicode_endswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11483 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11484 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11485 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11486 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11487 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11488 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11489 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11490 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11491 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11492 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11493 | if (PyTuple_Check(subobj)) { |
| 11494 | Py_ssize_t i; |
| 11495 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 11496 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11497 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11498 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11499 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11500 | result = tailmatch(self, substring, start, end, +1); |
| 11501 | Py_DECREF(substring); |
| 11502 | if (result) { |
| 11503 | Py_RETURN_TRUE; |
| 11504 | } |
| 11505 | } |
| 11506 | Py_RETURN_FALSE; |
| 11507 | } |
| 11508 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 11509 | if (substring == NULL) { |
| 11510 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 11511 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 11512 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11513 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 11514 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11515 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11516 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11517 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11518 | } |
| 11519 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11520 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 11521 | |
| 11522 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11523 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 11524 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 11525 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 11526 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 11527 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 11528 | PyDoc_STRVAR(format_map__doc__, |
| 11529 | "S.format_map(mapping) -> str\n\ |
| 11530 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 11531 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 11532 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 11533 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 11534 | static PyObject * |
| 11535 | unicode__format__(PyObject* self, PyObject* args) |
| 11536 | { |
| 11537 | PyObject *format_spec; |
| 11538 | |
| 11539 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 11540 | return NULL; |
| 11541 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11542 | return _PyUnicode_FormatAdvanced(self, format_spec, 0, |
| 11543 | PyUnicode_GET_LENGTH(format_spec)); |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 11544 | } |
| 11545 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 11546 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11547 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 11548 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 11549 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 11550 | |
| 11551 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 11552 | unicode__sizeof__(PyUnicodeObject *v) |
| 11553 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11554 | Py_ssize_t size; |
| 11555 | |
| 11556 | /* If it's a compact object, account for base structure + |
| 11557 | character data. */ |
| 11558 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 11559 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 11560 | else if (PyUnicode_IS_COMPACT(v)) |
| 11561 | size = sizeof(PyCompactUnicodeObject) + |
| 11562 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_CHARACTER_SIZE(v); |
| 11563 | else { |
| 11564 | /* If it is a two-block object, account for base object, and |
| 11565 | for character block if present. */ |
| 11566 | size = sizeof(PyUnicodeObject); |
| 11567 | if (v->data.any) |
| 11568 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
| 11569 | PyUnicode_CHARACTER_SIZE(v); |
| 11570 | } |
| 11571 | /* If the wstr pointer is present, account for it unless it is shared |
| 11572 | with the data pointer. Since PyUnicode_DATA will crash if the object |
| 11573 | is not ready, check whether it's either not ready (in which case the |
| 11574 | data is entirely in wstr) or if the data is not shared. */ |
| 11575 | if (_PyUnicode_WSTR(v) && |
| 11576 | (!PyUnicode_IS_READY(v) || |
| 11577 | (PyUnicode_DATA(v) != _PyUnicode_WSTR(v)))) |
| 11578 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
| 11579 | if (_PyUnicode_UTF8(v) && _PyUnicode_UTF8(v) != PyUnicode_DATA(v)) |
| 11580 | size += _PyUnicode_UTF8_LENGTH(v) + 1; |
| 11581 | |
| 11582 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 11583 | } |
| 11584 | |
| 11585 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11586 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 11587 | |
| 11588 | static PyObject * |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 11589 | unicode_getnewargs(PyUnicodeObject *v) |
| 11590 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11591 | PyObject *copy; |
| 11592 | unsigned char *data; |
| 11593 | int kind; |
| 11594 | if (PyUnicode_READY(v) == -1) |
| 11595 | return NULL; |
| 11596 | kind = PyUnicode_KIND(v); |
| 11597 | data = PyUnicode_1BYTE_DATA(v); |
| 11598 | copy = PyUnicode_FromKindAndData(kind, data, PyUnicode_GET_LENGTH(v)); |
| 11599 | if (!copy) |
| 11600 | return NULL; |
| 11601 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 11602 | } |
| 11603 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11604 | static PyMethodDef unicode_methods[] = { |
| 11605 | |
| 11606 | /* Order is according to common usage: often used methods should |
| 11607 | appear first, since lookup is done sequentially. */ |
| 11608 | |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 11609 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11610 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 11611 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11612 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11613 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 11614 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 11615 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 11616 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 11617 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 11618 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 11619 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11620 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11621 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 11622 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 11623 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11624 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11625 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 11626 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 11627 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11628 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11629 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 11630 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11631 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11632 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 11633 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 11634 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 11635 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 11636 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 11637 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 11638 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 11639 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 11640 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 11641 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 11642 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 11643 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 11644 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 11645 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11646 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11647 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11648 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 11649 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 11650 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 11651 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11652 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 11653 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 11654 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 11655 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11656 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11657 | #endif |
| 11658 | |
| 11659 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 11660 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 11661 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11662 | #endif |
| 11663 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11664 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11665 | {NULL, NULL} |
| 11666 | }; |
| 11667 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 11668 | static PyObject * |
| 11669 | unicode_mod(PyObject *v, PyObject *w) |
| 11670 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 11671 | if (!PyUnicode_Check(v)) |
| 11672 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11673 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 11674 | } |
| 11675 | |
| 11676 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11677 | 0, /*nb_add*/ |
| 11678 | 0, /*nb_subtract*/ |
| 11679 | 0, /*nb_multiply*/ |
| 11680 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 11681 | }; |
| 11682 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11683 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11684 | (lenfunc) unicode_length, /* sq_length */ |
| 11685 | PyUnicode_Concat, /* sq_concat */ |
| 11686 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 11687 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 11688 | 0, /* sq_slice */ |
| 11689 | 0, /* sq_ass_item */ |
| 11690 | 0, /* sq_ass_slice */ |
| 11691 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11692 | }; |
| 11693 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11694 | static PyObject* |
| 11695 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 11696 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11697 | if (PyUnicode_READY(self) == -1) |
| 11698 | return NULL; |
| 11699 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 11700 | if (PyIndex_Check(item)) { |
| 11701 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11702 | if (i == -1 && PyErr_Occurred()) |
| 11703 | return NULL; |
| 11704 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11705 | i += PyUnicode_GET_LENGTH(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11706 | return unicode_getitem(self, i); |
| 11707 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11708 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11709 | const Py_UNICODE* source_buf; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11710 | Py_UNICODE* result_buf; |
| 11711 | PyObject* result; |
| 11712 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11713 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11714 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11715 | return NULL; |
| 11716 | } |
| 11717 | |
| 11718 | if (slicelength <= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11719 | return PyUnicode_New(0, 0); |
| 11720 | } else if (start == 0 && step == 1 && |
| 11721 | slicelength == PyUnicode_GET_LENGTH(self) && |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 11722 | PyUnicode_CheckExact(self)) { |
| 11723 | Py_INCREF(self); |
| 11724 | return (PyObject *)self; |
| 11725 | } else if (step == 1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11726 | return substring(self, start, slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11727 | } else { |
| 11728 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 11729 | result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* |
| 11730 | sizeof(Py_UNICODE)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11731 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11732 | if (result_buf == NULL) |
| 11733 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11734 | |
| 11735 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 11736 | result_buf[i] = source_buf[cur]; |
| 11737 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11738 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11739 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 11740 | PyObject_FREE(result_buf); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11741 | return result; |
| 11742 | } |
| 11743 | } else { |
| 11744 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 11745 | return NULL; |
| 11746 | } |
| 11747 | } |
| 11748 | |
| 11749 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11750 | (lenfunc)unicode_length, /* mp_length */ |
| 11751 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 11752 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11753 | }; |
| 11754 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11755 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11756 | /* Helpers for PyUnicode_Format() */ |
| 11757 | |
| 11758 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11759 | 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] | 11760 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11761 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11762 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11763 | (*p_argidx)++; |
| 11764 | if (arglen < 0) |
| 11765 | return args; |
| 11766 | else |
| 11767 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11768 | } |
| 11769 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11770 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11771 | return NULL; |
| 11772 | } |
| 11773 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 11774 | /* 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] | 11775 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 11776 | static PyObject * |
| 11777 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11778 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 11779 | char *p; |
| 11780 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11781 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11782 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11783 | x = PyFloat_AsDouble(v); |
| 11784 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 11785 | return NULL; |
| 11786 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11787 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11788 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 11789 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 11790 | p = PyOS_double_to_string(x, type, prec, |
| 11791 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 11792 | if (p == NULL) |
| 11793 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11794 | result = PyUnicode_DecodeASCII(p, strlen(p), NULL); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 11795 | PyMem_Free(p); |
| 11796 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11797 | } |
| 11798 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 11799 | static PyObject* |
| 11800 | formatlong(PyObject *val, int flags, int prec, int type) |
| 11801 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11802 | char *buf; |
| 11803 | int len; |
| 11804 | PyObject *str; /* temporary string object. */ |
| 11805 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 11806 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11807 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 11808 | if (!str) |
| 11809 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11810 | result = PyUnicode_DecodeASCII(buf, len, NULL); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11811 | Py_DECREF(str); |
| 11812 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 11813 | } |
| 11814 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11815 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11816 | formatchar(Py_UCS4 *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 11817 | size_t buflen, |
| 11818 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11819 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 11820 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 11821 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11822 | if (PyUnicode_GET_LENGTH(v) == 1) { |
| 11823 | buf[0] = PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11824 | buf[1] = '\0'; |
| 11825 | return 1; |
| 11826 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11827 | goto onError; |
| 11828 | } |
| 11829 | else { |
| 11830 | /* Integer input truncated to a character */ |
| 11831 | long x; |
| 11832 | x = PyLong_AsLong(v); |
| 11833 | if (x == -1 && PyErr_Occurred()) |
| 11834 | goto onError; |
| 11835 | |
| 11836 | if (x < 0 || x > 0x10ffff) { |
| 11837 | PyErr_SetString(PyExc_OverflowError, |
| 11838 | "%c arg not in range(0x110000)"); |
| 11839 | return -1; |
| 11840 | } |
| 11841 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11842 | buf[0] = (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11843 | buf[1] = '\0'; |
| 11844 | return 1; |
| 11845 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 11846 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11847 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 11848 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11849 | "%c requires int or char"); |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 11850 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11851 | } |
| 11852 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 11853 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 11854 | 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] | 11855 | */ |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 11856 | #define FORMATBUFLEN (size_t)10 |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 11857 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11858 | PyObject * |
| 11859 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11860 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11861 | void *fmt; |
| 11862 | int fmtkind; |
| 11863 | PyObject *result; |
| 11864 | Py_UCS4 *res, *res0; |
| 11865 | Py_UCS4 max; |
| 11866 | int kind; |
| 11867 | Py_ssize_t fmtcnt, fmtpos, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11868 | int args_owned = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11869 | PyObject *dict = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11870 | PyUnicodeObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11871 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11872 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11873 | PyErr_BadInternalCall(); |
| 11874 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11875 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11876 | uformat = (PyUnicodeObject*)PyUnicode_FromObject(format); |
| 11877 | if (uformat == NULL || PyUnicode_READY(uformat) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11878 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11879 | fmt = PyUnicode_DATA(uformat); |
| 11880 | fmtkind = PyUnicode_KIND(uformat); |
| 11881 | fmtcnt = PyUnicode_GET_LENGTH(uformat); |
| 11882 | fmtpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11883 | |
| 11884 | reslen = rescnt = fmtcnt + 100; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11885 | res = res0 = PyMem_Malloc(reslen * sizeof(Py_UCS4)); |
| 11886 | if (res0 == NULL) { |
| 11887 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11888 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11889 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11890 | |
| 11891 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11892 | arglen = PyTuple_Size(args); |
| 11893 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11894 | } |
| 11895 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11896 | arglen = -1; |
| 11897 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11898 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 11899 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 11900 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11901 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11902 | |
| 11903 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11904 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11905 | if (--rescnt < 0) { |
| 11906 | rescnt = fmtcnt + 100; |
| 11907 | reslen += rescnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11908 | res0 = PyMem_Realloc(res0, reslen*sizeof(Py_UCS4)); |
| 11909 | if (res0 == NULL){ |
| 11910 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11911 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11912 | } |
| 11913 | res = res0 + reslen - rescnt; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11914 | --rescnt; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11915 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11916 | *res++ = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11917 | } |
| 11918 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11919 | /* Got a format specifier */ |
| 11920 | int flags = 0; |
| 11921 | Py_ssize_t width = -1; |
| 11922 | int prec = -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11923 | Py_UCS4 c = '\0'; |
| 11924 | Py_UCS4 fill; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11925 | int isnumok; |
| 11926 | PyObject *v = NULL; |
| 11927 | PyObject *temp = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11928 | void *pbuf; |
| 11929 | Py_ssize_t pindex; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11930 | Py_UNICODE sign; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11931 | Py_ssize_t len, len1; |
| 11932 | Py_UCS4 formatbuf[FORMATBUFLEN]; /* For formatchar() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11933 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11934 | fmtpos++; |
| 11935 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') { |
| 11936 | Py_ssize_t keystart; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11937 | Py_ssize_t keylen; |
| 11938 | PyObject *key; |
| 11939 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 11940 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11941 | if (dict == NULL) { |
| 11942 | PyErr_SetString(PyExc_TypeError, |
| 11943 | "format requires a mapping"); |
| 11944 | goto onError; |
| 11945 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11946 | ++fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11947 | --fmtcnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11948 | keystart = fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11949 | /* Skip over balanced parentheses */ |
| 11950 | while (pcount > 0 && --fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11951 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == ')') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11952 | --pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11953 | else if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11954 | ++pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11955 | fmtpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11956 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11957 | keylen = fmtpos - keystart - 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11958 | if (fmtcnt < 0 || pcount > 0) { |
| 11959 | PyErr_SetString(PyExc_ValueError, |
| 11960 | "incomplete format key"); |
| 11961 | goto onError; |
| 11962 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11963 | key = substring(uformat, keystart, keylen); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11964 | if (key == NULL) |
| 11965 | goto onError; |
| 11966 | if (args_owned) { |
| 11967 | Py_DECREF(args); |
| 11968 | args_owned = 0; |
| 11969 | } |
| 11970 | args = PyObject_GetItem(dict, key); |
| 11971 | Py_DECREF(key); |
| 11972 | if (args == NULL) { |
| 11973 | goto onError; |
| 11974 | } |
| 11975 | args_owned = 1; |
| 11976 | arglen = -1; |
| 11977 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11978 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11979 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11980 | switch (c = PyUnicode_READ(fmtkind, fmt, fmtpos++)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11981 | case '-': flags |= F_LJUST; continue; |
| 11982 | case '+': flags |= F_SIGN; continue; |
| 11983 | case ' ': flags |= F_BLANK; continue; |
| 11984 | case '#': flags |= F_ALT; continue; |
| 11985 | case '0': flags |= F_ZERO; continue; |
| 11986 | } |
| 11987 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11988 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11989 | if (c == '*') { |
| 11990 | v = getnextarg(args, arglen, &argidx); |
| 11991 | if (v == NULL) |
| 11992 | goto onError; |
| 11993 | if (!PyLong_Check(v)) { |
| 11994 | PyErr_SetString(PyExc_TypeError, |
| 11995 | "* wants int"); |
| 11996 | goto onError; |
| 11997 | } |
| 11998 | width = PyLong_AsLong(v); |
| 11999 | if (width == -1 && PyErr_Occurred()) |
| 12000 | goto onError; |
| 12001 | if (width < 0) { |
| 12002 | flags |= F_LJUST; |
| 12003 | width = -width; |
| 12004 | } |
| 12005 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12006 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12007 | } |
| 12008 | else if (c >= '0' && c <= '9') { |
| 12009 | width = c - '0'; |
| 12010 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12011 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12012 | if (c < '0' || c > '9') |
| 12013 | break; |
| 12014 | if ((width*10) / 10 != width) { |
| 12015 | PyErr_SetString(PyExc_ValueError, |
| 12016 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12017 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12018 | } |
| 12019 | width = width*10 + (c - '0'); |
| 12020 | } |
| 12021 | } |
| 12022 | if (c == '.') { |
| 12023 | prec = 0; |
| 12024 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12025 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12026 | if (c == '*') { |
| 12027 | v = getnextarg(args, arglen, &argidx); |
| 12028 | if (v == NULL) |
| 12029 | goto onError; |
| 12030 | if (!PyLong_Check(v)) { |
| 12031 | PyErr_SetString(PyExc_TypeError, |
| 12032 | "* wants int"); |
| 12033 | goto onError; |
| 12034 | } |
| 12035 | prec = PyLong_AsLong(v); |
| 12036 | if (prec == -1 && PyErr_Occurred()) |
| 12037 | goto onError; |
| 12038 | if (prec < 0) |
| 12039 | prec = 0; |
| 12040 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12041 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12042 | } |
| 12043 | else if (c >= '0' && c <= '9') { |
| 12044 | prec = c - '0'; |
| 12045 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12046 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12047 | if (c < '0' || c > '9') |
| 12048 | break; |
| 12049 | if ((prec*10) / 10 != prec) { |
| 12050 | PyErr_SetString(PyExc_ValueError, |
| 12051 | "prec too big"); |
| 12052 | goto onError; |
| 12053 | } |
| 12054 | prec = prec*10 + (c - '0'); |
| 12055 | } |
| 12056 | } |
| 12057 | } /* prec */ |
| 12058 | if (fmtcnt >= 0) { |
| 12059 | if (c == 'h' || c == 'l' || c == 'L') { |
| 12060 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12061 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12062 | } |
| 12063 | } |
| 12064 | if (fmtcnt < 0) { |
| 12065 | PyErr_SetString(PyExc_ValueError, |
| 12066 | "incomplete format"); |
| 12067 | goto onError; |
| 12068 | } |
| 12069 | if (c != '%') { |
| 12070 | v = getnextarg(args, arglen, &argidx); |
| 12071 | if (v == NULL) |
| 12072 | goto onError; |
| 12073 | } |
| 12074 | sign = 0; |
| 12075 | fill = ' '; |
| 12076 | switch (c) { |
| 12077 | |
| 12078 | case '%': |
| 12079 | pbuf = formatbuf; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12080 | kind = PyUnicode_4BYTE_KIND; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12081 | /* presume that buffer length is at least 1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12082 | PyUnicode_WRITE(kind, pbuf, 0, '%'); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12083 | len = 1; |
| 12084 | break; |
| 12085 | |
| 12086 | case 's': |
| 12087 | case 'r': |
| 12088 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 12089 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12090 | temp = v; |
| 12091 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12092 | } |
| 12093 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12094 | if (c == 's') |
| 12095 | temp = PyObject_Str(v); |
| 12096 | else if (c == 'r') |
| 12097 | temp = PyObject_Repr(v); |
| 12098 | else |
| 12099 | temp = PyObject_ASCII(v); |
| 12100 | if (temp == NULL) |
| 12101 | goto onError; |
| 12102 | if (PyUnicode_Check(temp)) |
| 12103 | /* nothing to do */; |
| 12104 | else { |
| 12105 | Py_DECREF(temp); |
| 12106 | PyErr_SetString(PyExc_TypeError, |
| 12107 | "%s argument has non-string str()"); |
| 12108 | goto onError; |
| 12109 | } |
| 12110 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12111 | if (PyUnicode_READY(temp) == -1) { |
| 12112 | Py_CLEAR(temp); |
| 12113 | goto onError; |
| 12114 | } |
| 12115 | pbuf = PyUnicode_DATA(temp); |
| 12116 | kind = PyUnicode_KIND(temp); |
| 12117 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12118 | if (prec >= 0 && len > prec) |
| 12119 | len = prec; |
| 12120 | break; |
| 12121 | |
| 12122 | case 'i': |
| 12123 | case 'd': |
| 12124 | case 'u': |
| 12125 | case 'o': |
| 12126 | case 'x': |
| 12127 | case 'X': |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12128 | isnumok = 0; |
| 12129 | if (PyNumber_Check(v)) { |
| 12130 | PyObject *iobj=NULL; |
| 12131 | |
| 12132 | if (PyLong_Check(v)) { |
| 12133 | iobj = v; |
| 12134 | Py_INCREF(iobj); |
| 12135 | } |
| 12136 | else { |
| 12137 | iobj = PyNumber_Long(v); |
| 12138 | } |
| 12139 | if (iobj!=NULL) { |
| 12140 | if (PyLong_Check(iobj)) { |
| 12141 | isnumok = 1; |
Senthil Kumaran | 9ebe08d | 2011-07-03 21:03:16 -0700 | [diff] [blame] | 12142 | temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12143 | Py_DECREF(iobj); |
| 12144 | if (!temp) |
| 12145 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12146 | if (PyUnicode_READY(temp) == -1) { |
| 12147 | Py_CLEAR(temp); |
| 12148 | goto onError; |
| 12149 | } |
| 12150 | pbuf = PyUnicode_DATA(temp); |
| 12151 | kind = PyUnicode_KIND(temp); |
| 12152 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12153 | sign = 1; |
| 12154 | } |
| 12155 | else { |
| 12156 | Py_DECREF(iobj); |
| 12157 | } |
| 12158 | } |
| 12159 | } |
| 12160 | if (!isnumok) { |
| 12161 | PyErr_Format(PyExc_TypeError, |
| 12162 | "%%%c format: a number is required, " |
| 12163 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 12164 | goto onError; |
| 12165 | } |
| 12166 | if (flags & F_ZERO) |
| 12167 | fill = '0'; |
| 12168 | break; |
| 12169 | |
| 12170 | case 'e': |
| 12171 | case 'E': |
| 12172 | case 'f': |
| 12173 | case 'F': |
| 12174 | case 'g': |
| 12175 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12176 | temp = formatfloat(v, flags, prec, c); |
| 12177 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12178 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12179 | if (PyUnicode_READY(temp) == -1) { |
| 12180 | Py_CLEAR(temp); |
| 12181 | goto onError; |
| 12182 | } |
| 12183 | pbuf = PyUnicode_DATA(temp); |
| 12184 | kind = PyUnicode_KIND(temp); |
| 12185 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12186 | sign = 1; |
| 12187 | if (flags & F_ZERO) |
| 12188 | fill = '0'; |
| 12189 | break; |
| 12190 | |
| 12191 | case 'c': |
| 12192 | pbuf = formatbuf; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12193 | kind = PyUnicode_4BYTE_KIND; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12194 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
| 12195 | if (len < 0) |
| 12196 | goto onError; |
| 12197 | break; |
| 12198 | |
| 12199 | default: |
| 12200 | PyErr_Format(PyExc_ValueError, |
| 12201 | "unsupported format character '%c' (0x%x) " |
| 12202 | "at index %zd", |
| 12203 | (31<=c && c<=126) ? (char)c : '?', |
| 12204 | (int)c, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12205 | fmtpos - 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12206 | goto onError; |
| 12207 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12208 | /* pbuf is initialized here. */ |
| 12209 | pindex = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12210 | if (sign) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12211 | if (PyUnicode_READ(kind, pbuf, pindex) == '-' || |
| 12212 | PyUnicode_READ(kind, pbuf, pindex) == '+') { |
| 12213 | sign = PyUnicode_READ(kind, pbuf, pindex++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12214 | len--; |
| 12215 | } |
| 12216 | else if (flags & F_SIGN) |
| 12217 | sign = '+'; |
| 12218 | else if (flags & F_BLANK) |
| 12219 | sign = ' '; |
| 12220 | else |
| 12221 | sign = 0; |
| 12222 | } |
| 12223 | if (width < len) |
| 12224 | width = len; |
| 12225 | if (rescnt - (sign != 0) < width) { |
| 12226 | reslen -= rescnt; |
| 12227 | rescnt = width + fmtcnt + 100; |
| 12228 | reslen += rescnt; |
| 12229 | if (reslen < 0) { |
| 12230 | Py_XDECREF(temp); |
| 12231 | PyErr_NoMemory(); |
| 12232 | goto onError; |
| 12233 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12234 | res0 = PyMem_Realloc(res0, reslen*sizeof(Py_UCS4)); |
| 12235 | if (res0 == 0) { |
| 12236 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12237 | Py_XDECREF(temp); |
| 12238 | goto onError; |
| 12239 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12240 | res = res0 + reslen - rescnt; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12241 | } |
| 12242 | if (sign) { |
| 12243 | if (fill != ' ') |
| 12244 | *res++ = sign; |
| 12245 | rescnt--; |
| 12246 | if (width > len) |
| 12247 | width--; |
| 12248 | } |
| 12249 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12250 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 12251 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12252 | if (fill != ' ') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12253 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
| 12254 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12255 | } |
| 12256 | rescnt -= 2; |
| 12257 | width -= 2; |
| 12258 | if (width < 0) |
| 12259 | width = 0; |
| 12260 | len -= 2; |
| 12261 | } |
| 12262 | if (width > len && !(flags & F_LJUST)) { |
| 12263 | do { |
| 12264 | --rescnt; |
| 12265 | *res++ = fill; |
| 12266 | } while (--width > len); |
| 12267 | } |
| 12268 | if (fill == ' ') { |
| 12269 | if (sign) |
| 12270 | *res++ = sign; |
| 12271 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12272 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 12273 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
| 12274 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
| 12275 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12276 | } |
| 12277 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12278 | /* Copy all characters, preserving len */ |
| 12279 | len1 = len; |
| 12280 | while (len1--) { |
| 12281 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
| 12282 | rescnt--; |
| 12283 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12284 | while (--width >= len) { |
| 12285 | --rescnt; |
| 12286 | *res++ = ' '; |
| 12287 | } |
| 12288 | if (dict && (argidx < arglen) && c != '%') { |
| 12289 | PyErr_SetString(PyExc_TypeError, |
| 12290 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 12291 | Py_XDECREF(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12292 | goto onError; |
| 12293 | } |
| 12294 | Py_XDECREF(temp); |
| 12295 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12296 | } /* until end */ |
| 12297 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12298 | PyErr_SetString(PyExc_TypeError, |
| 12299 | "not all arguments converted during string formatting"); |
| 12300 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12301 | } |
| 12302 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12303 | |
| 12304 | for (max=0, res = res0; res < res0+reslen-rescnt; res++) |
| 12305 | if (*res > max) |
| 12306 | max = *res; |
| 12307 | result = PyUnicode_New(reslen - rescnt, max); |
| 12308 | if (!result) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12309 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12310 | kind = PyUnicode_KIND(result); |
| 12311 | for (res = res0; res < res0+reslen-rescnt; res++) |
| 12312 | PyUnicode_WRITE(kind, PyUnicode_DATA(result), res-res0, *res); |
| 12313 | PyMem_Free(res0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12314 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12315 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12316 | } |
| 12317 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12318 | return (PyObject *)result; |
| 12319 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12320 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12321 | PyMem_Free(res0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12322 | Py_DECREF(uformat); |
| 12323 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12324 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12325 | } |
| 12326 | return NULL; |
| 12327 | } |
| 12328 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 12329 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 12330 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 12331 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12332 | static PyObject * |
| 12333 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 12334 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12335 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12336 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 12337 | char *encoding = NULL; |
| 12338 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12339 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12340 | if (type != &PyUnicode_Type) |
| 12341 | return unicode_subtype_new(type, args, kwds); |
| 12342 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12343 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12344 | return NULL; |
| 12345 | if (x == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12346 | return (PyObject *)PyUnicode_New(0, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12347 | if (encoding == NULL && errors == NULL) |
| 12348 | return PyObject_Str(x); |
| 12349 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12350 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12351 | } |
| 12352 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 12353 | static PyObject * |
| 12354 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 12355 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12356 | PyUnicodeObject *tmp, *pnew; |
| 12357 | Py_ssize_t n; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12358 | PyObject *err = NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 12359 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12360 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 12361 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 12362 | if (tmp == NULL) |
| 12363 | return NULL; |
| 12364 | assert(PyUnicode_Check(tmp)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12365 | // TODO: Verify the PyUnicode_GET_SIZE does the right thing. |
| 12366 | // it seems kind of strange that tp_alloc gets passed the size |
| 12367 | // of the unicode string because there will follow another |
| 12368 | // malloc. |
| 12369 | pnew = (PyUnicodeObject *) type->tp_alloc(type, |
| 12370 | n = PyUnicode_GET_SIZE(tmp)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12371 | if (pnew == NULL) { |
| 12372 | Py_DECREF(tmp); |
| 12373 | return NULL; |
| 12374 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12375 | _PyUnicode_WSTR(pnew) = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1)); |
| 12376 | if (_PyUnicode_WSTR(pnew) == NULL) { |
| 12377 | err = PyErr_NoMemory(); |
| 12378 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12379 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12380 | Py_UNICODE_COPY(_PyUnicode_WSTR(pnew), PyUnicode_AS_UNICODE(tmp), n+1); |
| 12381 | _PyUnicode_WSTR_LENGTH(pnew) = n; |
| 12382 | _PyUnicode_HASH(pnew) = _PyUnicode_HASH(tmp); |
| 12383 | _PyUnicode_STATE(pnew).interned = 0; |
| 12384 | _PyUnicode_STATE(pnew).kind = 0; |
| 12385 | _PyUnicode_STATE(pnew).compact = 0; |
| 12386 | _PyUnicode_STATE(pnew).ready = 0; |
| 12387 | _PyUnicode_STATE(pnew).ascii = 0; |
| 12388 | pnew->data.any = NULL; |
| 12389 | _PyUnicode_LENGTH(pnew) = 0; |
| 12390 | pnew->_base.utf8 = NULL; |
| 12391 | pnew->_base.utf8_length = 0; |
| 12392 | |
| 12393 | if (PyUnicode_READY(pnew) == -1) { |
| 12394 | PyObject_FREE(_PyUnicode_WSTR(pnew)); |
| 12395 | goto onError; |
| 12396 | } |
| 12397 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12398 | Py_DECREF(tmp); |
| 12399 | return (PyObject *)pnew; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12400 | |
| 12401 | onError: |
| 12402 | _Py_ForgetReference((PyObject *)pnew); |
| 12403 | PyObject_Del(pnew); |
| 12404 | Py_DECREF(tmp); |
| 12405 | return err; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 12406 | } |
| 12407 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12408 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12409 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12410 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 12411 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 12412 | encoding defaults to the current default string encoding.\n\ |
| 12413 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12414 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12415 | static PyObject *unicode_iter(PyObject *seq); |
| 12416 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12417 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 12418 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12419 | "str", /* tp_name */ |
| 12420 | sizeof(PyUnicodeObject), /* tp_size */ |
| 12421 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12422 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12423 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 12424 | 0, /* tp_print */ |
| 12425 | 0, /* tp_getattr */ |
| 12426 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 12427 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12428 | unicode_repr, /* tp_repr */ |
| 12429 | &unicode_as_number, /* tp_as_number */ |
| 12430 | &unicode_as_sequence, /* tp_as_sequence */ |
| 12431 | &unicode_as_mapping, /* tp_as_mapping */ |
| 12432 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 12433 | 0, /* tp_call*/ |
| 12434 | (reprfunc) unicode_str, /* tp_str */ |
| 12435 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 12436 | 0, /* tp_setattro */ |
| 12437 | 0, /* tp_as_buffer */ |
| 12438 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12439 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12440 | unicode_doc, /* tp_doc */ |
| 12441 | 0, /* tp_traverse */ |
| 12442 | 0, /* tp_clear */ |
| 12443 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 12444 | 0, /* tp_weaklistoffset */ |
| 12445 | unicode_iter, /* tp_iter */ |
| 12446 | 0, /* tp_iternext */ |
| 12447 | unicode_methods, /* tp_methods */ |
| 12448 | 0, /* tp_members */ |
| 12449 | 0, /* tp_getset */ |
| 12450 | &PyBaseObject_Type, /* tp_base */ |
| 12451 | 0, /* tp_dict */ |
| 12452 | 0, /* tp_descr_get */ |
| 12453 | 0, /* tp_descr_set */ |
| 12454 | 0, /* tp_dictoffset */ |
| 12455 | 0, /* tp_init */ |
| 12456 | 0, /* tp_alloc */ |
| 12457 | unicode_new, /* tp_new */ |
| 12458 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12459 | }; |
| 12460 | |
| 12461 | /* Initialize the Unicode implementation */ |
| 12462 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 12463 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12464 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 12465 | int i; |
| 12466 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12467 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12468 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12469 | 0x000A, /* LINE FEED */ |
| 12470 | 0x000D, /* CARRIAGE RETURN */ |
| 12471 | 0x001C, /* FILE SEPARATOR */ |
| 12472 | 0x001D, /* GROUP SEPARATOR */ |
| 12473 | 0x001E, /* RECORD SEPARATOR */ |
| 12474 | 0x0085, /* NEXT LINE */ |
| 12475 | 0x2028, /* LINE SEPARATOR */ |
| 12476 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 12477 | }; |
| 12478 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 12479 | /* Init the implementation */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12480 | unicode_empty = (PyUnicodeObject *) PyUnicode_New(0, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12481 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12482 | Py_FatalError("Can't create empty string"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12483 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 12484 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12485 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 12486 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12487 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12488 | |
| 12489 | /* initialize the linebreak bloom filter */ |
| 12490 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12491 | PyUnicode_2BYTE_KIND, linebreak, |
| 12492 | sizeof(linebreak) / sizeof(linebreak[0])); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12493 | |
| 12494 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12495 | } |
| 12496 | |
| 12497 | /* Finalize the Unicode implementation */ |
| 12498 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 12499 | int |
| 12500 | PyUnicode_ClearFreeList(void) |
| 12501 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12502 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 12503 | } |
| 12504 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12505 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 12506 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12507 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 12508 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12509 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 12510 | Py_XDECREF(unicode_empty); |
| 12511 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 12512 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 12513 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12514 | if (unicode_latin1[i]) { |
| 12515 | Py_DECREF(unicode_latin1[i]); |
| 12516 | unicode_latin1[i] = NULL; |
| 12517 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 12518 | } |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 12519 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12520 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 12521 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12522 | void |
| 12523 | PyUnicode_InternInPlace(PyObject **p) |
| 12524 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12525 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 12526 | PyObject *t; |
| 12527 | if (s == NULL || !PyUnicode_Check(s)) |
| 12528 | Py_FatalError( |
| 12529 | "PyUnicode_InternInPlace: unicode strings only please!"); |
| 12530 | /* If it's a subclass, we don't really know what putting |
| 12531 | it in the interned dict might do. */ |
| 12532 | if (!PyUnicode_CheckExact(s)) |
| 12533 | return; |
| 12534 | if (PyUnicode_CHECK_INTERNED(s)) |
| 12535 | return; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12536 | if (PyUnicode_READY(s) == -1) { |
| 12537 | assert(0 && "ready fail in intern..."); |
| 12538 | return; |
| 12539 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12540 | if (interned == NULL) { |
| 12541 | interned = PyDict_New(); |
| 12542 | if (interned == NULL) { |
| 12543 | PyErr_Clear(); /* Don't leave an exception */ |
| 12544 | return; |
| 12545 | } |
| 12546 | } |
| 12547 | /* It might be that the GetItem call fails even |
| 12548 | though the key is present in the dictionary, |
| 12549 | namely when this happens during a stack overflow. */ |
| 12550 | Py_ALLOW_RECURSION |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12551 | t = PyDict_GetItem(interned, (PyObject *)s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12552 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 12553 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12554 | if (t) { |
| 12555 | Py_INCREF(t); |
| 12556 | Py_DECREF(*p); |
| 12557 | *p = t; |
| 12558 | return; |
| 12559 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12560 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12561 | PyThreadState_GET()->recursion_critical = 1; |
| 12562 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 12563 | PyErr_Clear(); |
| 12564 | PyThreadState_GET()->recursion_critical = 0; |
| 12565 | return; |
| 12566 | } |
| 12567 | PyThreadState_GET()->recursion_critical = 0; |
| 12568 | /* The two references in interned are not counted by refcnt. |
| 12569 | The deallocator will take care of this */ |
| 12570 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12571 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12572 | } |
| 12573 | |
| 12574 | void |
| 12575 | PyUnicode_InternImmortal(PyObject **p) |
| 12576 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12577 | PyUnicodeObject *u = (PyUnicodeObject *)*p; |
| 12578 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12579 | PyUnicode_InternInPlace(p); |
| 12580 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12581 | _PyUnicode_STATE(u).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12582 | Py_INCREF(*p); |
| 12583 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12584 | } |
| 12585 | |
| 12586 | PyObject * |
| 12587 | PyUnicode_InternFromString(const char *cp) |
| 12588 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12589 | PyObject *s = PyUnicode_FromString(cp); |
| 12590 | if (s == NULL) |
| 12591 | return NULL; |
| 12592 | PyUnicode_InternInPlace(&s); |
| 12593 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12594 | } |
| 12595 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12596 | void |
| 12597 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12598 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12599 | PyObject *keys; |
| 12600 | PyUnicodeObject *s; |
| 12601 | Py_ssize_t i, n; |
| 12602 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12603 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12604 | if (interned == NULL || !PyDict_Check(interned)) |
| 12605 | return; |
| 12606 | keys = PyDict_Keys(interned); |
| 12607 | if (keys == NULL || !PyList_Check(keys)) { |
| 12608 | PyErr_Clear(); |
| 12609 | return; |
| 12610 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12611 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12612 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 12613 | detector, interned unicode strings are not forcibly deallocated; |
| 12614 | rather, we give them their stolen references back, and then clear |
| 12615 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12616 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12617 | n = PyList_GET_SIZE(keys); |
| 12618 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12619 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12620 | for (i = 0; i < n; i++) { |
| 12621 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12622 | if (PyUnicode_READY(s) == -1) |
| 12623 | fprintf(stderr, "could not ready string\n"); |
| 12624 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12625 | case SSTATE_NOT_INTERNED: |
| 12626 | /* XXX Shouldn't happen */ |
| 12627 | break; |
| 12628 | case SSTATE_INTERNED_IMMORTAL: |
| 12629 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12630 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12631 | break; |
| 12632 | case SSTATE_INTERNED_MORTAL: |
| 12633 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12634 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12635 | break; |
| 12636 | default: |
| 12637 | Py_FatalError("Inconsistent interned string state."); |
| 12638 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12639 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12640 | } |
| 12641 | fprintf(stderr, "total size of all interned strings: " |
| 12642 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 12643 | "mortal/immortal\n", mortal_size, immortal_size); |
| 12644 | Py_DECREF(keys); |
| 12645 | PyDict_Clear(interned); |
| 12646 | Py_DECREF(interned); |
| 12647 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12648 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12649 | |
| 12650 | |
| 12651 | /********************* Unicode Iterator **************************/ |
| 12652 | |
| 12653 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12654 | PyObject_HEAD |
| 12655 | Py_ssize_t it_index; |
| 12656 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12657 | } unicodeiterobject; |
| 12658 | |
| 12659 | static void |
| 12660 | unicodeiter_dealloc(unicodeiterobject *it) |
| 12661 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12662 | _PyObject_GC_UNTRACK(it); |
| 12663 | Py_XDECREF(it->it_seq); |
| 12664 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12665 | } |
| 12666 | |
| 12667 | static int |
| 12668 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 12669 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12670 | Py_VISIT(it->it_seq); |
| 12671 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12672 | } |
| 12673 | |
| 12674 | static PyObject * |
| 12675 | unicodeiter_next(unicodeiterobject *it) |
| 12676 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12677 | PyUnicodeObject *seq; |
| 12678 | PyObject *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12679 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12680 | assert(it != NULL); |
| 12681 | seq = it->it_seq; |
| 12682 | if (seq == NULL) |
| 12683 | return NULL; |
| 12684 | assert(PyUnicode_Check(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12685 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12686 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 12687 | int kind = PyUnicode_KIND(seq); |
| 12688 | void *data = PyUnicode_DATA(seq); |
| 12689 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 12690 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12691 | if (item != NULL) |
| 12692 | ++it->it_index; |
| 12693 | return item; |
| 12694 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12695 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12696 | Py_DECREF(seq); |
| 12697 | it->it_seq = NULL; |
| 12698 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12699 | } |
| 12700 | |
| 12701 | static PyObject * |
| 12702 | unicodeiter_len(unicodeiterobject *it) |
| 12703 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12704 | Py_ssize_t len = 0; |
| 12705 | if (it->it_seq) |
| 12706 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
| 12707 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12708 | } |
| 12709 | |
| 12710 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 12711 | |
| 12712 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12713 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12714 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12715 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12716 | }; |
| 12717 | |
| 12718 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12719 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 12720 | "str_iterator", /* tp_name */ |
| 12721 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 12722 | 0, /* tp_itemsize */ |
| 12723 | /* methods */ |
| 12724 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 12725 | 0, /* tp_print */ |
| 12726 | 0, /* tp_getattr */ |
| 12727 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 12728 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12729 | 0, /* tp_repr */ |
| 12730 | 0, /* tp_as_number */ |
| 12731 | 0, /* tp_as_sequence */ |
| 12732 | 0, /* tp_as_mapping */ |
| 12733 | 0, /* tp_hash */ |
| 12734 | 0, /* tp_call */ |
| 12735 | 0, /* tp_str */ |
| 12736 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 12737 | 0, /* tp_setattro */ |
| 12738 | 0, /* tp_as_buffer */ |
| 12739 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 12740 | 0, /* tp_doc */ |
| 12741 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 12742 | 0, /* tp_clear */ |
| 12743 | 0, /* tp_richcompare */ |
| 12744 | 0, /* tp_weaklistoffset */ |
| 12745 | PyObject_SelfIter, /* tp_iter */ |
| 12746 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 12747 | unicodeiter_methods, /* tp_methods */ |
| 12748 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12749 | }; |
| 12750 | |
| 12751 | static PyObject * |
| 12752 | unicode_iter(PyObject *seq) |
| 12753 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12754 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12755 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12756 | if (!PyUnicode_Check(seq)) { |
| 12757 | PyErr_BadInternalCall(); |
| 12758 | return NULL; |
| 12759 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12760 | if (PyUnicode_READY(seq) == -1) |
| 12761 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12762 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 12763 | if (it == NULL) |
| 12764 | return NULL; |
| 12765 | it->it_index = 0; |
| 12766 | Py_INCREF(seq); |
| 12767 | it->it_seq = (PyUnicodeObject *)seq; |
| 12768 | _PyObject_GC_TRACK(it); |
| 12769 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12770 | } |
| 12771 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12772 | #define UNIOP(x) Py_UNICODE_##x |
| 12773 | #define UNIOP_t Py_UNICODE |
| 12774 | #include "uniops.h" |
| 12775 | #undef UNIOP |
| 12776 | #undef UNIOP_t |
| 12777 | #define UNIOP(x) Py_UCS4_##x |
| 12778 | #define UNIOP_t Py_UCS4 |
| 12779 | #include "uniops.h" |
| 12780 | #undef UNIOP |
| 12781 | #undef UNIOP_t |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 12782 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 12783 | Py_UNICODE* |
Victor Stinner | 4640860 | 2010-09-03 16:18:00 +0000 | [diff] [blame] | 12784 | PyUnicode_AsUnicodeCopy(PyObject *object) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 12785 | { |
| 12786 | PyUnicodeObject *unicode = (PyUnicodeObject *)object; |
| 12787 | Py_UNICODE *copy; |
| 12788 | Py_ssize_t size; |
| 12789 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12790 | if (!PyUnicode_Check(unicode)) { |
| 12791 | PyErr_BadArgument(); |
| 12792 | return NULL; |
| 12793 | } |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 12794 | /* Ensure we won't overflow the size. */ |
| 12795 | if (PyUnicode_GET_SIZE(unicode) > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 12796 | PyErr_NoMemory(); |
| 12797 | return NULL; |
| 12798 | } |
| 12799 | size = PyUnicode_GET_SIZE(unicode) + 1; /* copy the nul character */ |
| 12800 | size *= sizeof(Py_UNICODE); |
| 12801 | copy = PyMem_Malloc(size); |
| 12802 | if (copy == NULL) { |
| 12803 | PyErr_NoMemory(); |
| 12804 | return NULL; |
| 12805 | } |
| 12806 | memcpy(copy, PyUnicode_AS_UNICODE(unicode), size); |
| 12807 | return copy; |
| 12808 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 12809 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 12810 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 12811 | to the string.Formatter class implemented in Python. */ |
| 12812 | |
| 12813 | static PyMethodDef _string_methods[] = { |
| 12814 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 12815 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 12816 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 12817 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 12818 | {NULL, NULL} |
| 12819 | }; |
| 12820 | |
| 12821 | static struct PyModuleDef _string_module = { |
| 12822 | PyModuleDef_HEAD_INIT, |
| 12823 | "_string", |
| 12824 | PyDoc_STR("string helper module"), |
| 12825 | 0, |
| 12826 | _string_methods, |
| 12827 | NULL, |
| 12828 | NULL, |
| 12829 | NULL, |
| 12830 | NULL |
| 12831 | }; |
| 12832 | |
| 12833 | PyMODINIT_FUNC |
| 12834 | PyInit__string(void) |
| 12835 | { |
| 12836 | return PyModule_Create(&_string_module); |
| 12837 | } |
| 12838 | |
| 12839 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12840 | #ifdef __cplusplus |
| 12841 | } |
| 12842 | #endif |