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 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 93 | /* This dictionary holds all interned unicode strings. Note that references |
| 94 | to strings in this dictionary are *not* counted in the string's ob_refcnt. |
| 95 | When the interned string reaches a refcnt of 0 the string deallocation |
| 96 | function will delete the reference from this dictionary. |
| 97 | |
| 98 | 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] | 99 | 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] | 100 | */ |
| 101 | static PyObject *interned; |
| 102 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 103 | /* Free list for Unicode objects */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 104 | static PyUnicodeObject *free_list; |
| 105 | static int numfree; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 106 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 107 | /* The empty Unicode object is shared to improve performance. */ |
| 108 | static PyUnicodeObject *unicode_empty; |
| 109 | |
| 110 | /* Single character Unicode strings in the Latin-1 range are being |
| 111 | shared as well. */ |
| 112 | static PyUnicodeObject *unicode_latin1[256]; |
| 113 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 114 | /* Fast detection of the most frequent whitespace characters */ |
| 115 | const unsigned char _Py_ascii_whitespace[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 116 | 0, 0, 0, 0, 0, 0, 0, 0, |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 117 | /* case 0x0009: * CHARACTER TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 118 | /* case 0x000A: * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 119 | /* case 0x000B: * LINE TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 120 | /* case 0x000C: * FORM FEED */ |
| 121 | /* case 0x000D: * CARRIAGE RETURN */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 122 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 123 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 124 | /* case 0x001C: * FILE SEPARATOR */ |
| 125 | /* case 0x001D: * GROUP SEPARATOR */ |
| 126 | /* case 0x001E: * RECORD SEPARATOR */ |
| 127 | /* case 0x001F: * UNIT SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 128 | 0, 0, 0, 0, 1, 1, 1, 1, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 129 | /* case 0x0020: * SPACE */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 130 | 1, 0, 0, 0, 0, 0, 0, 0, |
| 131 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 132 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 133 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 134 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 135 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 136 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 137 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 138 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 139 | 0, 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 | }; |
| 144 | |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 145 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
| 146 | PyObject **errorHandler,const char *encoding, const char *reason, |
| 147 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 148 | Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos); |
| 149 | |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 150 | static void raise_encode_exception(PyObject **exceptionObject, |
| 151 | const char *encoding, |
| 152 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 153 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 154 | const char *reason); |
| 155 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 156 | /* Same for linebreaks */ |
| 157 | static unsigned char ascii_linebreak[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 158 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 159 | /* 0x000A, * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 160 | /* 0x000B, * LINE TABULATION */ |
| 161 | /* 0x000C, * FORM FEED */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 162 | /* 0x000D, * CARRIAGE RETURN */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 163 | 0, 0, 1, 1, 1, 1, 0, 0, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 164 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 165 | /* 0x001C, * FILE SEPARATOR */ |
| 166 | /* 0x001D, * GROUP SEPARATOR */ |
| 167 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 168 | 0, 0, 0, 0, 1, 1, 1, 0, |
| 169 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 170 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 171 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 172 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 173 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 174 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 175 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 176 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 177 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 178 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 179 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 180 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 181 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 182 | }; |
| 183 | |
| 184 | |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 185 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 186 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 187 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 188 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 189 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 190 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 191 | /* This is actually an illegal character, so it should |
| 192 | not be passed to unichr. */ |
| 193 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 194 | #endif |
| 195 | } |
| 196 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 197 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 198 | |
| 199 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 200 | to keep things simple, we use a single bitmask, using the least 5 |
| 201 | bits from each unicode characters as the bit index. */ |
| 202 | |
| 203 | /* the linebreak mask is set up by Unicode_Init below */ |
| 204 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 205 | #if LONG_BIT >= 128 |
| 206 | #define BLOOM_WIDTH 128 |
| 207 | #elif LONG_BIT >= 64 |
| 208 | #define BLOOM_WIDTH 64 |
| 209 | #elif LONG_BIT >= 32 |
| 210 | #define BLOOM_WIDTH 32 |
| 211 | #else |
| 212 | #error "LONG_BIT is smaller than 32" |
| 213 | #endif |
| 214 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 215 | #define BLOOM_MASK unsigned long |
| 216 | |
| 217 | static BLOOM_MASK bloom_linebreak; |
| 218 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 219 | #define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
| 220 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 221 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 222 | #define BLOOM_LINEBREAK(ch) \ |
| 223 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 224 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 225 | |
| 226 | Py_LOCAL_INLINE(BLOOM_MASK) make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len) |
| 227 | { |
| 228 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 229 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 230 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 231 | Py_ssize_t i; |
| 232 | |
| 233 | mask = 0; |
| 234 | for (i = 0; i < len; i++) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 235 | BLOOM_ADD(mask, ptr[i]); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 236 | |
| 237 | return mask; |
| 238 | } |
| 239 | |
| 240 | Py_LOCAL_INLINE(int) unicode_member(Py_UNICODE chr, Py_UNICODE* set, Py_ssize_t setlen) |
| 241 | { |
| 242 | Py_ssize_t i; |
| 243 | |
| 244 | for (i = 0; i < setlen; i++) |
| 245 | if (set[i] == chr) |
| 246 | return 1; |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 251 | #define BLOOM_MEMBER(mask, chr, set, setlen) \ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 252 | BLOOM(mask, chr) && unicode_member(chr, set, setlen) |
| 253 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 254 | /* --- Unicode Object ----------------------------------------------------- */ |
| 255 | |
| 256 | static |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 257 | int unicode_resize(register PyUnicodeObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 258 | Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 259 | { |
| 260 | void *oldstr; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 261 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 262 | /* Shortcut if there's nothing much to do. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 263 | if (unicode->length == length) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 264 | goto reset; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 265 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 266 | /* Resizing shared object (unicode_empty or single character |
| 267 | objects) in-place is not allowed. Use PyUnicode_Resize() |
| 268 | instead ! */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 269 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 270 | if (unicode == unicode_empty || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 271 | (unicode->length == 1 && |
| 272 | unicode->str[0] < 256U && |
| 273 | unicode_latin1[unicode->str[0]] == unicode)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 274 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 275 | "can't resize shared str objects"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 276 | return -1; |
| 277 | } |
| 278 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 279 | /* We allocate one more byte to make sure the string is Ux0000 terminated. |
| 280 | The overallocation is also used by fastsearch, which assumes that it's |
| 281 | safe to look at str[length] (without making any assumptions about what |
| 282 | it contains). */ |
| 283 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 284 | oldstr = unicode->str; |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 285 | unicode->str = PyObject_REALLOC(unicode->str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 286 | sizeof(Py_UNICODE) * (length + 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 287 | if (!unicode->str) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 288 | unicode->str = (Py_UNICODE *)oldstr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 289 | PyErr_NoMemory(); |
| 290 | return -1; |
| 291 | } |
| 292 | unicode->str[length] = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 293 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 294 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 295 | reset: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 296 | /* Reset the object caches */ |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 297 | if (unicode->defenc) { |
Georg Brandl | 8ee604b | 2010-07-29 14:23:06 +0000 | [diff] [blame] | 298 | Py_CLEAR(unicode->defenc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 299 | } |
| 300 | unicode->hash = -1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 301 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | /* 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] | 306 | Ux0000 terminated; some code (e.g. new_identifier) |
| 307 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 308 | |
| 309 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 310 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 311 | |
| 312 | */ |
| 313 | |
| 314 | static |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 315 | PyUnicodeObject *_PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 316 | { |
| 317 | register PyUnicodeObject *unicode; |
| 318 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 319 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 320 | if (length == 0 && unicode_empty != NULL) { |
| 321 | Py_INCREF(unicode_empty); |
| 322 | return unicode_empty; |
| 323 | } |
| 324 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 325 | /* Ensure we won't overflow the size. */ |
| 326 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 327 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 328 | } |
| 329 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 330 | /* Unicode freelist & memory allocation */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 331 | if (free_list) { |
| 332 | unicode = free_list; |
| 333 | free_list = *(PyUnicodeObject **)unicode; |
| 334 | numfree--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 335 | if (unicode->str) { |
| 336 | /* Keep-Alive optimization: we only upsize the buffer, |
| 337 | never downsize it. */ |
| 338 | if ((unicode->length < length) && |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 339 | unicode_resize(unicode, length) < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 340 | PyObject_DEL(unicode->str); |
| 341 | unicode->str = NULL; |
| 342 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 343 | } |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 344 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 345 | size_t new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 346 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 347 | } |
| 348 | PyObject_INIT(unicode, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 349 | } |
| 350 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 351 | size_t new_size; |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 352 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 353 | if (unicode == NULL) |
| 354 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 355 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 356 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 359 | if (!unicode->str) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 360 | PyErr_NoMemory(); |
| 361 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 362 | } |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 363 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 364 | * the caller fails before initializing str -- unicode_resize() |
| 365 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 366 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 367 | * We don't want unicode_resize to read uninitialized memory in |
| 368 | * that case. |
| 369 | */ |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 370 | unicode->str[0] = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 371 | unicode->str[length] = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 372 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 373 | unicode->hash = -1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 374 | unicode->state = 0; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 375 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 376 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 377 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 378 | onError: |
Amaury Forgeot d'Arc | 7888d08 | 2008-08-01 01:06:32 +0000 | [diff] [blame] | 379 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
| 380 | _Py_DEC_REFTOTAL; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 381 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 382 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 383 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | static |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 387 | void unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 388 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 389 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 390 | case SSTATE_NOT_INTERNED: |
| 391 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 392 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 393 | case SSTATE_INTERNED_MORTAL: |
| 394 | /* revive dead object temporarily for DelItem */ |
| 395 | Py_REFCNT(unicode) = 3; |
| 396 | if (PyDict_DelItem(interned, (PyObject *)unicode) != 0) |
| 397 | Py_FatalError( |
| 398 | "deletion of interned string failed"); |
| 399 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 400 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 401 | case SSTATE_INTERNED_IMMORTAL: |
| 402 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 403 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 404 | default: |
| 405 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 408 | if (PyUnicode_CheckExact(unicode) && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 409 | numfree < PyUnicode_MAXFREELIST) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 410 | /* Keep-Alive optimization */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 411 | if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { |
| 412 | PyObject_DEL(unicode->str); |
| 413 | unicode->str = NULL; |
| 414 | unicode->length = 0; |
| 415 | } |
| 416 | if (unicode->defenc) { |
Georg Brandl | 8ee604b | 2010-07-29 14:23:06 +0000 | [diff] [blame] | 417 | Py_CLEAR(unicode->defenc); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 418 | } |
| 419 | /* Add to free list */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 420 | *(PyUnicodeObject **)unicode = free_list; |
| 421 | free_list = unicode; |
| 422 | numfree++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 423 | } |
| 424 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 425 | PyObject_DEL(unicode->str); |
| 426 | Py_XDECREF(unicode->defenc); |
| 427 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 431 | static |
| 432 | int _PyUnicode_Resize(PyUnicodeObject **unicode, Py_ssize_t length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 433 | { |
| 434 | register PyUnicodeObject *v; |
| 435 | |
| 436 | /* Argument checks */ |
| 437 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 438 | PyErr_BadInternalCall(); |
| 439 | return -1; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 440 | } |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 441 | v = *unicode; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 442 | if (v == NULL || !PyUnicode_Check(v) || Py_REFCNT(v) != 1 || length < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 443 | PyErr_BadInternalCall(); |
| 444 | return -1; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | /* Resizing unicode_empty and single character objects is not |
| 448 | possible since these are being shared. We simply return a fresh |
| 449 | copy with the same Unicode content. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 450 | if (v->length != length && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 451 | (v == unicode_empty || v->length == 1)) { |
| 452 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 453 | if (w == NULL) |
| 454 | return -1; |
| 455 | Py_UNICODE_COPY(w->str, v->str, |
| 456 | length < v->length ? length : v->length); |
| 457 | Py_DECREF(*unicode); |
| 458 | *unicode = w; |
| 459 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 463 | objects, since we can modify them in-place. */ |
| 464 | return unicode_resize(v, length); |
| 465 | } |
| 466 | |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 467 | int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) |
| 468 | { |
| 469 | return _PyUnicode_Resize((PyUnicodeObject **)unicode, length); |
| 470 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 471 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 472 | PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 473 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 474 | { |
| 475 | PyUnicodeObject *unicode; |
| 476 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 477 | /* If the Unicode data is known at construction time, we can apply |
| 478 | some optimizations which share commonly used objects. */ |
| 479 | if (u != NULL) { |
| 480 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 481 | /* Optimization for empty strings */ |
| 482 | if (size == 0 && unicode_empty != NULL) { |
| 483 | Py_INCREF(unicode_empty); |
| 484 | return (PyObject *)unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 485 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 486 | |
| 487 | /* Single character Unicode objects in the Latin-1 range are |
| 488 | shared when using this constructor */ |
| 489 | if (size == 1 && *u < 256) { |
| 490 | unicode = unicode_latin1[*u]; |
| 491 | if (!unicode) { |
| 492 | unicode = _PyUnicode_New(1); |
| 493 | if (!unicode) |
| 494 | return NULL; |
| 495 | unicode->str[0] = *u; |
| 496 | unicode_latin1[*u] = unicode; |
| 497 | } |
| 498 | Py_INCREF(unicode); |
| 499 | return (PyObject *)unicode; |
| 500 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 501 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 502 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 503 | unicode = _PyUnicode_New(size); |
| 504 | if (!unicode) |
| 505 | return NULL; |
| 506 | |
| 507 | /* Copy the Unicode data into the new object */ |
| 508 | if (u != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 509 | Py_UNICODE_COPY(unicode->str, u, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 510 | |
| 511 | return (PyObject *)unicode; |
| 512 | } |
| 513 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 514 | PyObject *PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 515 | { |
| 516 | PyUnicodeObject *unicode; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 517 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 518 | if (size < 0) { |
| 519 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 520 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 521 | return NULL; |
| 522 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 523 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 524 | /* 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] | 525 | some optimizations which share commonly used objects. |
| 526 | Also, this means the input must be UTF-8, so fall back to the |
| 527 | UTF-8 decoder at the end. */ |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 528 | if (u != NULL) { |
| 529 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 530 | /* Optimization for empty strings */ |
| 531 | if (size == 0 && unicode_empty != NULL) { |
| 532 | Py_INCREF(unicode_empty); |
| 533 | return (PyObject *)unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 534 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 535 | |
| 536 | /* Single characters are shared when using this constructor. |
| 537 | Restrict to ASCII, since the input must be UTF-8. */ |
| 538 | if (size == 1 && Py_CHARMASK(*u) < 128) { |
| 539 | unicode = unicode_latin1[Py_CHARMASK(*u)]; |
| 540 | if (!unicode) { |
| 541 | unicode = _PyUnicode_New(1); |
| 542 | if (!unicode) |
| 543 | return NULL; |
| 544 | unicode->str[0] = Py_CHARMASK(*u); |
| 545 | unicode_latin1[Py_CHARMASK(*u)] = unicode; |
| 546 | } |
| 547 | Py_INCREF(unicode); |
| 548 | return (PyObject *)unicode; |
| 549 | } |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 550 | |
| 551 | return PyUnicode_DecodeUTF8(u, size, NULL); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Walter Dörwald | 5550731 | 2007-05-18 13:12:10 +0000 | [diff] [blame] | 554 | unicode = _PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 555 | if (!unicode) |
| 556 | return NULL; |
| 557 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 558 | return (PyObject *)unicode; |
| 559 | } |
| 560 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 561 | PyObject *PyUnicode_FromString(const char *u) |
| 562 | { |
| 563 | size_t size = strlen(u); |
| 564 | if (size > PY_SSIZE_T_MAX) { |
| 565 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 566 | return NULL; |
| 567 | } |
| 568 | |
| 569 | return PyUnicode_FromStringAndSize(u, size); |
| 570 | } |
| 571 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 572 | #ifdef HAVE_WCHAR_H |
| 573 | |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 574 | #if (Py_UNICODE_SIZE == 2) && defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
| 575 | # define CONVERT_WCHAR_TO_SURROGATES |
| 576 | #endif |
| 577 | |
| 578 | #ifdef CONVERT_WCHAR_TO_SURROGATES |
| 579 | |
| 580 | /* Here sizeof(wchar_t) is 4 but Py_UNICODE_SIZE == 2, so we need |
| 581 | to convert from UTF32 to UTF16. */ |
| 582 | |
| 583 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
| 584 | Py_ssize_t size) |
| 585 | { |
| 586 | PyUnicodeObject *unicode; |
| 587 | register Py_ssize_t i; |
| 588 | Py_ssize_t alloc; |
| 589 | const wchar_t *orig_w; |
| 590 | |
| 591 | if (w == NULL) { |
| 592 | if (size == 0) |
| 593 | return PyUnicode_FromStringAndSize(NULL, 0); |
| 594 | PyErr_BadInternalCall(); |
| 595 | return NULL; |
| 596 | } |
| 597 | |
| 598 | if (size == -1) { |
| 599 | size = wcslen(w); |
| 600 | } |
| 601 | |
| 602 | alloc = size; |
| 603 | orig_w = w; |
| 604 | for (i = size; i > 0; i--) { |
| 605 | if (*w > 0xFFFF) |
| 606 | alloc++; |
| 607 | w++; |
| 608 | } |
| 609 | w = orig_w; |
| 610 | unicode = _PyUnicode_New(alloc); |
| 611 | if (!unicode) |
| 612 | return NULL; |
| 613 | |
| 614 | /* Copy the wchar_t data into the new object */ |
| 615 | { |
| 616 | register Py_UNICODE *u; |
| 617 | u = PyUnicode_AS_UNICODE(unicode); |
| 618 | for (i = size; i > 0; i--) { |
| 619 | if (*w > 0xFFFF) { |
| 620 | wchar_t ordinal = *w++; |
| 621 | ordinal -= 0x10000; |
| 622 | *u++ = 0xD800 | (ordinal >> 10); |
| 623 | *u++ = 0xDC00 | (ordinal & 0x3FF); |
| 624 | } |
| 625 | else |
| 626 | *u++ = *w++; |
| 627 | } |
| 628 | } |
| 629 | return (PyObject *)unicode; |
| 630 | } |
| 631 | |
| 632 | #else |
| 633 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 634 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 635 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 636 | { |
| 637 | PyUnicodeObject *unicode; |
| 638 | |
| 639 | if (w == NULL) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 640 | if (size == 0) |
| 641 | return PyUnicode_FromStringAndSize(NULL, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 642 | PyErr_BadInternalCall(); |
| 643 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 646 | if (size == -1) { |
| 647 | size = wcslen(w); |
| 648 | } |
| 649 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 650 | unicode = _PyUnicode_New(size); |
| 651 | if (!unicode) |
| 652 | return NULL; |
| 653 | |
| 654 | /* Copy the wchar_t data into the new object */ |
Daniel Stutzbach | 8515eae | 2010-08-24 21:57:33 +0000 | [diff] [blame] | 655 | #if Py_UNICODE_SIZE == SIZEOF_WCHAR_T |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 656 | memcpy(unicode->str, w, size * sizeof(wchar_t)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 657 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 658 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 659 | register Py_UNICODE *u; |
| 660 | register Py_ssize_t i; |
| 661 | u = PyUnicode_AS_UNICODE(unicode); |
| 662 | for (i = size; i > 0; i--) |
| 663 | *u++ = *w++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 664 | } |
| 665 | #endif |
| 666 | |
| 667 | return (PyObject *)unicode; |
| 668 | } |
| 669 | |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 670 | #endif /* CONVERT_WCHAR_TO_SURROGATES */ |
| 671 | |
| 672 | #undef CONVERT_WCHAR_TO_SURROGATES |
| 673 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 674 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 675 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 676 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 677 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 678 | *fmt++ = '%'; |
| 679 | if (width) { |
| 680 | if (zeropad) |
| 681 | *fmt++ = '0'; |
| 682 | fmt += sprintf(fmt, "%d", width); |
| 683 | } |
| 684 | if (precision) |
| 685 | fmt += sprintf(fmt, ".%d", precision); |
| 686 | if (longflag) |
| 687 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 688 | else if (longlongflag) { |
| 689 | /* longlongflag should only ever be nonzero on machines with |
| 690 | HAVE_LONG_LONG defined */ |
| 691 | #ifdef HAVE_LONG_LONG |
| 692 | char *f = PY_FORMAT_LONG_LONG; |
| 693 | while (*f) |
| 694 | *fmt++ = *f++; |
| 695 | #else |
| 696 | /* we shouldn't ever get here */ |
| 697 | assert(0); |
| 698 | *fmt++ = 'l'; |
| 699 | #endif |
| 700 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 701 | else if (size_tflag) { |
| 702 | char *f = PY_FORMAT_SIZE_T; |
| 703 | while (*f) |
| 704 | *fmt++ = *f++; |
| 705 | } |
| 706 | *fmt++ = c; |
| 707 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 710 | #define appendstring(string) {for (copy = string;*copy;) *s++ = *copy++;} |
| 711 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 712 | /* size of fixed-size buffer for formatting single arguments */ |
| 713 | #define ITEM_BUFFER_LEN 21 |
| 714 | /* maximum number of characters required for output of %ld. 21 characters |
| 715 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 716 | #define MAX_LONG_CHARS 21 |
| 717 | /* maximum number of characters required for output of %lld. |
| 718 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 719 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 720 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 721 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 722 | PyObject * |
| 723 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 724 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 725 | va_list count; |
| 726 | Py_ssize_t callcount = 0; |
| 727 | PyObject **callresults = NULL; |
| 728 | PyObject **callresult = NULL; |
| 729 | Py_ssize_t n = 0; |
| 730 | int width = 0; |
| 731 | int precision = 0; |
| 732 | int zeropad; |
| 733 | const char* f; |
| 734 | Py_UNICODE *s; |
| 735 | PyObject *string; |
| 736 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 737 | char buffer[ITEM_BUFFER_LEN+1]; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 738 | /* use abuffer instead of buffer, if we need more space |
| 739 | * (which can happen if there's a format specifier with width). */ |
| 740 | char *abuffer = NULL; |
| 741 | char *realbuffer; |
| 742 | Py_ssize_t abuffersize = 0; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 743 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 744 | const char *copy; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 745 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 746 | Py_VA_COPY(count, vargs); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 747 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 748 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 749 | * PyUnicode_DecodeUTF8() for these objects once during step 3 and put the |
| 750 | * result in an array) */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 751 | for (f = format; *f; f++) { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 752 | if (*f == '%') { |
| 753 | if (*(f+1)=='%') |
| 754 | continue; |
Victor Stinner | 2b574a2 | 2011-03-01 22:48:49 +0000 | [diff] [blame] | 755 | if (*(f+1)=='S' || *(f+1)=='R' || *(f+1)=='A' || *(f+1) == 'V') |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 756 | ++callcount; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 757 | while (Py_ISDIGIT((unsigned)*f)) |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 758 | width = (width*10) + *f++ - '0'; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 759 | while (*++f && *f != '%' && !Py_ISALPHA((unsigned)*f)) |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 760 | ; |
| 761 | if (*f == 's') |
| 762 | ++callcount; |
| 763 | } |
Benjamin Peterson | 9be0b2e | 2010-09-12 03:40:54 +0000 | [diff] [blame] | 764 | else if (128 <= (unsigned char)*f) { |
| 765 | PyErr_Format(PyExc_ValueError, |
| 766 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
Victor Stinner | 4c7db31 | 2010-09-12 07:51:18 +0000 | [diff] [blame] | 767 | "string, got a non-ASCII byte: 0x%02x", |
Benjamin Peterson | 9be0b2e | 2010-09-12 03:40:54 +0000 | [diff] [blame] | 768 | (unsigned char)*f); |
Benjamin Peterson | d4ac96a | 2010-09-12 16:40:53 +0000 | [diff] [blame] | 769 | return NULL; |
Benjamin Peterson | 9be0b2e | 2010-09-12 03:40:54 +0000 | [diff] [blame] | 770 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 771 | } |
| 772 | /* step 2: allocate memory for the results of |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 773 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 774 | if (callcount) { |
| 775 | callresults = PyObject_Malloc(sizeof(PyObject *)*callcount); |
| 776 | if (!callresults) { |
| 777 | PyErr_NoMemory(); |
| 778 | return NULL; |
| 779 | } |
| 780 | callresult = callresults; |
| 781 | } |
| 782 | /* step 3: figure out how large a buffer we need */ |
| 783 | for (f = format; *f; f++) { |
| 784 | if (*f == '%') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 785 | #ifdef HAVE_LONG_LONG |
| 786 | int longlongflag = 0; |
| 787 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 788 | const char* p = f; |
| 789 | width = 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 790 | while (Py_ISDIGIT((unsigned)*f)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 791 | width = (width*10) + *f++ - '0'; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 792 | while (*++f && *f != '%' && !Py_ISALPHA((unsigned)*f)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 793 | ; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 794 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 795 | /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
| 796 | * they don't affect the amount of space we reserve. |
| 797 | */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 798 | if (*f == 'l') { |
| 799 | if (f[1] == 'd' || f[1] == 'u') { |
| 800 | ++f; |
| 801 | } |
| 802 | #ifdef HAVE_LONG_LONG |
| 803 | else if (f[1] == 'l' && |
| 804 | (f[2] == 'd' || f[2] == 'u')) { |
| 805 | longlongflag = 1; |
| 806 | f += 2; |
| 807 | } |
| 808 | #endif |
| 809 | } |
| 810 | else if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 811 | ++f; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 812 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 813 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 814 | switch (*f) { |
| 815 | case 'c': |
Victor Stinner | 659eb84 | 2011-02-23 12:14:22 +0000 | [diff] [blame] | 816 | { |
| 817 | #ifndef Py_UNICODE_WIDE |
| 818 | int ordinal = va_arg(count, int); |
| 819 | if (ordinal > 0xffff) |
| 820 | n += 2; |
| 821 | else |
| 822 | n++; |
| 823 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 824 | (void)va_arg(count, int); |
Victor Stinner | 659eb84 | 2011-02-23 12:14:22 +0000 | [diff] [blame] | 825 | n++; |
| 826 | #endif |
| 827 | break; |
| 828 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 829 | case '%': |
| 830 | n++; |
| 831 | break; |
| 832 | case 'd': case 'u': case 'i': case 'x': |
| 833 | (void) va_arg(count, int); |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 834 | #ifdef HAVE_LONG_LONG |
| 835 | if (longlongflag) { |
| 836 | if (width < MAX_LONG_LONG_CHARS) |
| 837 | width = MAX_LONG_LONG_CHARS; |
| 838 | } |
| 839 | else |
| 840 | #endif |
| 841 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 842 | including sign. Decimal takes the most space. This |
| 843 | isn't enough for octal. If a width is specified we |
| 844 | need more (which we allocate later). */ |
| 845 | if (width < MAX_LONG_CHARS) |
| 846 | width = MAX_LONG_CHARS; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 847 | n += width; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 848 | /* XXX should allow for large precision here too. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 849 | if (abuffersize < width) |
| 850 | abuffersize = width; |
| 851 | break; |
| 852 | case 's': |
| 853 | { |
| 854 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 855 | const char *s = va_arg(count, const char*); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 856 | PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); |
| 857 | if (!str) |
| 858 | goto fail; |
| 859 | n += PyUnicode_GET_SIZE(str); |
| 860 | /* Remember the str and switch to the next slot */ |
| 861 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 862 | break; |
| 863 | } |
| 864 | case 'U': |
| 865 | { |
| 866 | PyObject *obj = va_arg(count, PyObject *); |
| 867 | assert(obj && PyUnicode_Check(obj)); |
| 868 | n += PyUnicode_GET_SIZE(obj); |
| 869 | break; |
| 870 | } |
| 871 | case 'V': |
| 872 | { |
| 873 | PyObject *obj = va_arg(count, PyObject *); |
| 874 | const char *str = va_arg(count, const char *); |
Victor Stinner | 2b574a2 | 2011-03-01 22:48:49 +0000 | [diff] [blame] | 875 | PyObject *str_obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 876 | assert(obj || str); |
| 877 | assert(!obj || PyUnicode_Check(obj)); |
Victor Stinner | 2b574a2 | 2011-03-01 22:48:49 +0000 | [diff] [blame] | 878 | if (obj) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 879 | n += PyUnicode_GET_SIZE(obj); |
Victor Stinner | 2b574a2 | 2011-03-01 22:48:49 +0000 | [diff] [blame] | 880 | *callresult++ = NULL; |
| 881 | } |
| 882 | else { |
| 883 | str_obj = PyUnicode_DecodeUTF8(str, strlen(str), "replace"); |
| 884 | if (!str_obj) |
| 885 | goto fail; |
| 886 | n += PyUnicode_GET_SIZE(str_obj); |
| 887 | *callresult++ = str_obj; |
| 888 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 889 | break; |
| 890 | } |
| 891 | case 'S': |
| 892 | { |
| 893 | PyObject *obj = va_arg(count, PyObject *); |
| 894 | PyObject *str; |
| 895 | assert(obj); |
| 896 | str = PyObject_Str(obj); |
| 897 | if (!str) |
| 898 | goto fail; |
| 899 | n += PyUnicode_GET_SIZE(str); |
| 900 | /* Remember the str and switch to the next slot */ |
| 901 | *callresult++ = str; |
| 902 | break; |
| 903 | } |
| 904 | case 'R': |
| 905 | { |
| 906 | PyObject *obj = va_arg(count, PyObject *); |
| 907 | PyObject *repr; |
| 908 | assert(obj); |
| 909 | repr = PyObject_Repr(obj); |
| 910 | if (!repr) |
| 911 | goto fail; |
| 912 | n += PyUnicode_GET_SIZE(repr); |
| 913 | /* Remember the repr and switch to the next slot */ |
| 914 | *callresult++ = repr; |
| 915 | break; |
| 916 | } |
| 917 | case 'A': |
| 918 | { |
| 919 | PyObject *obj = va_arg(count, PyObject *); |
| 920 | PyObject *ascii; |
| 921 | assert(obj); |
| 922 | ascii = PyObject_ASCII(obj); |
| 923 | if (!ascii) |
| 924 | goto fail; |
| 925 | n += PyUnicode_GET_SIZE(ascii); |
| 926 | /* Remember the repr and switch to the next slot */ |
| 927 | *callresult++ = ascii; |
| 928 | break; |
| 929 | } |
| 930 | case 'p': |
| 931 | (void) va_arg(count, int); |
| 932 | /* maximum 64-bit pointer representation: |
| 933 | * 0xffffffffffffffff |
| 934 | * so 19 characters is enough. |
| 935 | * XXX I count 18 -- what's the extra for? |
| 936 | */ |
| 937 | n += 19; |
| 938 | break; |
| 939 | default: |
| 940 | /* if we stumble upon an unknown |
| 941 | formatting code, copy the rest of |
| 942 | the format string to the output |
| 943 | string. (we cannot just skip the |
| 944 | code, since there's no way to know |
| 945 | what's in the argument list) */ |
| 946 | n += strlen(p); |
| 947 | goto expand; |
| 948 | } |
| 949 | } else |
| 950 | n++; |
| 951 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 952 | expand: |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 953 | if (abuffersize > ITEM_BUFFER_LEN) { |
| 954 | /* add 1 for sprintf's trailing null byte */ |
| 955 | abuffer = PyObject_Malloc(abuffersize + 1); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 956 | if (!abuffer) { |
| 957 | PyErr_NoMemory(); |
| 958 | goto fail; |
| 959 | } |
| 960 | realbuffer = abuffer; |
| 961 | } |
| 962 | else |
| 963 | realbuffer = buffer; |
| 964 | /* step 4: fill the buffer */ |
| 965 | /* Since we've analyzed how much space we need for the worst case, |
| 966 | we don't have to resize the string. |
| 967 | There can be no errors beyond this point. */ |
| 968 | string = PyUnicode_FromUnicode(NULL, n); |
| 969 | if (!string) |
| 970 | goto fail; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 971 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 972 | s = PyUnicode_AS_UNICODE(string); |
| 973 | callresult = callresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 974 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 975 | for (f = format; *f; f++) { |
| 976 | if (*f == '%') { |
| 977 | const char* p = f++; |
| 978 | int longflag = 0; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 979 | int longlongflag = 0; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 980 | int size_tflag = 0; |
| 981 | zeropad = (*f == '0'); |
| 982 | /* parse the width.precision part */ |
| 983 | width = 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 984 | while (Py_ISDIGIT((unsigned)*f)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 985 | width = (width*10) + *f++ - '0'; |
| 986 | precision = 0; |
| 987 | if (*f == '.') { |
| 988 | f++; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 989 | while (Py_ISDIGIT((unsigned)*f)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 990 | precision = (precision*10) + *f++ - '0'; |
| 991 | } |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 992 | /* Handle %ld, %lu, %lld and %llu. */ |
| 993 | if (*f == 'l') { |
| 994 | if (f[1] == 'd' || f[1] == 'u') { |
| 995 | longflag = 1; |
| 996 | ++f; |
| 997 | } |
| 998 | #ifdef HAVE_LONG_LONG |
| 999 | else if (f[1] == 'l' && |
| 1000 | (f[2] == 'd' || f[2] == 'u')) { |
| 1001 | longlongflag = 1; |
| 1002 | f += 2; |
| 1003 | } |
| 1004 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1005 | } |
| 1006 | /* handle the size_t flag. */ |
| 1007 | if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
| 1008 | size_tflag = 1; |
| 1009 | ++f; |
| 1010 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1011 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1012 | switch (*f) { |
| 1013 | case 'c': |
Victor Stinner | 659eb84 | 2011-02-23 12:14:22 +0000 | [diff] [blame] | 1014 | { |
| 1015 | int ordinal = va_arg(vargs, int); |
| 1016 | #ifndef Py_UNICODE_WIDE |
| 1017 | if (ordinal > 0xffff) { |
| 1018 | ordinal -= 0x10000; |
| 1019 | *s++ = 0xD800 | (ordinal >> 10); |
| 1020 | *s++ = 0xDC00 | (ordinal & 0x3FF); |
| 1021 | } else |
| 1022 | #endif |
| 1023 | *s++ = ordinal; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1024 | break; |
Victor Stinner | 659eb84 | 2011-02-23 12:14:22 +0000 | [diff] [blame] | 1025 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1026 | case 'd': |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1027 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 1028 | width, precision, 'd'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1029 | if (longflag) |
| 1030 | sprintf(realbuffer, fmt, va_arg(vargs, long)); |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1031 | #ifdef HAVE_LONG_LONG |
| 1032 | else if (longlongflag) |
| 1033 | sprintf(realbuffer, fmt, va_arg(vargs, PY_LONG_LONG)); |
| 1034 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1035 | else if (size_tflag) |
| 1036 | sprintf(realbuffer, fmt, va_arg(vargs, Py_ssize_t)); |
| 1037 | else |
| 1038 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 1039 | appendstring(realbuffer); |
| 1040 | break; |
| 1041 | case 'u': |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1042 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 1043 | width, precision, 'u'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1044 | if (longflag) |
| 1045 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned long)); |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1046 | #ifdef HAVE_LONG_LONG |
| 1047 | else if (longlongflag) |
| 1048 | sprintf(realbuffer, fmt, va_arg(vargs, |
| 1049 | unsigned PY_LONG_LONG)); |
| 1050 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1051 | else if (size_tflag) |
| 1052 | sprintf(realbuffer, fmt, va_arg(vargs, size_t)); |
| 1053 | else |
| 1054 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned int)); |
| 1055 | appendstring(realbuffer); |
| 1056 | break; |
| 1057 | case 'i': |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1058 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'i'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1059 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 1060 | appendstring(realbuffer); |
| 1061 | break; |
| 1062 | case 'x': |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1063 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1064 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 1065 | appendstring(realbuffer); |
| 1066 | break; |
| 1067 | case 's': |
| 1068 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1069 | /* unused, since we already have the result */ |
| 1070 | (void) va_arg(vargs, char *); |
| 1071 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(*callresult), |
| 1072 | PyUnicode_GET_SIZE(*callresult)); |
| 1073 | s += PyUnicode_GET_SIZE(*callresult); |
| 1074 | /* We're done with the unicode()/repr() => forget it */ |
| 1075 | Py_DECREF(*callresult); |
| 1076 | /* switch to next unicode()/repr() result */ |
| 1077 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1078 | break; |
| 1079 | } |
| 1080 | case 'U': |
| 1081 | { |
| 1082 | PyObject *obj = va_arg(vargs, PyObject *); |
| 1083 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 1084 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 1085 | s += size; |
| 1086 | break; |
| 1087 | } |
| 1088 | case 'V': |
| 1089 | { |
| 1090 | PyObject *obj = va_arg(vargs, PyObject *); |
Victor Stinner | 2b574a2 | 2011-03-01 22:48:49 +0000 | [diff] [blame] | 1091 | va_arg(vargs, const char *); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1092 | if (obj) { |
| 1093 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 1094 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 1095 | s += size; |
| 1096 | } else { |
Victor Stinner | 2b574a2 | 2011-03-01 22:48:49 +0000 | [diff] [blame] | 1097 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(*callresult), |
| 1098 | PyUnicode_GET_SIZE(*callresult)); |
| 1099 | s += PyUnicode_GET_SIZE(*callresult); |
| 1100 | Py_DECREF(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1101 | } |
Victor Stinner | 2b574a2 | 2011-03-01 22:48:49 +0000 | [diff] [blame] | 1102 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1103 | break; |
| 1104 | } |
| 1105 | case 'S': |
| 1106 | case 'R': |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 1107 | case 'A': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1108 | { |
| 1109 | Py_UNICODE *ucopy; |
| 1110 | Py_ssize_t usize; |
| 1111 | Py_ssize_t upos; |
| 1112 | /* unused, since we already have the result */ |
| 1113 | (void) va_arg(vargs, PyObject *); |
| 1114 | ucopy = PyUnicode_AS_UNICODE(*callresult); |
| 1115 | usize = PyUnicode_GET_SIZE(*callresult); |
| 1116 | for (upos = 0; upos<usize;) |
| 1117 | *s++ = ucopy[upos++]; |
| 1118 | /* We're done with the unicode()/repr() => forget it */ |
| 1119 | Py_DECREF(*callresult); |
| 1120 | /* switch to next unicode()/repr() result */ |
| 1121 | ++callresult; |
| 1122 | break; |
| 1123 | } |
| 1124 | case 'p': |
| 1125 | sprintf(buffer, "%p", va_arg(vargs, void*)); |
| 1126 | /* %p is ill-defined: ensure leading 0x. */ |
| 1127 | if (buffer[1] == 'X') |
| 1128 | buffer[1] = 'x'; |
| 1129 | else if (buffer[1] != 'x') { |
| 1130 | memmove(buffer+2, buffer, strlen(buffer)+1); |
| 1131 | buffer[0] = '0'; |
| 1132 | buffer[1] = 'x'; |
| 1133 | } |
| 1134 | appendstring(buffer); |
| 1135 | break; |
| 1136 | case '%': |
| 1137 | *s++ = '%'; |
| 1138 | break; |
| 1139 | default: |
| 1140 | appendstring(p); |
| 1141 | goto end; |
| 1142 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 1143 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 1144 | else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1145 | *s++ = *f; |
| 1146 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1147 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1148 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1149 | if (callresults) |
| 1150 | PyObject_Free(callresults); |
| 1151 | if (abuffer) |
| 1152 | PyObject_Free(abuffer); |
| 1153 | PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string)); |
| 1154 | return string; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1155 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1156 | if (callresults) { |
| 1157 | PyObject **callresult2 = callresults; |
| 1158 | while (callresult2 < callresult) { |
Victor Stinner | 2b574a2 | 2011-03-01 22:48:49 +0000 | [diff] [blame] | 1159 | Py_XDECREF(*callresult2); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1160 | ++callresult2; |
| 1161 | } |
| 1162 | PyObject_Free(callresults); |
| 1163 | } |
| 1164 | if (abuffer) |
| 1165 | PyObject_Free(abuffer); |
| 1166 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | #undef appendstring |
| 1170 | |
| 1171 | PyObject * |
| 1172 | PyUnicode_FromFormat(const char *format, ...) |
| 1173 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1174 | PyObject* ret; |
| 1175 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1176 | |
| 1177 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1178 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1179 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1180 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1181 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1182 | ret = PyUnicode_FromFormatV(format, vargs); |
| 1183 | va_end(vargs); |
| 1184 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1187 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 1188 | convert a Unicode object to a wide character string. |
| 1189 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame^] | 1190 | - 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] | 1191 | character) required to convert the unicode object. Ignore size argument. |
| 1192 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame^] | 1193 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1194 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame^] | 1195 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1196 | static Py_ssize_t |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1197 | unicode_aswidechar(PyUnicodeObject *unicode, |
| 1198 | wchar_t *w, |
| 1199 | Py_ssize_t size) |
| 1200 | { |
| 1201 | #if Py_UNICODE_SIZE == SIZEOF_WCHAR_T |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1202 | Py_ssize_t res; |
| 1203 | if (w != NULL) { |
| 1204 | res = PyUnicode_GET_SIZE(unicode); |
| 1205 | if (size > res) |
| 1206 | size = res + 1; |
| 1207 | else |
| 1208 | res = size; |
| 1209 | memcpy(w, unicode->str, size * sizeof(wchar_t)); |
| 1210 | return res; |
| 1211 | } |
| 1212 | else |
| 1213 | return PyUnicode_GET_SIZE(unicode) + 1; |
| 1214 | #elif Py_UNICODE_SIZE == 2 && SIZEOF_WCHAR_T == 4 |
| 1215 | register const Py_UNICODE *u; |
| 1216 | const Py_UNICODE *uend; |
| 1217 | const wchar_t *worig, *wend; |
| 1218 | Py_ssize_t nchar; |
| 1219 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1220 | u = PyUnicode_AS_UNICODE(unicode); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1221 | uend = u + PyUnicode_GET_SIZE(unicode); |
| 1222 | if (w != NULL) { |
| 1223 | worig = w; |
| 1224 | wend = w + size; |
| 1225 | while (u != uend && w != wend) { |
| 1226 | if (0xD800 <= u[0] && u[0] <= 0xDBFF |
| 1227 | && 0xDC00 <= u[1] && u[1] <= 0xDFFF) |
| 1228 | { |
| 1229 | *w = (((u[0] & 0x3FF) << 10) | (u[1] & 0x3FF)) + 0x10000; |
| 1230 | u += 2; |
| 1231 | } |
| 1232 | else { |
| 1233 | *w = *u; |
| 1234 | u++; |
| 1235 | } |
| 1236 | w++; |
| 1237 | } |
| 1238 | if (w != wend) |
| 1239 | *w = L'\0'; |
| 1240 | return w - worig; |
| 1241 | } |
| 1242 | else { |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame^] | 1243 | nchar = 1; /* null character at the end */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1244 | while (u != uend) { |
| 1245 | if (0xD800 <= u[0] && u[0] <= 0xDBFF |
| 1246 | && 0xDC00 <= u[1] && u[1] <= 0xDFFF) |
| 1247 | u += 2; |
| 1248 | else |
| 1249 | u++; |
| 1250 | nchar++; |
| 1251 | } |
| 1252 | } |
| 1253 | return nchar; |
| 1254 | #elif Py_UNICODE_SIZE == 4 && SIZEOF_WCHAR_T == 2 |
| 1255 | register Py_UNICODE *u, *uend, ordinal; |
| 1256 | register Py_ssize_t i; |
| 1257 | wchar_t *worig, *wend; |
| 1258 | Py_ssize_t nchar; |
| 1259 | |
| 1260 | u = PyUnicode_AS_UNICODE(unicode); |
| 1261 | uend = u + PyUnicode_GET_SIZE(u); |
| 1262 | if (w != NULL) { |
| 1263 | worig = w; |
| 1264 | wend = w + size; |
| 1265 | while (u != uend && w != wend) { |
| 1266 | ordinal = *u; |
| 1267 | if (ordinal > 0xffff) { |
| 1268 | ordinal -= 0x10000; |
| 1269 | *w++ = 0xD800 | (ordinal >> 10); |
| 1270 | *w++ = 0xDC00 | (ordinal & 0x3FF); |
| 1271 | } |
| 1272 | else |
| 1273 | *w++ = ordinal; |
| 1274 | u++; |
| 1275 | } |
| 1276 | if (w != wend) |
| 1277 | *w = 0; |
| 1278 | return w - worig; |
| 1279 | } |
| 1280 | else { |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame^] | 1281 | nchar = 1; /* null character */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1282 | while (u != uend) { |
| 1283 | if (*u > 0xffff) |
| 1284 | nchar += 2; |
| 1285 | else |
| 1286 | nchar++; |
| 1287 | u++; |
| 1288 | } |
| 1289 | return nchar; |
| 1290 | } |
| 1291 | #else |
| 1292 | # error "unsupported wchar_t and Py_UNICODE sizes, see issue #8670" |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1293 | #endif |
| 1294 | } |
| 1295 | |
| 1296 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 1297 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1298 | wchar_t *w, |
| 1299 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1300 | { |
| 1301 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1302 | PyErr_BadInternalCall(); |
| 1303 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1304 | } |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 1305 | return unicode_aswidechar((PyUnicodeObject*)unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1306 | } |
| 1307 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1308 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 1309 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1310 | Py_ssize_t *size) |
| 1311 | { |
| 1312 | wchar_t* buffer; |
| 1313 | Py_ssize_t buflen; |
| 1314 | |
| 1315 | if (unicode == NULL) { |
| 1316 | PyErr_BadInternalCall(); |
| 1317 | return NULL; |
| 1318 | } |
| 1319 | |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 1320 | buflen = unicode_aswidechar((PyUnicodeObject *)unicode, NULL, 0); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1321 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1322 | PyErr_NoMemory(); |
| 1323 | return NULL; |
| 1324 | } |
| 1325 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1326 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 1327 | if (buffer == NULL) { |
| 1328 | PyErr_NoMemory(); |
| 1329 | return NULL; |
| 1330 | } |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 1331 | buflen = unicode_aswidechar((PyUnicodeObject *)unicode, buffer, buflen); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1332 | if (size != NULL) |
| 1333 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1334 | return buffer; |
| 1335 | } |
| 1336 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1337 | #endif |
| 1338 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1339 | PyObject *PyUnicode_FromOrdinal(int ordinal) |
| 1340 | { |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1341 | Py_UNICODE s[2]; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1342 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1343 | if (ordinal < 0 || ordinal > 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1344 | PyErr_SetString(PyExc_ValueError, |
| 1345 | "chr() arg not in range(0x110000)"); |
| 1346 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1347 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1348 | |
| 1349 | #ifndef Py_UNICODE_WIDE |
| 1350 | if (ordinal > 0xffff) { |
| 1351 | ordinal -= 0x10000; |
| 1352 | s[0] = 0xD800 | (ordinal >> 10); |
| 1353 | s[1] = 0xDC00 | (ordinal & 0x3FF); |
| 1354 | return PyUnicode_FromUnicode(s, 2); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1355 | } |
| 1356 | #endif |
| 1357 | |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 1358 | s[0] = (Py_UNICODE)ordinal; |
| 1359 | return PyUnicode_FromUnicode(s, 1); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1360 | } |
| 1361 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1362 | PyObject *PyUnicode_FromObject(register PyObject *obj) |
| 1363 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1364 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1365 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1366 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1367 | Py_INCREF(obj); |
| 1368 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1369 | } |
| 1370 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1371 | /* For a Unicode subtype that's not a Unicode object, |
| 1372 | return a true Unicode object with the same data. */ |
| 1373 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 1374 | PyUnicode_GET_SIZE(obj)); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1375 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1376 | PyErr_Format(PyExc_TypeError, |
| 1377 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1378 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1379 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1380 | } |
| 1381 | |
| 1382 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1383 | const char *encoding, |
| 1384 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1385 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 1386 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1387 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1388 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1389 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1390 | PyErr_BadInternalCall(); |
| 1391 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1392 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1393 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 1394 | /* Decoding bytes objects is the most common case and should be fast */ |
| 1395 | if (PyBytes_Check(obj)) { |
| 1396 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 1397 | Py_INCREF(unicode_empty); |
| 1398 | v = (PyObject *) unicode_empty; |
| 1399 | } |
| 1400 | else { |
| 1401 | v = PyUnicode_Decode( |
| 1402 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 1403 | encoding, errors); |
| 1404 | } |
| 1405 | return v; |
| 1406 | } |
| 1407 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1408 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1409 | PyErr_SetString(PyExc_TypeError, |
| 1410 | "decoding str is not supported"); |
| 1411 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1412 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1413 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 1414 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 1415 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 1416 | PyErr_Format(PyExc_TypeError, |
| 1417 | "coercing to str: need bytes, bytearray " |
| 1418 | "or buffer-like object, %.80s found", |
| 1419 | Py_TYPE(obj)->tp_name); |
| 1420 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1421 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1422 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 1423 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1424 | Py_INCREF(unicode_empty); |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 1425 | v = (PyObject *) unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1426 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1427 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 1428 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 1429 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 1430 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1431 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1432 | } |
| 1433 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 1434 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1435 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 1436 | 1 on success. */ |
| 1437 | static int |
| 1438 | normalize_encoding(const char *encoding, |
| 1439 | char *lower, |
| 1440 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1441 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1442 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 1443 | char *l; |
| 1444 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1445 | |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1446 | e = encoding; |
| 1447 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 1448 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1449 | while (*e) { |
| 1450 | if (l == l_end) |
| 1451 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 1452 | if (Py_ISUPPER(*e)) { |
| 1453 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1454 | } |
| 1455 | else if (*e == '_') { |
| 1456 | *l++ = '-'; |
| 1457 | e++; |
| 1458 | } |
| 1459 | else { |
| 1460 | *l++ = *e++; |
| 1461 | } |
| 1462 | } |
| 1463 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1464 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
| 1467 | PyObject *PyUnicode_Decode(const char *s, |
| 1468 | Py_ssize_t size, |
| 1469 | const char *encoding, |
| 1470 | const char *errors) |
| 1471 | { |
| 1472 | PyObject *buffer = NULL, *unicode; |
| 1473 | Py_buffer info; |
| 1474 | char lower[11]; /* Enough for any encoding shortcut */ |
| 1475 | |
| 1476 | if (encoding == NULL) |
| 1477 | encoding = PyUnicode_GetDefaultEncoding(); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1478 | |
| 1479 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1480 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
| 1481 | if (strcmp(lower, "utf-8") == 0) |
| 1482 | return PyUnicode_DecodeUTF8(s, size, errors); |
| 1483 | else if ((strcmp(lower, "latin-1") == 0) || |
| 1484 | (strcmp(lower, "iso-8859-1") == 0)) |
| 1485 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1486 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1487 | else if (strcmp(lower, "mbcs") == 0) |
| 1488 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1489 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1490 | else if (strcmp(lower, "ascii") == 0) |
| 1491 | return PyUnicode_DecodeASCII(s, size, errors); |
| 1492 | else if (strcmp(lower, "utf-16") == 0) |
| 1493 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 1494 | else if (strcmp(lower, "utf-32") == 0) |
| 1495 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 1496 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1497 | |
| 1498 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1499 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 1500 | 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] | 1501 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 1502 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1503 | if (buffer == NULL) |
| 1504 | goto onError; |
| 1505 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 1506 | if (unicode == NULL) |
| 1507 | goto onError; |
| 1508 | if (!PyUnicode_Check(unicode)) { |
| 1509 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1510 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1511 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1512 | Py_DECREF(unicode); |
| 1513 | goto onError; |
| 1514 | } |
| 1515 | Py_DECREF(buffer); |
| 1516 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1517 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1518 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1519 | Py_XDECREF(buffer); |
| 1520 | return NULL; |
| 1521 | } |
| 1522 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1523 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 1524 | const char *encoding, |
| 1525 | const char *errors) |
| 1526 | { |
| 1527 | PyObject *v; |
| 1528 | |
| 1529 | if (!PyUnicode_Check(unicode)) { |
| 1530 | PyErr_BadArgument(); |
| 1531 | goto onError; |
| 1532 | } |
| 1533 | |
| 1534 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1535 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1536 | |
| 1537 | /* Decode via the codec registry */ |
| 1538 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1539 | if (v == NULL) |
| 1540 | goto onError; |
| 1541 | return v; |
| 1542 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1543 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1544 | return NULL; |
| 1545 | } |
| 1546 | |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1547 | PyObject *PyUnicode_AsDecodedUnicode(PyObject *unicode, |
| 1548 | const char *encoding, |
| 1549 | const char *errors) |
| 1550 | { |
| 1551 | PyObject *v; |
| 1552 | |
| 1553 | if (!PyUnicode_Check(unicode)) { |
| 1554 | PyErr_BadArgument(); |
| 1555 | goto onError; |
| 1556 | } |
| 1557 | |
| 1558 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1559 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1560 | |
| 1561 | /* Decode via the codec registry */ |
| 1562 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1563 | if (v == NULL) |
| 1564 | goto onError; |
| 1565 | if (!PyUnicode_Check(v)) { |
| 1566 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1567 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1568 | Py_TYPE(v)->tp_name); |
| 1569 | Py_DECREF(v); |
| 1570 | goto onError; |
| 1571 | } |
| 1572 | return v; |
| 1573 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1574 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1575 | return NULL; |
| 1576 | } |
| 1577 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1578 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1579 | Py_ssize_t size, |
| 1580 | const char *encoding, |
| 1581 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1582 | { |
| 1583 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1584 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1585 | unicode = PyUnicode_FromUnicode(s, size); |
| 1586 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1587 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1588 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 1589 | Py_DECREF(unicode); |
| 1590 | return v; |
| 1591 | } |
| 1592 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1593 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 1594 | const char *encoding, |
| 1595 | const char *errors) |
| 1596 | { |
| 1597 | PyObject *v; |
| 1598 | |
| 1599 | if (!PyUnicode_Check(unicode)) { |
| 1600 | PyErr_BadArgument(); |
| 1601 | goto onError; |
| 1602 | } |
| 1603 | |
| 1604 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1605 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1606 | |
| 1607 | /* Encode via the codec registry */ |
| 1608 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1609 | if (v == NULL) |
| 1610 | goto onError; |
| 1611 | return v; |
| 1612 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1613 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1614 | return NULL; |
| 1615 | } |
| 1616 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 1617 | PyObject * |
| 1618 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 1619 | { |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 1620 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 1621 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 1622 | PyUnicode_GET_SIZE(unicode), |
| 1623 | NULL); |
| 1624 | #elif defined(__APPLE__) |
| 1625 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 1626 | PyUnicode_GET_SIZE(unicode), |
| 1627 | "surrogateescape"); |
| 1628 | #else |
Victor Stinner | 3cbf14b | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 1629 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 1630 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 1631 | cannot use it to encode and decode filenames before it is loaded. Load |
| 1632 | the Python codec requires to encode at least its own filename. Use the C |
| 1633 | version of the locale codec until the codec registry is initialized and |
| 1634 | the Python codec is loaded. |
| 1635 | |
| 1636 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 1637 | cannot only rely on it: check also interp->fscodec_initialized for |
| 1638 | subinterpreters. */ |
| 1639 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 1640 | return PyUnicode_AsEncodedString(unicode, |
| 1641 | Py_FileSystemDefaultEncoding, |
| 1642 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 1643 | } |
| 1644 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 1645 | /* locale encoding with surrogateescape */ |
| 1646 | wchar_t *wchar; |
| 1647 | char *bytes; |
| 1648 | PyObject *bytes_obj; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 1649 | size_t error_pos; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 1650 | |
| 1651 | wchar = PyUnicode_AsWideCharString(unicode, NULL); |
| 1652 | if (wchar == NULL) |
| 1653 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 1654 | bytes = _Py_wchar2char(wchar, &error_pos); |
| 1655 | if (bytes == NULL) { |
| 1656 | if (error_pos != (size_t)-1) { |
| 1657 | char *errmsg = strerror(errno); |
| 1658 | PyObject *exc = NULL; |
| 1659 | if (errmsg == NULL) |
| 1660 | errmsg = "Py_wchar2char() failed"; |
| 1661 | raise_encode_exception(&exc, |
| 1662 | "filesystemencoding", |
| 1663 | PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), |
| 1664 | error_pos, error_pos+1, |
| 1665 | errmsg); |
| 1666 | Py_XDECREF(exc); |
| 1667 | } |
| 1668 | else |
| 1669 | PyErr_NoMemory(); |
| 1670 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 1671 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 1672 | } |
| 1673 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 1674 | |
| 1675 | bytes_obj = PyBytes_FromString(bytes); |
| 1676 | PyMem_Free(bytes); |
| 1677 | return bytes_obj; |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 1678 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 1679 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 1680 | } |
| 1681 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1682 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 1683 | const char *encoding, |
| 1684 | const char *errors) |
| 1685 | { |
| 1686 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 1687 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1688 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1689 | if (!PyUnicode_Check(unicode)) { |
| 1690 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1691 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1692 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1693 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1694 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1695 | encoding = PyUnicode_GetDefaultEncoding(); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1696 | |
| 1697 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1698 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
| 1699 | if (strcmp(lower, "utf-8") == 0) |
| 1700 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 1701 | PyUnicode_GET_SIZE(unicode), |
| 1702 | errors); |
| 1703 | else if ((strcmp(lower, "latin-1") == 0) || |
| 1704 | (strcmp(lower, "iso-8859-1") == 0)) |
| 1705 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 1706 | PyUnicode_GET_SIZE(unicode), |
| 1707 | errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1708 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1709 | else if (strcmp(lower, "mbcs") == 0) |
| 1710 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 1711 | PyUnicode_GET_SIZE(unicode), |
| 1712 | errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1713 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1714 | else if (strcmp(lower, "ascii") == 0) |
| 1715 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 1716 | PyUnicode_GET_SIZE(unicode), |
| 1717 | errors); |
| 1718 | } |
Victor Stinner | 59e62db | 2010-05-15 13:14:32 +0000 | [diff] [blame] | 1719 | /* During bootstrap, we may need to find the encodings |
| 1720 | package, to load the file system encoding, and require the |
| 1721 | file system encoding in order to load the encodings |
| 1722 | package. |
Christian Heimes | 6a27efa | 2008-10-30 21:48:26 +0000 | [diff] [blame] | 1723 | |
Victor Stinner | 59e62db | 2010-05-15 13:14:32 +0000 | [diff] [blame] | 1724 | Break out of this dependency by assuming that the path to |
| 1725 | the encodings module is ASCII-only. XXX could try wcstombs |
| 1726 | instead, if the file system encoding is the locale's |
| 1727 | encoding. */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1728 | if (Py_FileSystemDefaultEncoding && |
Victor Stinner | 59e62db | 2010-05-15 13:14:32 +0000 | [diff] [blame] | 1729 | strcmp(encoding, Py_FileSystemDefaultEncoding) == 0 && |
| 1730 | !PyThreadState_GET()->interp->codecs_initialized) |
| 1731 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 1732 | PyUnicode_GET_SIZE(unicode), |
| 1733 | errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1734 | |
| 1735 | /* Encode via the codec registry */ |
| 1736 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1737 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1738 | return NULL; |
| 1739 | |
| 1740 | /* The normal path */ |
| 1741 | if (PyBytes_Check(v)) |
| 1742 | return v; |
| 1743 | |
| 1744 | /* 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] | 1745 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 1746 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1747 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 1748 | |
| 1749 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 1750 | "encoder %s returned bytearray instead of bytes", |
| 1751 | encoding); |
| 1752 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1753 | Py_DECREF(v); |
| 1754 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1755 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1756 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1757 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 1758 | Py_DECREF(v); |
| 1759 | return b; |
| 1760 | } |
| 1761 | |
| 1762 | PyErr_Format(PyExc_TypeError, |
| 1763 | "encoder did not return a bytes object (type=%.400s)", |
| 1764 | Py_TYPE(v)->tp_name); |
| 1765 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1766 | return NULL; |
| 1767 | } |
| 1768 | |
| 1769 | PyObject *PyUnicode_AsEncodedUnicode(PyObject *unicode, |
| 1770 | const char *encoding, |
| 1771 | const char *errors) |
| 1772 | { |
| 1773 | PyObject *v; |
| 1774 | |
| 1775 | if (!PyUnicode_Check(unicode)) { |
| 1776 | PyErr_BadArgument(); |
| 1777 | goto onError; |
| 1778 | } |
| 1779 | |
| 1780 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1781 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1782 | |
| 1783 | /* Encode via the codec registry */ |
| 1784 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1785 | if (v == NULL) |
| 1786 | goto onError; |
| 1787 | if (!PyUnicode_Check(v)) { |
| 1788 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1789 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1790 | Py_TYPE(v)->tp_name); |
| 1791 | Py_DECREF(v); |
| 1792 | goto onError; |
| 1793 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1794 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1795 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1796 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1797 | return NULL; |
| 1798 | } |
| 1799 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1800 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1801 | const char *errors) |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1802 | { |
| 1803 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1804 | if (v) |
| 1805 | return v; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1806 | if (errors != NULL) |
| 1807 | Py_FatalError("non-NULL encoding in _PyUnicode_AsDefaultEncodedString"); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1808 | v = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Guido van Rossum | 0661009 | 2007-08-16 21:02:22 +0000 | [diff] [blame] | 1809 | PyUnicode_GET_SIZE(unicode), |
| 1810 | NULL); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1811 | if (!v) |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1812 | return NULL; |
Guido van Rossum | e7a0d39 | 2007-07-12 07:53:00 +0000 | [diff] [blame] | 1813 | ((PyUnicodeObject *)unicode)->defenc = v; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1814 | return v; |
| 1815 | } |
| 1816 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1817 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1818 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1819 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1820 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 1821 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1822 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1823 | PyObject* |
| 1824 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 1825 | { |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 1826 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 1827 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 1828 | #elif defined(__APPLE__) |
| 1829 | return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); |
| 1830 | #else |
Victor Stinner | 3cbf14b | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 1831 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 1832 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 1833 | cannot use it to encode and decode filenames before it is loaded. Load |
| 1834 | the Python codec requires to encode at least its own filename. Use the C |
| 1835 | version of the locale codec until the codec registry is initialized and |
| 1836 | the Python codec is loaded. |
| 1837 | |
| 1838 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 1839 | cannot only rely on it: check also interp->fscodec_initialized for |
| 1840 | subinterpreters. */ |
| 1841 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1842 | return PyUnicode_Decode(s, size, |
| 1843 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 1844 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1845 | } |
| 1846 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 1847 | /* locale encoding with surrogateescape */ |
| 1848 | wchar_t *wchar; |
| 1849 | PyObject *unicode; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 1850 | size_t len; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 1851 | |
| 1852 | if (s[size] != '\0' || size != strlen(s)) { |
| 1853 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 1854 | return NULL; |
| 1855 | } |
| 1856 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 1857 | wchar = _Py_char2wchar(s, &len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 1858 | if (wchar == NULL) |
Victor Stinner | d5af0a5 | 2010-11-08 23:34:29 +0000 | [diff] [blame] | 1859 | return PyErr_NoMemory(); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 1860 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 1861 | unicode = PyUnicode_FromWideChar(wchar, len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 1862 | PyMem_Free(wchar); |
| 1863 | return unicode; |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1864 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 1865 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1866 | } |
| 1867 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1868 | |
| 1869 | int |
| 1870 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 1871 | { |
| 1872 | PyObject *output = NULL; |
| 1873 | Py_ssize_t size; |
| 1874 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 1875 | if (arg == NULL) { |
| 1876 | Py_DECREF(*(PyObject**)addr); |
| 1877 | return 1; |
| 1878 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 1879 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1880 | output = arg; |
| 1881 | Py_INCREF(output); |
| 1882 | } |
| 1883 | else { |
| 1884 | arg = PyUnicode_FromObject(arg); |
| 1885 | if (!arg) |
| 1886 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 1887 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1888 | Py_DECREF(arg); |
| 1889 | if (!output) |
| 1890 | return 0; |
| 1891 | if (!PyBytes_Check(output)) { |
| 1892 | Py_DECREF(output); |
| 1893 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 1894 | return 0; |
| 1895 | } |
| 1896 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 1897 | size = PyBytes_GET_SIZE(output); |
| 1898 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1899 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 1900 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1901 | Py_DECREF(output); |
| 1902 | return 0; |
| 1903 | } |
| 1904 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 1905 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1906 | } |
| 1907 | |
| 1908 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 1909 | int |
| 1910 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 1911 | { |
| 1912 | PyObject *output = NULL; |
| 1913 | Py_ssize_t size; |
| 1914 | void *data; |
| 1915 | if (arg == NULL) { |
| 1916 | Py_DECREF(*(PyObject**)addr); |
| 1917 | return 1; |
| 1918 | } |
| 1919 | if (PyUnicode_Check(arg)) { |
| 1920 | output = arg; |
| 1921 | Py_INCREF(output); |
| 1922 | } |
| 1923 | else { |
| 1924 | arg = PyBytes_FromObject(arg); |
| 1925 | if (!arg) |
| 1926 | return 0; |
| 1927 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 1928 | PyBytes_GET_SIZE(arg)); |
| 1929 | Py_DECREF(arg); |
| 1930 | if (!output) |
| 1931 | return 0; |
| 1932 | if (!PyUnicode_Check(output)) { |
| 1933 | Py_DECREF(output); |
| 1934 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 1935 | return 0; |
| 1936 | } |
| 1937 | } |
| 1938 | size = PyUnicode_GET_SIZE(output); |
| 1939 | data = PyUnicode_AS_UNICODE(output); |
| 1940 | if (size != Py_UNICODE_strlen(data)) { |
| 1941 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 1942 | Py_DECREF(output); |
| 1943 | return 0; |
| 1944 | } |
| 1945 | *(PyObject**)addr = output; |
| 1946 | return Py_CLEANUP_SUPPORTED; |
| 1947 | } |
| 1948 | |
| 1949 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1950 | char* |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1951 | _PyUnicode_AsStringAndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1952 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1953 | PyObject *bytes; |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 1954 | if (!PyUnicode_Check(unicode)) { |
| 1955 | PyErr_BadArgument(); |
| 1956 | return NULL; |
| 1957 | } |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1958 | bytes = _PyUnicode_AsDefaultEncodedString(unicode, NULL); |
| 1959 | if (bytes == NULL) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1960 | return NULL; |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1961 | if (psize != NULL) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1962 | *psize = PyBytes_GET_SIZE(bytes); |
| 1963 | return PyBytes_AS_STRING(bytes); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1964 | } |
| 1965 | |
| 1966 | char* |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1967 | _PyUnicode_AsString(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1968 | { |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1969 | return _PyUnicode_AsStringAndSize(unicode, NULL); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1970 | } |
| 1971 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1972 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 1973 | { |
| 1974 | if (!PyUnicode_Check(unicode)) { |
| 1975 | PyErr_BadArgument(); |
| 1976 | goto onError; |
| 1977 | } |
| 1978 | return PyUnicode_AS_UNICODE(unicode); |
| 1979 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1980 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1981 | return NULL; |
| 1982 | } |
| 1983 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1984 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1985 | { |
| 1986 | if (!PyUnicode_Check(unicode)) { |
| 1987 | PyErr_BadArgument(); |
| 1988 | goto onError; |
| 1989 | } |
| 1990 | return PyUnicode_GET_SIZE(unicode); |
| 1991 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1992 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1993 | return -1; |
| 1994 | } |
| 1995 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 1996 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1997 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 1998 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1999 | } |
| 2000 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 2001 | /* create or adjust a UnicodeDecodeError */ |
| 2002 | static void |
| 2003 | make_decode_exception(PyObject **exceptionObject, |
| 2004 | const char *encoding, |
| 2005 | const char *input, Py_ssize_t length, |
| 2006 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 2007 | const char *reason) |
| 2008 | { |
| 2009 | if (*exceptionObject == NULL) { |
| 2010 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 2011 | encoding, input, length, startpos, endpos, reason); |
| 2012 | } |
| 2013 | else { |
| 2014 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 2015 | goto onError; |
| 2016 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 2017 | goto onError; |
| 2018 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 2019 | goto onError; |
| 2020 | } |
| 2021 | return; |
| 2022 | |
| 2023 | onError: |
| 2024 | Py_DECREF(*exceptionObject); |
| 2025 | *exceptionObject = NULL; |
| 2026 | } |
| 2027 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2028 | /* error handling callback helper: |
| 2029 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 2030 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2031 | and adjust various state variables. |
| 2032 | return 0 on success, -1 on error |
| 2033 | */ |
| 2034 | |
| 2035 | static |
| 2036 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2037 | const char *encoding, const char *reason, |
| 2038 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 2039 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 2040 | PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2041 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2042 | 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] | 2043 | |
| 2044 | PyObject *restuple = NULL; |
| 2045 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2046 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2047 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2048 | Py_ssize_t requiredsize; |
| 2049 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2050 | Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2051 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2052 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2053 | int res = -1; |
| 2054 | |
| 2055 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2056 | *errorHandler = PyCodec_LookupError(errors); |
| 2057 | if (*errorHandler == NULL) |
| 2058 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2059 | } |
| 2060 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 2061 | make_decode_exception(exceptionObject, |
| 2062 | encoding, |
| 2063 | *input, *inend - *input, |
| 2064 | *startinpos, *endinpos, |
| 2065 | reason); |
| 2066 | if (*exceptionObject == NULL) |
| 2067 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2068 | |
| 2069 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 2070 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2071 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2072 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 2073 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2074 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2075 | } |
| 2076 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2077 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2078 | |
| 2079 | /* Copy back the bytes variables, which might have been modified by the |
| 2080 | callback */ |
| 2081 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 2082 | if (!inputobj) |
| 2083 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2084 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2085 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2086 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2087 | *input = PyBytes_AS_STRING(inputobj); |
| 2088 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2089 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 2090 | /* we can DECREF safely, as the exception has another reference, |
| 2091 | so the object won't go away. */ |
| 2092 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2093 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2094 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2095 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 2096 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2097 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 2098 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 2099 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2100 | |
| 2101 | /* need more space? (at least enough for what we |
| 2102 | have+the replacement+the rest of the string (starting |
| 2103 | at the new input position), so we won't have to check space |
| 2104 | when there are no errors in the rest of the string) */ |
| 2105 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 2106 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 2107 | requiredsize = *outpos + repsize + insize-newpos; |
| 2108 | if (requiredsize > outsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2109 | if (requiredsize<2*outsize) |
| 2110 | requiredsize = 2*outsize; |
| 2111 | if (_PyUnicode_Resize(output, requiredsize) < 0) |
| 2112 | goto onError; |
| 2113 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2114 | } |
| 2115 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2116 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2117 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 2118 | *outptr += repsize; |
| 2119 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2120 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2121 | /* we made it! */ |
| 2122 | res = 0; |
| 2123 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2124 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2125 | Py_XDECREF(restuple); |
| 2126 | return res; |
| 2127 | } |
| 2128 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2129 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 2130 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2131 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 2132 | |
| 2133 | /* Three simple macros defining base-64. */ |
| 2134 | |
| 2135 | /* Is c a base-64 character? */ |
| 2136 | |
| 2137 | #define IS_BASE64(c) \ |
| 2138 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 2139 | ((c) >= 'a' && (c) <= 'z') || \ |
| 2140 | ((c) >= '0' && (c) <= '9') || \ |
| 2141 | (c) == '+' || (c) == '/') |
| 2142 | |
| 2143 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 2144 | |
| 2145 | #define FROM_BASE64(c) \ |
| 2146 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 2147 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 2148 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 2149 | (c) == '+' ? 62 : 63) |
| 2150 | |
| 2151 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 2152 | |
| 2153 | #define TO_BASE64(n) \ |
| 2154 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 2155 | |
| 2156 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 2157 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 2158 | * byte not decoding to itself is the + which begins a base64 |
| 2159 | * string. */ |
| 2160 | |
| 2161 | #define DECODE_DIRECT(c) \ |
| 2162 | ((c) <= 127 && (c) != '+') |
| 2163 | |
| 2164 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 2165 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 2166 | * the above). See RFC2152. This array identifies these different |
| 2167 | * sets: |
| 2168 | * 0 : "Set D" |
| 2169 | * alphanumeric and '(),-./:? |
| 2170 | * 1 : "Set O" |
| 2171 | * !"#$%&*;<=>@[]^_`{|} |
| 2172 | * 2 : "whitespace" |
| 2173 | * ht nl cr sp |
| 2174 | * 3 : special (must be base64 encoded) |
| 2175 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 2176 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2177 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2178 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2179 | char utf7_category[128] = { |
| 2180 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 2181 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 2182 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 2183 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 2184 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 2185 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 2186 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 2187 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 2188 | /* @ A B C D E F G H I J K L M N O */ |
| 2189 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2190 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 2191 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 2192 | /* ` a b c d e f g h i j k l m n o */ |
| 2193 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2194 | /* p q r s t u v w x y z { | } ~ del */ |
| 2195 | 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] | 2196 | }; |
| 2197 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2198 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 2199 | * answer depends on whether we are encoding set O as itself, and also |
| 2200 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 2201 | * clear that the answers to these questions vary between |
| 2202 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 2203 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2204 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 2205 | ((c) < 128 && (c) > 0 && \ |
| 2206 | ((utf7_category[(c)] == 0) || \ |
| 2207 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 2208 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2209 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2210 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2211 | Py_ssize_t size, |
| 2212 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2213 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2214 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 2215 | } |
| 2216 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2217 | /* The decoder. The only state we preserve is our read position, |
| 2218 | * i.e. how many characters we have consumed. So if we end in the |
| 2219 | * middle of a shift sequence we have to back off the read position |
| 2220 | * and the output to the beginning of the sequence, otherwise we lose |
| 2221 | * all the shift state (seen bits, number of bits seen, high |
| 2222 | * surrogate). */ |
| 2223 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2224 | PyObject *PyUnicode_DecodeUTF7Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2225 | Py_ssize_t size, |
| 2226 | const char *errors, |
| 2227 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2228 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2229 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2230 | Py_ssize_t startinpos; |
| 2231 | Py_ssize_t endinpos; |
| 2232 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2233 | const char *e; |
| 2234 | PyUnicodeObject *unicode; |
| 2235 | Py_UNICODE *p; |
| 2236 | const char *errmsg = ""; |
| 2237 | int inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2238 | Py_UNICODE *shiftOutStart; |
| 2239 | unsigned int base64bits = 0; |
| 2240 | unsigned long base64buffer = 0; |
| 2241 | Py_UNICODE surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2242 | PyObject *errorHandler = NULL; |
| 2243 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2244 | |
| 2245 | unicode = _PyUnicode_New(size); |
| 2246 | if (!unicode) |
| 2247 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2248 | if (size == 0) { |
| 2249 | if (consumed) |
| 2250 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2251 | return (PyObject *)unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2252 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2253 | |
| 2254 | p = unicode->str; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2255 | shiftOutStart = p; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2256 | e = s + size; |
| 2257 | |
| 2258 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2259 | Py_UNICODE ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2260 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 2261 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2262 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2263 | if (inShift) { /* in a base-64 section */ |
| 2264 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 2265 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 2266 | base64bits += 6; |
| 2267 | s++; |
| 2268 | if (base64bits >= 16) { |
| 2269 | /* we have enough bits for a UTF-16 value */ |
| 2270 | Py_UNICODE outCh = (Py_UNICODE) |
| 2271 | (base64buffer >> (base64bits-16)); |
| 2272 | base64bits -= 16; |
| 2273 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 2274 | if (surrogate) { |
| 2275 | /* expecting a second surrogate */ |
| 2276 | if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 2277 | #ifdef Py_UNICODE_WIDE |
| 2278 | *p++ = (((surrogate & 0x3FF)<<10) |
| 2279 | | (outCh & 0x3FF)) + 0x10000; |
| 2280 | #else |
| 2281 | *p++ = surrogate; |
| 2282 | *p++ = outCh; |
| 2283 | #endif |
| 2284 | surrogate = 0; |
| 2285 | } |
| 2286 | else { |
| 2287 | surrogate = 0; |
| 2288 | errmsg = "second surrogate missing"; |
| 2289 | goto utf7Error; |
| 2290 | } |
| 2291 | } |
| 2292 | else if (outCh >= 0xD800 && outCh <= 0xDBFF) { |
| 2293 | /* first surrogate */ |
| 2294 | surrogate = outCh; |
| 2295 | } |
| 2296 | else if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 2297 | errmsg = "unexpected second surrogate"; |
| 2298 | goto utf7Error; |
| 2299 | } |
| 2300 | else { |
| 2301 | *p++ = outCh; |
| 2302 | } |
| 2303 | } |
| 2304 | } |
| 2305 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2306 | inShift = 0; |
| 2307 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2308 | if (surrogate) { |
| 2309 | errmsg = "second surrogate missing at end of shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2310 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2311 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2312 | if (base64bits > 0) { /* left-over bits */ |
| 2313 | if (base64bits >= 6) { |
| 2314 | /* We've seen at least one base-64 character */ |
| 2315 | errmsg = "partial character in shift sequence"; |
| 2316 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2317 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2318 | else { |
| 2319 | /* Some bits remain; they should be zero */ |
| 2320 | if (base64buffer != 0) { |
| 2321 | errmsg = "non-zero padding bits in shift sequence"; |
| 2322 | goto utf7Error; |
| 2323 | } |
| 2324 | } |
| 2325 | } |
| 2326 | if (ch != '-') { |
| 2327 | /* '-' is absorbed; other terminating |
| 2328 | characters are preserved */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2329 | *p++ = ch; |
| 2330 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2331 | } |
| 2332 | } |
| 2333 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2334 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2335 | s++; /* consume '+' */ |
| 2336 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2337 | s++; |
| 2338 | *p++ = '+'; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2339 | } |
| 2340 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2341 | inShift = 1; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2342 | shiftOutStart = p; |
| 2343 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2344 | } |
| 2345 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2346 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2347 | *p++ = ch; |
| 2348 | s++; |
| 2349 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2350 | else { |
| 2351 | startinpos = s-starts; |
| 2352 | s++; |
| 2353 | errmsg = "unexpected special character"; |
| 2354 | goto utf7Error; |
| 2355 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2356 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2357 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2358 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2359 | endinpos = s-starts; |
| 2360 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2361 | errors, &errorHandler, |
| 2362 | "utf7", errmsg, |
| 2363 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2364 | &unicode, &outpos, &p)) |
| 2365 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2366 | } |
| 2367 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2368 | /* end of string */ |
| 2369 | |
| 2370 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 2371 | /* if we're in an inconsistent state, that's an error */ |
| 2372 | if (surrogate || |
| 2373 | (base64bits >= 6) || |
| 2374 | (base64bits > 0 && base64buffer != 0)) { |
| 2375 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2376 | endinpos = size; |
| 2377 | if (unicode_decode_call_errorhandler( |
| 2378 | errors, &errorHandler, |
| 2379 | "utf7", "unterminated shift sequence", |
| 2380 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2381 | &unicode, &outpos, &p)) |
| 2382 | goto onError; |
| 2383 | if (s < e) |
| 2384 | goto restart; |
| 2385 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2386 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2387 | |
| 2388 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2389 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2390 | if (inShift) { |
| 2391 | p = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2392 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2393 | } |
| 2394 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2395 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2396 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2397 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2398 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2399 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2400 | goto onError; |
| 2401 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2402 | Py_XDECREF(errorHandler); |
| 2403 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2404 | return (PyObject *)unicode; |
| 2405 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2406 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2407 | Py_XDECREF(errorHandler); |
| 2408 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2409 | Py_DECREF(unicode); |
| 2410 | return NULL; |
| 2411 | } |
| 2412 | |
| 2413 | |
| 2414 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2415 | Py_ssize_t size, |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2416 | int base64SetO, |
| 2417 | int base64WhiteSpace, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2418 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2419 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2420 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2421 | /* It might be possible to tighten this worst case */ |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 2422 | Py_ssize_t allocated = 8 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2423 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2424 | Py_ssize_t i = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2425 | unsigned int base64bits = 0; |
| 2426 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2427 | char * out; |
| 2428 | char * start; |
| 2429 | |
| 2430 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2431 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2432 | |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 2433 | if (allocated / 8 != size) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2434 | return PyErr_NoMemory(); |
| 2435 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2436 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2437 | if (v == NULL) |
| 2438 | return NULL; |
| 2439 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2440 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2441 | for (;i < size; ++i) { |
| 2442 | Py_UNICODE ch = s[i]; |
| 2443 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2444 | if (inShift) { |
| 2445 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 2446 | /* shifting out */ |
| 2447 | if (base64bits) { /* output remaining bits */ |
| 2448 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 2449 | base64buffer = 0; |
| 2450 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2451 | } |
| 2452 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2453 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 2454 | so no '-' is required, except if the character is itself a '-' */ |
| 2455 | if (IS_BASE64(ch) || ch == '-') { |
| 2456 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2457 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2458 | *out++ = (char) ch; |
| 2459 | } |
| 2460 | else { |
| 2461 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2462 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2463 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2464 | else { /* not in a shift sequence */ |
| 2465 | if (ch == '+') { |
| 2466 | *out++ = '+'; |
| 2467 | *out++ = '-'; |
| 2468 | } |
| 2469 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 2470 | *out++ = (char) ch; |
| 2471 | } |
| 2472 | else { |
| 2473 | *out++ = '+'; |
| 2474 | inShift = 1; |
| 2475 | goto encode_char; |
| 2476 | } |
| 2477 | } |
| 2478 | continue; |
| 2479 | encode_char: |
| 2480 | #ifdef Py_UNICODE_WIDE |
| 2481 | if (ch >= 0x10000) { |
| 2482 | /* code first surrogate */ |
| 2483 | base64bits += 16; |
| 2484 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 2485 | while (base64bits >= 6) { |
| 2486 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 2487 | base64bits -= 6; |
| 2488 | } |
| 2489 | /* prepare second surrogate */ |
| 2490 | ch = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2491 | } |
| 2492 | #endif |
| 2493 | base64bits += 16; |
| 2494 | base64buffer = (base64buffer << 16) | ch; |
| 2495 | while (base64bits >= 6) { |
| 2496 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 2497 | base64bits -= 6; |
| 2498 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 2499 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2500 | if (base64bits) |
| 2501 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 2502 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2503 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2504 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 2505 | return NULL; |
| 2506 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2507 | } |
| 2508 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2509 | #undef IS_BASE64 |
| 2510 | #undef FROM_BASE64 |
| 2511 | #undef TO_BASE64 |
| 2512 | #undef DECODE_DIRECT |
| 2513 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2514 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2515 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 2516 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2517 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2518 | char utf8_code_length[256] = { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2519 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 2520 | illegal prefix. See RFC 3629 for details */ |
| 2521 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 2522 | 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] | 2523 | 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] | 2524 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2525 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2526 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2527 | 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] | 2528 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 2529 | 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] | 2530 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2531 | 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] | 2532 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 2533 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 2534 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 2535 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 2536 | 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] | 2537 | }; |
| 2538 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2539 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2540 | Py_ssize_t size, |
| 2541 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2542 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2543 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 2544 | } |
| 2545 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2546 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 2547 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 2548 | |
| 2549 | /* Mask to quickly check whether a C 'long' contains a |
| 2550 | non-ASCII, UTF8-encoded char. */ |
| 2551 | #if (SIZEOF_LONG == 8) |
| 2552 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 2553 | #elif (SIZEOF_LONG == 4) |
| 2554 | # define ASCII_CHAR_MASK 0x80808080L |
| 2555 | #else |
| 2556 | # error C 'long' size should be either 4 or 8! |
| 2557 | #endif |
| 2558 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2559 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2560 | Py_ssize_t size, |
| 2561 | const char *errors, |
| 2562 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2563 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2564 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2565 | int n; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2566 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2567 | Py_ssize_t startinpos; |
| 2568 | Py_ssize_t endinpos; |
| 2569 | Py_ssize_t outpos; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2570 | const char *e, *aligned_end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2571 | PyUnicodeObject *unicode; |
| 2572 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2573 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2574 | PyObject *errorHandler = NULL; |
| 2575 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2576 | |
| 2577 | /* Note: size will always be longer than the resulting Unicode |
| 2578 | character count */ |
| 2579 | unicode = _PyUnicode_New(size); |
| 2580 | if (!unicode) |
| 2581 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2582 | if (size == 0) { |
| 2583 | if (consumed) |
| 2584 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2585 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2586 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2587 | |
| 2588 | /* Unpack UTF-8 encoded data */ |
| 2589 | p = unicode->str; |
| 2590 | e = s + size; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2591 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2592 | |
| 2593 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2594 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2595 | |
| 2596 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2597 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 2598 | input will consist of an overwhelming majority of ASCII |
| 2599 | characters, we try to optimize for this case by checking |
| 2600 | as many characters as a C 'long' can contain. |
| 2601 | First, check if we can do an aligned read, as most CPUs have |
| 2602 | a penalty for unaligned reads. |
| 2603 | */ |
| 2604 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 2605 | /* Help register allocation */ |
| 2606 | register const char *_s = s; |
| 2607 | register Py_UNICODE *_p = p; |
| 2608 | while (_s < aligned_end) { |
| 2609 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 2610 | and do a fast unrolled copy if it only contains ASCII |
| 2611 | characters. */ |
| 2612 | unsigned long data = *(unsigned long *) _s; |
| 2613 | if (data & ASCII_CHAR_MASK) |
| 2614 | break; |
| 2615 | _p[0] = (unsigned char) _s[0]; |
| 2616 | _p[1] = (unsigned char) _s[1]; |
| 2617 | _p[2] = (unsigned char) _s[2]; |
| 2618 | _p[3] = (unsigned char) _s[3]; |
| 2619 | #if (SIZEOF_LONG == 8) |
| 2620 | _p[4] = (unsigned char) _s[4]; |
| 2621 | _p[5] = (unsigned char) _s[5]; |
| 2622 | _p[6] = (unsigned char) _s[6]; |
| 2623 | _p[7] = (unsigned char) _s[7]; |
| 2624 | #endif |
| 2625 | _s += SIZEOF_LONG; |
| 2626 | _p += SIZEOF_LONG; |
| 2627 | } |
| 2628 | s = _s; |
| 2629 | p = _p; |
| 2630 | if (s == e) |
| 2631 | break; |
| 2632 | ch = (unsigned char)*s; |
| 2633 | } |
| 2634 | } |
| 2635 | |
| 2636 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2637 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2638 | s++; |
| 2639 | continue; |
| 2640 | } |
| 2641 | |
| 2642 | n = utf8_code_length[ch]; |
| 2643 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2644 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2645 | if (consumed) |
| 2646 | break; |
| 2647 | else { |
| 2648 | errmsg = "unexpected end of data"; |
| 2649 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2650 | endinpos = startinpos+1; |
| 2651 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 2652 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2653 | goto utf8Error; |
| 2654 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2655 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2656 | |
| 2657 | switch (n) { |
| 2658 | |
| 2659 | case 0: |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2660 | errmsg = "invalid start byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2661 | startinpos = s-starts; |
| 2662 | endinpos = startinpos+1; |
| 2663 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2664 | |
| 2665 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2666 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2667 | startinpos = s-starts; |
| 2668 | endinpos = startinpos+1; |
| 2669 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2670 | |
| 2671 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2672 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2673 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2674 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2675 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2676 | goto utf8Error; |
| 2677 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2678 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2679 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 2680 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2681 | break; |
| 2682 | |
| 2683 | case 3: |
Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 2684 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 2685 | will result in surrogates in range d800-dfff. Surrogates are |
| 2686 | not valid UTF-8 so they are rejected. |
| 2687 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 2688 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2689 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2690 | (s[2] & 0xc0) != 0x80 || |
| 2691 | ((unsigned char)s[0] == 0xE0 && |
| 2692 | (unsigned char)s[1] < 0xA0) || |
| 2693 | ((unsigned char)s[0] == 0xED && |
| 2694 | (unsigned char)s[1] > 0x9F)) { |
| 2695 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2696 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2697 | endinpos = startinpos + 1; |
| 2698 | |
| 2699 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 2700 | continuation byte is s[2], so increment endinpos by 1, |
| 2701 | if not, s[1] is invalid and endinpos doesn't need to |
| 2702 | be incremented. */ |
| 2703 | if ((s[1] & 0xC0) == 0x80) |
| 2704 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2705 | goto utf8Error; |
| 2706 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2707 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2708 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
| 2709 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2710 | break; |
| 2711 | |
| 2712 | case 4: |
| 2713 | if ((s[1] & 0xc0) != 0x80 || |
| 2714 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2715 | (s[3] & 0xc0) != 0x80 || |
| 2716 | ((unsigned char)s[0] == 0xF0 && |
| 2717 | (unsigned char)s[1] < 0x90) || |
| 2718 | ((unsigned char)s[0] == 0xF4 && |
| 2719 | (unsigned char)s[1] > 0x8F)) { |
| 2720 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2721 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2722 | endinpos = startinpos + 1; |
| 2723 | if ((s[1] & 0xC0) == 0x80) { |
| 2724 | endinpos++; |
| 2725 | if ((s[2] & 0xC0) == 0x80) |
| 2726 | endinpos++; |
| 2727 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2728 | goto utf8Error; |
| 2729 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2730 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2731 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 2732 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 2733 | |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2734 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2735 | *p++ = (Py_UNICODE)ch; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2736 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2737 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2738 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2739 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 2740 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2741 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2742 | /* high surrogate = top 10 bits added to D800 */ |
| 2743 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2744 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2745 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2746 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2747 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2748 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2749 | } |
| 2750 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2751 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2752 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2753 | utf8Error: |
| 2754 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2755 | if (unicode_decode_call_errorhandler( |
| 2756 | errors, &errorHandler, |
| 2757 | "utf8", errmsg, |
| 2758 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2759 | &unicode, &outpos, &p)) |
| 2760 | goto onError; |
| 2761 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2762 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2763 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2764 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2765 | |
| 2766 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2767 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2768 | goto onError; |
| 2769 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2770 | Py_XDECREF(errorHandler); |
| 2771 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2772 | return (PyObject *)unicode; |
| 2773 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2774 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2775 | Py_XDECREF(errorHandler); |
| 2776 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2777 | Py_DECREF(unicode); |
| 2778 | return NULL; |
| 2779 | } |
| 2780 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2781 | #undef ASCII_CHAR_MASK |
| 2782 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 2783 | #ifdef __APPLE__ |
| 2784 | |
| 2785 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
| 2786 | used to decode the command line arguments on Mac OS X. */ |
| 2787 | |
| 2788 | wchar_t* |
| 2789 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 2790 | { |
| 2791 | int n; |
| 2792 | const char *e; |
| 2793 | wchar_t *unicode, *p; |
| 2794 | |
| 2795 | /* Note: size will always be longer than the resulting Unicode |
| 2796 | character count */ |
| 2797 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) { |
| 2798 | PyErr_NoMemory(); |
| 2799 | return NULL; |
| 2800 | } |
| 2801 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 2802 | if (!unicode) |
| 2803 | return NULL; |
| 2804 | |
| 2805 | /* Unpack UTF-8 encoded data */ |
| 2806 | p = unicode; |
| 2807 | e = s + size; |
| 2808 | while (s < e) { |
| 2809 | Py_UCS4 ch = (unsigned char)*s; |
| 2810 | |
| 2811 | if (ch < 0x80) { |
| 2812 | *p++ = (wchar_t)ch; |
| 2813 | s++; |
| 2814 | continue; |
| 2815 | } |
| 2816 | |
| 2817 | n = utf8_code_length[ch]; |
| 2818 | if (s + n > e) { |
| 2819 | goto surrogateescape; |
| 2820 | } |
| 2821 | |
| 2822 | switch (n) { |
| 2823 | case 0: |
| 2824 | case 1: |
| 2825 | goto surrogateescape; |
| 2826 | |
| 2827 | case 2: |
| 2828 | if ((s[1] & 0xc0) != 0x80) |
| 2829 | goto surrogateescape; |
| 2830 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
| 2831 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 2832 | *p++ = (wchar_t)ch; |
| 2833 | break; |
| 2834 | |
| 2835 | case 3: |
| 2836 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 2837 | will result in surrogates in range d800-dfff. Surrogates are |
| 2838 | not valid UTF-8 so they are rejected. |
| 2839 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 2840 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
| 2841 | if ((s[1] & 0xc0) != 0x80 || |
| 2842 | (s[2] & 0xc0) != 0x80 || |
| 2843 | ((unsigned char)s[0] == 0xE0 && |
| 2844 | (unsigned char)s[1] < 0xA0) || |
| 2845 | ((unsigned char)s[0] == 0xED && |
| 2846 | (unsigned char)s[1] > 0x9F)) { |
| 2847 | |
| 2848 | goto surrogateescape; |
| 2849 | } |
| 2850 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
| 2851 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
| 2852 | *p++ = (Py_UNICODE)ch; |
| 2853 | break; |
| 2854 | |
| 2855 | case 4: |
| 2856 | if ((s[1] & 0xc0) != 0x80 || |
| 2857 | (s[2] & 0xc0) != 0x80 || |
| 2858 | (s[3] & 0xc0) != 0x80 || |
| 2859 | ((unsigned char)s[0] == 0xF0 && |
| 2860 | (unsigned char)s[1] < 0x90) || |
| 2861 | ((unsigned char)s[0] == 0xF4 && |
| 2862 | (unsigned char)s[1] > 0x8F)) { |
| 2863 | goto surrogateescape; |
| 2864 | } |
| 2865 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 2866 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 2867 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 2868 | |
| 2869 | #if SIZEOF_WCHAR_T == 4 |
| 2870 | *p++ = (wchar_t)ch; |
| 2871 | #else |
| 2872 | /* compute and append the two surrogates: */ |
| 2873 | |
| 2874 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 2875 | ch -= 0x10000; |
| 2876 | |
| 2877 | /* high surrogate = top 10 bits added to D800 */ |
| 2878 | *p++ = (wchar_t)(0xD800 + (ch >> 10)); |
| 2879 | |
| 2880 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 2881 | *p++ = (wchar_t)(0xDC00 + (ch & 0x03FF)); |
| 2882 | #endif |
| 2883 | break; |
| 2884 | } |
| 2885 | s += n; |
| 2886 | continue; |
| 2887 | |
| 2888 | surrogateescape: |
| 2889 | *p++ = 0xDC00 + ch; |
| 2890 | s++; |
| 2891 | } |
| 2892 | *p = L'\0'; |
| 2893 | return unicode; |
| 2894 | } |
| 2895 | |
| 2896 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2897 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2898 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 2899 | and allocate exactly as much space needed at the end. Else allocate the |
| 2900 | maximum possible needed (4 result bytes per Unicode character), and return |
| 2901 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2902 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 2903 | PyObject * |
| 2904 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2905 | Py_ssize_t size, |
| 2906 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2907 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2908 | #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] | 2909 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2910 | Py_ssize_t i; /* index into s of next input byte */ |
| 2911 | PyObject *result; /* result string object */ |
| 2912 | char *p; /* next free byte in output buffer */ |
| 2913 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 2914 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2915 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2916 | PyObject *errorHandler = NULL; |
| 2917 | PyObject *exc = NULL; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2918 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2919 | assert(s != NULL); |
| 2920 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2921 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2922 | if (size <= MAX_SHORT_UNICHARS) { |
| 2923 | /* Write into the stack buffer; nallocated can't overflow. |
| 2924 | * At the end, we'll allocate exactly as much heap space as it |
| 2925 | * turns out we need. |
| 2926 | */ |
| 2927 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2928 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2929 | p = stackbuf; |
| 2930 | } |
| 2931 | else { |
| 2932 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 2933 | nallocated = size * 4; |
| 2934 | if (nallocated / 4 != size) /* overflow! */ |
| 2935 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2936 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2937 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2938 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2939 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2940 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2941 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2942 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2943 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2944 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2945 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2946 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2947 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2948 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2949 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2950 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 2951 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 2952 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2953 | } else if (0xD800 <= ch && ch <= 0xDFFF) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2954 | #ifndef Py_UNICODE_WIDE |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2955 | /* Special case: check for high and low surrogate */ |
| 2956 | if (ch <= 0xDBFF && i != size && 0xDC00 <= s[i] && s[i] <= 0xDFFF) { |
| 2957 | Py_UCS4 ch2 = s[i]; |
| 2958 | /* Combine the two surrogates to form a UCS4 value */ |
| 2959 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
| 2960 | i++; |
| 2961 | |
| 2962 | /* Encode UCS4 Unicode ordinals */ |
| 2963 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2964 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2965 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2966 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2967 | } else { |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 2968 | #endif |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2969 | Py_ssize_t newpos; |
| 2970 | PyObject *rep; |
| 2971 | Py_ssize_t repsize, k; |
| 2972 | rep = unicode_encode_call_errorhandler |
| 2973 | (errors, &errorHandler, "utf-8", "surrogates not allowed", |
| 2974 | s, size, &exc, i-1, i, &newpos); |
| 2975 | if (!rep) |
| 2976 | goto error; |
| 2977 | |
| 2978 | if (PyBytes_Check(rep)) |
| 2979 | repsize = PyBytes_GET_SIZE(rep); |
| 2980 | else |
| 2981 | repsize = PyUnicode_GET_SIZE(rep); |
| 2982 | |
| 2983 | if (repsize > 4) { |
| 2984 | Py_ssize_t offset; |
| 2985 | |
| 2986 | if (result == NULL) |
| 2987 | offset = p - stackbuf; |
| 2988 | else |
| 2989 | offset = p - PyBytes_AS_STRING(result); |
| 2990 | |
| 2991 | if (nallocated > PY_SSIZE_T_MAX - repsize + 4) { |
| 2992 | /* integer overflow */ |
| 2993 | PyErr_NoMemory(); |
| 2994 | goto error; |
| 2995 | } |
| 2996 | nallocated += repsize - 4; |
| 2997 | if (result != NULL) { |
| 2998 | if (_PyBytes_Resize(&result, nallocated) < 0) |
| 2999 | goto error; |
| 3000 | } else { |
| 3001 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
| 3002 | if (result == NULL) |
| 3003 | goto error; |
| 3004 | Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset); |
| 3005 | } |
| 3006 | p = PyBytes_AS_STRING(result) + offset; |
| 3007 | } |
| 3008 | |
| 3009 | if (PyBytes_Check(rep)) { |
| 3010 | char *prep = PyBytes_AS_STRING(rep); |
| 3011 | for(k = repsize; k > 0; k--) |
| 3012 | *p++ = *prep++; |
| 3013 | } else /* rep is unicode */ { |
| 3014 | Py_UNICODE *prep = PyUnicode_AS_UNICODE(rep); |
| 3015 | Py_UNICODE c; |
| 3016 | |
| 3017 | for(k=0; k<repsize; k++) { |
| 3018 | c = prep[k]; |
| 3019 | if (0x80 <= c) { |
| 3020 | raise_encode_exception(&exc, "utf-8", s, size, |
| 3021 | i-1, i, "surrogates not allowed"); |
| 3022 | goto error; |
| 3023 | } |
| 3024 | *p++ = (char)prep[k]; |
| 3025 | } |
| 3026 | } |
| 3027 | Py_DECREF(rep); |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 3028 | #ifndef Py_UNICODE_WIDE |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 3029 | } |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 3030 | #endif |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 3031 | } else if (ch < 0x10000) { |
| 3032 | *p++ = (char)(0xe0 | (ch >> 12)); |
| 3033 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 3034 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 3035 | } else /* ch >= 0x10000 */ { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 3036 | /* Encode UCS4 Unicode ordinals */ |
| 3037 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 3038 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 3039 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 3040 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 3041 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3042 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 3043 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3044 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 3045 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3046 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 3047 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3048 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 3049 | } |
| 3050 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3051 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3052 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 3053 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3054 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 3055 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 3056 | Py_XDECREF(errorHandler); |
| 3057 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3058 | return result; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 3059 | error: |
| 3060 | Py_XDECREF(errorHandler); |
| 3061 | Py_XDECREF(exc); |
| 3062 | Py_XDECREF(result); |
| 3063 | return NULL; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 3064 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 3065 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3066 | } |
| 3067 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3068 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 3069 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3070 | if (!PyUnicode_Check(unicode)) { |
| 3071 | PyErr_BadArgument(); |
| 3072 | return NULL; |
| 3073 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 3074 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3075 | PyUnicode_GET_SIZE(unicode), |
| 3076 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3077 | } |
| 3078 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3079 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 3080 | |
| 3081 | PyObject * |
| 3082 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3083 | Py_ssize_t size, |
| 3084 | const char *errors, |
| 3085 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3086 | { |
| 3087 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 3088 | } |
| 3089 | |
| 3090 | PyObject * |
| 3091 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3092 | Py_ssize_t size, |
| 3093 | const char *errors, |
| 3094 | int *byteorder, |
| 3095 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3096 | { |
| 3097 | const char *starts = s; |
| 3098 | Py_ssize_t startinpos; |
| 3099 | Py_ssize_t endinpos; |
| 3100 | Py_ssize_t outpos; |
| 3101 | PyUnicodeObject *unicode; |
| 3102 | Py_UNICODE *p; |
| 3103 | #ifndef Py_UNICODE_WIDE |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 3104 | int pairs = 0; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 3105 | const unsigned char *qq; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3106 | #else |
| 3107 | const int pairs = 0; |
| 3108 | #endif |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 3109 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3110 | int bo = 0; /* assume native ordering by default */ |
| 3111 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3112 | /* Offsets from q for retrieving bytes in the right order. */ |
| 3113 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3114 | int iorder[] = {0, 1, 2, 3}; |
| 3115 | #else |
| 3116 | int iorder[] = {3, 2, 1, 0}; |
| 3117 | #endif |
| 3118 | PyObject *errorHandler = NULL; |
| 3119 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 3120 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3121 | q = (unsigned char *)s; |
| 3122 | e = q + size; |
| 3123 | |
| 3124 | if (byteorder) |
| 3125 | bo = *byteorder; |
| 3126 | |
| 3127 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 3128 | byte order setting accordingly. In native mode, the leading BOM |
| 3129 | mark is skipped, in all other modes, it is copied to the output |
| 3130 | stream as-is (giving a ZWNBSP character). */ |
| 3131 | if (bo == 0) { |
| 3132 | if (size >= 4) { |
| 3133 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3134 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3135 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3136 | if (bom == 0x0000FEFF) { |
| 3137 | q += 4; |
| 3138 | bo = -1; |
| 3139 | } |
| 3140 | else if (bom == 0xFFFE0000) { |
| 3141 | q += 4; |
| 3142 | bo = 1; |
| 3143 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3144 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3145 | if (bom == 0x0000FEFF) { |
| 3146 | q += 4; |
| 3147 | bo = 1; |
| 3148 | } |
| 3149 | else if (bom == 0xFFFE0000) { |
| 3150 | q += 4; |
| 3151 | bo = -1; |
| 3152 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3153 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3154 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3155 | } |
| 3156 | |
| 3157 | if (bo == -1) { |
| 3158 | /* force LE */ |
| 3159 | iorder[0] = 0; |
| 3160 | iorder[1] = 1; |
| 3161 | iorder[2] = 2; |
| 3162 | iorder[3] = 3; |
| 3163 | } |
| 3164 | else if (bo == 1) { |
| 3165 | /* force BE */ |
| 3166 | iorder[0] = 3; |
| 3167 | iorder[1] = 2; |
| 3168 | iorder[2] = 1; |
| 3169 | iorder[3] = 0; |
| 3170 | } |
| 3171 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 3172 | /* On narrow builds we split characters outside the BMP into two |
| 3173 | codepoints => count how much extra space we need. */ |
| 3174 | #ifndef Py_UNICODE_WIDE |
| 3175 | for (qq = q; qq < e; qq += 4) |
| 3176 | if (qq[iorder[2]] != 0 || qq[iorder[3]] != 0) |
| 3177 | pairs++; |
| 3178 | #endif |
| 3179 | |
| 3180 | /* This might be one to much, because of a BOM */ |
| 3181 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 3182 | if (!unicode) |
| 3183 | return NULL; |
| 3184 | if (size == 0) |
| 3185 | return (PyObject *)unicode; |
| 3186 | |
| 3187 | /* Unpack UTF-32 encoded data */ |
| 3188 | p = unicode->str; |
| 3189 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3190 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3191 | Py_UCS4 ch; |
| 3192 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 3193 | if (e-q<4) { |
| 3194 | if (consumed) |
| 3195 | break; |
| 3196 | errmsg = "truncated data"; |
| 3197 | startinpos = ((const char *)q)-starts; |
| 3198 | endinpos = ((const char *)e)-starts; |
| 3199 | goto utf32Error; |
| 3200 | /* The remaining input chars are ignored if the callback |
| 3201 | chooses to skip the input */ |
| 3202 | } |
| 3203 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 3204 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3205 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3206 | if (ch >= 0x110000) |
| 3207 | { |
| 3208 | errmsg = "codepoint not in range(0x110000)"; |
| 3209 | startinpos = ((const char *)q)-starts; |
| 3210 | endinpos = startinpos+4; |
| 3211 | goto utf32Error; |
| 3212 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3213 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3214 | if (ch >= 0x10000) |
| 3215 | { |
| 3216 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 3217 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 3218 | } |
| 3219 | else |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3220 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3221 | *p++ = ch; |
| 3222 | q += 4; |
| 3223 | continue; |
| 3224 | utf32Error: |
| 3225 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 3226 | if (unicode_decode_call_errorhandler( |
| 3227 | errors, &errorHandler, |
| 3228 | "utf32", errmsg, |
| 3229 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
| 3230 | &unicode, &outpos, &p)) |
| 3231 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3232 | } |
| 3233 | |
| 3234 | if (byteorder) |
| 3235 | *byteorder = bo; |
| 3236 | |
| 3237 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3238 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3239 | |
| 3240 | /* Adjust length */ |
| 3241 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
| 3242 | goto onError; |
| 3243 | |
| 3244 | Py_XDECREF(errorHandler); |
| 3245 | Py_XDECREF(exc); |
| 3246 | return (PyObject *)unicode; |
| 3247 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3248 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3249 | Py_DECREF(unicode); |
| 3250 | Py_XDECREF(errorHandler); |
| 3251 | Py_XDECREF(exc); |
| 3252 | return NULL; |
| 3253 | } |
| 3254 | |
| 3255 | PyObject * |
| 3256 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3257 | Py_ssize_t size, |
| 3258 | const char *errors, |
| 3259 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3260 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3261 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3262 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3263 | Py_ssize_t nsize, bytesize; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3264 | #ifndef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3265 | Py_ssize_t i, pairs; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3266 | #else |
| 3267 | const int pairs = 0; |
| 3268 | #endif |
| 3269 | /* Offsets from p for storing byte pairs in the right order. */ |
| 3270 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3271 | int iorder[] = {0, 1, 2, 3}; |
| 3272 | #else |
| 3273 | int iorder[] = {3, 2, 1, 0}; |
| 3274 | #endif |
| 3275 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3276 | #define STORECHAR(CH) \ |
| 3277 | do { \ |
| 3278 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 3279 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 3280 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 3281 | p[iorder[0]] = (CH) & 0xff; \ |
| 3282 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3283 | } while(0) |
| 3284 | |
| 3285 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 3286 | so we need less space. */ |
| 3287 | #ifndef Py_UNICODE_WIDE |
| 3288 | for (i = pairs = 0; i < size-1; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3289 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 3290 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 3291 | pairs++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3292 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3293 | nsize = (size - pairs + (byteorder == 0)); |
| 3294 | bytesize = nsize * 4; |
| 3295 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3296 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3297 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3298 | if (v == NULL) |
| 3299 | return NULL; |
| 3300 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3301 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3302 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3303 | STORECHAR(0xFEFF); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3304 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3305 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3306 | |
| 3307 | if (byteorder == -1) { |
| 3308 | /* force LE */ |
| 3309 | iorder[0] = 0; |
| 3310 | iorder[1] = 1; |
| 3311 | iorder[2] = 2; |
| 3312 | iorder[3] = 3; |
| 3313 | } |
| 3314 | else if (byteorder == 1) { |
| 3315 | /* force BE */ |
| 3316 | iorder[0] = 3; |
| 3317 | iorder[1] = 2; |
| 3318 | iorder[2] = 1; |
| 3319 | iorder[3] = 0; |
| 3320 | } |
| 3321 | |
| 3322 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3323 | Py_UCS4 ch = *s++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3324 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3325 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 3326 | Py_UCS4 ch2 = *s; |
| 3327 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 3328 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 3329 | s++; |
| 3330 | size--; |
| 3331 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3332 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3333 | #endif |
| 3334 | STORECHAR(ch); |
| 3335 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3336 | |
| 3337 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3338 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3339 | #undef STORECHAR |
| 3340 | } |
| 3341 | |
| 3342 | PyObject *PyUnicode_AsUTF32String(PyObject *unicode) |
| 3343 | { |
| 3344 | if (!PyUnicode_Check(unicode)) { |
| 3345 | PyErr_BadArgument(); |
| 3346 | return NULL; |
| 3347 | } |
| 3348 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3349 | PyUnicode_GET_SIZE(unicode), |
| 3350 | NULL, |
| 3351 | 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3352 | } |
| 3353 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3354 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 3355 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3356 | PyObject * |
| 3357 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3358 | Py_ssize_t size, |
| 3359 | const char *errors, |
| 3360 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3361 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3362 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 3363 | } |
| 3364 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3365 | /* Two masks for fast checking of whether a C 'long' may contain |
| 3366 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 3367 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 3368 | rare in most input. |
| 3369 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 3370 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3371 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3372 | #if (SIZEOF_LONG == 8) |
| 3373 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 3374 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 3375 | #elif (SIZEOF_LONG == 4) |
| 3376 | # define FAST_CHAR_MASK 0x80008000L |
| 3377 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 3378 | #else |
| 3379 | # error C 'long' size should be either 4 or 8! |
| 3380 | #endif |
| 3381 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3382 | PyObject * |
| 3383 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3384 | Py_ssize_t size, |
| 3385 | const char *errors, |
| 3386 | int *byteorder, |
| 3387 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3388 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3389 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3390 | Py_ssize_t startinpos; |
| 3391 | Py_ssize_t endinpos; |
| 3392 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3393 | PyUnicodeObject *unicode; |
| 3394 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3395 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3396 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3397 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 3398 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3399 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 3400 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3401 | int ihi = 1, ilo = 0; |
| 3402 | #else |
| 3403 | int ihi = 0, ilo = 1; |
| 3404 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3405 | PyObject *errorHandler = NULL; |
| 3406 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3407 | |
| 3408 | /* Note: size will always be longer than the resulting Unicode |
| 3409 | character count */ |
| 3410 | unicode = _PyUnicode_New(size); |
| 3411 | if (!unicode) |
| 3412 | return NULL; |
| 3413 | if (size == 0) |
| 3414 | return (PyObject *)unicode; |
| 3415 | |
| 3416 | /* Unpack UTF-16 encoded data */ |
| 3417 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3418 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3419 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3420 | |
| 3421 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3422 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3423 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3424 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 3425 | byte order setting accordingly. In native mode, the leading BOM |
| 3426 | mark is skipped, in all other modes, it is copied to the output |
| 3427 | stream as-is (giving a ZWNBSP character). */ |
| 3428 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3429 | if (size >= 2) { |
| 3430 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3431 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3432 | if (bom == 0xFEFF) { |
| 3433 | q += 2; |
| 3434 | bo = -1; |
| 3435 | } |
| 3436 | else if (bom == 0xFFFE) { |
| 3437 | q += 2; |
| 3438 | bo = 1; |
| 3439 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3440 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3441 | if (bom == 0xFEFF) { |
| 3442 | q += 2; |
| 3443 | bo = 1; |
| 3444 | } |
| 3445 | else if (bom == 0xFFFE) { |
| 3446 | q += 2; |
| 3447 | bo = -1; |
| 3448 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3449 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3450 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3451 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3452 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3453 | if (bo == -1) { |
| 3454 | /* force LE */ |
| 3455 | ihi = 1; |
| 3456 | ilo = 0; |
| 3457 | } |
| 3458 | else if (bo == 1) { |
| 3459 | /* force BE */ |
| 3460 | ihi = 0; |
| 3461 | ilo = 1; |
| 3462 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3463 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3464 | native_ordering = ilo < ihi; |
| 3465 | #else |
| 3466 | native_ordering = ilo > ihi; |
| 3467 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3468 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3469 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3470 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3471 | Py_UNICODE ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3472 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 3473 | reads are more expensive, better to defer to another iteration. */ |
| 3474 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 3475 | /* Fast path for runs of non-surrogate chars. */ |
| 3476 | register const unsigned char *_q = q; |
| 3477 | Py_UNICODE *_p = p; |
| 3478 | if (native_ordering) { |
| 3479 | /* Native ordering is simple: as long as the input cannot |
| 3480 | possibly contain a surrogate char, do an unrolled copy |
| 3481 | of several 16-bit code points to the target object. |
| 3482 | The non-surrogate check is done on several input bytes |
| 3483 | at a time (as many as a C 'long' can contain). */ |
| 3484 | while (_q < aligned_end) { |
| 3485 | unsigned long data = * (unsigned long *) _q; |
| 3486 | if (data & FAST_CHAR_MASK) |
| 3487 | break; |
| 3488 | _p[0] = ((unsigned short *) _q)[0]; |
| 3489 | _p[1] = ((unsigned short *) _q)[1]; |
| 3490 | #if (SIZEOF_LONG == 8) |
| 3491 | _p[2] = ((unsigned short *) _q)[2]; |
| 3492 | _p[3] = ((unsigned short *) _q)[3]; |
| 3493 | #endif |
| 3494 | _q += SIZEOF_LONG; |
| 3495 | _p += SIZEOF_LONG / 2; |
| 3496 | } |
| 3497 | } |
| 3498 | else { |
| 3499 | /* Byteswapped ordering is similar, but we must decompose |
| 3500 | the copy bytewise, and take care of zero'ing out the |
| 3501 | upper bytes if the target object is in 32-bit units |
| 3502 | (that is, in UCS-4 builds). */ |
| 3503 | while (_q < aligned_end) { |
| 3504 | unsigned long data = * (unsigned long *) _q; |
| 3505 | if (data & SWAPPED_FAST_CHAR_MASK) |
| 3506 | break; |
| 3507 | /* Zero upper bytes in UCS-4 builds */ |
| 3508 | #if (Py_UNICODE_SIZE > 2) |
| 3509 | _p[0] = 0; |
| 3510 | _p[1] = 0; |
| 3511 | #if (SIZEOF_LONG == 8) |
| 3512 | _p[2] = 0; |
| 3513 | _p[3] = 0; |
| 3514 | #endif |
| 3515 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 3516 | /* Issue #4916; UCS-4 builds on big endian machines must |
| 3517 | fill the two last bytes of each 4-byte unit. */ |
| 3518 | #if (!defined(BYTEORDER_IS_LITTLE_ENDIAN) && Py_UNICODE_SIZE > 2) |
| 3519 | # define OFF 2 |
| 3520 | #else |
| 3521 | # define OFF 0 |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3522 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 3523 | ((unsigned char *) _p)[OFF + 1] = _q[0]; |
| 3524 | ((unsigned char *) _p)[OFF + 0] = _q[1]; |
| 3525 | ((unsigned char *) _p)[OFF + 1 + Py_UNICODE_SIZE] = _q[2]; |
| 3526 | ((unsigned char *) _p)[OFF + 0 + Py_UNICODE_SIZE] = _q[3]; |
| 3527 | #if (SIZEOF_LONG == 8) |
| 3528 | ((unsigned char *) _p)[OFF + 1 + 2 * Py_UNICODE_SIZE] = _q[4]; |
| 3529 | ((unsigned char *) _p)[OFF + 0 + 2 * Py_UNICODE_SIZE] = _q[5]; |
| 3530 | ((unsigned char *) _p)[OFF + 1 + 3 * Py_UNICODE_SIZE] = _q[6]; |
| 3531 | ((unsigned char *) _p)[OFF + 0 + 3 * Py_UNICODE_SIZE] = _q[7]; |
| 3532 | #endif |
| 3533 | #undef OFF |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3534 | _q += SIZEOF_LONG; |
| 3535 | _p += SIZEOF_LONG / 2; |
| 3536 | } |
| 3537 | } |
| 3538 | p = _p; |
| 3539 | q = _q; |
| 3540 | if (q >= e) |
| 3541 | break; |
| 3542 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3543 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3544 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3545 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3546 | |
| 3547 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 3548 | *p++ = ch; |
| 3549 | continue; |
| 3550 | } |
| 3551 | |
| 3552 | /* UTF-16 code pair: */ |
| 3553 | if (q > e) { |
| 3554 | errmsg = "unexpected end of data"; |
| 3555 | startinpos = (((const char *)q) - 2) - starts; |
| 3556 | endinpos = ((const char *)e) + 1 - starts; |
| 3557 | goto utf16Error; |
| 3558 | } |
| 3559 | if (0xD800 <= ch && ch <= 0xDBFF) { |
| 3560 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 3561 | q += 2; |
| 3562 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 3563 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3564 | *p++ = ch; |
| 3565 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3566 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3567 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3568 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3569 | continue; |
| 3570 | } |
| 3571 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3572 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3573 | startinpos = (((const char *)q)-4)-starts; |
| 3574 | endinpos = startinpos+2; |
| 3575 | goto utf16Error; |
| 3576 | } |
| 3577 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3578 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3579 | errmsg = "illegal encoding"; |
| 3580 | startinpos = (((const char *)q)-2)-starts; |
| 3581 | endinpos = startinpos+2; |
| 3582 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3583 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3584 | utf16Error: |
| 3585 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 3586 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3587 | errors, |
| 3588 | &errorHandler, |
| 3589 | "utf16", errmsg, |
| 3590 | &starts, |
| 3591 | (const char **)&e, |
| 3592 | &startinpos, |
| 3593 | &endinpos, |
| 3594 | &exc, |
| 3595 | (const char **)&q, |
| 3596 | &unicode, |
| 3597 | &outpos, |
| 3598 | &p)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3599 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3600 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3601 | /* remaining byte at the end? (size should be even) */ |
| 3602 | if (e == q) { |
| 3603 | if (!consumed) { |
| 3604 | errmsg = "truncated data"; |
| 3605 | startinpos = ((const char *)q) - starts; |
| 3606 | endinpos = ((const char *)e) + 1 - starts; |
| 3607 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 3608 | if (unicode_decode_call_errorhandler( |
| 3609 | errors, |
| 3610 | &errorHandler, |
| 3611 | "utf16", errmsg, |
| 3612 | &starts, |
| 3613 | (const char **)&e, |
| 3614 | &startinpos, |
| 3615 | &endinpos, |
| 3616 | &exc, |
| 3617 | (const char **)&q, |
| 3618 | &unicode, |
| 3619 | &outpos, |
| 3620 | &p)) |
| 3621 | goto onError; |
| 3622 | /* The remaining input chars are ignored if the callback |
| 3623 | chooses to skip the input */ |
| 3624 | } |
| 3625 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3626 | |
| 3627 | if (byteorder) |
| 3628 | *byteorder = bo; |
| 3629 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3630 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3631 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3632 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3633 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 3634 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3635 | goto onError; |
| 3636 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3637 | Py_XDECREF(errorHandler); |
| 3638 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3639 | return (PyObject *)unicode; |
| 3640 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3641 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3642 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3643 | Py_XDECREF(errorHandler); |
| 3644 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3645 | return NULL; |
| 3646 | } |
| 3647 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3648 | #undef FAST_CHAR_MASK |
| 3649 | #undef SWAPPED_FAST_CHAR_MASK |
| 3650 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3651 | PyObject * |
| 3652 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3653 | Py_ssize_t size, |
| 3654 | const char *errors, |
| 3655 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3656 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3657 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3658 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3659 | Py_ssize_t nsize, bytesize; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3660 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3661 | Py_ssize_t i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3662 | #else |
| 3663 | const int pairs = 0; |
| 3664 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3665 | /* Offsets from p for storing byte pairs in the right order. */ |
| 3666 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3667 | int ihi = 1, ilo = 0; |
| 3668 | #else |
| 3669 | int ihi = 0, ilo = 1; |
| 3670 | #endif |
| 3671 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3672 | #define STORECHAR(CH) \ |
| 3673 | do { \ |
| 3674 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 3675 | p[ilo] = (CH) & 0xff; \ |
| 3676 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3677 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3678 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3679 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3680 | for (i = pairs = 0; i < size; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3681 | if (s[i] >= 0x10000) |
| 3682 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3683 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3684 | /* 2 * (size + pairs + (byteorder == 0)) */ |
| 3685 | if (size > PY_SSIZE_T_MAX || |
| 3686 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3687 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3688 | nsize = size + pairs + (byteorder == 0); |
| 3689 | bytesize = nsize * 2; |
| 3690 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3691 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3692 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3693 | if (v == NULL) |
| 3694 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3695 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3696 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3697 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3698 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 3699 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3700 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3701 | |
| 3702 | if (byteorder == -1) { |
| 3703 | /* force LE */ |
| 3704 | ihi = 1; |
| 3705 | ilo = 0; |
| 3706 | } |
| 3707 | else if (byteorder == 1) { |
| 3708 | /* force BE */ |
| 3709 | ihi = 0; |
| 3710 | ilo = 1; |
| 3711 | } |
| 3712 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3713 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3714 | Py_UNICODE ch = *s++; |
| 3715 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3716 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3717 | if (ch >= 0x10000) { |
| 3718 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 3719 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 3720 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3721 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3722 | STORECHAR(ch); |
| 3723 | if (ch2) |
| 3724 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3725 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3726 | |
| 3727 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3728 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3729 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3730 | } |
| 3731 | |
| 3732 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 3733 | { |
| 3734 | if (!PyUnicode_Check(unicode)) { |
| 3735 | PyErr_BadArgument(); |
| 3736 | return NULL; |
| 3737 | } |
| 3738 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3739 | PyUnicode_GET_SIZE(unicode), |
| 3740 | NULL, |
| 3741 | 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3742 | } |
| 3743 | |
| 3744 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 3745 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 3746 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 3747 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3748 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3749 | Py_ssize_t size, |
| 3750 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3751 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3752 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3753 | Py_ssize_t startinpos; |
| 3754 | Py_ssize_t endinpos; |
| 3755 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3756 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3757 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3758 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3759 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3760 | char* message; |
| 3761 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3762 | PyObject *errorHandler = NULL; |
| 3763 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3764 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3765 | /* Escaped strings will always be longer than the resulting |
| 3766 | 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] | 3767 | length after conversion to the true value. |
| 3768 | (but if the error callback returns a long replacement string |
| 3769 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3770 | v = _PyUnicode_New(size); |
| 3771 | if (v == NULL) |
| 3772 | goto onError; |
| 3773 | if (size == 0) |
| 3774 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3775 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3776 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3777 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3778 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3779 | while (s < end) { |
| 3780 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 3781 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3782 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3783 | |
| 3784 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3785 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3786 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3787 | continue; |
| 3788 | } |
| 3789 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3790 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3791 | /* \ - Escapes */ |
| 3792 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3793 | c = *s++; |
| 3794 | if (s > end) |
| 3795 | c = '\0'; /* Invalid after \ */ |
| 3796 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3797 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3798 | /* \x escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3799 | case '\n': break; |
| 3800 | case '\\': *p++ = '\\'; break; |
| 3801 | case '\'': *p++ = '\''; break; |
| 3802 | case '\"': *p++ = '\"'; break; |
| 3803 | case 'b': *p++ = '\b'; break; |
| 3804 | case 'f': *p++ = '\014'; break; /* FF */ |
| 3805 | case 't': *p++ = '\t'; break; |
| 3806 | case 'n': *p++ = '\n'; break; |
| 3807 | case 'r': *p++ = '\r'; break; |
| 3808 | case 'v': *p++ = '\013'; break; /* VT */ |
| 3809 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 3810 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3811 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3812 | case '0': case '1': case '2': case '3': |
| 3813 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3814 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3815 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3816 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3817 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3818 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3819 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3820 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3821 | break; |
| 3822 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3823 | /* hex escapes */ |
| 3824 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3825 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3826 | digits = 2; |
| 3827 | message = "truncated \\xXX escape"; |
| 3828 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3829 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3830 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3831 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3832 | digits = 4; |
| 3833 | message = "truncated \\uXXXX escape"; |
| 3834 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3835 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3836 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3837 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3838 | digits = 8; |
| 3839 | message = "truncated \\UXXXXXXXX escape"; |
| 3840 | hexescape: |
| 3841 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3842 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3843 | if (s+digits>end) { |
| 3844 | endinpos = size; |
| 3845 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3846 | errors, &errorHandler, |
| 3847 | "unicodeescape", "end of string in escape sequence", |
| 3848 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3849 | &v, &outpos, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3850 | goto onError; |
| 3851 | goto nextByte; |
| 3852 | } |
| 3853 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3854 | c = (unsigned char) s[i]; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 3855 | if (!Py_ISXDIGIT(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3856 | endinpos = (s+i+1)-starts; |
| 3857 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3858 | errors, &errorHandler, |
| 3859 | "unicodeescape", message, |
| 3860 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3861 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3862 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3863 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3864 | } |
| 3865 | chr = (chr<<4) & ~0xF; |
| 3866 | if (c >= '0' && c <= '9') |
| 3867 | chr += c - '0'; |
| 3868 | else if (c >= 'a' && c <= 'f') |
| 3869 | chr += 10 + c - 'a'; |
| 3870 | else |
| 3871 | chr += 10 + c - 'A'; |
| 3872 | } |
| 3873 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 3874 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3875 | /* _decoding_error will have already written into the |
| 3876 | target buffer. */ |
| 3877 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3878 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3879 | /* when we get here, chr is a 32-bit unicode character */ |
| 3880 | if (chr <= 0xffff) |
| 3881 | /* UCS-2 character */ |
| 3882 | *p++ = (Py_UNICODE) chr; |
| 3883 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3884 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3885 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 3886 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3887 | *p++ = chr; |
| 3888 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3889 | chr -= 0x10000L; |
| 3890 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 3891 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3892 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3893 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3894 | endinpos = s-starts; |
| 3895 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3896 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3897 | errors, &errorHandler, |
| 3898 | "unicodeescape", "illegal Unicode character", |
| 3899 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3900 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3901 | goto onError; |
| 3902 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3903 | break; |
| 3904 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3905 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3906 | case 'N': |
| 3907 | message = "malformed \\N character escape"; |
| 3908 | if (ucnhash_CAPI == NULL) { |
| 3909 | /* load the unicode data module */ |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3910 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import(PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3911 | if (ucnhash_CAPI == NULL) |
| 3912 | goto ucnhashError; |
| 3913 | } |
| 3914 | if (*s == '{') { |
| 3915 | const char *start = s+1; |
| 3916 | /* look for the closing brace */ |
| 3917 | while (*s != '}' && s < end) |
| 3918 | s++; |
| 3919 | if (s > start && s < end && *s == '}') { |
| 3920 | /* found a name. look it up in the unicode database */ |
| 3921 | message = "unknown Unicode character name"; |
| 3922 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 3923 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3924 | goto store; |
| 3925 | } |
| 3926 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3927 | endinpos = s-starts; |
| 3928 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3929 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3930 | errors, &errorHandler, |
| 3931 | "unicodeescape", message, |
| 3932 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3933 | &v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3934 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3935 | break; |
| 3936 | |
| 3937 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3938 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3939 | message = "\\ at end of string"; |
| 3940 | s--; |
| 3941 | endinpos = s-starts; |
| 3942 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3943 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3944 | errors, &errorHandler, |
| 3945 | "unicodeescape", message, |
| 3946 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3947 | &v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3948 | goto onError; |
| 3949 | } |
| 3950 | else { |
| 3951 | *p++ = '\\'; |
| 3952 | *p++ = (unsigned char)s[-1]; |
| 3953 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3954 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3955 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3956 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3957 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3958 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3959 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3960 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 3961 | Py_XDECREF(errorHandler); |
| 3962 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3963 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3964 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3965 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 3966 | PyErr_SetString( |
| 3967 | PyExc_UnicodeError, |
| 3968 | "\\N escapes not supported (can't load unicodedata module)" |
| 3969 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3970 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3971 | Py_XDECREF(errorHandler); |
| 3972 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 3973 | return NULL; |
| 3974 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3975 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3976 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3977 | Py_XDECREF(errorHandler); |
| 3978 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3979 | return NULL; |
| 3980 | } |
| 3981 | |
| 3982 | /* Return a Unicode-Escape string version of the Unicode object. |
| 3983 | |
| 3984 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 3985 | appropriate. |
| 3986 | |
| 3987 | */ |
| 3988 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3989 | Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3990 | Py_ssize_t size, |
| 3991 | Py_UNICODE ch) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3992 | { |
| 3993 | /* like wcschr, but doesn't stop at NULL characters */ |
| 3994 | |
| 3995 | while (size-- > 0) { |
| 3996 | if (*s == ch) |
| 3997 | return s; |
| 3998 | s++; |
| 3999 | } |
| 4000 | |
| 4001 | return NULL; |
| 4002 | } |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 4003 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 4004 | static const char *hexdigits = "0123456789abcdef"; |
| 4005 | |
| 4006 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4007 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4008 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4009 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4010 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4011 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4012 | #ifdef Py_UNICODE_WIDE |
| 4013 | const Py_ssize_t expandsize = 10; |
| 4014 | #else |
| 4015 | const Py_ssize_t expandsize = 6; |
| 4016 | #endif |
| 4017 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4018 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 4019 | better to choose a different scheme. Perhaps scan the |
| 4020 | first N-chars of the string and allocate based on that size. |
| 4021 | */ |
| 4022 | /* Initial allocation is based on the longest-possible unichr |
| 4023 | escape. |
| 4024 | |
| 4025 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 4026 | unichr, so in this case it's the longest unichr escape. In |
| 4027 | narrow (UTF-16) builds this is five chars per source unichr |
| 4028 | since there are two unichrs in the surrogate pair, so in narrow |
| 4029 | (UTF-16) builds it's not the longest unichr escape. |
| 4030 | |
| 4031 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 4032 | so in the narrow (UTF-16) build case it's the longest unichr |
| 4033 | escape. |
| 4034 | */ |
| 4035 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4036 | if (size == 0) |
| 4037 | return PyBytes_FromStringAndSize(NULL, 0); |
| 4038 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4039 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4040 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4041 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4042 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4043 | 2 |
| 4044 | + expandsize*size |
| 4045 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4046 | if (repr == NULL) |
| 4047 | return NULL; |
| 4048 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4049 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4050 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4051 | while (size-- > 0) { |
| 4052 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 4053 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 4054 | /* Escape backslashes */ |
| 4055 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4056 | *p++ = '\\'; |
| 4057 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 4058 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4059 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 4060 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 4061 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4062 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 4063 | else if (ch >= 0x10000) { |
| 4064 | *p++ = '\\'; |
| 4065 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 4066 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 4067 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 4068 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 4069 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 4070 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 4071 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 4072 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 4073 | *p++ = hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4074 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4075 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4076 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4077 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 4078 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 4079 | Py_UNICODE ch2; |
| 4080 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4081 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4082 | ch2 = *s++; |
| 4083 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 4084 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4085 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 4086 | *p++ = '\\'; |
| 4087 | *p++ = 'U'; |
| 4088 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 4089 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 4090 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 4091 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 4092 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 4093 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 4094 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 4095 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 4096 | continue; |
| 4097 | } |
| 4098 | /* Fall through: isolated surrogates are copied as-is */ |
| 4099 | s--; |
| 4100 | size++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4101 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4102 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 4103 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4104 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 4105 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4106 | *p++ = '\\'; |
| 4107 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 4108 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 4109 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 4110 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 4111 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4112 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 4113 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 4114 | /* Map special whitespace to '\t', \n', '\r' */ |
| 4115 | else if (ch == '\t') { |
| 4116 | *p++ = '\\'; |
| 4117 | *p++ = 't'; |
| 4118 | } |
| 4119 | else if (ch == '\n') { |
| 4120 | *p++ = '\\'; |
| 4121 | *p++ = 'n'; |
| 4122 | } |
| 4123 | else if (ch == '\r') { |
| 4124 | *p++ = '\\'; |
| 4125 | *p++ = 'r'; |
| 4126 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 4127 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 4128 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 4129 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4130 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 4131 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 4132 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 4133 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4134 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 4135 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4136 | /* Copy everything else as-is */ |
| 4137 | else |
| 4138 | *p++ = (char) ch; |
| 4139 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4140 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4141 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 4142 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 4143 | return NULL; |
| 4144 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4145 | } |
| 4146 | |
Alexandre Vassalotti | 2056bed | 2008-12-27 19:46:35 +0000 | [diff] [blame] | 4147 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4148 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 4149 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4150 | if (!PyUnicode_Check(unicode)) { |
| 4151 | PyErr_BadArgument(); |
| 4152 | return NULL; |
| 4153 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 4154 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 4155 | PyUnicode_GET_SIZE(unicode)); |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 4156 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4157 | } |
| 4158 | |
| 4159 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 4160 | |
| 4161 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4162 | Py_ssize_t size, |
| 4163 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4164 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4165 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4166 | Py_ssize_t startinpos; |
| 4167 | Py_ssize_t endinpos; |
| 4168 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4169 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4170 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4171 | const char *end; |
| 4172 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4173 | PyObject *errorHandler = NULL; |
| 4174 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4175 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4176 | /* Escaped strings will always be longer than the resulting |
| 4177 | 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] | 4178 | length after conversion to the true value. (But decoding error |
| 4179 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4180 | v = _PyUnicode_New(size); |
| 4181 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4182 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4183 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4184 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4185 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4186 | end = s + size; |
| 4187 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4188 | unsigned char c; |
| 4189 | Py_UCS4 x; |
| 4190 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4191 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4192 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4193 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 4194 | if (*s != '\\') { |
| 4195 | *p++ = (unsigned char)*s++; |
| 4196 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4197 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4198 | startinpos = s-starts; |
| 4199 | |
| 4200 | /* \u-escapes are only interpreted iff the number of leading |
| 4201 | backslashes if odd */ |
| 4202 | bs = s; |
| 4203 | for (;s < end;) { |
| 4204 | if (*s != '\\') |
| 4205 | break; |
| 4206 | *p++ = (unsigned char)*s++; |
| 4207 | } |
| 4208 | if (((s - bs) & 1) == 0 || |
| 4209 | s >= end || |
| 4210 | (*s != 'u' && *s != 'U')) { |
| 4211 | continue; |
| 4212 | } |
| 4213 | p--; |
| 4214 | count = *s=='u' ? 4 : 8; |
| 4215 | s++; |
| 4216 | |
| 4217 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
| 4218 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4219 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 4220 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 4221 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4222 | endinpos = s-starts; |
| 4223 | if (unicode_decode_call_errorhandler( |
| 4224 | errors, &errorHandler, |
| 4225 | "rawunicodeescape", "truncated \\uXXXX", |
| 4226 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 4227 | &v, &outpos, &p)) |
| 4228 | goto onError; |
| 4229 | goto nextByte; |
| 4230 | } |
| 4231 | x = (x<<4) & ~0xF; |
| 4232 | if (c >= '0' && c <= '9') |
| 4233 | x += c - '0'; |
| 4234 | else if (c >= 'a' && c <= 'f') |
| 4235 | x += 10 + c - 'a'; |
| 4236 | else |
| 4237 | x += 10 + c - 'A'; |
| 4238 | } |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4239 | if (x <= 0xffff) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4240 | /* UCS-2 character */ |
| 4241 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4242 | else if (x <= 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4243 | /* UCS-4 character. Either store directly, or as |
| 4244 | surrogate pair. */ |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4245 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4246 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4247 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4248 | x -= 0x10000L; |
| 4249 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 4250 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4251 | #endif |
| 4252 | } else { |
| 4253 | endinpos = s-starts; |
| 4254 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4255 | if (unicode_decode_call_errorhandler( |
| 4256 | errors, &errorHandler, |
| 4257 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4258 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 4259 | &v, &outpos, &p)) |
| 4260 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4261 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4262 | nextByte: |
| 4263 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4264 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4265 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4266 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4267 | Py_XDECREF(errorHandler); |
| 4268 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4269 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4270 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4271 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4272 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4273 | Py_XDECREF(errorHandler); |
| 4274 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4275 | return NULL; |
| 4276 | } |
| 4277 | |
| 4278 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4279 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4280 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4281 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4282 | char *p; |
| 4283 | char *q; |
| 4284 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4285 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4286 | const Py_ssize_t expandsize = 10; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4287 | #else |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4288 | const Py_ssize_t expandsize = 6; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4289 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4290 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4291 | if (size > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4292 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4293 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4294 | repr = PyBytes_FromStringAndSize(NULL, expandsize * size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4295 | if (repr == NULL) |
| 4296 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 4297 | if (size == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4298 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4299 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4300 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4301 | while (size-- > 0) { |
| 4302 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4303 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4304 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 4305 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4306 | *p++ = '\\'; |
| 4307 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 4308 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 4309 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 4310 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 4311 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 4312 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 4313 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 4314 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 4315 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4316 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4317 | else |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4318 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4319 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 4320 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 4321 | Py_UNICODE ch2; |
| 4322 | Py_UCS4 ucs; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4323 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4324 | ch2 = *s++; |
| 4325 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 4326 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4327 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 4328 | *p++ = '\\'; |
| 4329 | *p++ = 'U'; |
| 4330 | *p++ = hexdigits[(ucs >> 28) & 0xf]; |
| 4331 | *p++ = hexdigits[(ucs >> 24) & 0xf]; |
| 4332 | *p++ = hexdigits[(ucs >> 20) & 0xf]; |
| 4333 | *p++ = hexdigits[(ucs >> 16) & 0xf]; |
| 4334 | *p++ = hexdigits[(ucs >> 12) & 0xf]; |
| 4335 | *p++ = hexdigits[(ucs >> 8) & 0xf]; |
| 4336 | *p++ = hexdigits[(ucs >> 4) & 0xf]; |
| 4337 | *p++ = hexdigits[ucs & 0xf]; |
| 4338 | continue; |
| 4339 | } |
| 4340 | /* Fall through: isolated surrogates are copied as-is */ |
| 4341 | s--; |
| 4342 | size++; |
| 4343 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4344 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4345 | /* Map 16-bit characters to '\uxxxx' */ |
| 4346 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4347 | *p++ = '\\'; |
| 4348 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 4349 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 4350 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 4351 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 4352 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4353 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4354 | /* Copy everything else as-is */ |
| 4355 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4356 | *p++ = (char) ch; |
| 4357 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4358 | size = p - q; |
| 4359 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4360 | assert(size > 0); |
| 4361 | if (_PyBytes_Resize(&repr, size) < 0) |
| 4362 | return NULL; |
| 4363 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4364 | } |
| 4365 | |
| 4366 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 4367 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 4368 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4369 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 4370 | PyErr_BadArgument(); |
| 4371 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4372 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 4373 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 4374 | PyUnicode_GET_SIZE(unicode)); |
| 4375 | |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 4376 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4377 | } |
| 4378 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4379 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 4380 | |
| 4381 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4382 | Py_ssize_t size, |
| 4383 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4384 | { |
| 4385 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4386 | Py_ssize_t startinpos; |
| 4387 | Py_ssize_t endinpos; |
| 4388 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4389 | PyUnicodeObject *v; |
| 4390 | Py_UNICODE *p; |
| 4391 | const char *end; |
| 4392 | const char *reason; |
| 4393 | PyObject *errorHandler = NULL; |
| 4394 | PyObject *exc = NULL; |
| 4395 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 4396 | #ifdef Py_UNICODE_WIDE |
| 4397 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 4398 | #endif |
| 4399 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4400 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4401 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 4402 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4403 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4404 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4405 | return (PyObject *)v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4406 | p = PyUnicode_AS_UNICODE(v); |
| 4407 | end = s + size; |
| 4408 | |
| 4409 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4410 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4411 | /* We have to sanity check the raw data, otherwise doom looms for |
| 4412 | some malformed UCS-4 data. */ |
| 4413 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4414 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4415 | *p > unimax || *p < 0 || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4416 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4417 | end-s < Py_UNICODE_SIZE |
| 4418 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4419 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4420 | startinpos = s - starts; |
| 4421 | if (end-s < Py_UNICODE_SIZE) { |
| 4422 | endinpos = end-starts; |
| 4423 | reason = "truncated input"; |
| 4424 | } |
| 4425 | else { |
| 4426 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 4427 | reason = "illegal code point (> 0x10FFFF)"; |
| 4428 | } |
| 4429 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 4430 | if (unicode_decode_call_errorhandler( |
| 4431 | errors, &errorHandler, |
| 4432 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4433 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 4434 | &v, &outpos, &p)) { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4435 | goto onError; |
| 4436 | } |
| 4437 | } |
| 4438 | else { |
| 4439 | p++; |
| 4440 | s += Py_UNICODE_SIZE; |
| 4441 | } |
| 4442 | } |
| 4443 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4444 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4445 | goto onError; |
| 4446 | Py_XDECREF(errorHandler); |
| 4447 | Py_XDECREF(exc); |
| 4448 | return (PyObject *)v; |
| 4449 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4450 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4451 | Py_XDECREF(v); |
| 4452 | Py_XDECREF(errorHandler); |
| 4453 | Py_XDECREF(exc); |
| 4454 | return NULL; |
| 4455 | } |
| 4456 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4457 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 4458 | |
| 4459 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4460 | Py_ssize_t size, |
| 4461 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4462 | { |
| 4463 | PyUnicodeObject *v; |
| 4464 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4465 | const char *e, *unrolled_end; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4466 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4467 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4468 | if (size == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4469 | Py_UNICODE r = *(unsigned char*)s; |
| 4470 | return PyUnicode_FromUnicode(&r, 1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4471 | } |
| 4472 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4473 | v = _PyUnicode_New(size); |
| 4474 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4475 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4476 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4477 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4478 | p = PyUnicode_AS_UNICODE(v); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4479 | e = s + size; |
| 4480 | /* Unrolling the copy makes it much faster by reducing the looping |
| 4481 | overhead. This is similar to what many memcpy() implementations do. */ |
| 4482 | unrolled_end = e - 4; |
| 4483 | while (s < unrolled_end) { |
| 4484 | p[0] = (unsigned char) s[0]; |
| 4485 | p[1] = (unsigned char) s[1]; |
| 4486 | p[2] = (unsigned char) s[2]; |
| 4487 | p[3] = (unsigned char) s[3]; |
| 4488 | s += 4; |
| 4489 | p += 4; |
| 4490 | } |
| 4491 | while (s < e) |
| 4492 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4493 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4494 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4495 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4496 | Py_XDECREF(v); |
| 4497 | return NULL; |
| 4498 | } |
| 4499 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4500 | /* create or adjust a UnicodeEncodeError */ |
| 4501 | static void make_encode_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4502 | const char *encoding, |
| 4503 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4504 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4505 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4506 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4507 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4508 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 4509 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4510 | } |
| 4511 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4512 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 4513 | goto onError; |
| 4514 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 4515 | goto onError; |
| 4516 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 4517 | goto onError; |
| 4518 | return; |
| 4519 | onError: |
| 4520 | Py_DECREF(*exceptionObject); |
| 4521 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4522 | } |
| 4523 | } |
| 4524 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4525 | /* raises a UnicodeEncodeError */ |
| 4526 | static void raise_encode_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4527 | const char *encoding, |
| 4528 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4529 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4530 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4531 | { |
| 4532 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4533 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4534 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4535 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4536 | } |
| 4537 | |
| 4538 | /* error handling callback helper: |
| 4539 | build arguments, call the callback and check the arguments, |
| 4540 | put the result into newpos and return the replacement string, which |
| 4541 | has to be freed by the caller */ |
| 4542 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4543 | PyObject **errorHandler, |
| 4544 | const char *encoding, const char *reason, |
| 4545 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 4546 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4547 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4548 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4549 | 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] | 4550 | |
| 4551 | PyObject *restuple; |
| 4552 | PyObject *resunicode; |
| 4553 | |
| 4554 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4555 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4556 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4557 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4558 | } |
| 4559 | |
| 4560 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4561 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4562 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4563 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4564 | |
| 4565 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4566 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4567 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4568 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4569 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4570 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4571 | Py_DECREF(restuple); |
| 4572 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4573 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4574 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4575 | &resunicode, newpos)) { |
| 4576 | Py_DECREF(restuple); |
| 4577 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4578 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4579 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 4580 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 4581 | Py_DECREF(restuple); |
| 4582 | return NULL; |
| 4583 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4584 | if (*newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4585 | *newpos = size+*newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4586 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4587 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 4588 | Py_DECREF(restuple); |
| 4589 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4590 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4591 | Py_INCREF(resunicode); |
| 4592 | Py_DECREF(restuple); |
| 4593 | return resunicode; |
| 4594 | } |
| 4595 | |
| 4596 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4597 | Py_ssize_t size, |
| 4598 | const char *errors, |
| 4599 | int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4600 | { |
| 4601 | /* output object */ |
| 4602 | PyObject *res; |
| 4603 | /* pointers to the beginning and end+1 of input */ |
| 4604 | const Py_UNICODE *startp = p; |
| 4605 | const Py_UNICODE *endp = p + size; |
| 4606 | /* pointer to the beginning of the unencodable characters */ |
| 4607 | /* const Py_UNICODE *badp = NULL; */ |
| 4608 | /* pointer into the output */ |
| 4609 | char *str; |
| 4610 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4611 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4612 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 4613 | 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] | 4614 | PyObject *errorHandler = NULL; |
| 4615 | PyObject *exc = NULL; |
| 4616 | /* the following variable is used for caching string comparisons |
| 4617 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 4618 | int known_errorHandler = -1; |
| 4619 | |
| 4620 | /* allocate enough for a simple encoding without |
| 4621 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4622 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4623 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4624 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4625 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4626 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4627 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4628 | ressize = size; |
| 4629 | |
| 4630 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4631 | Py_UNICODE c = *p; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4632 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4633 | /* can we encode this? */ |
| 4634 | if (c<limit) { |
| 4635 | /* no overflow check, because we know that the space is enough */ |
| 4636 | *str++ = (char)c; |
| 4637 | ++p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4638 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4639 | else { |
| 4640 | Py_ssize_t unicodepos = p-startp; |
| 4641 | Py_ssize_t requiredsize; |
| 4642 | PyObject *repunicode; |
| 4643 | Py_ssize_t repsize; |
| 4644 | Py_ssize_t newpos; |
| 4645 | Py_ssize_t respos; |
| 4646 | Py_UNICODE *uni2; |
| 4647 | /* startpos for collecting unencodable chars */ |
| 4648 | const Py_UNICODE *collstart = p; |
| 4649 | const Py_UNICODE *collend = p; |
| 4650 | /* find all unecodable characters */ |
| 4651 | while ((collend < endp) && ((*collend)>=limit)) |
| 4652 | ++collend; |
| 4653 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 4654 | if (known_errorHandler==-1) { |
| 4655 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4656 | known_errorHandler = 1; |
| 4657 | else if (!strcmp(errors, "replace")) |
| 4658 | known_errorHandler = 2; |
| 4659 | else if (!strcmp(errors, "ignore")) |
| 4660 | known_errorHandler = 3; |
| 4661 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4662 | known_errorHandler = 4; |
| 4663 | else |
| 4664 | known_errorHandler = 0; |
| 4665 | } |
| 4666 | switch (known_errorHandler) { |
| 4667 | case 1: /* strict */ |
| 4668 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 4669 | goto onError; |
| 4670 | case 2: /* replace */ |
| 4671 | while (collstart++<collend) |
| 4672 | *str++ = '?'; /* fall through */ |
| 4673 | case 3: /* ignore */ |
| 4674 | p = collend; |
| 4675 | break; |
| 4676 | case 4: /* xmlcharrefreplace */ |
| 4677 | respos = str - PyBytes_AS_STRING(res); |
| 4678 | /* determine replacement size (temporarily (mis)uses p) */ |
| 4679 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 4680 | if (*p<10) |
| 4681 | repsize += 2+1+1; |
| 4682 | else if (*p<100) |
| 4683 | repsize += 2+2+1; |
| 4684 | else if (*p<1000) |
| 4685 | repsize += 2+3+1; |
| 4686 | else if (*p<10000) |
| 4687 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 4688 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4689 | else |
| 4690 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 4691 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4692 | else if (*p<100000) |
| 4693 | repsize += 2+5+1; |
| 4694 | else if (*p<1000000) |
| 4695 | repsize += 2+6+1; |
| 4696 | else |
| 4697 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4698 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4699 | } |
| 4700 | requiredsize = respos+repsize+(endp-collend); |
| 4701 | if (requiredsize > ressize) { |
| 4702 | if (requiredsize<2*ressize) |
| 4703 | requiredsize = 2*ressize; |
| 4704 | if (_PyBytes_Resize(&res, requiredsize)) |
| 4705 | goto onError; |
| 4706 | str = PyBytes_AS_STRING(res) + respos; |
| 4707 | ressize = requiredsize; |
| 4708 | } |
| 4709 | /* generate replacement (temporarily (mis)uses p) */ |
| 4710 | for (p = collstart; p < collend; ++p) { |
| 4711 | str += sprintf(str, "&#%d;", (int)*p); |
| 4712 | } |
| 4713 | p = collend; |
| 4714 | break; |
| 4715 | default: |
| 4716 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 4717 | encoding, reason, startp, size, &exc, |
| 4718 | collstart-startp, collend-startp, &newpos); |
| 4719 | if (repunicode == NULL) |
| 4720 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4721 | if (PyBytes_Check(repunicode)) { |
| 4722 | /* Directly copy bytes result to output. */ |
| 4723 | repsize = PyBytes_Size(repunicode); |
| 4724 | if (repsize > 1) { |
| 4725 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 4726 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4727 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 4728 | Py_DECREF(repunicode); |
| 4729 | goto onError; |
| 4730 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 4731 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4732 | ressize += repsize-1; |
| 4733 | } |
| 4734 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 4735 | str += repsize; |
| 4736 | p = startp + newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4737 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4738 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4739 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4740 | /* need more space? (at least enough for what we |
| 4741 | have+the replacement+the rest of the string, so |
| 4742 | we won't have to check space for encodable characters) */ |
| 4743 | respos = str - PyBytes_AS_STRING(res); |
| 4744 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4745 | requiredsize = respos+repsize+(endp-collend); |
| 4746 | if (requiredsize > ressize) { |
| 4747 | if (requiredsize<2*ressize) |
| 4748 | requiredsize = 2*ressize; |
| 4749 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 4750 | Py_DECREF(repunicode); |
| 4751 | goto onError; |
| 4752 | } |
| 4753 | str = PyBytes_AS_STRING(res) + respos; |
| 4754 | ressize = requiredsize; |
| 4755 | } |
| 4756 | /* check if there is anything unencodable in the replacement |
| 4757 | and copy it to the output */ |
| 4758 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 4759 | c = *uni2; |
| 4760 | if (c >= limit) { |
| 4761 | raise_encode_exception(&exc, encoding, startp, size, |
| 4762 | unicodepos, unicodepos+1, reason); |
| 4763 | Py_DECREF(repunicode); |
| 4764 | goto onError; |
| 4765 | } |
| 4766 | *str = (char)c; |
| 4767 | } |
| 4768 | p = startp + newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4769 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4770 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4771 | } |
| 4772 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4773 | /* Resize if we allocated to much */ |
| 4774 | size = str - PyBytes_AS_STRING(res); |
| 4775 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 4776 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4777 | if (_PyBytes_Resize(&res, size) < 0) |
| 4778 | goto onError; |
| 4779 | } |
| 4780 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4781 | Py_XDECREF(errorHandler); |
| 4782 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4783 | return res; |
| 4784 | |
| 4785 | onError: |
| 4786 | Py_XDECREF(res); |
| 4787 | Py_XDECREF(errorHandler); |
| 4788 | Py_XDECREF(exc); |
| 4789 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4790 | } |
| 4791 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4792 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4793 | Py_ssize_t size, |
| 4794 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4795 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4796 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4797 | } |
| 4798 | |
| 4799 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 4800 | { |
| 4801 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4802 | PyErr_BadArgument(); |
| 4803 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4804 | } |
| 4805 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4806 | PyUnicode_GET_SIZE(unicode), |
| 4807 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4808 | } |
| 4809 | |
| 4810 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 4811 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4812 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4813 | Py_ssize_t size, |
| 4814 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4815 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4816 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4817 | PyUnicodeObject *v; |
| 4818 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4819 | Py_ssize_t startinpos; |
| 4820 | Py_ssize_t endinpos; |
| 4821 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4822 | const char *e; |
| 4823 | PyObject *errorHandler = NULL; |
| 4824 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4825 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4826 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4827 | if (size == 1 && *(unsigned char*)s < 128) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4828 | Py_UNICODE r = *(unsigned char*)s; |
| 4829 | return PyUnicode_FromUnicode(&r, 1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4830 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4831 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4832 | v = _PyUnicode_New(size); |
| 4833 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4834 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4835 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4836 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4837 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4838 | e = s + size; |
| 4839 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4840 | register unsigned char c = (unsigned char)*s; |
| 4841 | if (c < 128) { |
| 4842 | *p++ = c; |
| 4843 | ++s; |
| 4844 | } |
| 4845 | else { |
| 4846 | startinpos = s-starts; |
| 4847 | endinpos = startinpos + 1; |
| 4848 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
| 4849 | if (unicode_decode_call_errorhandler( |
| 4850 | errors, &errorHandler, |
| 4851 | "ascii", "ordinal not in range(128)", |
| 4852 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 4853 | &v, &outpos, &p)) |
| 4854 | goto onError; |
| 4855 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4856 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 4857 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4858 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 4859 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4860 | Py_XDECREF(errorHandler); |
| 4861 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4862 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4863 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4864 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4865 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4866 | Py_XDECREF(errorHandler); |
| 4867 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4868 | return NULL; |
| 4869 | } |
| 4870 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4871 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4872 | Py_ssize_t size, |
| 4873 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4874 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4875 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4876 | } |
| 4877 | |
| 4878 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 4879 | { |
| 4880 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4881 | PyErr_BadArgument(); |
| 4882 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4883 | } |
| 4884 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4885 | PyUnicode_GET_SIZE(unicode), |
| 4886 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4887 | } |
| 4888 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4889 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4890 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4891 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4892 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 4893 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4894 | #define NEED_RETRY |
| 4895 | #endif |
| 4896 | |
| 4897 | /* XXX This code is limited to "true" double-byte encodings, as |
| 4898 | a) it assumes an incomplete character consists of a single byte, and |
| 4899 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4900 | encodings, see IsDBCSLeadByteEx documentation. */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4901 | |
| 4902 | static int is_dbcs_lead_byte(const char *s, int offset) |
| 4903 | { |
| 4904 | const char *curr = s + offset; |
| 4905 | |
| 4906 | if (IsDBCSLeadByte(*curr)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4907 | const char *prev = CharPrev(s, curr); |
| 4908 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4909 | } |
| 4910 | return 0; |
| 4911 | } |
| 4912 | |
| 4913 | /* |
| 4914 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 4915 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 4916 | */ |
| 4917 | static int decode_mbcs(PyUnicodeObject **v, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4918 | const char *s, /* MBCS string */ |
| 4919 | int size, /* sizeof MBCS string */ |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4920 | int final, |
| 4921 | const char *errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4922 | { |
| 4923 | Py_UNICODE *p; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4924 | Py_ssize_t n; |
| 4925 | DWORD usize; |
| 4926 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4927 | |
| 4928 | assert(size >= 0); |
| 4929 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4930 | /* check and handle 'errors' arg */ |
| 4931 | if (errors==NULL || strcmp(errors, "strict")==0) |
| 4932 | flags = MB_ERR_INVALID_CHARS; |
| 4933 | else if (strcmp(errors, "ignore")==0) |
| 4934 | flags = 0; |
| 4935 | else { |
| 4936 | PyErr_Format(PyExc_ValueError, |
| 4937 | "mbcs encoding does not support errors='%s'", |
| 4938 | errors); |
| 4939 | return -1; |
| 4940 | } |
| 4941 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4942 | /* Skip trailing lead-byte unless 'final' is set */ |
| 4943 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4944 | --size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4945 | |
| 4946 | /* First get the size of the result */ |
| 4947 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4948 | usize = MultiByteToWideChar(CP_ACP, flags, s, size, NULL, 0); |
| 4949 | if (usize==0) |
| 4950 | goto mbcs_decode_error; |
| 4951 | } else |
| 4952 | usize = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4953 | |
| 4954 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4955 | /* Create unicode object */ |
| 4956 | *v = _PyUnicode_New(usize); |
| 4957 | if (*v == NULL) |
| 4958 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4959 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4960 | } |
| 4961 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4962 | /* Extend unicode object */ |
| 4963 | n = PyUnicode_GET_SIZE(*v); |
| 4964 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 4965 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4966 | } |
| 4967 | |
| 4968 | /* Do the conversion */ |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4969 | if (usize > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4970 | p = PyUnicode_AS_UNICODE(*v) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4971 | if (0 == MultiByteToWideChar(CP_ACP, flags, s, size, p, usize)) { |
| 4972 | goto mbcs_decode_error; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4973 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4974 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4975 | return size; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4976 | |
| 4977 | mbcs_decode_error: |
| 4978 | /* If the last error was ERROR_NO_UNICODE_TRANSLATION, then |
| 4979 | we raise a UnicodeDecodeError - else it is a 'generic' |
| 4980 | windows error |
| 4981 | */ |
| 4982 | if (GetLastError()==ERROR_NO_UNICODE_TRANSLATION) { |
| 4983 | /* Ideally, we should get reason from FormatMessage - this |
| 4984 | is the Windows 2000 English version of the message |
| 4985 | */ |
| 4986 | PyObject *exc = NULL; |
| 4987 | const char *reason = "No mapping for the Unicode character exists " |
| 4988 | "in the target multi-byte code page."; |
| 4989 | make_decode_exception(&exc, "mbcs", s, size, 0, 0, reason); |
| 4990 | if (exc != NULL) { |
| 4991 | PyCodec_StrictErrors(exc); |
| 4992 | Py_DECREF(exc); |
| 4993 | } |
| 4994 | } else { |
| 4995 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4996 | } |
| 4997 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4998 | } |
| 4999 | |
| 5000 | PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5001 | Py_ssize_t size, |
| 5002 | const char *errors, |
| 5003 | Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5004 | { |
| 5005 | PyUnicodeObject *v = NULL; |
| 5006 | int done; |
| 5007 | |
| 5008 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5009 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5010 | |
| 5011 | #ifdef NEED_RETRY |
| 5012 | retry: |
| 5013 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 5014 | done = decode_mbcs(&v, s, INT_MAX, 0, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5015 | else |
| 5016 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 5017 | done = decode_mbcs(&v, s, (int)size, !consumed, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5018 | |
| 5019 | if (done < 0) { |
| 5020 | Py_XDECREF(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5021 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5022 | } |
| 5023 | |
| 5024 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5025 | *consumed += done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5026 | |
| 5027 | #ifdef NEED_RETRY |
| 5028 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5029 | s += done; |
| 5030 | size -= done; |
| 5031 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5032 | } |
| 5033 | #endif |
| 5034 | |
| 5035 | return (PyObject *)v; |
| 5036 | } |
| 5037 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 5038 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5039 | Py_ssize_t size, |
| 5040 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 5041 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5042 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 5043 | } |
| 5044 | |
| 5045 | /* |
| 5046 | * Convert unicode into string object (MBCS). |
| 5047 | * Returns 0 if succeed, -1 otherwise. |
| 5048 | */ |
| 5049 | static int encode_mbcs(PyObject **repr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5050 | const Py_UNICODE *p, /* unicode */ |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 5051 | int size, /* size of unicode */ |
| 5052 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5053 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 5054 | BOOL usedDefaultChar = FALSE; |
| 5055 | BOOL *pusedDefaultChar; |
| 5056 | int mbcssize; |
| 5057 | Py_ssize_t n; |
| 5058 | PyObject *exc = NULL; |
| 5059 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5060 | |
| 5061 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 5062 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 5063 | /* check and handle 'errors' arg */ |
| 5064 | if (errors==NULL || strcmp(errors, "strict")==0) { |
| 5065 | flags = WC_NO_BEST_FIT_CHARS; |
| 5066 | pusedDefaultChar = &usedDefaultChar; |
| 5067 | } else if (strcmp(errors, "replace")==0) { |
| 5068 | flags = 0; |
| 5069 | pusedDefaultChar = NULL; |
| 5070 | } else { |
| 5071 | PyErr_Format(PyExc_ValueError, |
| 5072 | "mbcs encoding does not support errors='%s'", |
| 5073 | errors); |
| 5074 | return -1; |
| 5075 | } |
| 5076 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 5077 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5078 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 5079 | mbcssize = WideCharToMultiByte(CP_ACP, flags, p, size, NULL, 0, |
| 5080 | NULL, pusedDefaultChar); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5081 | if (mbcssize == 0) { |
| 5082 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 5083 | return -1; |
| 5084 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 5085 | /* If we used a default char, then we failed! */ |
| 5086 | if (pusedDefaultChar && *pusedDefaultChar) |
| 5087 | goto mbcs_encode_error; |
| 5088 | } else { |
| 5089 | mbcssize = 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 5090 | } |
| 5091 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5092 | if (*repr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5093 | /* Create string object */ |
| 5094 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); |
| 5095 | if (*repr == NULL) |
| 5096 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 5097 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5098 | } |
| 5099 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5100 | /* Extend string object */ |
| 5101 | n = PyBytes_Size(*repr); |
| 5102 | if (_PyBytes_Resize(repr, n + mbcssize) < 0) |
| 5103 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5104 | } |
| 5105 | |
| 5106 | /* Do the conversion */ |
| 5107 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5108 | char *s = PyBytes_AS_STRING(*repr) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 5109 | if (0 == WideCharToMultiByte(CP_ACP, flags, p, size, s, mbcssize, |
| 5110 | NULL, pusedDefaultChar)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5111 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 5112 | return -1; |
| 5113 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 5114 | if (pusedDefaultChar && *pusedDefaultChar) |
| 5115 | goto mbcs_encode_error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5116 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5117 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 5118 | |
| 5119 | mbcs_encode_error: |
| 5120 | raise_encode_exception(&exc, "mbcs", p, size, 0, 0, "invalid character"); |
| 5121 | Py_XDECREF(exc); |
| 5122 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 5123 | } |
| 5124 | |
| 5125 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5126 | Py_ssize_t size, |
| 5127 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 5128 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5129 | PyObject *repr = NULL; |
| 5130 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 5131 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5132 | #ifdef NEED_RETRY |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5133 | retry: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5134 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 5135 | ret = encode_mbcs(&repr, p, INT_MAX, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5136 | else |
| 5137 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 5138 | ret = encode_mbcs(&repr, p, (int)size, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 5139 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5140 | if (ret < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5141 | Py_XDECREF(repr); |
| 5142 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 5143 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5144 | |
| 5145 | #ifdef NEED_RETRY |
| 5146 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5147 | p += INT_MAX; |
| 5148 | size -= INT_MAX; |
| 5149 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5150 | } |
| 5151 | #endif |
| 5152 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 5153 | return repr; |
| 5154 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 5155 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 5156 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 5157 | { |
| 5158 | if (!PyUnicode_Check(unicode)) { |
| 5159 | PyErr_BadArgument(); |
| 5160 | return NULL; |
| 5161 | } |
| 5162 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5163 | PyUnicode_GET_SIZE(unicode), |
| 5164 | NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 5165 | } |
| 5166 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5167 | #undef NEED_RETRY |
| 5168 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5169 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 5170 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5171 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 5172 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5173 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5174 | Py_ssize_t size, |
| 5175 | PyObject *mapping, |
| 5176 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5177 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5178 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5179 | Py_ssize_t startinpos; |
| 5180 | Py_ssize_t endinpos; |
| 5181 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5182 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5183 | PyUnicodeObject *v; |
| 5184 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5185 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5186 | PyObject *errorHandler = NULL; |
| 5187 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 5188 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5189 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5190 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5191 | /* Default to Latin-1 */ |
| 5192 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5193 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5194 | |
| 5195 | v = _PyUnicode_New(size); |
| 5196 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5197 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5198 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5199 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5200 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5201 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 5202 | if (PyUnicode_CheckExact(mapping)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5203 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 5204 | maplen = PyUnicode_GET_SIZE(mapping); |
| 5205 | while (s < e) { |
| 5206 | unsigned char ch = *s; |
| 5207 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5208 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5209 | if (ch < maplen) |
| 5210 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5211 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5212 | if (x == 0xfffe) { |
| 5213 | /* undefined mapping */ |
| 5214 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 5215 | startinpos = s-starts; |
| 5216 | endinpos = startinpos+1; |
| 5217 | if (unicode_decode_call_errorhandler( |
| 5218 | errors, &errorHandler, |
| 5219 | "charmap", "character maps to <undefined>", |
| 5220 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 5221 | &v, &outpos, &p)) { |
| 5222 | goto onError; |
| 5223 | } |
| 5224 | continue; |
| 5225 | } |
| 5226 | *p++ = x; |
| 5227 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5228 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 5229 | } |
| 5230 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5231 | while (s < e) { |
| 5232 | unsigned char ch = *s; |
| 5233 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 5234 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5235 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 5236 | w = PyLong_FromLong((long)ch); |
| 5237 | if (w == NULL) |
| 5238 | goto onError; |
| 5239 | x = PyObject_GetItem(mapping, w); |
| 5240 | Py_DECREF(w); |
| 5241 | if (x == NULL) { |
| 5242 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 5243 | /* No mapping found means: mapping is undefined. */ |
| 5244 | PyErr_Clear(); |
| 5245 | x = Py_None; |
| 5246 | Py_INCREF(x); |
| 5247 | } else |
| 5248 | goto onError; |
| 5249 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5250 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5251 | /* Apply mapping */ |
| 5252 | if (PyLong_Check(x)) { |
| 5253 | long value = PyLong_AS_LONG(x); |
| 5254 | if (value < 0 || value > 65535) { |
| 5255 | PyErr_SetString(PyExc_TypeError, |
| 5256 | "character mapping must be in range(65536)"); |
| 5257 | Py_DECREF(x); |
| 5258 | goto onError; |
| 5259 | } |
| 5260 | *p++ = (Py_UNICODE)value; |
| 5261 | } |
| 5262 | else if (x == Py_None) { |
| 5263 | /* undefined mapping */ |
| 5264 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 5265 | startinpos = s-starts; |
| 5266 | endinpos = startinpos+1; |
| 5267 | if (unicode_decode_call_errorhandler( |
| 5268 | errors, &errorHandler, |
| 5269 | "charmap", "character maps to <undefined>", |
| 5270 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 5271 | &v, &outpos, &p)) { |
| 5272 | Py_DECREF(x); |
| 5273 | goto onError; |
| 5274 | } |
| 5275 | Py_DECREF(x); |
| 5276 | continue; |
| 5277 | } |
| 5278 | else if (PyUnicode_Check(x)) { |
| 5279 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5280 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5281 | if (targetsize == 1) |
| 5282 | /* 1-1 mapping */ |
| 5283 | *p++ = *PyUnicode_AS_UNICODE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5284 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5285 | else if (targetsize > 1) { |
| 5286 | /* 1-n mapping */ |
| 5287 | if (targetsize > extrachars) { |
| 5288 | /* resize first */ |
| 5289 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 5290 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 5291 | (targetsize << 2); |
| 5292 | extrachars += needed; |
| 5293 | /* XXX overflow detection missing */ |
| 5294 | if (_PyUnicode_Resize(&v, |
| 5295 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 5296 | Py_DECREF(x); |
| 5297 | goto onError; |
| 5298 | } |
| 5299 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 5300 | } |
| 5301 | Py_UNICODE_COPY(p, |
| 5302 | PyUnicode_AS_UNICODE(x), |
| 5303 | targetsize); |
| 5304 | p += targetsize; |
| 5305 | extrachars -= targetsize; |
| 5306 | } |
| 5307 | /* 1-0 mapping: skip the character */ |
| 5308 | } |
| 5309 | else { |
| 5310 | /* wrong return value */ |
| 5311 | PyErr_SetString(PyExc_TypeError, |
| 5312 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5313 | Py_DECREF(x); |
| 5314 | goto onError; |
| 5315 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5316 | Py_DECREF(x); |
| 5317 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5318 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5319 | } |
| 5320 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5321 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 5322 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5323 | Py_XDECREF(errorHandler); |
| 5324 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5325 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5326 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5327 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5328 | Py_XDECREF(errorHandler); |
| 5329 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5330 | Py_XDECREF(v); |
| 5331 | return NULL; |
| 5332 | } |
| 5333 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5334 | /* Charmap encoding: the lookup table */ |
| 5335 | |
| 5336 | struct encoding_map{ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5337 | PyObject_HEAD |
| 5338 | unsigned char level1[32]; |
| 5339 | int count2, count3; |
| 5340 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5341 | }; |
| 5342 | |
| 5343 | static PyObject* |
| 5344 | encoding_map_size(PyObject *obj, PyObject* args) |
| 5345 | { |
| 5346 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5347 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5348 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5349 | } |
| 5350 | |
| 5351 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5352 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5353 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 5354 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5355 | }; |
| 5356 | |
| 5357 | static void |
| 5358 | encoding_map_dealloc(PyObject* o) |
| 5359 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5360 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5361 | } |
| 5362 | |
| 5363 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5364 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5365 | "EncodingMap", /*tp_name*/ |
| 5366 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 5367 | 0, /*tp_itemsize*/ |
| 5368 | /* methods */ |
| 5369 | encoding_map_dealloc, /*tp_dealloc*/ |
| 5370 | 0, /*tp_print*/ |
| 5371 | 0, /*tp_getattr*/ |
| 5372 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 5373 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5374 | 0, /*tp_repr*/ |
| 5375 | 0, /*tp_as_number*/ |
| 5376 | 0, /*tp_as_sequence*/ |
| 5377 | 0, /*tp_as_mapping*/ |
| 5378 | 0, /*tp_hash*/ |
| 5379 | 0, /*tp_call*/ |
| 5380 | 0, /*tp_str*/ |
| 5381 | 0, /*tp_getattro*/ |
| 5382 | 0, /*tp_setattro*/ |
| 5383 | 0, /*tp_as_buffer*/ |
| 5384 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 5385 | 0, /*tp_doc*/ |
| 5386 | 0, /*tp_traverse*/ |
| 5387 | 0, /*tp_clear*/ |
| 5388 | 0, /*tp_richcompare*/ |
| 5389 | 0, /*tp_weaklistoffset*/ |
| 5390 | 0, /*tp_iter*/ |
| 5391 | 0, /*tp_iternext*/ |
| 5392 | encoding_map_methods, /*tp_methods*/ |
| 5393 | 0, /*tp_members*/ |
| 5394 | 0, /*tp_getset*/ |
| 5395 | 0, /*tp_base*/ |
| 5396 | 0, /*tp_dict*/ |
| 5397 | 0, /*tp_descr_get*/ |
| 5398 | 0, /*tp_descr_set*/ |
| 5399 | 0, /*tp_dictoffset*/ |
| 5400 | 0, /*tp_init*/ |
| 5401 | 0, /*tp_alloc*/ |
| 5402 | 0, /*tp_new*/ |
| 5403 | 0, /*tp_free*/ |
| 5404 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5405 | }; |
| 5406 | |
| 5407 | PyObject* |
| 5408 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 5409 | { |
| 5410 | Py_UNICODE *decode; |
| 5411 | PyObject *result; |
| 5412 | struct encoding_map *mresult; |
| 5413 | int i; |
| 5414 | int need_dict = 0; |
| 5415 | unsigned char level1[32]; |
| 5416 | unsigned char level2[512]; |
| 5417 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 5418 | int count2 = 0, count3 = 0; |
| 5419 | |
| 5420 | if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { |
| 5421 | PyErr_BadArgument(); |
| 5422 | return NULL; |
| 5423 | } |
| 5424 | decode = PyUnicode_AS_UNICODE(string); |
| 5425 | memset(level1, 0xFF, sizeof level1); |
| 5426 | memset(level2, 0xFF, sizeof level2); |
| 5427 | |
| 5428 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 5429 | or if there are non-BMP characters, we need to use |
| 5430 | a mapping dictionary. */ |
| 5431 | if (decode[0] != 0) |
| 5432 | need_dict = 1; |
| 5433 | for (i = 1; i < 256; i++) { |
| 5434 | int l1, l2; |
| 5435 | if (decode[i] == 0 |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5436 | #ifdef Py_UNICODE_WIDE |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5437 | || decode[i] > 0xFFFF |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5438 | #endif |
| 5439 | ) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5440 | need_dict = 1; |
| 5441 | break; |
| 5442 | } |
| 5443 | if (decode[i] == 0xFFFE) |
| 5444 | /* unmapped character */ |
| 5445 | continue; |
| 5446 | l1 = decode[i] >> 11; |
| 5447 | l2 = decode[i] >> 7; |
| 5448 | if (level1[l1] == 0xFF) |
| 5449 | level1[l1] = count2++; |
| 5450 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5451 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5452 | } |
| 5453 | |
| 5454 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 5455 | need_dict = 1; |
| 5456 | |
| 5457 | if (need_dict) { |
| 5458 | PyObject *result = PyDict_New(); |
| 5459 | PyObject *key, *value; |
| 5460 | if (!result) |
| 5461 | return NULL; |
| 5462 | for (i = 0; i < 256; i++) { |
| 5463 | key = value = NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5464 | key = PyLong_FromLong(decode[i]); |
| 5465 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5466 | if (!key || !value) |
| 5467 | goto failed1; |
| 5468 | if (PyDict_SetItem(result, key, value) == -1) |
| 5469 | goto failed1; |
| 5470 | Py_DECREF(key); |
| 5471 | Py_DECREF(value); |
| 5472 | } |
| 5473 | return result; |
| 5474 | failed1: |
| 5475 | Py_XDECREF(key); |
| 5476 | Py_XDECREF(value); |
| 5477 | Py_DECREF(result); |
| 5478 | return NULL; |
| 5479 | } |
| 5480 | |
| 5481 | /* Create a three-level trie */ |
| 5482 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 5483 | 16*count2 + 128*count3 - 1); |
| 5484 | if (!result) |
| 5485 | return PyErr_NoMemory(); |
| 5486 | PyObject_Init(result, &EncodingMapType); |
| 5487 | mresult = (struct encoding_map*)result; |
| 5488 | mresult->count2 = count2; |
| 5489 | mresult->count3 = count3; |
| 5490 | mlevel1 = mresult->level1; |
| 5491 | mlevel2 = mresult->level23; |
| 5492 | mlevel3 = mresult->level23 + 16*count2; |
| 5493 | memcpy(mlevel1, level1, 32); |
| 5494 | memset(mlevel2, 0xFF, 16*count2); |
| 5495 | memset(mlevel3, 0, 128*count3); |
| 5496 | count3 = 0; |
| 5497 | for (i = 1; i < 256; i++) { |
| 5498 | int o1, o2, o3, i2, i3; |
| 5499 | if (decode[i] == 0xFFFE) |
| 5500 | /* unmapped character */ |
| 5501 | continue; |
| 5502 | o1 = decode[i]>>11; |
| 5503 | o2 = (decode[i]>>7) & 0xF; |
| 5504 | i2 = 16*mlevel1[o1] + o2; |
| 5505 | if (mlevel2[i2] == 0xFF) |
| 5506 | mlevel2[i2] = count3++; |
| 5507 | o3 = decode[i] & 0x7F; |
| 5508 | i3 = 128*mlevel2[i2] + o3; |
| 5509 | mlevel3[i3] = i; |
| 5510 | } |
| 5511 | return result; |
| 5512 | } |
| 5513 | |
| 5514 | static int |
| 5515 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 5516 | { |
| 5517 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 5518 | int l1 = c>>11; |
| 5519 | int l2 = (c>>7) & 0xF; |
| 5520 | int l3 = c & 0x7F; |
| 5521 | int i; |
| 5522 | |
| 5523 | #ifdef Py_UNICODE_WIDE |
| 5524 | if (c > 0xFFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5525 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5526 | } |
| 5527 | #endif |
| 5528 | if (c == 0) |
| 5529 | return 0; |
| 5530 | /* level 1*/ |
| 5531 | i = map->level1[l1]; |
| 5532 | if (i == 0xFF) { |
| 5533 | return -1; |
| 5534 | } |
| 5535 | /* level 2*/ |
| 5536 | i = map->level23[16*i+l2]; |
| 5537 | if (i == 0xFF) { |
| 5538 | return -1; |
| 5539 | } |
| 5540 | /* level 3 */ |
| 5541 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 5542 | if (i == 0) { |
| 5543 | return -1; |
| 5544 | } |
| 5545 | return i; |
| 5546 | } |
| 5547 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5548 | /* Lookup the character ch in the mapping. If the character |
| 5549 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 5550 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5551 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5552 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5553 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5554 | PyObject *x; |
| 5555 | |
| 5556 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5557 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5558 | x = PyObject_GetItem(mapping, w); |
| 5559 | Py_DECREF(w); |
| 5560 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5561 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 5562 | /* No mapping found means: mapping is undefined. */ |
| 5563 | PyErr_Clear(); |
| 5564 | x = Py_None; |
| 5565 | Py_INCREF(x); |
| 5566 | return x; |
| 5567 | } else |
| 5568 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5569 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 5570 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5571 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5572 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5573 | long value = PyLong_AS_LONG(x); |
| 5574 | if (value < 0 || value > 255) { |
| 5575 | PyErr_SetString(PyExc_TypeError, |
| 5576 | "character mapping must be in range(256)"); |
| 5577 | Py_DECREF(x); |
| 5578 | return NULL; |
| 5579 | } |
| 5580 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5581 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5582 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5583 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5584 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5585 | /* wrong return value */ |
| 5586 | PyErr_Format(PyExc_TypeError, |
| 5587 | "character mapping must return integer, bytes or None, not %.400s", |
| 5588 | x->ob_type->tp_name); |
| 5589 | Py_DECREF(x); |
| 5590 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5591 | } |
| 5592 | } |
| 5593 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5594 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5595 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5596 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5597 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 5598 | /* exponentially overallocate to minimize reallocations */ |
| 5599 | if (requiredsize < 2*outsize) |
| 5600 | requiredsize = 2*outsize; |
| 5601 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 5602 | return -1; |
| 5603 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5604 | } |
| 5605 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5606 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5607 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5608 | }charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5609 | /* 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] | 5610 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5611 | space is available. Return a new reference to the object that |
| 5612 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 5613 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 5614 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5615 | static |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5616 | charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5617 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5618 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5619 | PyObject *rep; |
| 5620 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5621 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5622 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5623 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5624 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5625 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5626 | if (res == -1) |
| 5627 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5628 | if (outsize<requiredsize) |
| 5629 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 5630 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5631 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5632 | outstart[(*outpos)++] = (char)res; |
| 5633 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5634 | } |
| 5635 | |
| 5636 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5637 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5638 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5639 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5640 | Py_DECREF(rep); |
| 5641 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5642 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5643 | if (PyLong_Check(rep)) { |
| 5644 | Py_ssize_t requiredsize = *outpos+1; |
| 5645 | if (outsize<requiredsize) |
| 5646 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 5647 | Py_DECREF(rep); |
| 5648 | return enc_EXCEPTION; |
| 5649 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5650 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5651 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5652 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5653 | else { |
| 5654 | const char *repchars = PyBytes_AS_STRING(rep); |
| 5655 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 5656 | Py_ssize_t requiredsize = *outpos+repsize; |
| 5657 | if (outsize<requiredsize) |
| 5658 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 5659 | Py_DECREF(rep); |
| 5660 | return enc_EXCEPTION; |
| 5661 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5662 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5663 | memcpy(outstart + *outpos, repchars, repsize); |
| 5664 | *outpos += repsize; |
| 5665 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5666 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5667 | Py_DECREF(rep); |
| 5668 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5669 | } |
| 5670 | |
| 5671 | /* handle an error in PyUnicode_EncodeCharmap |
| 5672 | Return 0 on success, -1 on error */ |
| 5673 | static |
| 5674 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5675 | 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] | 5676 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 5677 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5678 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5679 | { |
| 5680 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5681 | Py_ssize_t repsize; |
| 5682 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5683 | Py_UNICODE *uni2; |
| 5684 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5685 | Py_ssize_t collstartpos = *inpos; |
| 5686 | Py_ssize_t collendpos = *inpos+1; |
| 5687 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5688 | char *encoding = "charmap"; |
| 5689 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5690 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5691 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5692 | /* find all unencodable characters */ |
| 5693 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5694 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5695 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5696 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 5697 | if (res != -1) |
| 5698 | break; |
| 5699 | ++collendpos; |
| 5700 | continue; |
| 5701 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5702 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5703 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 5704 | if (rep==NULL) |
| 5705 | return -1; |
| 5706 | else if (rep!=Py_None) { |
| 5707 | Py_DECREF(rep); |
| 5708 | break; |
| 5709 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5710 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5711 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5712 | } |
| 5713 | /* cache callback name lookup |
| 5714 | * (if not done yet, i.e. it's the first error) */ |
| 5715 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5716 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5717 | *known_errorHandler = 1; |
| 5718 | else if (!strcmp(errors, "replace")) |
| 5719 | *known_errorHandler = 2; |
| 5720 | else if (!strcmp(errors, "ignore")) |
| 5721 | *known_errorHandler = 3; |
| 5722 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5723 | *known_errorHandler = 4; |
| 5724 | else |
| 5725 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5726 | } |
| 5727 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5728 | case 1: /* strict */ |
| 5729 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5730 | return -1; |
| 5731 | case 2: /* replace */ |
| 5732 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5733 | x = charmapencode_output('?', mapping, res, respos); |
| 5734 | if (x==enc_EXCEPTION) { |
| 5735 | return -1; |
| 5736 | } |
| 5737 | else if (x==enc_FAILED) { |
| 5738 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5739 | return -1; |
| 5740 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5741 | } |
| 5742 | /* fall through */ |
| 5743 | case 3: /* ignore */ |
| 5744 | *inpos = collendpos; |
| 5745 | break; |
| 5746 | case 4: /* xmlcharrefreplace */ |
| 5747 | /* generate replacement (temporarily (mis)uses p) */ |
| 5748 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5749 | char buffer[2+29+1+1]; |
| 5750 | char *cp; |
| 5751 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 5752 | for (cp = buffer; *cp; ++cp) { |
| 5753 | x = charmapencode_output(*cp, mapping, res, respos); |
| 5754 | if (x==enc_EXCEPTION) |
| 5755 | return -1; |
| 5756 | else if (x==enc_FAILED) { |
| 5757 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5758 | return -1; |
| 5759 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5760 | } |
| 5761 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5762 | *inpos = collendpos; |
| 5763 | break; |
| 5764 | default: |
| 5765 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5766 | encoding, reason, p, size, exceptionObject, |
| 5767 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5768 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5769 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5770 | if (PyBytes_Check(repunicode)) { |
| 5771 | /* Directly copy bytes result to output. */ |
| 5772 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 5773 | Py_ssize_t requiredsize; |
| 5774 | repsize = PyBytes_Size(repunicode); |
| 5775 | requiredsize = *respos + repsize; |
| 5776 | if (requiredsize > outsize) |
| 5777 | /* Make room for all additional bytes. */ |
| 5778 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 5779 | Py_DECREF(repunicode); |
| 5780 | return -1; |
| 5781 | } |
| 5782 | memcpy(PyBytes_AsString(*res) + *respos, |
| 5783 | PyBytes_AsString(repunicode), repsize); |
| 5784 | *respos += repsize; |
| 5785 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5786 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5787 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5788 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5789 | /* generate replacement */ |
| 5790 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5791 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5792 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 5793 | if (x==enc_EXCEPTION) { |
| 5794 | return -1; |
| 5795 | } |
| 5796 | else if (x==enc_FAILED) { |
| 5797 | Py_DECREF(repunicode); |
| 5798 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5799 | return -1; |
| 5800 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5801 | } |
| 5802 | *inpos = newpos; |
| 5803 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5804 | } |
| 5805 | return 0; |
| 5806 | } |
| 5807 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5808 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5809 | Py_ssize_t size, |
| 5810 | PyObject *mapping, |
| 5811 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5812 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5813 | /* output object */ |
| 5814 | PyObject *res = NULL; |
| 5815 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5816 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5817 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5818 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5819 | PyObject *errorHandler = NULL; |
| 5820 | PyObject *exc = NULL; |
| 5821 | /* the following variable is used for caching string comparisons |
| 5822 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 5823 | * 3=ignore, 4=xmlcharrefreplace */ |
| 5824 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5825 | |
| 5826 | /* Default to Latin-1 */ |
| 5827 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5828 | return PyUnicode_EncodeLatin1(p, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5829 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5830 | /* allocate enough for a simple encoding without |
| 5831 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5832 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5833 | if (res == NULL) |
| 5834 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 5835 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5836 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5837 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5838 | while (inpos<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5839 | /* try to encode it */ |
| 5840 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 5841 | if (x==enc_EXCEPTION) /* error */ |
| 5842 | goto onError; |
| 5843 | if (x==enc_FAILED) { /* unencodable character */ |
| 5844 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 5845 | &exc, |
| 5846 | &known_errorHandler, &errorHandler, errors, |
| 5847 | &res, &respos)) { |
| 5848 | goto onError; |
| 5849 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5850 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5851 | else |
| 5852 | /* done with this character => adjust input position */ |
| 5853 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5854 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5855 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5856 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5857 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5858 | if (_PyBytes_Resize(&res, respos) < 0) |
| 5859 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5860 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5861 | Py_XDECREF(exc); |
| 5862 | Py_XDECREF(errorHandler); |
| 5863 | return res; |
| 5864 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5865 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5866 | Py_XDECREF(res); |
| 5867 | Py_XDECREF(exc); |
| 5868 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5869 | return NULL; |
| 5870 | } |
| 5871 | |
| 5872 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5873 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5874 | { |
| 5875 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5876 | PyErr_BadArgument(); |
| 5877 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5878 | } |
| 5879 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5880 | PyUnicode_GET_SIZE(unicode), |
| 5881 | mapping, |
| 5882 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5883 | } |
| 5884 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5885 | /* create or adjust a UnicodeTranslateError */ |
| 5886 | static void make_translate_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5887 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5888 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5889 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5890 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5891 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5892 | *exceptionObject = PyUnicodeTranslateError_Create( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5893 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5894 | } |
| 5895 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5896 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 5897 | goto onError; |
| 5898 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 5899 | goto onError; |
| 5900 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 5901 | goto onError; |
| 5902 | return; |
| 5903 | onError: |
| 5904 | Py_DECREF(*exceptionObject); |
| 5905 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5906 | } |
| 5907 | } |
| 5908 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5909 | /* raises a UnicodeTranslateError */ |
| 5910 | static void raise_translate_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5911 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5912 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5913 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5914 | { |
| 5915 | make_translate_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5916 | unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5917 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5918 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5919 | } |
| 5920 | |
| 5921 | /* error handling callback helper: |
| 5922 | build arguments, call the callback and check the arguments, |
| 5923 | put the result into newpos and return the replacement string, which |
| 5924 | has to be freed by the caller */ |
| 5925 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5926 | PyObject **errorHandler, |
| 5927 | const char *reason, |
| 5928 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 5929 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5930 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5931 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 5932 | 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] | 5933 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5934 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5935 | PyObject *restuple; |
| 5936 | PyObject *resunicode; |
| 5937 | |
| 5938 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5939 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5940 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5941 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5942 | } |
| 5943 | |
| 5944 | make_translate_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5945 | unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5946 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5947 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5948 | |
| 5949 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5950 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5951 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5952 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5953 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 5954 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5955 | Py_DECREF(restuple); |
| 5956 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5957 | } |
| 5958 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5959 | &resunicode, &i_newpos)) { |
| 5960 | Py_DECREF(restuple); |
| 5961 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5962 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5963 | if (i_newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5964 | *newpos = size+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5965 | else |
| 5966 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5967 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5968 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 5969 | Py_DECREF(restuple); |
| 5970 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5971 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5972 | Py_INCREF(resunicode); |
| 5973 | Py_DECREF(restuple); |
| 5974 | return resunicode; |
| 5975 | } |
| 5976 | |
| 5977 | /* Lookup the character ch in the mapping and put the result in result, |
| 5978 | which must be decrefed by the caller. |
| 5979 | Return 0 on success, -1 on error */ |
| 5980 | static |
| 5981 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 5982 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5983 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5984 | PyObject *x; |
| 5985 | |
| 5986 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5987 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5988 | x = PyObject_GetItem(mapping, w); |
| 5989 | Py_DECREF(w); |
| 5990 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5991 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 5992 | /* No mapping found means: use 1:1 mapping. */ |
| 5993 | PyErr_Clear(); |
| 5994 | *result = NULL; |
| 5995 | return 0; |
| 5996 | } else |
| 5997 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5998 | } |
| 5999 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6000 | *result = x; |
| 6001 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6002 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6003 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6004 | long value = PyLong_AS_LONG(x); |
| 6005 | long max = PyUnicode_GetMax(); |
| 6006 | if (value < 0 || value > max) { |
| 6007 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 6008 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6009 | Py_DECREF(x); |
| 6010 | return -1; |
| 6011 | } |
| 6012 | *result = x; |
| 6013 | return 0; |
| 6014 | } |
| 6015 | else if (PyUnicode_Check(x)) { |
| 6016 | *result = x; |
| 6017 | return 0; |
| 6018 | } |
| 6019 | else { |
| 6020 | /* wrong return value */ |
| 6021 | PyErr_SetString(PyExc_TypeError, |
| 6022 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6023 | Py_DECREF(x); |
| 6024 | return -1; |
| 6025 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6026 | } |
| 6027 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6028 | if not reallocate and adjust various state variables. |
| 6029 | Return 0 on success, -1 on error */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6030 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 6031 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6032 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6033 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6034 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 6035 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6036 | /* remember old output position */ |
| 6037 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
| 6038 | /* exponentially overallocate to minimize reallocations */ |
| 6039 | if (requiredsize < 2 * oldsize) |
| 6040 | requiredsize = 2 * oldsize; |
| 6041 | if (PyUnicode_Resize(outobj, requiredsize) < 0) |
| 6042 | return -1; |
| 6043 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6044 | } |
| 6045 | return 0; |
| 6046 | } |
| 6047 | /* lookup the character, put the result in the output string and adjust |
| 6048 | various state variables. Return a new reference to the object that |
| 6049 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 6050 | undefined (in which case no character was written). |
| 6051 | The called must decref result. |
| 6052 | Return 0 on success, -1 on error. */ |
| 6053 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 6054 | int charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6055 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
| 6056 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6057 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 6058 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6059 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6060 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6061 | /* not found => default to 1:1 mapping */ |
| 6062 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6063 | } |
| 6064 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6065 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6066 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6067 | /* no overflow check, because we know that the space is enough */ |
| 6068 | *(*outp)++ = (Py_UNICODE)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6069 | } |
| 6070 | else if (PyUnicode_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6071 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
| 6072 | if (repsize==1) { |
| 6073 | /* no overflow check, because we know that the space is enough */ |
| 6074 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 6075 | } |
| 6076 | else if (repsize!=0) { |
| 6077 | /* more than one character */ |
| 6078 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
| 6079 | (insize - (curinp-startinp)) + |
| 6080 | repsize - 1; |
| 6081 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
| 6082 | return -1; |
| 6083 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 6084 | *outp += repsize; |
| 6085 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6086 | } |
| 6087 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6088 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6089 | return 0; |
| 6090 | } |
| 6091 | |
| 6092 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6093 | Py_ssize_t size, |
| 6094 | PyObject *mapping, |
| 6095 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6096 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6097 | /* output object */ |
| 6098 | PyObject *res = NULL; |
| 6099 | /* pointers to the beginning and end+1 of input */ |
| 6100 | const Py_UNICODE *startp = p; |
| 6101 | const Py_UNICODE *endp = p + size; |
| 6102 | /* pointer into the output */ |
| 6103 | Py_UNICODE *str; |
| 6104 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6105 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6106 | char *reason = "character maps to <undefined>"; |
| 6107 | PyObject *errorHandler = NULL; |
| 6108 | PyObject *exc = NULL; |
| 6109 | /* the following variable is used for caching string comparisons |
| 6110 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 6111 | * 3=ignore, 4=xmlcharrefreplace */ |
| 6112 | int known_errorHandler = -1; |
| 6113 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6114 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6115 | PyErr_BadArgument(); |
| 6116 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6117 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6118 | |
| 6119 | /* allocate enough for a simple 1:1 translation without |
| 6120 | replacements, if we need more, we'll resize */ |
| 6121 | res = PyUnicode_FromUnicode(NULL, size); |
| 6122 | if (res == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6123 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6124 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6125 | return res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6126 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6127 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6128 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6129 | /* try to encode it */ |
| 6130 | PyObject *x = NULL; |
| 6131 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
| 6132 | Py_XDECREF(x); |
| 6133 | goto onError; |
| 6134 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6135 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6136 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 6137 | ++p; |
| 6138 | else { /* untranslatable character */ |
| 6139 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 6140 | Py_ssize_t repsize; |
| 6141 | Py_ssize_t newpos; |
| 6142 | Py_UNICODE *uni2; |
| 6143 | /* startpos for collecting untranslatable chars */ |
| 6144 | const Py_UNICODE *collstart = p; |
| 6145 | const Py_UNICODE *collend = p+1; |
| 6146 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6147 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6148 | /* find all untranslatable characters */ |
| 6149 | while (collend < endp) { |
| 6150 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
| 6151 | goto onError; |
| 6152 | Py_XDECREF(x); |
| 6153 | if (x!=Py_None) |
| 6154 | break; |
| 6155 | ++collend; |
| 6156 | } |
| 6157 | /* cache callback name lookup |
| 6158 | * (if not done yet, i.e. it's the first error) */ |
| 6159 | if (known_errorHandler==-1) { |
| 6160 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6161 | known_errorHandler = 1; |
| 6162 | else if (!strcmp(errors, "replace")) |
| 6163 | known_errorHandler = 2; |
| 6164 | else if (!strcmp(errors, "ignore")) |
| 6165 | known_errorHandler = 3; |
| 6166 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6167 | known_errorHandler = 4; |
| 6168 | else |
| 6169 | known_errorHandler = 0; |
| 6170 | } |
| 6171 | switch (known_errorHandler) { |
| 6172 | case 1: /* strict */ |
| 6173 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6174 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6175 | case 2: /* replace */ |
| 6176 | /* No need to check for space, this is a 1:1 replacement */ |
| 6177 | for (coll = collstart; coll<collend; ++coll) |
| 6178 | *str++ = '?'; |
| 6179 | /* fall through */ |
| 6180 | case 3: /* ignore */ |
| 6181 | p = collend; |
| 6182 | break; |
| 6183 | case 4: /* xmlcharrefreplace */ |
| 6184 | /* generate replacement (temporarily (mis)uses p) */ |
| 6185 | for (p = collstart; p < collend; ++p) { |
| 6186 | char buffer[2+29+1+1]; |
| 6187 | char *cp; |
| 6188 | sprintf(buffer, "&#%d;", (int)*p); |
| 6189 | if (charmaptranslate_makespace(&res, &str, |
| 6190 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 6191 | goto onError; |
| 6192 | for (cp = buffer; *cp; ++cp) |
| 6193 | *str++ = *cp; |
| 6194 | } |
| 6195 | p = collend; |
| 6196 | break; |
| 6197 | default: |
| 6198 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 6199 | reason, startp, size, &exc, |
| 6200 | collstart-startp, collend-startp, &newpos); |
| 6201 | if (repunicode == NULL) |
| 6202 | goto onError; |
| 6203 | /* generate replacement */ |
| 6204 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 6205 | if (charmaptranslate_makespace(&res, &str, |
| 6206 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 6207 | Py_DECREF(repunicode); |
| 6208 | goto onError; |
| 6209 | } |
| 6210 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 6211 | *str++ = *uni2; |
| 6212 | p = startp + newpos; |
| 6213 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6214 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6215 | } |
| 6216 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6217 | /* Resize if we allocated to much */ |
| 6218 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 6219 | if (respos<PyUnicode_GET_SIZE(res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6220 | if (PyUnicode_Resize(&res, respos) < 0) |
| 6221 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6222 | } |
| 6223 | Py_XDECREF(exc); |
| 6224 | Py_XDECREF(errorHandler); |
| 6225 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6226 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6227 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6228 | Py_XDECREF(res); |
| 6229 | Py_XDECREF(exc); |
| 6230 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6231 | return NULL; |
| 6232 | } |
| 6233 | |
| 6234 | PyObject *PyUnicode_Translate(PyObject *str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6235 | PyObject *mapping, |
| 6236 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6237 | { |
| 6238 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6239 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6240 | str = PyUnicode_FromObject(str); |
| 6241 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6242 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6243 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6244 | PyUnicode_GET_SIZE(str), |
| 6245 | mapping, |
| 6246 | errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6247 | Py_DECREF(str); |
| 6248 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6249 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6250 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6251 | Py_XDECREF(str); |
| 6252 | return NULL; |
| 6253 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6254 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 6255 | PyObject * |
| 6256 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 6257 | Py_ssize_t length) |
| 6258 | { |
| 6259 | PyObject *result; |
| 6260 | Py_UNICODE *p; /* write pointer into result */ |
| 6261 | Py_ssize_t i; |
| 6262 | /* Copy to a new string */ |
| 6263 | result = (PyObject *)_PyUnicode_New(length); |
| 6264 | Py_UNICODE_COPY(PyUnicode_AS_UNICODE(result), s, length); |
| 6265 | if (result == NULL) |
| 6266 | return result; |
| 6267 | p = PyUnicode_AS_UNICODE(result); |
| 6268 | /* Iterate over code points */ |
| 6269 | for (i = 0; i < length; i++) { |
| 6270 | Py_UNICODE ch =s[i]; |
| 6271 | if (ch > 127) { |
| 6272 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 6273 | if (decimal >= 0) |
| 6274 | p[i] = '0' + decimal; |
| 6275 | } |
| 6276 | } |
| 6277 | return result; |
| 6278 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6279 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 6280 | |
| 6281 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6282 | Py_ssize_t length, |
| 6283 | char *output, |
| 6284 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6285 | { |
| 6286 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6287 | PyObject *errorHandler = NULL; |
| 6288 | PyObject *exc = NULL; |
| 6289 | const char *encoding = "decimal"; |
| 6290 | const char *reason = "invalid decimal Unicode string"; |
| 6291 | /* the following variable is used for caching string comparisons |
| 6292 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6293 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6294 | |
| 6295 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6296 | PyErr_BadArgument(); |
| 6297 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6298 | } |
| 6299 | |
| 6300 | p = s; |
| 6301 | end = s + length; |
| 6302 | while (p < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6303 | register Py_UNICODE ch = *p; |
| 6304 | int decimal; |
| 6305 | PyObject *repunicode; |
| 6306 | Py_ssize_t repsize; |
| 6307 | Py_ssize_t newpos; |
| 6308 | Py_UNICODE *uni2; |
| 6309 | Py_UNICODE *collstart; |
| 6310 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6311 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6312 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6313 | *output++ = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6314 | ++p; |
| 6315 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6316 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6317 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 6318 | if (decimal >= 0) { |
| 6319 | *output++ = '0' + decimal; |
| 6320 | ++p; |
| 6321 | continue; |
| 6322 | } |
| 6323 | if (0 < ch && ch < 256) { |
| 6324 | *output++ = (char)ch; |
| 6325 | ++p; |
| 6326 | continue; |
| 6327 | } |
| 6328 | /* All other characters are considered unencodable */ |
| 6329 | collstart = p; |
| 6330 | collend = p+1; |
| 6331 | while (collend < end) { |
| 6332 | if ((0 < *collend && *collend < 256) || |
| 6333 | !Py_UNICODE_ISSPACE(*collend) || |
| 6334 | Py_UNICODE_TODECIMAL(*collend)) |
| 6335 | break; |
| 6336 | } |
| 6337 | /* cache callback name lookup |
| 6338 | * (if not done yet, i.e. it's the first error) */ |
| 6339 | if (known_errorHandler==-1) { |
| 6340 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6341 | known_errorHandler = 1; |
| 6342 | else if (!strcmp(errors, "replace")) |
| 6343 | known_errorHandler = 2; |
| 6344 | else if (!strcmp(errors, "ignore")) |
| 6345 | known_errorHandler = 3; |
| 6346 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6347 | known_errorHandler = 4; |
| 6348 | else |
| 6349 | known_errorHandler = 0; |
| 6350 | } |
| 6351 | switch (known_errorHandler) { |
| 6352 | case 1: /* strict */ |
| 6353 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 6354 | goto onError; |
| 6355 | case 2: /* replace */ |
| 6356 | for (p = collstart; p < collend; ++p) |
| 6357 | *output++ = '?'; |
| 6358 | /* fall through */ |
| 6359 | case 3: /* ignore */ |
| 6360 | p = collend; |
| 6361 | break; |
| 6362 | case 4: /* xmlcharrefreplace */ |
| 6363 | /* generate replacement (temporarily (mis)uses p) */ |
| 6364 | for (p = collstart; p < collend; ++p) |
| 6365 | output += sprintf(output, "&#%d;", (int)*p); |
| 6366 | p = collend; |
| 6367 | break; |
| 6368 | default: |
| 6369 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 6370 | encoding, reason, s, length, &exc, |
| 6371 | collstart-s, collend-s, &newpos); |
| 6372 | if (repunicode == NULL) |
| 6373 | goto onError; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6374 | if (!PyUnicode_Check(repunicode)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6375 | /* Byte results not supported, since they have no decimal property. */ |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6376 | PyErr_SetString(PyExc_TypeError, "error handler should return unicode"); |
| 6377 | Py_DECREF(repunicode); |
| 6378 | goto onError; |
| 6379 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6380 | /* generate replacement */ |
| 6381 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 6382 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 6383 | Py_UNICODE ch = *uni2; |
| 6384 | if (Py_UNICODE_ISSPACE(ch)) |
| 6385 | *output++ = ' '; |
| 6386 | else { |
| 6387 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 6388 | if (decimal >= 0) |
| 6389 | *output++ = '0' + decimal; |
| 6390 | else if (0 < ch && ch < 256) |
| 6391 | *output++ = (char)ch; |
| 6392 | else { |
| 6393 | Py_DECREF(repunicode); |
| 6394 | raise_encode_exception(&exc, encoding, |
| 6395 | s, length, collstart-s, collend-s, reason); |
| 6396 | goto onError; |
| 6397 | } |
| 6398 | } |
| 6399 | } |
| 6400 | p = s + newpos; |
| 6401 | Py_DECREF(repunicode); |
| 6402 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6403 | } |
| 6404 | /* 0-terminate the output string */ |
| 6405 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6406 | Py_XDECREF(exc); |
| 6407 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6408 | return 0; |
| 6409 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6410 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6411 | Py_XDECREF(exc); |
| 6412 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6413 | return -1; |
| 6414 | } |
| 6415 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6416 | /* --- Helpers ------------------------------------------------------------ */ |
| 6417 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 6418 | #include "stringlib/unicodedefs.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6419 | #include "stringlib/fastsearch.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6420 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6421 | #include "stringlib/count.h" |
| 6422 | #include "stringlib/find.h" |
| 6423 | #include "stringlib/partition.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6424 | #include "stringlib/split.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6425 | |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 6426 | #define _Py_InsertThousandsGrouping _PyUnicode_InsertThousandsGrouping |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame] | 6427 | #define _Py_InsertThousandsGroupingLocale _PyUnicode_InsertThousandsGroupingLocale |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 6428 | #include "stringlib/localeutil.h" |
| 6429 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6430 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6431 | #define ADJUST_INDICES(start, end, len) \ |
| 6432 | if (end > len) \ |
| 6433 | end = len; \ |
| 6434 | else if (end < 0) { \ |
| 6435 | end += len; \ |
| 6436 | if (end < 0) \ |
| 6437 | end = 0; \ |
| 6438 | } \ |
| 6439 | if (start < 0) { \ |
| 6440 | start += len; \ |
| 6441 | if (start < 0) \ |
| 6442 | start = 0; \ |
| 6443 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6444 | |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 6445 | /* _Py_UNICODE_NEXT is a private macro used to retrieve the character pointed |
| 6446 | * by 'ptr', possibly combining surrogate pairs on narrow builds. |
| 6447 | * 'ptr' and 'end' must be Py_UNICODE*, with 'ptr' pointing at the character |
| 6448 | * that should be returned and 'end' pointing to the end of the buffer. |
| 6449 | * ('end' is used on narrow builds to detect a lone surrogate at the |
| 6450 | * end of the buffer that should be returned unchanged.) |
| 6451 | * The ptr and end arguments should be side-effect free and ptr must an lvalue. |
| 6452 | * The type of the returned char is always Py_UCS4. |
| 6453 | * |
| 6454 | * Note: the macro advances ptr to next char, so it might have side-effects |
| 6455 | * (especially if used with other macros). |
| 6456 | */ |
| 6457 | |
| 6458 | /* helper macros used by _Py_UNICODE_NEXT */ |
| 6459 | #define _Py_UNICODE_IS_HIGH_SURROGATE(ch) (0xD800 <= ch && ch <= 0xDBFF) |
| 6460 | #define _Py_UNICODE_IS_LOW_SURROGATE(ch) (0xDC00 <= ch && ch <= 0xDFFF) |
| 6461 | /* Join two surrogate characters and return a single Py_UCS4 value. */ |
| 6462 | #define _Py_UNICODE_JOIN_SURROGATES(high, low) \ |
| 6463 | (((((Py_UCS4)(high) & 0x03FF) << 10) | \ |
| 6464 | ((Py_UCS4)(low) & 0x03FF)) + 0x10000) |
| 6465 | |
| 6466 | #ifdef Py_UNICODE_WIDE |
| 6467 | #define _Py_UNICODE_NEXT(ptr, end) *(ptr)++ |
| 6468 | #else |
| 6469 | #define _Py_UNICODE_NEXT(ptr, end) \ |
| 6470 | (((_Py_UNICODE_IS_HIGH_SURROGATE(*(ptr)) && (ptr) < (end)) && \ |
| 6471 | _Py_UNICODE_IS_LOW_SURROGATE((ptr)[1])) ? \ |
| 6472 | ((ptr) += 2,_Py_UNICODE_JOIN_SURROGATES((ptr)[-2], (ptr)[-1])) : \ |
| 6473 | (Py_UCS4)*(ptr)++) |
| 6474 | #endif |
| 6475 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6476 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6477 | PyObject *substr, |
| 6478 | Py_ssize_t start, |
| 6479 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6480 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6481 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6482 | PyUnicodeObject* str_obj; |
| 6483 | PyUnicodeObject* sub_obj; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6484 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6485 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
| 6486 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6487 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6488 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
| 6489 | if (!sub_obj) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6490 | Py_DECREF(str_obj); |
| 6491 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6492 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6493 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6494 | ADJUST_INDICES(start, end, str_obj->length); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6495 | result = stringlib_count( |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6496 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length, |
| 6497 | PY_SSIZE_T_MAX |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6498 | ); |
| 6499 | |
| 6500 | Py_DECREF(sub_obj); |
| 6501 | Py_DECREF(str_obj); |
| 6502 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6503 | return result; |
| 6504 | } |
| 6505 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6506 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6507 | PyObject *sub, |
| 6508 | Py_ssize_t start, |
| 6509 | Py_ssize_t end, |
| 6510 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6511 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6512 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6513 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6514 | str = PyUnicode_FromObject(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6515 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6516 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6517 | sub = PyUnicode_FromObject(sub); |
| 6518 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6519 | Py_DECREF(str); |
| 6520 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6521 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6522 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6523 | if (direction > 0) |
| 6524 | result = stringlib_find_slice( |
| 6525 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 6526 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 6527 | start, end |
| 6528 | ); |
| 6529 | else |
| 6530 | result = stringlib_rfind_slice( |
| 6531 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 6532 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 6533 | start, end |
| 6534 | ); |
| 6535 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6536 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6537 | Py_DECREF(sub); |
| 6538 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6539 | return result; |
| 6540 | } |
| 6541 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6542 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6543 | int tailmatch(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6544 | PyUnicodeObject *substring, |
| 6545 | Py_ssize_t start, |
| 6546 | Py_ssize_t end, |
| 6547 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6548 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6549 | if (substring->length == 0) |
| 6550 | return 1; |
| 6551 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6552 | ADJUST_INDICES(start, end, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6553 | end -= substring->length; |
| 6554 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6555 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6556 | |
| 6557 | if (direction > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6558 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 6559 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6560 | } else { |
| 6561 | if (Py_UNICODE_MATCH(self, start, substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6562 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6563 | } |
| 6564 | |
| 6565 | return 0; |
| 6566 | } |
| 6567 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6568 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6569 | PyObject *substr, |
| 6570 | Py_ssize_t start, |
| 6571 | Py_ssize_t end, |
| 6572 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6573 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6574 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6575 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6576 | str = PyUnicode_FromObject(str); |
| 6577 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6578 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6579 | substr = PyUnicode_FromObject(substr); |
| 6580 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6581 | Py_DECREF(str); |
| 6582 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6583 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6584 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6585 | result = tailmatch((PyUnicodeObject *)str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6586 | (PyUnicodeObject *)substr, |
| 6587 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6588 | Py_DECREF(str); |
| 6589 | Py_DECREF(substr); |
| 6590 | return result; |
| 6591 | } |
| 6592 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6593 | /* Apply fixfct filter to the Unicode object self and return a |
| 6594 | reference to the modified object */ |
| 6595 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6596 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6597 | PyObject *fixup(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6598 | int (*fixfct)(PyUnicodeObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6599 | { |
| 6600 | |
| 6601 | PyUnicodeObject *u; |
| 6602 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6603 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6604 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6605 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6606 | |
| 6607 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6608 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6609 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6610 | /* fixfct should return TRUE if it modified the buffer. If |
| 6611 | FALSE, return a reference to the original buffer instead |
| 6612 | (to save space, not time) */ |
| 6613 | Py_INCREF(self); |
| 6614 | Py_DECREF(u); |
| 6615 | return (PyObject*) self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6616 | } |
| 6617 | return (PyObject*) u; |
| 6618 | } |
| 6619 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6620 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6621 | int fixupper(PyUnicodeObject *self) |
| 6622 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6623 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6624 | Py_UNICODE *s = self->str; |
| 6625 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6626 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6627 | while (len-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6628 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6629 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6630 | ch = Py_UNICODE_TOUPPER(*s); |
| 6631 | if (ch != *s) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6632 | status = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6633 | *s = ch; |
| 6634 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6635 | s++; |
| 6636 | } |
| 6637 | |
| 6638 | return status; |
| 6639 | } |
| 6640 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6641 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6642 | int fixlower(PyUnicodeObject *self) |
| 6643 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6644 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6645 | Py_UNICODE *s = self->str; |
| 6646 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6647 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6648 | while (len-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6649 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6650 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6651 | ch = Py_UNICODE_TOLOWER(*s); |
| 6652 | if (ch != *s) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6653 | status = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6654 | *s = ch; |
| 6655 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6656 | s++; |
| 6657 | } |
| 6658 | |
| 6659 | return status; |
| 6660 | } |
| 6661 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6662 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6663 | int fixswapcase(PyUnicodeObject *self) |
| 6664 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6665 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6666 | Py_UNICODE *s = self->str; |
| 6667 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6668 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6669 | while (len-- > 0) { |
| 6670 | if (Py_UNICODE_ISUPPER(*s)) { |
| 6671 | *s = Py_UNICODE_TOLOWER(*s); |
| 6672 | status = 1; |
| 6673 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 6674 | *s = Py_UNICODE_TOUPPER(*s); |
| 6675 | status = 1; |
| 6676 | } |
| 6677 | s++; |
| 6678 | } |
| 6679 | |
| 6680 | return status; |
| 6681 | } |
| 6682 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6683 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6684 | int fixcapitalize(PyUnicodeObject *self) |
| 6685 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6686 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6687 | Py_UNICODE *s = self->str; |
| 6688 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6689 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6690 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6691 | return 0; |
Ezio Melotti | ee8d998 | 2011-08-15 09:09:57 +0300 | [diff] [blame] | 6692 | if (!Py_UNICODE_ISUPPER(*s)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6693 | *s = Py_UNICODE_TOUPPER(*s); |
| 6694 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6695 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6696 | s++; |
| 6697 | while (--len > 0) { |
Ezio Melotti | ee8d998 | 2011-08-15 09:09:57 +0300 | [diff] [blame] | 6698 | if (!Py_UNICODE_ISLOWER(*s)) { |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6699 | *s = Py_UNICODE_TOLOWER(*s); |
| 6700 | status = 1; |
| 6701 | } |
| 6702 | s++; |
| 6703 | } |
| 6704 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6705 | } |
| 6706 | |
| 6707 | static |
| 6708 | int fixtitle(PyUnicodeObject *self) |
| 6709 | { |
| 6710 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6711 | register Py_UNICODE *e; |
| 6712 | int previous_is_cased; |
| 6713 | |
| 6714 | /* Shortcut for single character strings */ |
| 6715 | if (PyUnicode_GET_SIZE(self) == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6716 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 6717 | if (*p != ch) { |
| 6718 | *p = ch; |
| 6719 | return 1; |
| 6720 | } |
| 6721 | else |
| 6722 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6723 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6724 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6725 | e = p + PyUnicode_GET_SIZE(self); |
| 6726 | previous_is_cased = 0; |
| 6727 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6728 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6729 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6730 | if (previous_is_cased) |
| 6731 | *p = Py_UNICODE_TOLOWER(ch); |
| 6732 | else |
| 6733 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6734 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6735 | if (Py_UNICODE_ISLOWER(ch) || |
| 6736 | Py_UNICODE_ISUPPER(ch) || |
| 6737 | Py_UNICODE_ISTITLE(ch)) |
| 6738 | previous_is_cased = 1; |
| 6739 | else |
| 6740 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6741 | } |
| 6742 | return 1; |
| 6743 | } |
| 6744 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6745 | PyObject * |
| 6746 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6747 | { |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 6748 | const Py_UNICODE blank = ' '; |
| 6749 | const Py_UNICODE *sep = ␣ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6750 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6751 | PyUnicodeObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6752 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 6753 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6754 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 6755 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6756 | PyObject *item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6757 | Py_ssize_t sz, i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6758 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6759 | fseq = PySequence_Fast(seq, ""); |
| 6760 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6761 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6762 | } |
| 6763 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6764 | /* NOTE: the following code can't call back into Python code, |
| 6765 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 6766 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6767 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6768 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 6769 | /* If empty sequence, return u"". */ |
| 6770 | if (seqlen == 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6771 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 6772 | goto Done; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6773 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6774 | items = PySequence_Fast_ITEMS(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6775 | /* If singleton sequence with an exact Unicode, return that. */ |
| 6776 | if (seqlen == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6777 | item = items[0]; |
| 6778 | if (PyUnicode_CheckExact(item)) { |
| 6779 | Py_INCREF(item); |
| 6780 | res = (PyUnicodeObject *)item; |
| 6781 | goto Done; |
| 6782 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6783 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6784 | else { |
| 6785 | /* Set up sep and seplen */ |
| 6786 | if (separator == NULL) { |
| 6787 | sep = ␣ |
| 6788 | seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6789 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6790 | else { |
| 6791 | if (!PyUnicode_Check(separator)) { |
| 6792 | PyErr_Format(PyExc_TypeError, |
| 6793 | "separator: expected str instance," |
| 6794 | " %.80s found", |
| 6795 | Py_TYPE(separator)->tp_name); |
| 6796 | goto onError; |
| 6797 | } |
| 6798 | sep = PyUnicode_AS_UNICODE(separator); |
| 6799 | seplen = PyUnicode_GET_SIZE(separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6800 | } |
| 6801 | } |
| 6802 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6803 | /* There are at least two things to join, or else we have a subclass |
| 6804 | * of str in the sequence. |
| 6805 | * Do a pre-pass to figure out the total amount of space we'll |
| 6806 | * need (sz), and see whether all argument are strings. |
| 6807 | */ |
| 6808 | sz = 0; |
| 6809 | for (i = 0; i < seqlen; i++) { |
| 6810 | const Py_ssize_t old_sz = sz; |
| 6811 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6812 | if (!PyUnicode_Check(item)) { |
| 6813 | PyErr_Format(PyExc_TypeError, |
| 6814 | "sequence item %zd: expected str instance," |
| 6815 | " %.80s found", |
| 6816 | i, Py_TYPE(item)->tp_name); |
| 6817 | goto onError; |
| 6818 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6819 | sz += PyUnicode_GET_SIZE(item); |
| 6820 | if (i != 0) |
| 6821 | sz += seplen; |
| 6822 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 6823 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6824 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6825 | goto onError; |
| 6826 | } |
| 6827 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6828 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6829 | res = _PyUnicode_New(sz); |
| 6830 | if (res == NULL) |
| 6831 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 6832 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6833 | /* Catenate everything. */ |
| 6834 | res_p = PyUnicode_AS_UNICODE(res); |
| 6835 | for (i = 0; i < seqlen; ++i) { |
| 6836 | Py_ssize_t itemlen; |
| 6837 | item = items[i]; |
| 6838 | itemlen = PyUnicode_GET_SIZE(item); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6839 | /* Copy item, and maybe the separator. */ |
| 6840 | if (i) { |
| 6841 | Py_UNICODE_COPY(res_p, sep, seplen); |
| 6842 | res_p += seplen; |
| 6843 | } |
| 6844 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
| 6845 | res_p += itemlen; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6846 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6847 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6848 | Done: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6849 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6850 | return (PyObject *)res; |
| 6851 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6852 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6853 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6854 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6855 | return NULL; |
| 6856 | } |
| 6857 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6858 | static |
| 6859 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6860 | Py_ssize_t left, |
| 6861 | Py_ssize_t right, |
| 6862 | Py_UNICODE fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6863 | { |
| 6864 | PyUnicodeObject *u; |
| 6865 | |
| 6866 | if (left < 0) |
| 6867 | left = 0; |
| 6868 | if (right < 0) |
| 6869 | right = 0; |
| 6870 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6871 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6872 | Py_INCREF(self); |
| 6873 | return self; |
| 6874 | } |
| 6875 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6876 | if (left > PY_SSIZE_T_MAX - self->length || |
| 6877 | right > PY_SSIZE_T_MAX - (left + self->length)) { |
| 6878 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 6879 | return NULL; |
| 6880 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6881 | u = _PyUnicode_New(left + self->length + right); |
| 6882 | if (u) { |
| 6883 | if (left) |
| 6884 | Py_UNICODE_FILL(u->str, fill, left); |
| 6885 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 6886 | if (right) |
| 6887 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 6888 | } |
| 6889 | |
| 6890 | return u; |
| 6891 | } |
| 6892 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6893 | PyObject *PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6894 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6895 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6896 | |
| 6897 | string = PyUnicode_FromObject(string); |
| 6898 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6899 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6900 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6901 | list = stringlib_splitlines( |
| 6902 | (PyObject*) string, PyUnicode_AS_UNICODE(string), |
| 6903 | PyUnicode_GET_SIZE(string), keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6904 | |
| 6905 | Py_DECREF(string); |
| 6906 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6907 | } |
| 6908 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6909 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6910 | PyObject *split(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6911 | PyUnicodeObject *substring, |
| 6912 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6913 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6914 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6915 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6916 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6917 | if (substring == NULL) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6918 | return stringlib_split_whitespace( |
| 6919 | (PyObject*) self, self->str, self->length, maxcount |
| 6920 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6921 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6922 | return stringlib_split( |
| 6923 | (PyObject*) self, self->str, self->length, |
| 6924 | substring->str, substring->length, |
| 6925 | maxcount |
| 6926 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6927 | } |
| 6928 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6929 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6930 | PyObject *rsplit(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6931 | PyUnicodeObject *substring, |
| 6932 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6933 | { |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6934 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6935 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6936 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6937 | if (substring == NULL) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6938 | return stringlib_rsplit_whitespace( |
| 6939 | (PyObject*) self, self->str, self->length, maxcount |
| 6940 | ); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6941 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6942 | return stringlib_rsplit( |
| 6943 | (PyObject*) self, self->str, self->length, |
| 6944 | substring->str, substring->length, |
| 6945 | maxcount |
| 6946 | ); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6947 | } |
| 6948 | |
| 6949 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6950 | PyObject *replace(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6951 | PyUnicodeObject *str1, |
| 6952 | PyUnicodeObject *str2, |
| 6953 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6954 | { |
| 6955 | PyUnicodeObject *u; |
| 6956 | |
| 6957 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6958 | maxcount = PY_SSIZE_T_MAX; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6959 | else if (maxcount == 0 || self->length == 0) |
| 6960 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6961 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6962 | if (str1->length == str2->length) { |
Antoine Pitrou | cbfdee3 | 2010-01-13 08:58:08 +0000 | [diff] [blame] | 6963 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6964 | /* same length */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6965 | if (str1->length == 0) |
| 6966 | goto nothing; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6967 | if (str1->length == 1) { |
| 6968 | /* replace characters */ |
| 6969 | Py_UNICODE u1, u2; |
| 6970 | if (!findchar(self->str, self->length, str1->str[0])) |
| 6971 | goto nothing; |
| 6972 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6973 | if (!u) |
| 6974 | return NULL; |
| 6975 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6976 | u1 = str1->str[0]; |
| 6977 | u2 = str2->str[0]; |
| 6978 | for (i = 0; i < u->length; i++) |
| 6979 | if (u->str[i] == u1) { |
| 6980 | if (--maxcount < 0) |
| 6981 | break; |
| 6982 | u->str[i] = u2; |
| 6983 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6984 | } else { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6985 | i = stringlib_find( |
| 6986 | self->str, self->length, str1->str, str1->length, 0 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6987 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6988 | if (i < 0) |
| 6989 | goto nothing; |
| 6990 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6991 | if (!u) |
| 6992 | return NULL; |
| 6993 | Py_UNICODE_COPY(u->str, self->str, self->length); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6994 | |
| 6995 | /* change everything in-place, starting with this one */ |
| 6996 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 6997 | i += str1->length; |
| 6998 | |
| 6999 | while ( --maxcount > 0) { |
| 7000 | i = stringlib_find(self->str+i, self->length-i, |
| 7001 | str1->str, str1->length, |
| 7002 | i); |
| 7003 | if (i == -1) |
| 7004 | break; |
| 7005 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 7006 | i += str1->length; |
| 7007 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7008 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7009 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7010 | |
| 7011 | Py_ssize_t n, i, j, e; |
| 7012 | Py_ssize_t product, new_size, delta; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7013 | Py_UNICODE *p; |
| 7014 | |
| 7015 | /* replace strings */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 7016 | n = stringlib_count(self->str, self->length, str1->str, str1->length, |
| 7017 | maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7018 | if (n == 0) |
| 7019 | goto nothing; |
| 7020 | /* new_size = self->length + n * (str2->length - str1->length)); */ |
| 7021 | delta = (str2->length - str1->length); |
| 7022 | if (delta == 0) { |
| 7023 | new_size = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7024 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7025 | product = n * (str2->length - str1->length); |
| 7026 | if ((product / (str2->length - str1->length)) != n) { |
| 7027 | PyErr_SetString(PyExc_OverflowError, |
| 7028 | "replace string is too long"); |
| 7029 | return NULL; |
| 7030 | } |
| 7031 | new_size = self->length + product; |
| 7032 | if (new_size < 0) { |
| 7033 | PyErr_SetString(PyExc_OverflowError, |
| 7034 | "replace string is too long"); |
| 7035 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7036 | } |
| 7037 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7038 | u = _PyUnicode_New(new_size); |
| 7039 | if (!u) |
| 7040 | return NULL; |
| 7041 | i = 0; |
| 7042 | p = u->str; |
| 7043 | e = self->length - str1->length; |
| 7044 | if (str1->length > 0) { |
| 7045 | while (n-- > 0) { |
| 7046 | /* look for next match */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 7047 | j = stringlib_find(self->str+i, self->length-i, |
| 7048 | str1->str, str1->length, |
| 7049 | i); |
| 7050 | if (j == -1) |
| 7051 | break; |
| 7052 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7053 | /* copy unchanged part [i:j] */ |
| 7054 | Py_UNICODE_COPY(p, self->str+i, j-i); |
| 7055 | p += j - i; |
| 7056 | } |
| 7057 | /* copy substitution string */ |
| 7058 | if (str2->length > 0) { |
| 7059 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 7060 | p += str2->length; |
| 7061 | } |
| 7062 | i = j + str1->length; |
| 7063 | } |
| 7064 | if (i < self->length) |
| 7065 | /* copy tail [i:] */ |
| 7066 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 7067 | } else { |
| 7068 | /* interleave */ |
| 7069 | while (n > 0) { |
| 7070 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 7071 | p += str2->length; |
| 7072 | if (--n <= 0) |
| 7073 | break; |
| 7074 | *p++ = self->str[i++]; |
| 7075 | } |
| 7076 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 7077 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7078 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7079 | return (PyObject *) u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7080 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7081 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7082 | /* nothing to replace; return original string (when possible) */ |
| 7083 | if (PyUnicode_CheckExact(self)) { |
| 7084 | Py_INCREF(self); |
| 7085 | return (PyObject *) self; |
| 7086 | } |
| 7087 | return PyUnicode_FromUnicode(self->str, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7088 | } |
| 7089 | |
| 7090 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 7091 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7092 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7093 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7094 | \n\ |
| 7095 | 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] | 7096 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7097 | |
| 7098 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7099 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7100 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7101 | return fixup(self, fixtitle); |
| 7102 | } |
| 7103 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7104 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7105 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7106 | \n\ |
| 7107 | 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] | 7108 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7109 | |
| 7110 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7111 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7112 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7113 | return fixup(self, fixcapitalize); |
| 7114 | } |
| 7115 | |
| 7116 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7117 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7118 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7119 | \n\ |
| 7120 | 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] | 7121 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7122 | |
| 7123 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7124 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7125 | { |
| 7126 | PyObject *list; |
| 7127 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7128 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7129 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7130 | /* Split into words */ |
| 7131 | list = split(self, NULL, -1); |
| 7132 | if (!list) |
| 7133 | return NULL; |
| 7134 | |
| 7135 | /* Capitalize each word */ |
| 7136 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 7137 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7138 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7139 | if (item == NULL) |
| 7140 | goto onError; |
| 7141 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 7142 | PyList_SET_ITEM(list, i, item); |
| 7143 | } |
| 7144 | |
| 7145 | /* Join the words to form a new string */ |
| 7146 | item = PyUnicode_Join(NULL, list); |
| 7147 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7148 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7149 | Py_DECREF(list); |
| 7150 | return (PyObject *)item; |
| 7151 | } |
| 7152 | #endif |
| 7153 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7154 | /* Argument converter. Coerces to a single unicode character */ |
| 7155 | |
| 7156 | static int |
| 7157 | convert_uc(PyObject *obj, void *addr) |
| 7158 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7159 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 7160 | PyObject *uniobj; |
| 7161 | Py_UNICODE *unistr; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7162 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7163 | uniobj = PyUnicode_FromObject(obj); |
| 7164 | if (uniobj == NULL) { |
| 7165 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7166 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7167 | return 0; |
| 7168 | } |
| 7169 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 7170 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7171 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7172 | Py_DECREF(uniobj); |
| 7173 | return 0; |
| 7174 | } |
| 7175 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 7176 | *fillcharloc = unistr[0]; |
| 7177 | Py_DECREF(uniobj); |
| 7178 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7179 | } |
| 7180 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7181 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7182 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7183 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7184 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7185 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7186 | |
| 7187 | static PyObject * |
| 7188 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 7189 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7190 | Py_ssize_t marg, left; |
| 7191 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7192 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7193 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 7194 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7195 | return NULL; |
| 7196 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7197 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7198 | Py_INCREF(self); |
| 7199 | return (PyObject*) self; |
| 7200 | } |
| 7201 | |
| 7202 | marg = width - self->length; |
| 7203 | left = marg / 2 + (marg & width & 1); |
| 7204 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7205 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7206 | } |
| 7207 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 7208 | #if 0 |
| 7209 | |
| 7210 | /* This code should go into some future Unicode collation support |
| 7211 | module. The basic comparison should compare ordinals on a naive |
Georg Brandl | c6c3178 | 2009-06-08 13:41:29 +0000 | [diff] [blame] | 7212 | basis (this is what Java does and thus Jython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 7213 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 7214 | /* speedy UTF-16 code point order comparison */ |
| 7215 | /* gleaned from: */ |
| 7216 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 7217 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 7218 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 7219 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 7220 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7221 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 7222 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 7223 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 7224 | }; |
| 7225 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7226 | static int |
| 7227 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 7228 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7229 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 7230 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7231 | Py_UNICODE *s1 = str1->str; |
| 7232 | Py_UNICODE *s2 = str2->str; |
| 7233 | |
| 7234 | len1 = str1->length; |
| 7235 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7236 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7237 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7238 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 7239 | |
| 7240 | c1 = *s1++; |
| 7241 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 7242 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7243 | if (c1 > (1<<11) * 26) |
| 7244 | c1 += utf16Fixup[c1>>11]; |
| 7245 | if (c2 > (1<<11) * 26) |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 7246 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 7247 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 7248 | |
| 7249 | if (c1 != c2) |
| 7250 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7251 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 7252 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7253 | } |
| 7254 | |
| 7255 | return (len1 < len2) ? -1 : (len1 != len2); |
| 7256 | } |
| 7257 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 7258 | #else |
| 7259 | |
| 7260 | static int |
| 7261 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 7262 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7263 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 7264 | |
| 7265 | Py_UNICODE *s1 = str1->str; |
| 7266 | Py_UNICODE *s2 = str2->str; |
| 7267 | |
| 7268 | len1 = str1->length; |
| 7269 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7270 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 7271 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7272 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 7273 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 7274 | c1 = *s1++; |
| 7275 | c2 = *s2++; |
| 7276 | |
| 7277 | if (c1 != c2) |
| 7278 | return (c1 < c2) ? -1 : 1; |
| 7279 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 7280 | len1--; len2--; |
| 7281 | } |
| 7282 | |
| 7283 | return (len1 < len2) ? -1 : (len1 != len2); |
| 7284 | } |
| 7285 | |
| 7286 | #endif |
| 7287 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7288 | int PyUnicode_Compare(PyObject *left, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7289 | PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7290 | { |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 7291 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) |
| 7292 | return unicode_compare((PyUnicodeObject *)left, |
| 7293 | (PyUnicodeObject *)right); |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 7294 | PyErr_Format(PyExc_TypeError, |
| 7295 | "Can't compare %.100s and %.100s", |
| 7296 | left->ob_type->tp_name, |
| 7297 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7298 | return -1; |
| 7299 | } |
| 7300 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 7301 | int |
| 7302 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 7303 | { |
| 7304 | int i; |
| 7305 | Py_UNICODE *id; |
| 7306 | assert(PyUnicode_Check(uni)); |
| 7307 | id = PyUnicode_AS_UNICODE(uni); |
| 7308 | /* Compare Unicode string and source character set string */ |
| 7309 | for (i = 0; id[i] && str[i]; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7310 | if (id[i] != str[i]) |
| 7311 | return ((int)id[i] < (int)str[i]) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 7312 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 7313 | to C strings identical up to that point. */ |
Benjamin Peterson | a23831f | 2010-04-25 21:54:00 +0000 | [diff] [blame] | 7314 | if (PyUnicode_GET_SIZE(uni) != i || id[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7315 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 7316 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7317 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 7318 | return 0; |
| 7319 | } |
| 7320 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 7321 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7322 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7323 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 7324 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 7325 | PyObject *PyUnicode_RichCompare(PyObject *left, |
| 7326 | PyObject *right, |
| 7327 | int op) |
| 7328 | { |
| 7329 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7330 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 7331 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 7332 | PyObject *v; |
| 7333 | if (((PyUnicodeObject *) left)->length != |
| 7334 | ((PyUnicodeObject *) right)->length) { |
| 7335 | if (op == Py_EQ) { |
| 7336 | Py_INCREF(Py_False); |
| 7337 | return Py_False; |
| 7338 | } |
| 7339 | if (op == Py_NE) { |
| 7340 | Py_INCREF(Py_True); |
| 7341 | return Py_True; |
| 7342 | } |
| 7343 | } |
| 7344 | if (left == right) |
| 7345 | result = 0; |
| 7346 | else |
| 7347 | result = unicode_compare((PyUnicodeObject *)left, |
| 7348 | (PyUnicodeObject *)right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7349 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 7350 | /* Convert the return value to a Boolean */ |
| 7351 | switch (op) { |
| 7352 | case Py_EQ: |
| 7353 | v = TEST_COND(result == 0); |
| 7354 | break; |
| 7355 | case Py_NE: |
| 7356 | v = TEST_COND(result != 0); |
| 7357 | break; |
| 7358 | case Py_LE: |
| 7359 | v = TEST_COND(result <= 0); |
| 7360 | break; |
| 7361 | case Py_GE: |
| 7362 | v = TEST_COND(result >= 0); |
| 7363 | break; |
| 7364 | case Py_LT: |
| 7365 | v = TEST_COND(result == -1); |
| 7366 | break; |
| 7367 | case Py_GT: |
| 7368 | v = TEST_COND(result == 1); |
| 7369 | break; |
| 7370 | default: |
| 7371 | PyErr_BadArgument(); |
| 7372 | return NULL; |
| 7373 | } |
| 7374 | Py_INCREF(v); |
| 7375 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 7376 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7377 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 7378 | Py_INCREF(Py_NotImplemented); |
| 7379 | return Py_NotImplemented; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 7380 | } |
| 7381 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7382 | int PyUnicode_Contains(PyObject *container, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7383 | PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7384 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7385 | PyObject *str, *sub; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7386 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7387 | |
| 7388 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7389 | sub = PyUnicode_FromObject(element); |
| 7390 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7391 | PyErr_Format(PyExc_TypeError, |
| 7392 | "'in <string>' requires string as left operand, not %s", |
| 7393 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7394 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7395 | } |
| 7396 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7397 | str = PyUnicode_FromObject(container); |
| 7398 | if (!str) { |
| 7399 | Py_DECREF(sub); |
| 7400 | return -1; |
| 7401 | } |
| 7402 | |
| 7403 | result = stringlib_contains_obj(str, sub); |
| 7404 | |
| 7405 | Py_DECREF(str); |
| 7406 | Py_DECREF(sub); |
| 7407 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7408 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7409 | } |
| 7410 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7411 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 7412 | |
| 7413 | PyObject *PyUnicode_Concat(PyObject *left, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7414 | PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7415 | { |
| 7416 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 7417 | |
| 7418 | /* Coerce the two arguments */ |
| 7419 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 7420 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7421 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7422 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 7423 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7424 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7425 | |
| 7426 | /* Shortcuts */ |
| 7427 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7428 | Py_DECREF(v); |
| 7429 | return (PyObject *)u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7430 | } |
| 7431 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7432 | Py_DECREF(u); |
| 7433 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7434 | } |
| 7435 | |
| 7436 | /* Concat the two Unicode strings */ |
| 7437 | w = _PyUnicode_New(u->length + v->length); |
| 7438 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7439 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7440 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 7441 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 7442 | |
| 7443 | Py_DECREF(u); |
| 7444 | Py_DECREF(v); |
| 7445 | return (PyObject *)w; |
| 7446 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7447 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7448 | Py_XDECREF(u); |
| 7449 | Py_XDECREF(v); |
| 7450 | return NULL; |
| 7451 | } |
| 7452 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7453 | void |
| 7454 | PyUnicode_Append(PyObject **pleft, PyObject *right) |
| 7455 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7456 | PyObject *new; |
| 7457 | if (*pleft == NULL) |
| 7458 | return; |
| 7459 | if (right == NULL || !PyUnicode_Check(*pleft)) { |
| 7460 | Py_DECREF(*pleft); |
| 7461 | *pleft = NULL; |
| 7462 | return; |
| 7463 | } |
| 7464 | new = PyUnicode_Concat(*pleft, right); |
| 7465 | Py_DECREF(*pleft); |
| 7466 | *pleft = new; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7467 | } |
| 7468 | |
| 7469 | void |
| 7470 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 7471 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7472 | PyUnicode_Append(pleft, right); |
| 7473 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7474 | } |
| 7475 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7476 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7477 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7478 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7479 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7480 | 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] | 7481 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7482 | |
| 7483 | static PyObject * |
| 7484 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 7485 | { |
| 7486 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7487 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7488 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7489 | PyObject *result; |
| 7490 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 7491 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 7492 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7493 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7494 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 7495 | ADJUST_INDICES(start, end, self->length); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7496 | result = PyLong_FromSsize_t( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7497 | stringlib_count(self->str + start, end - start, |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 7498 | substring->str, substring->length, |
| 7499 | PY_SSIZE_T_MAX) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7500 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7501 | |
| 7502 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7503 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7504 | return result; |
| 7505 | } |
| 7506 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7507 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 7508 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7509 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 7510 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 7511 | 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] | 7512 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7513 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 7514 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 7515 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7516 | |
| 7517 | static PyObject * |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 7518 | unicode_encode(PyUnicodeObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7519 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 7520 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7521 | char *encoding = NULL; |
| 7522 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 7523 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 7524 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 7525 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7526 | return NULL; |
Georg Brandl | 3b9406b | 2010-12-03 07:54:09 +0000 | [diff] [blame] | 7527 | return PyUnicode_AsEncodedString((PyObject *)self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7528 | } |
| 7529 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7530 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7531 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7532 | \n\ |
| 7533 | 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] | 7534 | 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] | 7535 | |
| 7536 | static PyObject* |
| 7537 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 7538 | { |
| 7539 | Py_UNICODE *e; |
| 7540 | Py_UNICODE *p; |
| 7541 | Py_UNICODE *q; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7542 | Py_UNICODE *qe; |
| 7543 | Py_ssize_t i, j, incr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7544 | PyUnicodeObject *u; |
| 7545 | int tabsize = 8; |
| 7546 | |
| 7547 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7548 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7549 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 7550 | /* First pass: determine size of output string */ |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7551 | i = 0; /* chars up to and including most recent \n or \r */ |
| 7552 | j = 0; /* chars since most recent \n or \r (use in tab calculations) */ |
| 7553 | e = self->str + self->length; /* end of input */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7554 | for (p = self->str; p < e; p++) |
| 7555 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7556 | if (tabsize > 0) { |
| 7557 | incr = tabsize - (j % tabsize); /* cannot overflow */ |
| 7558 | if (j > PY_SSIZE_T_MAX - incr) |
| 7559 | goto overflow1; |
| 7560 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7561 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7562 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7563 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7564 | if (j > PY_SSIZE_T_MAX - 1) |
| 7565 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7566 | j++; |
| 7567 | if (*p == '\n' || *p == '\r') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7568 | if (i > PY_SSIZE_T_MAX - j) |
| 7569 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7570 | i += j; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7571 | j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7572 | } |
| 7573 | } |
| 7574 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7575 | if (i > PY_SSIZE_T_MAX - j) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7576 | goto overflow1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 7577 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7578 | /* Second pass: create output string and fill it */ |
| 7579 | u = _PyUnicode_New(i + j); |
| 7580 | if (!u) |
| 7581 | return NULL; |
| 7582 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7583 | j = 0; /* same as in first pass */ |
| 7584 | q = u->str; /* next output char */ |
| 7585 | qe = u->str + u->length; /* end of output */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7586 | |
| 7587 | for (p = self->str; p < e; p++) |
| 7588 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7589 | if (tabsize > 0) { |
| 7590 | i = tabsize - (j % tabsize); |
| 7591 | j += i; |
| 7592 | while (i--) { |
| 7593 | if (q >= qe) |
| 7594 | goto overflow2; |
| 7595 | *q++ = ' '; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7596 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7597 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7598 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7599 | else { |
| 7600 | if (q >= qe) |
| 7601 | goto overflow2; |
| 7602 | *q++ = *p; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7603 | j++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7604 | if (*p == '\n' || *p == '\r') |
| 7605 | j = 0; |
| 7606 | } |
| 7607 | |
| 7608 | return (PyObject*) u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7609 | |
| 7610 | overflow2: |
| 7611 | Py_DECREF(u); |
| 7612 | overflow1: |
| 7613 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 7614 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7615 | } |
| 7616 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7617 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7618 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7619 | \n\ |
| 7620 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 7621 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7622 | arguments start and end are interpreted as in slice notation.\n\ |
| 7623 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7624 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7625 | |
| 7626 | static PyObject * |
| 7627 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 7628 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 7629 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7630 | Py_ssize_t start; |
| 7631 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7632 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7633 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 7634 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 7635 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7636 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7637 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7638 | result = stringlib_find_slice( |
| 7639 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7640 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7641 | start, end |
| 7642 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7643 | |
| 7644 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7645 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7646 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7647 | } |
| 7648 | |
| 7649 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7650 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7651 | { |
| 7652 | if (index < 0 || index >= self->length) { |
| 7653 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 7654 | return NULL; |
| 7655 | } |
| 7656 | |
| 7657 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 7658 | } |
| 7659 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7660 | /* Believe it or not, this produces the same value for ASCII strings |
| 7661 | as string_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 7662 | static Py_hash_t |
Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 7663 | unicode_hash(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7664 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7665 | Py_ssize_t len; |
| 7666 | Py_UNICODE *p; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 7667 | Py_hash_t x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7668 | |
| 7669 | if (self->hash != -1) |
| 7670 | return self->hash; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7671 | len = Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7672 | p = self->str; |
| 7673 | x = *p << 7; |
| 7674 | while (--len >= 0) |
| 7675 | x = (1000003*x) ^ *p++; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7676 | x ^= Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7677 | if (x == -1) |
| 7678 | x = -2; |
| 7679 | self->hash = x; |
| 7680 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7681 | } |
| 7682 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7683 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7684 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7685 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7686 | 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] | 7687 | |
| 7688 | static PyObject * |
| 7689 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 7690 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7691 | Py_ssize_t result; |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 7692 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7693 | Py_ssize_t start; |
| 7694 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7695 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 7696 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 7697 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7698 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7699 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7700 | result = stringlib_find_slice( |
| 7701 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7702 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7703 | start, end |
| 7704 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7705 | |
| 7706 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7707 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7708 | if (result < 0) { |
| 7709 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 7710 | return NULL; |
| 7711 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7712 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7713 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7714 | } |
| 7715 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7716 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7717 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7718 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7719 | 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] | 7720 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7721 | |
| 7722 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7723 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7724 | { |
| 7725 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7726 | register const Py_UNICODE *e; |
| 7727 | int cased; |
| 7728 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7729 | /* Shortcut for single character strings */ |
| 7730 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7731 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7732 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7733 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7734 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7735 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7736 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7737 | e = p + PyUnicode_GET_SIZE(self); |
| 7738 | cased = 0; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 7739 | while (p < e) { |
| 7740 | const Py_UCS4 ch = _Py_UNICODE_NEXT(p, e); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7741 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7742 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 7743 | return PyBool_FromLong(0); |
| 7744 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 7745 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7746 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7747 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7748 | } |
| 7749 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7750 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7751 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7752 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7753 | 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] | 7754 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7755 | |
| 7756 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7757 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7758 | { |
| 7759 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7760 | register const Py_UNICODE *e; |
| 7761 | int cased; |
| 7762 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7763 | /* Shortcut for single character strings */ |
| 7764 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7765 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7766 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7767 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7768 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7769 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7770 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7771 | e = p + PyUnicode_GET_SIZE(self); |
| 7772 | cased = 0; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 7773 | while (p < e) { |
| 7774 | const Py_UCS4 ch = _Py_UNICODE_NEXT(p, e); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7775 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7776 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 7777 | return PyBool_FromLong(0); |
| 7778 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 7779 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7780 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7781 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7782 | } |
| 7783 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7784 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7785 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7786 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7787 | Return True if S is a titlecased string and there is at least one\n\ |
| 7788 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 7789 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 7790 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7791 | |
| 7792 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7793 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7794 | { |
| 7795 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7796 | register const Py_UNICODE *e; |
| 7797 | int cased, previous_is_cased; |
| 7798 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7799 | /* Shortcut for single character strings */ |
| 7800 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7801 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 7802 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7803 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7804 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7805 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7806 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7807 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7808 | e = p + PyUnicode_GET_SIZE(self); |
| 7809 | cased = 0; |
| 7810 | previous_is_cased = 0; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 7811 | while (p < e) { |
| 7812 | const Py_UCS4 ch = _Py_UNICODE_NEXT(p, e); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7813 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7814 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 7815 | if (previous_is_cased) |
| 7816 | return PyBool_FromLong(0); |
| 7817 | previous_is_cased = 1; |
| 7818 | cased = 1; |
| 7819 | } |
| 7820 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 7821 | if (!previous_is_cased) |
| 7822 | return PyBool_FromLong(0); |
| 7823 | previous_is_cased = 1; |
| 7824 | cased = 1; |
| 7825 | } |
| 7826 | else |
| 7827 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7828 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7829 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7830 | } |
| 7831 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7832 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7833 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7834 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7835 | Return True if all characters in S are whitespace\n\ |
| 7836 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7837 | |
| 7838 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7839 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7840 | { |
| 7841 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7842 | register const Py_UNICODE *e; |
| 7843 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7844 | /* Shortcut for single character strings */ |
| 7845 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7846 | Py_UNICODE_ISSPACE(*p)) |
| 7847 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7848 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7849 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7850 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7851 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7852 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7853 | e = p + PyUnicode_GET_SIZE(self); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 7854 | while (p < e) { |
| 7855 | const Py_UCS4 ch = _Py_UNICODE_NEXT(p, e); |
| 7856 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7857 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7858 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7859 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7860 | } |
| 7861 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7862 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7863 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7864 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7865 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7866 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7867 | |
| 7868 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7869 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7870 | { |
| 7871 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7872 | register const Py_UNICODE *e; |
| 7873 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7874 | /* Shortcut for single character strings */ |
| 7875 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7876 | Py_UNICODE_ISALPHA(*p)) |
| 7877 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7878 | |
| 7879 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7880 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7881 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7882 | |
| 7883 | e = p + PyUnicode_GET_SIZE(self); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 7884 | while (p < e) { |
| 7885 | if (!Py_UNICODE_ISALPHA(_Py_UNICODE_NEXT(p, e))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7886 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7887 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7888 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7889 | } |
| 7890 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7891 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7892 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7893 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7894 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7895 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7896 | |
| 7897 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7898 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7899 | { |
| 7900 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7901 | register const Py_UNICODE *e; |
| 7902 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7903 | /* Shortcut for single character strings */ |
| 7904 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7905 | Py_UNICODE_ISALNUM(*p)) |
| 7906 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7907 | |
| 7908 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7909 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7910 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7911 | |
| 7912 | e = p + PyUnicode_GET_SIZE(self); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 7913 | while (p < e) { |
| 7914 | const Py_UCS4 ch = _Py_UNICODE_NEXT(p, e); |
| 7915 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7916 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7917 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7918 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7919 | } |
| 7920 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7921 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7922 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7923 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7924 | 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] | 7925 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7926 | |
| 7927 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7928 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7929 | { |
| 7930 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7931 | register const Py_UNICODE *e; |
| 7932 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7933 | /* Shortcut for single character strings */ |
| 7934 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7935 | Py_UNICODE_ISDECIMAL(*p)) |
| 7936 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7937 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7938 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7939 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7940 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7941 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7942 | e = p + PyUnicode_GET_SIZE(self); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 7943 | while (p < e) { |
| 7944 | if (!Py_UNICODE_ISDECIMAL(_Py_UNICODE_NEXT(p, e))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7945 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7946 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7947 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7948 | } |
| 7949 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7950 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7951 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7952 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7953 | Return True if all characters in S are digits\n\ |
| 7954 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7955 | |
| 7956 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7957 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7958 | { |
| 7959 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7960 | register const Py_UNICODE *e; |
| 7961 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7962 | /* Shortcut for single character strings */ |
| 7963 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7964 | Py_UNICODE_ISDIGIT(*p)) |
| 7965 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7966 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7967 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7968 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7969 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7970 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7971 | e = p + PyUnicode_GET_SIZE(self); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 7972 | while (p < e) { |
| 7973 | if (!Py_UNICODE_ISDIGIT(_Py_UNICODE_NEXT(p, e))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7974 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7975 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7976 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7977 | } |
| 7978 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7979 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7980 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7981 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7982 | 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] | 7983 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7984 | |
| 7985 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7986 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7987 | { |
| 7988 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7989 | register const Py_UNICODE *e; |
| 7990 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7991 | /* Shortcut for single character strings */ |
| 7992 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7993 | Py_UNICODE_ISNUMERIC(*p)) |
| 7994 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7995 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7996 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7997 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7998 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7999 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8000 | e = p + PyUnicode_GET_SIZE(self); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 8001 | while (p < e) { |
| 8002 | if (!Py_UNICODE_ISNUMERIC(_Py_UNICODE_NEXT(p, e))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8003 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8004 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 8005 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8006 | } |
| 8007 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 8008 | int |
| 8009 | PyUnicode_IsIdentifier(PyObject *self) |
| 8010 | { |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 8011 | const Py_UNICODE *p = PyUnicode_AS_UNICODE((PyUnicodeObject*)self); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 8012 | const Py_UNICODE *e; |
| 8013 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 8014 | |
| 8015 | /* Special case for empty strings */ |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 8016 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8017 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 8018 | |
| 8019 | /* PEP 3131 says that the first character must be in |
| 8020 | XID_Start and subsequent characters in XID_Continue, |
| 8021 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8022 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 8023 | letters, digits, underscore). However, given the current |
| 8024 | definition of XID_Start and XID_Continue, it is sufficient |
| 8025 | to check just for these, except that _ must be allowed |
| 8026 | as starting an identifier. */ |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 8027 | e = p + PyUnicode_GET_SIZE(self); |
| 8028 | first = _Py_UNICODE_NEXT(p, e); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 8029 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 8030 | return 0; |
| 8031 | |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 8032 | while (p < e) |
| 8033 | if (!_PyUnicode_IsXidContinue(_Py_UNICODE_NEXT(p, e))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8034 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 8035 | return 1; |
| 8036 | } |
| 8037 | |
| 8038 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8039 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 8040 | \n\ |
| 8041 | Return True if S is a valid identifier according\n\ |
| 8042 | to the language definition."); |
| 8043 | |
| 8044 | static PyObject* |
| 8045 | unicode_isidentifier(PyObject *self) |
| 8046 | { |
| 8047 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 8048 | } |
| 8049 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8050 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8051 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8052 | \n\ |
| 8053 | Return True if all characters in S are considered\n\ |
| 8054 | printable in repr() or S is empty, False otherwise."); |
| 8055 | |
| 8056 | static PyObject* |
| 8057 | unicode_isprintable(PyObject *self) |
| 8058 | { |
| 8059 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 8060 | register const Py_UNICODE *e; |
| 8061 | |
| 8062 | /* Shortcut for single character strings */ |
| 8063 | if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISPRINTABLE(*p)) { |
| 8064 | Py_RETURN_TRUE; |
| 8065 | } |
| 8066 | |
| 8067 | e = p + PyUnicode_GET_SIZE(self); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 8068 | while (p < e) { |
| 8069 | if (!Py_UNICODE_ISPRINTABLE(_Py_UNICODE_NEXT(p, e))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8070 | Py_RETURN_FALSE; |
| 8071 | } |
| 8072 | } |
| 8073 | Py_RETURN_TRUE; |
| 8074 | } |
| 8075 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8076 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 8077 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8078 | \n\ |
| 8079 | 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] | 8080 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8081 | |
| 8082 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8083 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8084 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8085 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8086 | } |
| 8087 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8088 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8089 | unicode_length(PyUnicodeObject *self) |
| 8090 | { |
| 8091 | return self->length; |
| 8092 | } |
| 8093 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8094 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8095 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8096 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 8097 | 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] | 8098 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8099 | |
| 8100 | static PyObject * |
| 8101 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 8102 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8103 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 8104 | Py_UNICODE fillchar = ' '; |
| 8105 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8106 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8107 | return NULL; |
| 8108 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 8109 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8110 | Py_INCREF(self); |
| 8111 | return (PyObject*) self; |
| 8112 | } |
| 8113 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 8114 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8115 | } |
| 8116 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8117 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8118 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8119 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8120 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8121 | |
| 8122 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8123 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8124 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8125 | return fixup(self, fixlower); |
| 8126 | } |
| 8127 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8128 | #define LEFTSTRIP 0 |
| 8129 | #define RIGHTSTRIP 1 |
| 8130 | #define BOTHSTRIP 2 |
| 8131 | |
| 8132 | /* Arrays indexed by above */ |
| 8133 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 8134 | |
| 8135 | #define STRIPNAME(i) (stripformat[i]+3) |
| 8136 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8137 | /* externally visible for str.strip(unicode) */ |
| 8138 | PyObject * |
| 8139 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 8140 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8141 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 8142 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
| 8143 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
| 8144 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 8145 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8146 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8147 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8148 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8149 | i = 0; |
| 8150 | if (striptype != RIGHTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8151 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 8152 | i++; |
| 8153 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8154 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8155 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8156 | j = len; |
| 8157 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8158 | do { |
| 8159 | j--; |
| 8160 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 8161 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8162 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8163 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8164 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8165 | Py_INCREF(self); |
| 8166 | return (PyObject*)self; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8167 | } |
| 8168 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8169 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8170 | } |
| 8171 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8172 | |
| 8173 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8174 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8175 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8176 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 8177 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8178 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8179 | i = 0; |
| 8180 | if (striptype != RIGHTSTRIP) { |
| 8181 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 8182 | i++; |
| 8183 | } |
| 8184 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8185 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8186 | j = len; |
| 8187 | if (striptype != LEFTSTRIP) { |
| 8188 | do { |
| 8189 | j--; |
| 8190 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 8191 | j++; |
| 8192 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8193 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8194 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 8195 | Py_INCREF(self); |
| 8196 | return (PyObject*)self; |
| 8197 | } |
| 8198 | else |
| 8199 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8200 | } |
| 8201 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8202 | |
| 8203 | static PyObject * |
| 8204 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 8205 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8206 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8207 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8208 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 8209 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8210 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8211 | if (sep != NULL && sep != Py_None) { |
| 8212 | if (PyUnicode_Check(sep)) |
| 8213 | return _PyUnicode_XStrip(self, striptype, sep); |
| 8214 | else { |
| 8215 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8216 | "%s arg must be None or str", |
| 8217 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8218 | return NULL; |
| 8219 | } |
| 8220 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8221 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8222 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8223 | } |
| 8224 | |
| 8225 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8226 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8227 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8228 | \n\ |
| 8229 | Return a copy of the string S with leading and trailing\n\ |
| 8230 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8231 | 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] | 8232 | |
| 8233 | static PyObject * |
| 8234 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 8235 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8236 | if (PyTuple_GET_SIZE(args) == 0) |
| 8237 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 8238 | else |
| 8239 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8240 | } |
| 8241 | |
| 8242 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8243 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8244 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8245 | \n\ |
| 8246 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8247 | 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] | 8248 | |
| 8249 | static PyObject * |
| 8250 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 8251 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8252 | if (PyTuple_GET_SIZE(args) == 0) |
| 8253 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 8254 | else |
| 8255 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8256 | } |
| 8257 | |
| 8258 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8259 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8260 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8261 | \n\ |
| 8262 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8263 | 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] | 8264 | |
| 8265 | static PyObject * |
| 8266 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 8267 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8268 | if (PyTuple_GET_SIZE(args) == 0) |
| 8269 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 8270 | else |
| 8271 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8272 | } |
| 8273 | |
| 8274 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8275 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8276 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8277 | { |
| 8278 | PyUnicodeObject *u; |
| 8279 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8280 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 8281 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8282 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 8283 | if (len < 1) { |
| 8284 | Py_INCREF(unicode_empty); |
| 8285 | return (PyObject *)unicode_empty; |
| 8286 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8287 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 8288 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8289 | /* no repeat, return original string */ |
| 8290 | Py_INCREF(str); |
| 8291 | return (PyObject*) str; |
| 8292 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 8293 | |
| 8294 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 8295 | * needed doesn't overflow size_t |
| 8296 | */ |
| 8297 | nchars = len * str->length; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 8298 | if (nchars / len != str->length) { |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 8299 | PyErr_SetString(PyExc_OverflowError, |
| 8300 | "repeated string is too long"); |
| 8301 | return NULL; |
| 8302 | } |
| 8303 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 8304 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 8305 | PyErr_SetString(PyExc_OverflowError, |
| 8306 | "repeated string is too long"); |
| 8307 | return NULL; |
| 8308 | } |
| 8309 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8310 | if (!u) |
| 8311 | return NULL; |
| 8312 | |
| 8313 | p = u->str; |
| 8314 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 8315 | if (str->length == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8316 | Py_UNICODE_FILL(p, str->str[0], len); |
| 8317 | } else { |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 8318 | Py_ssize_t done = str->length; /* number of characters copied this far */ |
| 8319 | Py_UNICODE_COPY(p, str->str, str->length); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8320 | while (done < nchars) { |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 8321 | Py_ssize_t n = (done <= nchars-done) ? done : nchars-done; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8322 | Py_UNICODE_COPY(p+done, p, n); |
| 8323 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8324 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8325 | } |
| 8326 | |
| 8327 | return (PyObject*) u; |
| 8328 | } |
| 8329 | |
| 8330 | PyObject *PyUnicode_Replace(PyObject *obj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8331 | PyObject *subobj, |
| 8332 | PyObject *replobj, |
| 8333 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8334 | { |
| 8335 | PyObject *self; |
| 8336 | PyObject *str1; |
| 8337 | PyObject *str2; |
| 8338 | PyObject *result; |
| 8339 | |
| 8340 | self = PyUnicode_FromObject(obj); |
| 8341 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8342 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8343 | str1 = PyUnicode_FromObject(subobj); |
| 8344 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8345 | Py_DECREF(self); |
| 8346 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8347 | } |
| 8348 | str2 = PyUnicode_FromObject(replobj); |
| 8349 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8350 | Py_DECREF(self); |
| 8351 | Py_DECREF(str1); |
| 8352 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8353 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8354 | result = replace((PyUnicodeObject *)self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8355 | (PyUnicodeObject *)str1, |
| 8356 | (PyUnicodeObject *)str2, |
| 8357 | maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8358 | Py_DECREF(self); |
| 8359 | Py_DECREF(str1); |
| 8360 | Py_DECREF(str2); |
| 8361 | return result; |
| 8362 | } |
| 8363 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8364 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 8365 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8366 | \n\ |
| 8367 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 8368 | old replaced by new. If the optional argument count is\n\ |
| 8369 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8370 | |
| 8371 | static PyObject* |
| 8372 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 8373 | { |
| 8374 | PyUnicodeObject *str1; |
| 8375 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8376 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8377 | PyObject *result; |
| 8378 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8379 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8380 | return NULL; |
| 8381 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 8382 | if (str1 == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8383 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8384 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 8385 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8386 | Py_DECREF(str1); |
| 8387 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 8388 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8389 | |
| 8390 | result = replace(self, str1, str2, maxcount); |
| 8391 | |
| 8392 | Py_DECREF(str1); |
| 8393 | Py_DECREF(str2); |
| 8394 | return result; |
| 8395 | } |
| 8396 | |
| 8397 | static |
| 8398 | PyObject *unicode_repr(PyObject *unicode) |
| 8399 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8400 | PyObject *repr; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8401 | Py_UNICODE *p; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8402 | Py_UNICODE *s = PyUnicode_AS_UNICODE(unicode); |
| 8403 | Py_ssize_t size = PyUnicode_GET_SIZE(unicode); |
| 8404 | |
| 8405 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 8406 | better to choose a different scheme. Perhaps scan the |
| 8407 | first N-chars of the string and allocate based on that size. |
| 8408 | */ |
| 8409 | /* Initial allocation is based on the longest-possible unichr |
| 8410 | escape. |
| 8411 | |
| 8412 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 8413 | unichr, so in this case it's the longest unichr escape. In |
| 8414 | narrow (UTF-16) builds this is five chars per source unichr |
| 8415 | since there are two unichrs in the surrogate pair, so in narrow |
| 8416 | (UTF-16) builds it's not the longest unichr escape. |
| 8417 | |
| 8418 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 8419 | so in the narrow (UTF-16) build case it's the longest unichr |
| 8420 | escape. |
| 8421 | */ |
| 8422 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8423 | repr = PyUnicode_FromUnicode(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8424 | 2 /* quotes */ |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8425 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8426 | + 10*size |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8427 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8428 | + 6*size |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8429 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8430 | + 1); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8431 | if (repr == NULL) |
| 8432 | return NULL; |
| 8433 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8434 | p = PyUnicode_AS_UNICODE(repr); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8435 | |
| 8436 | /* Add quote */ |
| 8437 | *p++ = (findchar(s, size, '\'') && |
| 8438 | !findchar(s, size, '"')) ? '"' : '\''; |
| 8439 | while (size-- > 0) { |
| 8440 | Py_UNICODE ch = *s++; |
| 8441 | |
| 8442 | /* Escape quotes and backslashes */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8443 | if ((ch == PyUnicode_AS_UNICODE(repr)[0]) || (ch == '\\')) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8444 | *p++ = '\\'; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8445 | *p++ = ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8446 | continue; |
| 8447 | } |
| 8448 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8449 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8450 | if (ch == '\t') { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8451 | *p++ = '\\'; |
| 8452 | *p++ = 't'; |
| 8453 | } |
| 8454 | else if (ch == '\n') { |
| 8455 | *p++ = '\\'; |
| 8456 | *p++ = 'n'; |
| 8457 | } |
| 8458 | else if (ch == '\r') { |
| 8459 | *p++ = '\\'; |
| 8460 | *p++ = 'r'; |
| 8461 | } |
| 8462 | |
| 8463 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8464 | else if (ch < ' ' || ch == 0x7F) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8465 | *p++ = '\\'; |
| 8466 | *p++ = 'x'; |
| 8467 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 8468 | *p++ = hexdigits[ch & 0x000F]; |
| 8469 | } |
| 8470 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8471 | /* Copy ASCII characters as-is */ |
| 8472 | else if (ch < 0x7F) { |
| 8473 | *p++ = ch; |
| 8474 | } |
| 8475 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8476 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8477 | else { |
| 8478 | Py_UCS4 ucs = ch; |
| 8479 | |
| 8480 | #ifndef Py_UNICODE_WIDE |
| 8481 | Py_UNICODE ch2 = 0; |
| 8482 | /* Get code point from surrogate pair */ |
| 8483 | if (size > 0) { |
| 8484 | ch2 = *s; |
| 8485 | if (ch >= 0xD800 && ch < 0xDC00 && ch2 >= 0xDC00 |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8486 | && ch2 <= 0xDFFF) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8487 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8488 | + 0x00010000; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8489 | s++; |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8490 | size--; |
| 8491 | } |
| 8492 | } |
| 8493 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8494 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8495 | (categories Z* and C* except ASCII space) |
| 8496 | */ |
| 8497 | if (!Py_UNICODE_ISPRINTABLE(ucs)) { |
| 8498 | /* Map 8-bit characters to '\xhh' */ |
| 8499 | if (ucs <= 0xff) { |
| 8500 | *p++ = '\\'; |
| 8501 | *p++ = 'x'; |
| 8502 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 8503 | *p++ = hexdigits[ch & 0x000F]; |
| 8504 | } |
| 8505 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 8506 | else if (ucs >= 0x10000) { |
| 8507 | *p++ = '\\'; |
| 8508 | *p++ = 'U'; |
| 8509 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 8510 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 8511 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 8512 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 8513 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 8514 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 8515 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 8516 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 8517 | } |
| 8518 | /* Map 16-bit characters to '\uxxxx' */ |
| 8519 | else { |
| 8520 | *p++ = '\\'; |
| 8521 | *p++ = 'u'; |
| 8522 | *p++ = hexdigits[(ucs >> 12) & 0x000F]; |
| 8523 | *p++ = hexdigits[(ucs >> 8) & 0x000F]; |
| 8524 | *p++ = hexdigits[(ucs >> 4) & 0x000F]; |
| 8525 | *p++ = hexdigits[ucs & 0x000F]; |
| 8526 | } |
| 8527 | } |
| 8528 | /* Copy characters as-is */ |
| 8529 | else { |
| 8530 | *p++ = ch; |
| 8531 | #ifndef Py_UNICODE_WIDE |
| 8532 | if (ucs >= 0x10000) |
| 8533 | *p++ = ch2; |
| 8534 | #endif |
| 8535 | } |
| 8536 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8537 | } |
| 8538 | /* Add quote */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8539 | *p++ = PyUnicode_AS_UNICODE(repr)[0]; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8540 | |
| 8541 | *p = '\0'; |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 8542 | PyUnicode_Resize(&repr, p - PyUnicode_AS_UNICODE(repr)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8543 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8544 | } |
| 8545 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8546 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8547 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8548 | \n\ |
| 8549 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 8550 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8551 | arguments start and end are interpreted as in slice notation.\n\ |
| 8552 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8553 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8554 | |
| 8555 | static PyObject * |
| 8556 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 8557 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 8558 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8559 | Py_ssize_t start; |
| 8560 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8561 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8562 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 8563 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 8564 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8565 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8566 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8567 | result = stringlib_rfind_slice( |
| 8568 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 8569 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 8570 | start, end |
| 8571 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8572 | |
| 8573 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8574 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8575 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8576 | } |
| 8577 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8578 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8579 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8580 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8581 | 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] | 8582 | |
| 8583 | static PyObject * |
| 8584 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 8585 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 8586 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8587 | Py_ssize_t start; |
| 8588 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8589 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8590 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 8591 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 8592 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8593 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8594 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8595 | result = stringlib_rfind_slice( |
| 8596 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 8597 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 8598 | start, end |
| 8599 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8600 | |
| 8601 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8602 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8603 | if (result < 0) { |
| 8604 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 8605 | return NULL; |
| 8606 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8607 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8608 | } |
| 8609 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8610 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8611 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8612 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 8613 | 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] | 8614 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8615 | |
| 8616 | static PyObject * |
| 8617 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 8618 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8619 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 8620 | Py_UNICODE fillchar = ' '; |
| 8621 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8622 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8623 | return NULL; |
| 8624 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 8625 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8626 | Py_INCREF(self); |
| 8627 | return (PyObject*) self; |
| 8628 | } |
| 8629 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 8630 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8631 | } |
| 8632 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8633 | PyObject *PyUnicode_Split(PyObject *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8634 | PyObject *sep, |
| 8635 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8636 | { |
| 8637 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8638 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8639 | s = PyUnicode_FromObject(s); |
| 8640 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8641 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8642 | if (sep != NULL) { |
| 8643 | sep = PyUnicode_FromObject(sep); |
| 8644 | if (sep == NULL) { |
| 8645 | Py_DECREF(s); |
| 8646 | return NULL; |
| 8647 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8648 | } |
| 8649 | |
| 8650 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 8651 | |
| 8652 | Py_DECREF(s); |
| 8653 | Py_XDECREF(sep); |
| 8654 | return result; |
| 8655 | } |
| 8656 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8657 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8658 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8659 | \n\ |
| 8660 | Return a list of the words in S, using sep as the\n\ |
| 8661 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 8662 | 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] | 8663 | whitespace string is a separator and empty strings are\n\ |
| 8664 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8665 | |
| 8666 | static PyObject* |
| 8667 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 8668 | { |
| 8669 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8670 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8671 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8672 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8673 | return NULL; |
| 8674 | |
| 8675 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8676 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8677 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8678 | return split(self, (PyUnicodeObject *)substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8679 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8680 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8681 | } |
| 8682 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8683 | PyObject * |
| 8684 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 8685 | { |
| 8686 | PyObject* str_obj; |
| 8687 | PyObject* sep_obj; |
| 8688 | PyObject* out; |
| 8689 | |
| 8690 | str_obj = PyUnicode_FromObject(str_in); |
| 8691 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8692 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8693 | sep_obj = PyUnicode_FromObject(sep_in); |
| 8694 | if (!sep_obj) { |
| 8695 | Py_DECREF(str_obj); |
| 8696 | return NULL; |
| 8697 | } |
| 8698 | |
| 8699 | out = stringlib_partition( |
| 8700 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 8701 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 8702 | ); |
| 8703 | |
| 8704 | Py_DECREF(sep_obj); |
| 8705 | Py_DECREF(str_obj); |
| 8706 | |
| 8707 | return out; |
| 8708 | } |
| 8709 | |
| 8710 | |
| 8711 | PyObject * |
| 8712 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 8713 | { |
| 8714 | PyObject* str_obj; |
| 8715 | PyObject* sep_obj; |
| 8716 | PyObject* out; |
| 8717 | |
| 8718 | str_obj = PyUnicode_FromObject(str_in); |
| 8719 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8720 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8721 | sep_obj = PyUnicode_FromObject(sep_in); |
| 8722 | if (!sep_obj) { |
| 8723 | Py_DECREF(str_obj); |
| 8724 | return NULL; |
| 8725 | } |
| 8726 | |
| 8727 | out = stringlib_rpartition( |
| 8728 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 8729 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 8730 | ); |
| 8731 | |
| 8732 | Py_DECREF(sep_obj); |
| 8733 | Py_DECREF(str_obj); |
| 8734 | |
| 8735 | return out; |
| 8736 | } |
| 8737 | |
| 8738 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8739 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8740 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8741 | 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] | 8742 | 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] | 8743 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8744 | |
| 8745 | static PyObject* |
| 8746 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
| 8747 | { |
| 8748 | return PyUnicode_Partition((PyObject *)self, separator); |
| 8749 | } |
| 8750 | |
| 8751 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 8752 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8753 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8754 | 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] | 8755 | 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] | 8756 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8757 | |
| 8758 | static PyObject* |
| 8759 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 8760 | { |
| 8761 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 8762 | } |
| 8763 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8764 | PyObject *PyUnicode_RSplit(PyObject *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8765 | PyObject *sep, |
| 8766 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8767 | { |
| 8768 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8769 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8770 | s = PyUnicode_FromObject(s); |
| 8771 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8772 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8773 | if (sep != NULL) { |
| 8774 | sep = PyUnicode_FromObject(sep); |
| 8775 | if (sep == NULL) { |
| 8776 | Py_DECREF(s); |
| 8777 | return NULL; |
| 8778 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8779 | } |
| 8780 | |
| 8781 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 8782 | |
| 8783 | Py_DECREF(s); |
| 8784 | Py_XDECREF(sep); |
| 8785 | return result; |
| 8786 | } |
| 8787 | |
| 8788 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8789 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8790 | \n\ |
| 8791 | Return a list of the words in S, using sep as the\n\ |
| 8792 | delimiter string, starting at the end of the string and\n\ |
| 8793 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 8794 | splits are done. If sep is not specified, any whitespace string\n\ |
| 8795 | is a separator."); |
| 8796 | |
| 8797 | static PyObject* |
| 8798 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 8799 | { |
| 8800 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8801 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8802 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8803 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8804 | return NULL; |
| 8805 | |
| 8806 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8807 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8808 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8809 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8810 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8811 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8812 | } |
| 8813 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8814 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8815 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8816 | \n\ |
| 8817 | 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] | 8818 | 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] | 8819 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8820 | |
| 8821 | static PyObject* |
| 8822 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 8823 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8824 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8825 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8826 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8827 | return NULL; |
| 8828 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8829 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8830 | } |
| 8831 | |
| 8832 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 8833 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8834 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 8835 | if (PyUnicode_CheckExact(self)) { |
| 8836 | Py_INCREF(self); |
| 8837 | return self; |
| 8838 | } else |
| 8839 | /* Subtype -- return genuine unicode string with the same value. */ |
| 8840 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(self), |
| 8841 | PyUnicode_GET_SIZE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8842 | } |
| 8843 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8844 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8845 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8846 | \n\ |
| 8847 | 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] | 8848 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8849 | |
| 8850 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8851 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8852 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8853 | return fixup(self, fixswapcase); |
| 8854 | } |
| 8855 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8856 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8857 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8858 | \n\ |
| 8859 | Return a translation table usable for str.translate().\n\ |
| 8860 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 8861 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8862 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8863 | If there are two arguments, they must be strings of equal length, and\n\ |
| 8864 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 8865 | character at the same position in y. If there is a third argument, it\n\ |
| 8866 | must be a string, whose characters will be mapped to None in the result."); |
| 8867 | |
| 8868 | static PyObject* |
| 8869 | unicode_maketrans(PyUnicodeObject *null, PyObject *args) |
| 8870 | { |
| 8871 | PyObject *x, *y = NULL, *z = NULL; |
| 8872 | PyObject *new = NULL, *key, *value; |
| 8873 | Py_ssize_t i = 0; |
| 8874 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8875 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8876 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 8877 | return NULL; |
| 8878 | new = PyDict_New(); |
| 8879 | if (!new) |
| 8880 | return NULL; |
| 8881 | if (y != NULL) { |
| 8882 | /* x must be a string too, of equal length */ |
| 8883 | Py_ssize_t ylen = PyUnicode_GET_SIZE(y); |
| 8884 | if (!PyUnicode_Check(x)) { |
| 8885 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 8886 | "be a string if there is a second argument"); |
| 8887 | goto err; |
| 8888 | } |
| 8889 | if (PyUnicode_GET_SIZE(x) != ylen) { |
| 8890 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 8891 | "arguments must have equal length"); |
| 8892 | goto err; |
| 8893 | } |
| 8894 | /* create entries for translating chars in x to those in y */ |
| 8895 | for (i = 0; i < PyUnicode_GET_SIZE(x); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8896 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(x)[i]); |
| 8897 | value = PyLong_FromLong(PyUnicode_AS_UNICODE(y)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8898 | if (!key || !value) |
| 8899 | goto err; |
| 8900 | res = PyDict_SetItem(new, key, value); |
| 8901 | Py_DECREF(key); |
| 8902 | Py_DECREF(value); |
| 8903 | if (res < 0) |
| 8904 | goto err; |
| 8905 | } |
| 8906 | /* create entries for deleting chars in z */ |
| 8907 | if (z != NULL) { |
| 8908 | for (i = 0; i < PyUnicode_GET_SIZE(z); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8909 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(z)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8910 | if (!key) |
| 8911 | goto err; |
| 8912 | res = PyDict_SetItem(new, key, Py_None); |
| 8913 | Py_DECREF(key); |
| 8914 | if (res < 0) |
| 8915 | goto err; |
| 8916 | } |
| 8917 | } |
| 8918 | } else { |
| 8919 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 8920 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8921 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 8922 | "to maketrans it must be a dict"); |
| 8923 | goto err; |
| 8924 | } |
| 8925 | /* copy entries into the new dict, converting string keys to int keys */ |
| 8926 | while (PyDict_Next(x, &i, &key, &value)) { |
| 8927 | if (PyUnicode_Check(key)) { |
| 8928 | /* convert string keys to integer keys */ |
| 8929 | PyObject *newkey; |
| 8930 | if (PyUnicode_GET_SIZE(key) != 1) { |
| 8931 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 8932 | "table must be of length 1"); |
| 8933 | goto err; |
| 8934 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8935 | newkey = PyLong_FromLong(PyUnicode_AS_UNICODE(key)[0]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8936 | if (!newkey) |
| 8937 | goto err; |
| 8938 | res = PyDict_SetItem(new, newkey, value); |
| 8939 | Py_DECREF(newkey); |
| 8940 | if (res < 0) |
| 8941 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8942 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8943 | /* just keep integer keys */ |
| 8944 | if (PyDict_SetItem(new, key, value) < 0) |
| 8945 | goto err; |
| 8946 | } else { |
| 8947 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 8948 | "be strings or integers"); |
| 8949 | goto err; |
| 8950 | } |
| 8951 | } |
| 8952 | } |
| 8953 | return new; |
| 8954 | err: |
| 8955 | Py_DECREF(new); |
| 8956 | return NULL; |
| 8957 | } |
| 8958 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8959 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8960 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8961 | \n\ |
| 8962 | Return a copy of the string S, where all characters have been mapped\n\ |
| 8963 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8964 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 8965 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 8966 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8967 | |
| 8968 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8969 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8970 | { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8971 | return PyUnicode_TranslateCharmap(self->str, self->length, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8972 | } |
| 8973 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8974 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8975 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8976 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8977 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8978 | |
| 8979 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8980 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8981 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8982 | return fixup(self, fixupper); |
| 8983 | } |
| 8984 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8985 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8986 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8987 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 8988 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 8989 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8990 | |
| 8991 | static PyObject * |
| 8992 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 8993 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8994 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8995 | PyUnicodeObject *u; |
| 8996 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8997 | Py_ssize_t width; |
| 8998 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8999 | return NULL; |
| 9000 | |
| 9001 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 9002 | if (PyUnicode_CheckExact(self)) { |
| 9003 | Py_INCREF(self); |
| 9004 | return (PyObject*) self; |
| 9005 | } |
| 9006 | else |
| 9007 | return PyUnicode_FromUnicode( |
| 9008 | PyUnicode_AS_UNICODE(self), |
| 9009 | PyUnicode_GET_SIZE(self) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9010 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9011 | } |
| 9012 | |
| 9013 | fill = width - self->length; |
| 9014 | |
| 9015 | u = pad(self, fill, 0, '0'); |
| 9016 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 9017 | if (u == NULL) |
| 9018 | return NULL; |
| 9019 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9020 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 9021 | /* move sign to beginning of string */ |
| 9022 | u->str[0] = u->str[fill]; |
| 9023 | u->str[fill] = '0'; |
| 9024 | } |
| 9025 | |
| 9026 | return (PyObject*) u; |
| 9027 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9028 | |
| 9029 | #if 0 |
| 9030 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9031 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9032 | { |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 9033 | return PyLong_FromLong(numfree); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9034 | } |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9035 | |
| 9036 | static PyObject * |
| 9037 | unicode__decimal2ascii(PyObject *self) |
| 9038 | { |
| 9039 | return PyUnicode_TransformDecimalToASCII(PyUnicode_AS_UNICODE(self), |
| 9040 | PyUnicode_GET_SIZE(self)); |
| 9041 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9042 | #endif |
| 9043 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9044 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9045 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9046 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 9047 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 9048 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9049 | With optional end, stop comparing S at that position.\n\ |
| 9050 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9051 | |
| 9052 | static PyObject * |
| 9053 | unicode_startswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9054 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9055 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9056 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9057 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9058 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9059 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9060 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9061 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 9062 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9063 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9064 | if (PyTuple_Check(subobj)) { |
| 9065 | Py_ssize_t i; |
| 9066 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 9067 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9068 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9069 | if (substring == NULL) |
| 9070 | return NULL; |
| 9071 | result = tailmatch(self, substring, start, end, -1); |
| 9072 | Py_DECREF(substring); |
| 9073 | if (result) { |
| 9074 | Py_RETURN_TRUE; |
| 9075 | } |
| 9076 | } |
| 9077 | /* nothing matched */ |
| 9078 | Py_RETURN_FALSE; |
| 9079 | } |
| 9080 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 9081 | if (substring == NULL) { |
| 9082 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 9083 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 9084 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9085 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 9086 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9087 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9088 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9089 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9090 | } |
| 9091 | |
| 9092 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9093 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9094 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9095 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 9096 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 9097 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9098 | With optional end, stop comparing S at that position.\n\ |
| 9099 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9100 | |
| 9101 | static PyObject * |
| 9102 | unicode_endswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9103 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9104 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9105 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9106 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9107 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9108 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9109 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9110 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 9111 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9112 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9113 | if (PyTuple_Check(subobj)) { |
| 9114 | Py_ssize_t i; |
| 9115 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 9116 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9117 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9118 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9119 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9120 | result = tailmatch(self, substring, start, end, +1); |
| 9121 | Py_DECREF(substring); |
| 9122 | if (result) { |
| 9123 | Py_RETURN_TRUE; |
| 9124 | } |
| 9125 | } |
| 9126 | Py_RETURN_FALSE; |
| 9127 | } |
| 9128 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 9129 | if (substring == NULL) { |
| 9130 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 9131 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 9132 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9133 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 9134 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9135 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9136 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9137 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9138 | } |
| 9139 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 9140 | #include "stringlib/string_format.h" |
| 9141 | |
| 9142 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9143 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 9144 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 9145 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 9146 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 9147 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 9148 | PyDoc_STRVAR(format_map__doc__, |
| 9149 | "S.format_map(mapping) -> str\n\ |
| 9150 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 9151 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 9152 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 9153 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 9154 | static PyObject * |
| 9155 | unicode__format__(PyObject* self, PyObject* args) |
| 9156 | { |
| 9157 | PyObject *format_spec; |
| 9158 | |
| 9159 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 9160 | return NULL; |
| 9161 | |
| 9162 | return _PyUnicode_FormatAdvanced(self, |
| 9163 | PyUnicode_AS_UNICODE(format_spec), |
| 9164 | PyUnicode_GET_SIZE(format_spec)); |
| 9165 | } |
| 9166 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 9167 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9168 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 9169 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 9170 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 9171 | |
| 9172 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 9173 | unicode__sizeof__(PyUnicodeObject *v) |
| 9174 | { |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 9175 | return PyLong_FromSsize_t(sizeof(PyUnicodeObject) + |
| 9176 | sizeof(Py_UNICODE) * (v->length + 1)); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 9177 | } |
| 9178 | |
| 9179 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9180 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 9181 | |
| 9182 | static PyObject * |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 9183 | unicode_getnewargs(PyUnicodeObject *v) |
| 9184 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9185 | return Py_BuildValue("(u#)", v->str, v->length); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 9186 | } |
| 9187 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9188 | static PyMethodDef unicode_methods[] = { |
| 9189 | |
| 9190 | /* Order is according to common usage: often used methods should |
| 9191 | appear first, since lookup is done sequentially. */ |
| 9192 | |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 9193 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9194 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 9195 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9196 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9197 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 9198 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 9199 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 9200 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 9201 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 9202 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 9203 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9204 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9205 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 9206 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 9207 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 9208 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9209 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 9210 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 9211 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 9212 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9213 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9214 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 9215 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9216 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 9217 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 9218 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 9219 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 9220 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 9221 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 9222 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 9223 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 9224 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 9225 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 9226 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 9227 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 9228 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 9229 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 9230 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 9231 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9232 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 9233 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 9234 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 9235 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 9236 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 9237 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 9238 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 9239 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9240 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9241 | #endif |
| 9242 | |
| 9243 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9244 | /* These methods are just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9245 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9246 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9247 | #endif |
| 9248 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9249 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9250 | {NULL, NULL} |
| 9251 | }; |
| 9252 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 9253 | static PyObject * |
| 9254 | unicode_mod(PyObject *v, PyObject *w) |
| 9255 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9256 | if (!PyUnicode_Check(v)) { |
| 9257 | Py_INCREF(Py_NotImplemented); |
| 9258 | return Py_NotImplemented; |
| 9259 | } |
| 9260 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 9261 | } |
| 9262 | |
| 9263 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9264 | 0, /*nb_add*/ |
| 9265 | 0, /*nb_subtract*/ |
| 9266 | 0, /*nb_multiply*/ |
| 9267 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 9268 | }; |
| 9269 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9270 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9271 | (lenfunc) unicode_length, /* sq_length */ |
| 9272 | PyUnicode_Concat, /* sq_concat */ |
| 9273 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 9274 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 9275 | 0, /* sq_slice */ |
| 9276 | 0, /* sq_ass_item */ |
| 9277 | 0, /* sq_ass_slice */ |
| 9278 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9279 | }; |
| 9280 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9281 | static PyObject* |
| 9282 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 9283 | { |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9284 | if (PyIndex_Check(item)) { |
| 9285 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9286 | if (i == -1 && PyErr_Occurred()) |
| 9287 | return NULL; |
| 9288 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 9289 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9290 | return unicode_getitem(self, i); |
| 9291 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9292 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9293 | Py_UNICODE* source_buf; |
| 9294 | Py_UNICODE* result_buf; |
| 9295 | PyObject* result; |
| 9296 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 9297 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_SIZE(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9298 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9299 | return NULL; |
| 9300 | } |
| 9301 | |
| 9302 | if (slicelength <= 0) { |
| 9303 | return PyUnicode_FromUnicode(NULL, 0); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 9304 | } else if (start == 0 && step == 1 && slicelength == self->length && |
| 9305 | PyUnicode_CheckExact(self)) { |
| 9306 | Py_INCREF(self); |
| 9307 | return (PyObject *)self; |
| 9308 | } else if (step == 1) { |
| 9309 | return PyUnicode_FromUnicode(self->str + start, slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9310 | } else { |
| 9311 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 9312 | result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* |
| 9313 | sizeof(Py_UNICODE)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9314 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9315 | if (result_buf == NULL) |
| 9316 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9317 | |
| 9318 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 9319 | result_buf[i] = source_buf[cur]; |
| 9320 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9321 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9322 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 9323 | PyObject_FREE(result_buf); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9324 | return result; |
| 9325 | } |
| 9326 | } else { |
| 9327 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 9328 | return NULL; |
| 9329 | } |
| 9330 | } |
| 9331 | |
| 9332 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9333 | (lenfunc)unicode_length, /* mp_length */ |
| 9334 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 9335 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9336 | }; |
| 9337 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9338 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9339 | /* Helpers for PyUnicode_Format() */ |
| 9340 | |
| 9341 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9342 | 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] | 9343 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9344 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9345 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9346 | (*p_argidx)++; |
| 9347 | if (arglen < 0) |
| 9348 | return args; |
| 9349 | else |
| 9350 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9351 | } |
| 9352 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9353 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9354 | return NULL; |
| 9355 | } |
| 9356 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9357 | /* 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] | 9358 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9359 | static PyObject * |
| 9360 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9361 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9362 | char *p; |
| 9363 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9364 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9365 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9366 | x = PyFloat_AsDouble(v); |
| 9367 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9368 | return NULL; |
| 9369 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9370 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9371 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 9372 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 9373 | p = PyOS_double_to_string(x, type, prec, |
| 9374 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9375 | if (p == NULL) |
| 9376 | return NULL; |
| 9377 | result = PyUnicode_FromStringAndSize(p, strlen(p)); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 9378 | PyMem_Free(p); |
| 9379 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9380 | } |
| 9381 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9382 | static PyObject* |
| 9383 | formatlong(PyObject *val, int flags, int prec, int type) |
| 9384 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9385 | char *buf; |
| 9386 | int len; |
| 9387 | PyObject *str; /* temporary string object. */ |
| 9388 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9389 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9390 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 9391 | if (!str) |
| 9392 | return NULL; |
| 9393 | result = PyUnicode_FromStringAndSize(buf, len); |
| 9394 | Py_DECREF(str); |
| 9395 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9396 | } |
| 9397 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9398 | static int |
| 9399 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9400 | size_t buflen, |
| 9401 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9402 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 9403 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 9404 | if (PyUnicode_Check(v)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9405 | if (PyUnicode_GET_SIZE(v) == 1) { |
| 9406 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
| 9407 | buf[1] = '\0'; |
| 9408 | return 1; |
| 9409 | } |
| 9410 | #ifndef Py_UNICODE_WIDE |
| 9411 | if (PyUnicode_GET_SIZE(v) == 2) { |
| 9412 | /* Decode a valid surrogate pair */ |
| 9413 | int c0 = PyUnicode_AS_UNICODE(v)[0]; |
| 9414 | int c1 = PyUnicode_AS_UNICODE(v)[1]; |
| 9415 | if (0xD800 <= c0 && c0 <= 0xDBFF && |
| 9416 | 0xDC00 <= c1 && c1 <= 0xDFFF) { |
| 9417 | buf[0] = c0; |
| 9418 | buf[1] = c1; |
| 9419 | buf[2] = '\0'; |
| 9420 | return 2; |
| 9421 | } |
| 9422 | } |
| 9423 | #endif |
| 9424 | goto onError; |
| 9425 | } |
| 9426 | else { |
| 9427 | /* Integer input truncated to a character */ |
| 9428 | long x; |
| 9429 | x = PyLong_AsLong(v); |
| 9430 | if (x == -1 && PyErr_Occurred()) |
| 9431 | goto onError; |
| 9432 | |
| 9433 | if (x < 0 || x > 0x10ffff) { |
| 9434 | PyErr_SetString(PyExc_OverflowError, |
| 9435 | "%c arg not in range(0x110000)"); |
| 9436 | return -1; |
| 9437 | } |
| 9438 | |
| 9439 | #ifndef Py_UNICODE_WIDE |
| 9440 | if (x > 0xffff) { |
| 9441 | x -= 0x10000; |
| 9442 | buf[0] = (Py_UNICODE)(0xD800 | (x >> 10)); |
| 9443 | buf[1] = (Py_UNICODE)(0xDC00 | (x & 0x3FF)); |
| 9444 | return 2; |
| 9445 | } |
| 9446 | #endif |
| 9447 | buf[0] = (Py_UNICODE) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9448 | buf[1] = '\0'; |
| 9449 | return 1; |
| 9450 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 9451 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9452 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 9453 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9454 | "%c requires int or char"); |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 9455 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9456 | } |
| 9457 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9458 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9459 | 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] | 9460 | */ |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9461 | #define FORMATBUFLEN (size_t)10 |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9462 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9463 | PyObject *PyUnicode_Format(PyObject *format, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9464 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9465 | { |
| 9466 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9467 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9468 | int args_owned = 0; |
| 9469 | PyUnicodeObject *result = NULL; |
| 9470 | PyObject *dict = NULL; |
| 9471 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9472 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9473 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9474 | PyErr_BadInternalCall(); |
| 9475 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9476 | } |
| 9477 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 9478 | if (uformat == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9479 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9480 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 9481 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 9482 | |
| 9483 | reslen = rescnt = fmtcnt + 100; |
| 9484 | result = _PyUnicode_New(reslen); |
| 9485 | if (result == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9486 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9487 | res = PyUnicode_AS_UNICODE(result); |
| 9488 | |
| 9489 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9490 | arglen = PyTuple_Size(args); |
| 9491 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9492 | } |
| 9493 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9494 | arglen = -1; |
| 9495 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9496 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 9497 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 9498 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9499 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9500 | |
| 9501 | while (--fmtcnt >= 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9502 | if (*fmt != '%') { |
| 9503 | if (--rescnt < 0) { |
| 9504 | rescnt = fmtcnt + 100; |
| 9505 | reslen += rescnt; |
| 9506 | if (_PyUnicode_Resize(&result, reslen) < 0) |
| 9507 | goto onError; |
| 9508 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 9509 | --rescnt; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9510 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9511 | *res++ = *fmt++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9512 | } |
| 9513 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9514 | /* Got a format specifier */ |
| 9515 | int flags = 0; |
| 9516 | Py_ssize_t width = -1; |
| 9517 | int prec = -1; |
| 9518 | Py_UNICODE c = '\0'; |
| 9519 | Py_UNICODE fill; |
| 9520 | int isnumok; |
| 9521 | PyObject *v = NULL; |
| 9522 | PyObject *temp = NULL; |
| 9523 | Py_UNICODE *pbuf; |
| 9524 | Py_UNICODE sign; |
| 9525 | Py_ssize_t len; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9526 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For formatchar() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9527 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9528 | fmt++; |
| 9529 | if (*fmt == '(') { |
| 9530 | Py_UNICODE *keystart; |
| 9531 | Py_ssize_t keylen; |
| 9532 | PyObject *key; |
| 9533 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 9534 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9535 | if (dict == NULL) { |
| 9536 | PyErr_SetString(PyExc_TypeError, |
| 9537 | "format requires a mapping"); |
| 9538 | goto onError; |
| 9539 | } |
| 9540 | ++fmt; |
| 9541 | --fmtcnt; |
| 9542 | keystart = fmt; |
| 9543 | /* Skip over balanced parentheses */ |
| 9544 | while (pcount > 0 && --fmtcnt >= 0) { |
| 9545 | if (*fmt == ')') |
| 9546 | --pcount; |
| 9547 | else if (*fmt == '(') |
| 9548 | ++pcount; |
| 9549 | fmt++; |
| 9550 | } |
| 9551 | keylen = fmt - keystart - 1; |
| 9552 | if (fmtcnt < 0 || pcount > 0) { |
| 9553 | PyErr_SetString(PyExc_ValueError, |
| 9554 | "incomplete format key"); |
| 9555 | goto onError; |
| 9556 | } |
| 9557 | #if 0 |
| 9558 | /* keys are converted to strings using UTF-8 and |
| 9559 | then looked up since Python uses strings to hold |
| 9560 | variables names etc. in its namespaces and we |
| 9561 | wouldn't want to break common idioms. */ |
| 9562 | key = PyUnicode_EncodeUTF8(keystart, |
| 9563 | keylen, |
| 9564 | NULL); |
| 9565 | #else |
| 9566 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 9567 | #endif |
| 9568 | if (key == NULL) |
| 9569 | goto onError; |
| 9570 | if (args_owned) { |
| 9571 | Py_DECREF(args); |
| 9572 | args_owned = 0; |
| 9573 | } |
| 9574 | args = PyObject_GetItem(dict, key); |
| 9575 | Py_DECREF(key); |
| 9576 | if (args == NULL) { |
| 9577 | goto onError; |
| 9578 | } |
| 9579 | args_owned = 1; |
| 9580 | arglen = -1; |
| 9581 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9582 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9583 | while (--fmtcnt >= 0) { |
| 9584 | switch (c = *fmt++) { |
| 9585 | case '-': flags |= F_LJUST; continue; |
| 9586 | case '+': flags |= F_SIGN; continue; |
| 9587 | case ' ': flags |= F_BLANK; continue; |
| 9588 | case '#': flags |= F_ALT; continue; |
| 9589 | case '0': flags |= F_ZERO; continue; |
| 9590 | } |
| 9591 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9592 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9593 | if (c == '*') { |
| 9594 | v = getnextarg(args, arglen, &argidx); |
| 9595 | if (v == NULL) |
| 9596 | goto onError; |
| 9597 | if (!PyLong_Check(v)) { |
| 9598 | PyErr_SetString(PyExc_TypeError, |
| 9599 | "* wants int"); |
| 9600 | goto onError; |
| 9601 | } |
| 9602 | width = PyLong_AsLong(v); |
| 9603 | if (width == -1 && PyErr_Occurred()) |
| 9604 | goto onError; |
| 9605 | if (width < 0) { |
| 9606 | flags |= F_LJUST; |
| 9607 | width = -width; |
| 9608 | } |
| 9609 | if (--fmtcnt >= 0) |
| 9610 | c = *fmt++; |
| 9611 | } |
| 9612 | else if (c >= '0' && c <= '9') { |
| 9613 | width = c - '0'; |
| 9614 | while (--fmtcnt >= 0) { |
| 9615 | c = *fmt++; |
| 9616 | if (c < '0' || c > '9') |
| 9617 | break; |
| 9618 | if ((width*10) / 10 != width) { |
| 9619 | PyErr_SetString(PyExc_ValueError, |
| 9620 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9621 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9622 | } |
| 9623 | width = width*10 + (c - '0'); |
| 9624 | } |
| 9625 | } |
| 9626 | if (c == '.') { |
| 9627 | prec = 0; |
| 9628 | if (--fmtcnt >= 0) |
| 9629 | c = *fmt++; |
| 9630 | if (c == '*') { |
| 9631 | v = getnextarg(args, arglen, &argidx); |
| 9632 | if (v == NULL) |
| 9633 | goto onError; |
| 9634 | if (!PyLong_Check(v)) { |
| 9635 | PyErr_SetString(PyExc_TypeError, |
| 9636 | "* wants int"); |
| 9637 | goto onError; |
| 9638 | } |
| 9639 | prec = PyLong_AsLong(v); |
| 9640 | if (prec == -1 && PyErr_Occurred()) |
| 9641 | goto onError; |
| 9642 | if (prec < 0) |
| 9643 | prec = 0; |
| 9644 | if (--fmtcnt >= 0) |
| 9645 | c = *fmt++; |
| 9646 | } |
| 9647 | else if (c >= '0' && c <= '9') { |
| 9648 | prec = c - '0'; |
| 9649 | while (--fmtcnt >= 0) { |
Stefan Krah | 99212f6 | 2010-07-19 17:58:26 +0000 | [diff] [blame] | 9650 | c = *fmt++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9651 | if (c < '0' || c > '9') |
| 9652 | break; |
| 9653 | if ((prec*10) / 10 != prec) { |
| 9654 | PyErr_SetString(PyExc_ValueError, |
| 9655 | "prec too big"); |
| 9656 | goto onError; |
| 9657 | } |
| 9658 | prec = prec*10 + (c - '0'); |
| 9659 | } |
| 9660 | } |
| 9661 | } /* prec */ |
| 9662 | if (fmtcnt >= 0) { |
| 9663 | if (c == 'h' || c == 'l' || c == 'L') { |
| 9664 | if (--fmtcnt >= 0) |
| 9665 | c = *fmt++; |
| 9666 | } |
| 9667 | } |
| 9668 | if (fmtcnt < 0) { |
| 9669 | PyErr_SetString(PyExc_ValueError, |
| 9670 | "incomplete format"); |
| 9671 | goto onError; |
| 9672 | } |
| 9673 | if (c != '%') { |
| 9674 | v = getnextarg(args, arglen, &argidx); |
| 9675 | if (v == NULL) |
| 9676 | goto onError; |
| 9677 | } |
| 9678 | sign = 0; |
| 9679 | fill = ' '; |
| 9680 | switch (c) { |
| 9681 | |
| 9682 | case '%': |
| 9683 | pbuf = formatbuf; |
| 9684 | /* presume that buffer length is at least 1 */ |
| 9685 | pbuf[0] = '%'; |
| 9686 | len = 1; |
| 9687 | break; |
| 9688 | |
| 9689 | case 's': |
| 9690 | case 'r': |
| 9691 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 9692 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9693 | temp = v; |
| 9694 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9695 | } |
| 9696 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9697 | if (c == 's') |
| 9698 | temp = PyObject_Str(v); |
| 9699 | else if (c == 'r') |
| 9700 | temp = PyObject_Repr(v); |
| 9701 | else |
| 9702 | temp = PyObject_ASCII(v); |
| 9703 | if (temp == NULL) |
| 9704 | goto onError; |
| 9705 | if (PyUnicode_Check(temp)) |
| 9706 | /* nothing to do */; |
| 9707 | else { |
| 9708 | Py_DECREF(temp); |
| 9709 | PyErr_SetString(PyExc_TypeError, |
| 9710 | "%s argument has non-string str()"); |
| 9711 | goto onError; |
| 9712 | } |
| 9713 | } |
| 9714 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9715 | len = PyUnicode_GET_SIZE(temp); |
| 9716 | if (prec >= 0 && len > prec) |
| 9717 | len = prec; |
| 9718 | break; |
| 9719 | |
| 9720 | case 'i': |
| 9721 | case 'd': |
| 9722 | case 'u': |
| 9723 | case 'o': |
| 9724 | case 'x': |
| 9725 | case 'X': |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9726 | isnumok = 0; |
| 9727 | if (PyNumber_Check(v)) { |
| 9728 | PyObject *iobj=NULL; |
| 9729 | |
| 9730 | if (PyLong_Check(v)) { |
| 9731 | iobj = v; |
| 9732 | Py_INCREF(iobj); |
| 9733 | } |
| 9734 | else { |
| 9735 | iobj = PyNumber_Long(v); |
| 9736 | } |
| 9737 | if (iobj!=NULL) { |
| 9738 | if (PyLong_Check(iobj)) { |
| 9739 | isnumok = 1; |
Senthil Kumaran | 9ebe08d | 2011-07-03 21:03:16 -0700 | [diff] [blame] | 9740 | temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9741 | Py_DECREF(iobj); |
| 9742 | if (!temp) |
| 9743 | goto onError; |
| 9744 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9745 | len = PyUnicode_GET_SIZE(temp); |
| 9746 | sign = 1; |
| 9747 | } |
| 9748 | else { |
| 9749 | Py_DECREF(iobj); |
| 9750 | } |
| 9751 | } |
| 9752 | } |
| 9753 | if (!isnumok) { |
| 9754 | PyErr_Format(PyExc_TypeError, |
| 9755 | "%%%c format: a number is required, " |
| 9756 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 9757 | goto onError; |
| 9758 | } |
| 9759 | if (flags & F_ZERO) |
| 9760 | fill = '0'; |
| 9761 | break; |
| 9762 | |
| 9763 | case 'e': |
| 9764 | case 'E': |
| 9765 | case 'f': |
| 9766 | case 'F': |
| 9767 | case 'g': |
| 9768 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9769 | temp = formatfloat(v, flags, prec, c); |
| 9770 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9771 | goto onError; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9772 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9773 | len = PyUnicode_GET_SIZE(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9774 | sign = 1; |
| 9775 | if (flags & F_ZERO) |
| 9776 | fill = '0'; |
| 9777 | break; |
| 9778 | |
| 9779 | case 'c': |
| 9780 | pbuf = formatbuf; |
| 9781 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
| 9782 | if (len < 0) |
| 9783 | goto onError; |
| 9784 | break; |
| 9785 | |
| 9786 | default: |
| 9787 | PyErr_Format(PyExc_ValueError, |
| 9788 | "unsupported format character '%c' (0x%x) " |
| 9789 | "at index %zd", |
| 9790 | (31<=c && c<=126) ? (char)c : '?', |
| 9791 | (int)c, |
| 9792 | (Py_ssize_t)(fmt - 1 - |
| 9793 | PyUnicode_AS_UNICODE(uformat))); |
| 9794 | goto onError; |
| 9795 | } |
| 9796 | if (sign) { |
| 9797 | if (*pbuf == '-' || *pbuf == '+') { |
| 9798 | sign = *pbuf++; |
| 9799 | len--; |
| 9800 | } |
| 9801 | else if (flags & F_SIGN) |
| 9802 | sign = '+'; |
| 9803 | else if (flags & F_BLANK) |
| 9804 | sign = ' '; |
| 9805 | else |
| 9806 | sign = 0; |
| 9807 | } |
| 9808 | if (width < len) |
| 9809 | width = len; |
| 9810 | if (rescnt - (sign != 0) < width) { |
| 9811 | reslen -= rescnt; |
| 9812 | rescnt = width + fmtcnt + 100; |
| 9813 | reslen += rescnt; |
| 9814 | if (reslen < 0) { |
| 9815 | Py_XDECREF(temp); |
| 9816 | PyErr_NoMemory(); |
| 9817 | goto onError; |
| 9818 | } |
| 9819 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 9820 | Py_XDECREF(temp); |
| 9821 | goto onError; |
| 9822 | } |
| 9823 | res = PyUnicode_AS_UNICODE(result) |
| 9824 | + reslen - rescnt; |
| 9825 | } |
| 9826 | if (sign) { |
| 9827 | if (fill != ' ') |
| 9828 | *res++ = sign; |
| 9829 | rescnt--; |
| 9830 | if (width > len) |
| 9831 | width--; |
| 9832 | } |
| 9833 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
| 9834 | assert(pbuf[0] == '0'); |
| 9835 | assert(pbuf[1] == c); |
| 9836 | if (fill != ' ') { |
| 9837 | *res++ = *pbuf++; |
| 9838 | *res++ = *pbuf++; |
| 9839 | } |
| 9840 | rescnt -= 2; |
| 9841 | width -= 2; |
| 9842 | if (width < 0) |
| 9843 | width = 0; |
| 9844 | len -= 2; |
| 9845 | } |
| 9846 | if (width > len && !(flags & F_LJUST)) { |
| 9847 | do { |
| 9848 | --rescnt; |
| 9849 | *res++ = fill; |
| 9850 | } while (--width > len); |
| 9851 | } |
| 9852 | if (fill == ' ') { |
| 9853 | if (sign) |
| 9854 | *res++ = sign; |
| 9855 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
| 9856 | assert(pbuf[0] == '0'); |
| 9857 | assert(pbuf[1] == c); |
| 9858 | *res++ = *pbuf++; |
| 9859 | *res++ = *pbuf++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9860 | } |
| 9861 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9862 | Py_UNICODE_COPY(res, pbuf, len); |
| 9863 | res += len; |
| 9864 | rescnt -= len; |
| 9865 | while (--width >= len) { |
| 9866 | --rescnt; |
| 9867 | *res++ = ' '; |
| 9868 | } |
| 9869 | if (dict && (argidx < arglen) && c != '%') { |
| 9870 | PyErr_SetString(PyExc_TypeError, |
| 9871 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9872 | Py_XDECREF(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9873 | goto onError; |
| 9874 | } |
| 9875 | Py_XDECREF(temp); |
| 9876 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9877 | } /* until end */ |
| 9878 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9879 | PyErr_SetString(PyExc_TypeError, |
| 9880 | "not all arguments converted during string formatting"); |
| 9881 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9882 | } |
| 9883 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9884 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9885 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9886 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9887 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9888 | } |
| 9889 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9890 | return (PyObject *)result; |
| 9891 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9892 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9893 | Py_XDECREF(result); |
| 9894 | Py_DECREF(uformat); |
| 9895 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9896 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9897 | } |
| 9898 | return NULL; |
| 9899 | } |
| 9900 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 9901 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9902 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 9903 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9904 | static PyObject * |
| 9905 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9906 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9907 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9908 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 9909 | char *encoding = NULL; |
| 9910 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9911 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9912 | if (type != &PyUnicode_Type) |
| 9913 | return unicode_subtype_new(type, args, kwds); |
| 9914 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9915 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9916 | return NULL; |
| 9917 | if (x == NULL) |
| 9918 | return (PyObject *)_PyUnicode_New(0); |
| 9919 | if (encoding == NULL && errors == NULL) |
| 9920 | return PyObject_Str(x); |
| 9921 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9922 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9923 | } |
| 9924 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9925 | static PyObject * |
| 9926 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9927 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9928 | PyUnicodeObject *tmp, *pnew; |
| 9929 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9930 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9931 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 9932 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 9933 | if (tmp == NULL) |
| 9934 | return NULL; |
| 9935 | assert(PyUnicode_Check(tmp)); |
| 9936 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
| 9937 | if (pnew == NULL) { |
| 9938 | Py_DECREF(tmp); |
| 9939 | return NULL; |
| 9940 | } |
| 9941 | pnew->str = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1)); |
| 9942 | if (pnew->str == NULL) { |
| 9943 | _Py_ForgetReference((PyObject *)pnew); |
| 9944 | PyObject_Del(pnew); |
| 9945 | Py_DECREF(tmp); |
| 9946 | return PyErr_NoMemory(); |
| 9947 | } |
| 9948 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 9949 | pnew->length = n; |
| 9950 | pnew->hash = tmp->hash; |
| 9951 | Py_DECREF(tmp); |
| 9952 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9953 | } |
| 9954 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9955 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9956 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9957 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 9958 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 9959 | encoding defaults to the current default string encoding.\n\ |
| 9960 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9961 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9962 | static PyObject *unicode_iter(PyObject *seq); |
| 9963 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9964 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 9965 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9966 | "str", /* tp_name */ |
| 9967 | sizeof(PyUnicodeObject), /* tp_size */ |
| 9968 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9969 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9970 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 9971 | 0, /* tp_print */ |
| 9972 | 0, /* tp_getattr */ |
| 9973 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 9974 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9975 | unicode_repr, /* tp_repr */ |
| 9976 | &unicode_as_number, /* tp_as_number */ |
| 9977 | &unicode_as_sequence, /* tp_as_sequence */ |
| 9978 | &unicode_as_mapping, /* tp_as_mapping */ |
| 9979 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 9980 | 0, /* tp_call*/ |
| 9981 | (reprfunc) unicode_str, /* tp_str */ |
| 9982 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9983 | 0, /* tp_setattro */ |
| 9984 | 0, /* tp_as_buffer */ |
| 9985 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9986 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9987 | unicode_doc, /* tp_doc */ |
| 9988 | 0, /* tp_traverse */ |
| 9989 | 0, /* tp_clear */ |
| 9990 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 9991 | 0, /* tp_weaklistoffset */ |
| 9992 | unicode_iter, /* tp_iter */ |
| 9993 | 0, /* tp_iternext */ |
| 9994 | unicode_methods, /* tp_methods */ |
| 9995 | 0, /* tp_members */ |
| 9996 | 0, /* tp_getset */ |
| 9997 | &PyBaseObject_Type, /* tp_base */ |
| 9998 | 0, /* tp_dict */ |
| 9999 | 0, /* tp_descr_get */ |
| 10000 | 0, /* tp_descr_set */ |
| 10001 | 0, /* tp_dictoffset */ |
| 10002 | 0, /* tp_init */ |
| 10003 | 0, /* tp_alloc */ |
| 10004 | unicode_new, /* tp_new */ |
| 10005 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10006 | }; |
| 10007 | |
| 10008 | /* Initialize the Unicode implementation */ |
| 10009 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 10010 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10011 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 10012 | int i; |
| 10013 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10014 | /* XXX - move this array to unicodectype.c ? */ |
| 10015 | Py_UNICODE linebreak[] = { |
| 10016 | 0x000A, /* LINE FEED */ |
| 10017 | 0x000D, /* CARRIAGE RETURN */ |
| 10018 | 0x001C, /* FILE SEPARATOR */ |
| 10019 | 0x001D, /* GROUP SEPARATOR */ |
| 10020 | 0x001E, /* RECORD SEPARATOR */ |
| 10021 | 0x0085, /* NEXT LINE */ |
| 10022 | 0x2028, /* LINE SEPARATOR */ |
| 10023 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 10024 | }; |
| 10025 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 10026 | /* Init the implementation */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 10027 | free_list = NULL; |
| 10028 | numfree = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10029 | unicode_empty = _PyUnicode_New(0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 10030 | if (!unicode_empty) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10031 | return; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 10032 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 10033 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10034 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 10035 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10036 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10037 | |
| 10038 | /* initialize the linebreak bloom filter */ |
| 10039 | bloom_linebreak = make_bloom_mask( |
| 10040 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 10041 | ); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 10042 | |
| 10043 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10044 | } |
| 10045 | |
| 10046 | /* Finalize the Unicode implementation */ |
| 10047 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 10048 | int |
| 10049 | PyUnicode_ClearFreeList(void) |
| 10050 | { |
| 10051 | int freelist_size = numfree; |
| 10052 | PyUnicodeObject *u; |
| 10053 | |
| 10054 | for (u = free_list; u != NULL;) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10055 | PyUnicodeObject *v = u; |
| 10056 | u = *(PyUnicodeObject **)u; |
| 10057 | if (v->str) |
| 10058 | PyObject_DEL(v->str); |
| 10059 | Py_XDECREF(v->defenc); |
| 10060 | PyObject_Del(v); |
| 10061 | numfree--; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 10062 | } |
| 10063 | free_list = NULL; |
| 10064 | assert(numfree == 0); |
| 10065 | return freelist_size; |
| 10066 | } |
| 10067 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10068 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 10069 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10070 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 10071 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10072 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 10073 | Py_XDECREF(unicode_empty); |
| 10074 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 10075 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 10076 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10077 | if (unicode_latin1[i]) { |
| 10078 | Py_DECREF(unicode_latin1[i]); |
| 10079 | unicode_latin1[i] = NULL; |
| 10080 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 10081 | } |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 10082 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10083 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 10084 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 10085 | void |
| 10086 | PyUnicode_InternInPlace(PyObject **p) |
| 10087 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10088 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 10089 | PyObject *t; |
| 10090 | if (s == NULL || !PyUnicode_Check(s)) |
| 10091 | Py_FatalError( |
| 10092 | "PyUnicode_InternInPlace: unicode strings only please!"); |
| 10093 | /* If it's a subclass, we don't really know what putting |
| 10094 | it in the interned dict might do. */ |
| 10095 | if (!PyUnicode_CheckExact(s)) |
| 10096 | return; |
| 10097 | if (PyUnicode_CHECK_INTERNED(s)) |
| 10098 | return; |
| 10099 | if (interned == NULL) { |
| 10100 | interned = PyDict_New(); |
| 10101 | if (interned == NULL) { |
| 10102 | PyErr_Clear(); /* Don't leave an exception */ |
| 10103 | return; |
| 10104 | } |
| 10105 | } |
| 10106 | /* It might be that the GetItem call fails even |
| 10107 | though the key is present in the dictionary, |
| 10108 | namely when this happens during a stack overflow. */ |
| 10109 | Py_ALLOW_RECURSION |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10110 | t = PyDict_GetItem(interned, (PyObject *)s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10111 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10112 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10113 | if (t) { |
| 10114 | Py_INCREF(t); |
| 10115 | Py_DECREF(*p); |
| 10116 | *p = t; |
| 10117 | return; |
| 10118 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 10119 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10120 | PyThreadState_GET()->recursion_critical = 1; |
| 10121 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 10122 | PyErr_Clear(); |
| 10123 | PyThreadState_GET()->recursion_critical = 0; |
| 10124 | return; |
| 10125 | } |
| 10126 | PyThreadState_GET()->recursion_critical = 0; |
| 10127 | /* The two references in interned are not counted by refcnt. |
| 10128 | The deallocator will take care of this */ |
| 10129 | Py_REFCNT(s) -= 2; |
| 10130 | PyUnicode_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 10131 | } |
| 10132 | |
| 10133 | void |
| 10134 | PyUnicode_InternImmortal(PyObject **p) |
| 10135 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10136 | PyUnicode_InternInPlace(p); |
| 10137 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
| 10138 | PyUnicode_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL; |
| 10139 | Py_INCREF(*p); |
| 10140 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 10141 | } |
| 10142 | |
| 10143 | PyObject * |
| 10144 | PyUnicode_InternFromString(const char *cp) |
| 10145 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10146 | PyObject *s = PyUnicode_FromString(cp); |
| 10147 | if (s == NULL) |
| 10148 | return NULL; |
| 10149 | PyUnicode_InternInPlace(&s); |
| 10150 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 10151 | } |
| 10152 | |
| 10153 | void _Py_ReleaseInternedUnicodeStrings(void) |
| 10154 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10155 | PyObject *keys; |
| 10156 | PyUnicodeObject *s; |
| 10157 | Py_ssize_t i, n; |
| 10158 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 10159 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10160 | if (interned == NULL || !PyDict_Check(interned)) |
| 10161 | return; |
| 10162 | keys = PyDict_Keys(interned); |
| 10163 | if (keys == NULL || !PyList_Check(keys)) { |
| 10164 | PyErr_Clear(); |
| 10165 | return; |
| 10166 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 10167 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10168 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 10169 | detector, interned unicode strings are not forcibly deallocated; |
| 10170 | rather, we give them their stolen references back, and then clear |
| 10171 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 10172 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10173 | n = PyList_GET_SIZE(keys); |
| 10174 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10175 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10176 | for (i = 0; i < n; i++) { |
| 10177 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
| 10178 | switch (s->state) { |
| 10179 | case SSTATE_NOT_INTERNED: |
| 10180 | /* XXX Shouldn't happen */ |
| 10181 | break; |
| 10182 | case SSTATE_INTERNED_IMMORTAL: |
| 10183 | Py_REFCNT(s) += 1; |
| 10184 | immortal_size += s->length; |
| 10185 | break; |
| 10186 | case SSTATE_INTERNED_MORTAL: |
| 10187 | Py_REFCNT(s) += 2; |
| 10188 | mortal_size += s->length; |
| 10189 | break; |
| 10190 | default: |
| 10191 | Py_FatalError("Inconsistent interned string state."); |
| 10192 | } |
| 10193 | s->state = SSTATE_NOT_INTERNED; |
| 10194 | } |
| 10195 | fprintf(stderr, "total size of all interned strings: " |
| 10196 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 10197 | "mortal/immortal\n", mortal_size, immortal_size); |
| 10198 | Py_DECREF(keys); |
| 10199 | PyDict_Clear(interned); |
| 10200 | Py_DECREF(interned); |
| 10201 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 10202 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10203 | |
| 10204 | |
| 10205 | /********************* Unicode Iterator **************************/ |
| 10206 | |
| 10207 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10208 | PyObject_HEAD |
| 10209 | Py_ssize_t it_index; |
| 10210 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10211 | } unicodeiterobject; |
| 10212 | |
| 10213 | static void |
| 10214 | unicodeiter_dealloc(unicodeiterobject *it) |
| 10215 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10216 | _PyObject_GC_UNTRACK(it); |
| 10217 | Py_XDECREF(it->it_seq); |
| 10218 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10219 | } |
| 10220 | |
| 10221 | static int |
| 10222 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 10223 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10224 | Py_VISIT(it->it_seq); |
| 10225 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10226 | } |
| 10227 | |
| 10228 | static PyObject * |
| 10229 | unicodeiter_next(unicodeiterobject *it) |
| 10230 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10231 | PyUnicodeObject *seq; |
| 10232 | PyObject *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10233 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10234 | assert(it != NULL); |
| 10235 | seq = it->it_seq; |
| 10236 | if (seq == NULL) |
| 10237 | return NULL; |
| 10238 | assert(PyUnicode_Check(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10239 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10240 | if (it->it_index < PyUnicode_GET_SIZE(seq)) { |
| 10241 | item = PyUnicode_FromUnicode( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10242 | PyUnicode_AS_UNICODE(seq)+it->it_index, 1); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10243 | if (item != NULL) |
| 10244 | ++it->it_index; |
| 10245 | return item; |
| 10246 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10247 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10248 | Py_DECREF(seq); |
| 10249 | it->it_seq = NULL; |
| 10250 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10251 | } |
| 10252 | |
| 10253 | static PyObject * |
| 10254 | unicodeiter_len(unicodeiterobject *it) |
| 10255 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10256 | Py_ssize_t len = 0; |
| 10257 | if (it->it_seq) |
| 10258 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
| 10259 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10260 | } |
| 10261 | |
| 10262 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 10263 | |
| 10264 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10265 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10266 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10267 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10268 | }; |
| 10269 | |
| 10270 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10271 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 10272 | "str_iterator", /* tp_name */ |
| 10273 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 10274 | 0, /* tp_itemsize */ |
| 10275 | /* methods */ |
| 10276 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 10277 | 0, /* tp_print */ |
| 10278 | 0, /* tp_getattr */ |
| 10279 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 10280 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10281 | 0, /* tp_repr */ |
| 10282 | 0, /* tp_as_number */ |
| 10283 | 0, /* tp_as_sequence */ |
| 10284 | 0, /* tp_as_mapping */ |
| 10285 | 0, /* tp_hash */ |
| 10286 | 0, /* tp_call */ |
| 10287 | 0, /* tp_str */ |
| 10288 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 10289 | 0, /* tp_setattro */ |
| 10290 | 0, /* tp_as_buffer */ |
| 10291 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 10292 | 0, /* tp_doc */ |
| 10293 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 10294 | 0, /* tp_clear */ |
| 10295 | 0, /* tp_richcompare */ |
| 10296 | 0, /* tp_weaklistoffset */ |
| 10297 | PyObject_SelfIter, /* tp_iter */ |
| 10298 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 10299 | unicodeiter_methods, /* tp_methods */ |
| 10300 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10301 | }; |
| 10302 | |
| 10303 | static PyObject * |
| 10304 | unicode_iter(PyObject *seq) |
| 10305 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10306 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10307 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10308 | if (!PyUnicode_Check(seq)) { |
| 10309 | PyErr_BadInternalCall(); |
| 10310 | return NULL; |
| 10311 | } |
| 10312 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 10313 | if (it == NULL) |
| 10314 | return NULL; |
| 10315 | it->it_index = 0; |
| 10316 | Py_INCREF(seq); |
| 10317 | it->it_seq = (PyUnicodeObject *)seq; |
| 10318 | _PyObject_GC_TRACK(it); |
| 10319 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10320 | } |
| 10321 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10322 | size_t |
| 10323 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 10324 | { |
| 10325 | int res = 0; |
| 10326 | while(*u++) |
| 10327 | res++; |
| 10328 | return res; |
| 10329 | } |
| 10330 | |
| 10331 | Py_UNICODE* |
| 10332 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 10333 | { |
| 10334 | Py_UNICODE *u = s1; |
| 10335 | while ((*u++ = *s2++)); |
| 10336 | return s1; |
| 10337 | } |
| 10338 | |
| 10339 | Py_UNICODE* |
| 10340 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 10341 | { |
| 10342 | Py_UNICODE *u = s1; |
| 10343 | while ((*u++ = *s2++)) |
| 10344 | if (n-- == 0) |
| 10345 | break; |
| 10346 | return s1; |
| 10347 | } |
| 10348 | |
Victor Stinner | c4eb765 | 2010-09-01 23:43:50 +0000 | [diff] [blame] | 10349 | Py_UNICODE* |
| 10350 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 10351 | { |
| 10352 | Py_UNICODE *u1 = s1; |
| 10353 | u1 += Py_UNICODE_strlen(u1); |
| 10354 | Py_UNICODE_strcpy(u1, s2); |
| 10355 | return s1; |
| 10356 | } |
| 10357 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10358 | int |
| 10359 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 10360 | { |
| 10361 | while (*s1 && *s2 && *s1 == *s2) |
| 10362 | s1++, s2++; |
| 10363 | if (*s1 && *s2) |
| 10364 | return (*s1 < *s2) ? -1 : +1; |
| 10365 | if (*s1) |
| 10366 | return 1; |
| 10367 | if (*s2) |
| 10368 | return -1; |
| 10369 | return 0; |
| 10370 | } |
| 10371 | |
Victor Stinner | ef8d95c | 2010-08-16 22:03:11 +0000 | [diff] [blame] | 10372 | int |
| 10373 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 10374 | { |
| 10375 | register Py_UNICODE u1, u2; |
| 10376 | for (; n != 0; n--) { |
| 10377 | u1 = *s1; |
| 10378 | u2 = *s2; |
| 10379 | if (u1 != u2) |
| 10380 | return (u1 < u2) ? -1 : +1; |
| 10381 | if (u1 == '\0') |
| 10382 | return 0; |
| 10383 | s1++; |
| 10384 | s2++; |
| 10385 | } |
| 10386 | return 0; |
| 10387 | } |
| 10388 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10389 | Py_UNICODE* |
| 10390 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 10391 | { |
| 10392 | const Py_UNICODE *p; |
| 10393 | for (p = s; *p; p++) |
| 10394 | if (*p == c) |
| 10395 | return (Py_UNICODE*)p; |
| 10396 | return NULL; |
| 10397 | } |
| 10398 | |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 10399 | Py_UNICODE* |
| 10400 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 10401 | { |
| 10402 | const Py_UNICODE *p; |
| 10403 | p = s + Py_UNICODE_strlen(s); |
| 10404 | while (p != s) { |
| 10405 | p--; |
| 10406 | if (*p == c) |
| 10407 | return (Py_UNICODE*)p; |
| 10408 | } |
| 10409 | return NULL; |
| 10410 | } |
| 10411 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 10412 | Py_UNICODE* |
Victor Stinner | 4640860 | 2010-09-03 16:18:00 +0000 | [diff] [blame] | 10413 | PyUnicode_AsUnicodeCopy(PyObject *object) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 10414 | { |
| 10415 | PyUnicodeObject *unicode = (PyUnicodeObject *)object; |
| 10416 | Py_UNICODE *copy; |
| 10417 | Py_ssize_t size; |
| 10418 | |
| 10419 | /* Ensure we won't overflow the size. */ |
| 10420 | if (PyUnicode_GET_SIZE(unicode) > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 10421 | PyErr_NoMemory(); |
| 10422 | return NULL; |
| 10423 | } |
| 10424 | size = PyUnicode_GET_SIZE(unicode) + 1; /* copy the nul character */ |
| 10425 | size *= sizeof(Py_UNICODE); |
| 10426 | copy = PyMem_Malloc(size); |
| 10427 | if (copy == NULL) { |
| 10428 | PyErr_NoMemory(); |
| 10429 | return NULL; |
| 10430 | } |
| 10431 | memcpy(copy, PyUnicode_AS_UNICODE(unicode), size); |
| 10432 | return copy; |
| 10433 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10434 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 10435 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 10436 | to the string.Formatter class implemented in Python. */ |
| 10437 | |
| 10438 | static PyMethodDef _string_methods[] = { |
| 10439 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 10440 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 10441 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 10442 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 10443 | {NULL, NULL} |
| 10444 | }; |
| 10445 | |
| 10446 | static struct PyModuleDef _string_module = { |
| 10447 | PyModuleDef_HEAD_INIT, |
| 10448 | "_string", |
| 10449 | PyDoc_STR("string helper module"), |
| 10450 | 0, |
| 10451 | _string_methods, |
| 10452 | NULL, |
| 10453 | NULL, |
| 10454 | NULL, |
| 10455 | NULL |
| 10456 | }; |
| 10457 | |
| 10458 | PyMODINIT_FUNC |
| 10459 | PyInit__string(void) |
| 10460 | { |
| 10461 | return PyModule_Create(&_string_module); |
| 10462 | } |
| 10463 | |
| 10464 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10465 | #ifdef __cplusplus |
| 10466 | } |
| 10467 | #endif |