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" |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 44 | #include "bytes_methods.h" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 45 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 46 | #include "unicodeobject.h" |
Marc-André Lemburg | d49e5b4 | 2000-06-30 14:58:20 +0000 | [diff] [blame] | 47 | #include "ucnhash.h" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 48 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 49 | #ifdef MS_WINDOWS |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 50 | #include <windows.h> |
| 51 | #endif |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 52 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 53 | /* Limit for the Unicode object free list */ |
| 54 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 55 | #define PyUnicode_MAXFREELIST 1024 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 56 | |
| 57 | /* Limit for the Unicode object free list stay alive optimization. |
| 58 | |
| 59 | The implementation will keep allocated Unicode memory intact for |
| 60 | all objects on the free list having a size less than this |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 61 | limit. This reduces malloc() overhead for small Unicode objects. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 62 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 63 | At worst this will result in PyUnicode_MAXFREELIST * |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 64 | (sizeof(PyUnicodeObject) + KEEPALIVE_SIZE_LIMIT + |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 65 | malloc()-overhead) bytes of unused garbage. |
| 66 | |
| 67 | Setting the limit to 0 effectively turns the feature off. |
| 68 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 69 | Note: This is an experimental feature ! If you get core dumps when |
| 70 | using Unicode objects, turn this feature off. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 71 | |
| 72 | */ |
| 73 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 74 | #define KEEPALIVE_SIZE_LIMIT 9 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 75 | |
| 76 | /* Endianness switches; defaults to little endian */ |
| 77 | |
| 78 | #ifdef WORDS_BIGENDIAN |
| 79 | # define BYTEORDER_IS_BIG_ENDIAN |
| 80 | #else |
| 81 | # define BYTEORDER_IS_LITTLE_ENDIAN |
| 82 | #endif |
| 83 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 84 | /* --- Globals ------------------------------------------------------------ |
| 85 | |
| 86 | The globals are initialized by the _PyUnicode_Init() API and should |
| 87 | not be used before calling that API. |
| 88 | |
| 89 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 90 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 91 | |
| 92 | #ifdef __cplusplus |
| 93 | extern "C" { |
| 94 | #endif |
| 95 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 96 | /* This dictionary holds all interned unicode strings. Note that references |
| 97 | to strings in this dictionary are *not* counted in the string's ob_refcnt. |
| 98 | When the interned string reaches a refcnt of 0 the string deallocation |
| 99 | function will delete the reference from this dictionary. |
| 100 | |
| 101 | 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] | 102 | 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] | 103 | */ |
| 104 | static PyObject *interned; |
| 105 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 106 | /* Free list for Unicode objects */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 107 | static PyUnicodeObject *free_list; |
| 108 | static int numfree; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 109 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 110 | /* The empty Unicode object is shared to improve performance. */ |
| 111 | static PyUnicodeObject *unicode_empty; |
| 112 | |
| 113 | /* Single character Unicode strings in the Latin-1 range are being |
| 114 | shared as well. */ |
| 115 | static PyUnicodeObject *unicode_latin1[256]; |
| 116 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 117 | /* Fast detection of the most frequent whitespace characters */ |
| 118 | const unsigned char _Py_ascii_whitespace[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 119 | 0, 0, 0, 0, 0, 0, 0, 0, |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 120 | /* case 0x0009: * CHARACTER TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 121 | /* case 0x000A: * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 122 | /* case 0x000B: * LINE TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 123 | /* case 0x000C: * FORM FEED */ |
| 124 | /* case 0x000D: * CARRIAGE RETURN */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 125 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 126 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 127 | /* case 0x001C: * FILE SEPARATOR */ |
| 128 | /* case 0x001D: * GROUP SEPARATOR */ |
| 129 | /* case 0x001E: * RECORD SEPARATOR */ |
| 130 | /* case 0x001F: * UNIT SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 131 | 0, 0, 0, 0, 1, 1, 1, 1, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 132 | /* case 0x0020: * SPACE */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 133 | 1, 0, 0, 0, 0, 0, 0, 0, |
| 134 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 135 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 136 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 137 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 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, |
| 143 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 144 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 145 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 146 | }; |
| 147 | |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 148 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
| 149 | PyObject **errorHandler,const char *encoding, const char *reason, |
| 150 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 151 | Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos); |
| 152 | |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 153 | static void raise_encode_exception(PyObject **exceptionObject, |
| 154 | const char *encoding, |
| 155 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 156 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 157 | const char *reason); |
| 158 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 159 | /* Same for linebreaks */ |
| 160 | static unsigned char ascii_linebreak[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 161 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 162 | /* 0x000A, * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 163 | /* 0x000B, * LINE TABULATION */ |
| 164 | /* 0x000C, * FORM FEED */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 165 | /* 0x000D, * CARRIAGE RETURN */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 166 | 0, 0, 1, 1, 1, 1, 0, 0, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 167 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 168 | /* 0x001C, * FILE SEPARATOR */ |
| 169 | /* 0x001D, * GROUP SEPARATOR */ |
| 170 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 171 | 0, 0, 0, 0, 1, 1, 1, 0, |
| 172 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 173 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 174 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 175 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 176 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 177 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 178 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 179 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 180 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 181 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 182 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 183 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 184 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 188 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 189 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 190 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 191 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 192 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 193 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 194 | /* This is actually an illegal character, so it should |
| 195 | not be passed to unichr. */ |
| 196 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 197 | #endif |
| 198 | } |
| 199 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 200 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 201 | |
| 202 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 203 | to keep things simple, we use a single bitmask, using the least 5 |
| 204 | bits from each unicode characters as the bit index. */ |
| 205 | |
| 206 | /* the linebreak mask is set up by Unicode_Init below */ |
| 207 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 208 | #if LONG_BIT >= 128 |
| 209 | #define BLOOM_WIDTH 128 |
| 210 | #elif LONG_BIT >= 64 |
| 211 | #define BLOOM_WIDTH 64 |
| 212 | #elif LONG_BIT >= 32 |
| 213 | #define BLOOM_WIDTH 32 |
| 214 | #else |
| 215 | #error "LONG_BIT is smaller than 32" |
| 216 | #endif |
| 217 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 218 | #define BLOOM_MASK unsigned long |
| 219 | |
| 220 | static BLOOM_MASK bloom_linebreak; |
| 221 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 222 | #define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
| 223 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 224 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 225 | #define BLOOM_LINEBREAK(ch) \ |
| 226 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 227 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 228 | |
| 229 | Py_LOCAL_INLINE(BLOOM_MASK) make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len) |
| 230 | { |
| 231 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 232 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 233 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 234 | Py_ssize_t i; |
| 235 | |
| 236 | mask = 0; |
| 237 | for (i = 0; i < len; i++) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 238 | BLOOM_ADD(mask, ptr[i]); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 239 | |
| 240 | return mask; |
| 241 | } |
| 242 | |
| 243 | Py_LOCAL_INLINE(int) unicode_member(Py_UNICODE chr, Py_UNICODE* set, Py_ssize_t setlen) |
| 244 | { |
| 245 | Py_ssize_t i; |
| 246 | |
| 247 | for (i = 0; i < setlen; i++) |
| 248 | if (set[i] == chr) |
| 249 | return 1; |
| 250 | |
| 251 | return 0; |
| 252 | } |
| 253 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 254 | #define BLOOM_MEMBER(mask, chr, set, setlen) \ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 255 | BLOOM(mask, chr) && unicode_member(chr, set, setlen) |
| 256 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 257 | /* --- Unicode Object ----------------------------------------------------- */ |
| 258 | |
| 259 | static |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 260 | int unicode_resize(register PyUnicodeObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 261 | Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 262 | { |
| 263 | void *oldstr; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 264 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 265 | /* Shortcut if there's nothing much to do. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 266 | if (unicode->length == length) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 267 | goto reset; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 268 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 269 | /* Resizing shared object (unicode_empty or single character |
| 270 | objects) in-place is not allowed. Use PyUnicode_Resize() |
| 271 | instead ! */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 272 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 273 | if (unicode == unicode_empty || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 274 | (unicode->length == 1 && |
| 275 | unicode->str[0] < 256U && |
| 276 | unicode_latin1[unicode->str[0]] == unicode)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 277 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 278 | "can't resize shared str objects"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 279 | return -1; |
| 280 | } |
| 281 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 282 | /* We allocate one more byte to make sure the string is Ux0000 terminated. |
| 283 | The overallocation is also used by fastsearch, which assumes that it's |
| 284 | safe to look at str[length] (without making any assumptions about what |
| 285 | it contains). */ |
| 286 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 287 | oldstr = unicode->str; |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 288 | unicode->str = PyObject_REALLOC(unicode->str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 289 | sizeof(Py_UNICODE) * (length + 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 290 | if (!unicode->str) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 291 | unicode->str = (Py_UNICODE *)oldstr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 292 | PyErr_NoMemory(); |
| 293 | return -1; |
| 294 | } |
| 295 | unicode->str[length] = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 296 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 297 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 298 | reset: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 299 | /* Reset the object caches */ |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 300 | if (unicode->defenc) { |
Georg Brandl | 8ee604b | 2010-07-29 14:23:06 +0000 | [diff] [blame] | 301 | Py_CLEAR(unicode->defenc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 302 | } |
| 303 | unicode->hash = -1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 304 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | /* 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] | 309 | Ux0000 terminated; some code (e.g. new_identifier) |
| 310 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 311 | |
| 312 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 313 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 314 | |
| 315 | */ |
| 316 | |
| 317 | static |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 318 | PyUnicodeObject *_PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 319 | { |
| 320 | register PyUnicodeObject *unicode; |
| 321 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 322 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 323 | if (length == 0 && unicode_empty != NULL) { |
| 324 | Py_INCREF(unicode_empty); |
| 325 | return unicode_empty; |
| 326 | } |
| 327 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 328 | /* Ensure we won't overflow the size. */ |
| 329 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 330 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 331 | } |
| 332 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 333 | /* Unicode freelist & memory allocation */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 334 | if (free_list) { |
| 335 | unicode = free_list; |
| 336 | free_list = *(PyUnicodeObject **)unicode; |
| 337 | numfree--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 338 | if (unicode->str) { |
| 339 | /* Keep-Alive optimization: we only upsize the buffer, |
| 340 | never downsize it. */ |
| 341 | if ((unicode->length < length) && |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 342 | unicode_resize(unicode, length) < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 343 | PyObject_DEL(unicode->str); |
| 344 | unicode->str = NULL; |
| 345 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 346 | } |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 347 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 348 | size_t new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 349 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 350 | } |
| 351 | PyObject_INIT(unicode, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 352 | } |
| 353 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 354 | size_t new_size; |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 355 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 356 | if (unicode == NULL) |
| 357 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 358 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 359 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 362 | if (!unicode->str) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 363 | PyErr_NoMemory(); |
| 364 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 365 | } |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 366 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 367 | * the caller fails before initializing str -- unicode_resize() |
| 368 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 369 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 370 | * We don't want unicode_resize to read uninitialized memory in |
| 371 | * that case. |
| 372 | */ |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 373 | unicode->str[0] = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 374 | unicode->str[length] = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 375 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 376 | unicode->hash = -1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 377 | unicode->state = 0; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 378 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 379 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 380 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 381 | onError: |
Amaury Forgeot d'Arc | 7888d08 | 2008-08-01 01:06:32 +0000 | [diff] [blame] | 382 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
| 383 | _Py_DEC_REFTOTAL; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 384 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 385 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 386 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | static |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 390 | void unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 391 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 392 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 393 | case SSTATE_NOT_INTERNED: |
| 394 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 395 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 396 | case SSTATE_INTERNED_MORTAL: |
| 397 | /* revive dead object temporarily for DelItem */ |
| 398 | Py_REFCNT(unicode) = 3; |
| 399 | if (PyDict_DelItem(interned, (PyObject *)unicode) != 0) |
| 400 | Py_FatalError( |
| 401 | "deletion of interned string failed"); |
| 402 | break; |
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 | case SSTATE_INTERNED_IMMORTAL: |
| 405 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 406 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 407 | default: |
| 408 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 411 | if (PyUnicode_CheckExact(unicode) && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 412 | numfree < PyUnicode_MAXFREELIST) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 413 | /* Keep-Alive optimization */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 414 | if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { |
| 415 | PyObject_DEL(unicode->str); |
| 416 | unicode->str = NULL; |
| 417 | unicode->length = 0; |
| 418 | } |
| 419 | if (unicode->defenc) { |
Georg Brandl | 8ee604b | 2010-07-29 14:23:06 +0000 | [diff] [blame] | 420 | Py_CLEAR(unicode->defenc); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 421 | } |
| 422 | /* Add to free list */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 423 | *(PyUnicodeObject **)unicode = free_list; |
| 424 | free_list = unicode; |
| 425 | numfree++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 426 | } |
| 427 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 428 | PyObject_DEL(unicode->str); |
| 429 | Py_XDECREF(unicode->defenc); |
| 430 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 434 | static |
| 435 | int _PyUnicode_Resize(PyUnicodeObject **unicode, Py_ssize_t length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 436 | { |
| 437 | register PyUnicodeObject *v; |
| 438 | |
| 439 | /* Argument checks */ |
| 440 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 441 | PyErr_BadInternalCall(); |
| 442 | return -1; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 443 | } |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 444 | v = *unicode; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 445 | if (v == NULL || !PyUnicode_Check(v) || Py_REFCNT(v) != 1 || length < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 446 | PyErr_BadInternalCall(); |
| 447 | return -1; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | /* Resizing unicode_empty and single character objects is not |
| 451 | possible since these are being shared. We simply return a fresh |
| 452 | copy with the same Unicode content. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 453 | if (v->length != length && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 454 | (v == unicode_empty || v->length == 1)) { |
| 455 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 456 | if (w == NULL) |
| 457 | return -1; |
| 458 | Py_UNICODE_COPY(w->str, v->str, |
| 459 | length < v->length ? length : v->length); |
| 460 | Py_DECREF(*unicode); |
| 461 | *unicode = w; |
| 462 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 466 | objects, since we can modify them in-place. */ |
| 467 | return unicode_resize(v, length); |
| 468 | } |
| 469 | |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 470 | int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) |
| 471 | { |
| 472 | return _PyUnicode_Resize((PyUnicodeObject **)unicode, length); |
| 473 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 474 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 475 | PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 476 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 477 | { |
| 478 | PyUnicodeObject *unicode; |
| 479 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 480 | /* If the Unicode data is known at construction time, we can apply |
| 481 | some optimizations which share commonly used objects. */ |
| 482 | if (u != NULL) { |
| 483 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 484 | /* Optimization for empty strings */ |
| 485 | if (size == 0 && unicode_empty != NULL) { |
| 486 | Py_INCREF(unicode_empty); |
| 487 | return (PyObject *)unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 488 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 489 | |
| 490 | /* Single character Unicode objects in the Latin-1 range are |
| 491 | shared when using this constructor */ |
| 492 | if (size == 1 && *u < 256) { |
| 493 | unicode = unicode_latin1[*u]; |
| 494 | if (!unicode) { |
| 495 | unicode = _PyUnicode_New(1); |
| 496 | if (!unicode) |
| 497 | return NULL; |
| 498 | unicode->str[0] = *u; |
| 499 | unicode_latin1[*u] = unicode; |
| 500 | } |
| 501 | Py_INCREF(unicode); |
| 502 | return (PyObject *)unicode; |
| 503 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 504 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 505 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 506 | unicode = _PyUnicode_New(size); |
| 507 | if (!unicode) |
| 508 | return NULL; |
| 509 | |
| 510 | /* Copy the Unicode data into the new object */ |
| 511 | if (u != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 512 | Py_UNICODE_COPY(unicode->str, u, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 513 | |
| 514 | return (PyObject *)unicode; |
| 515 | } |
| 516 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 517 | PyObject *PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 518 | { |
| 519 | PyUnicodeObject *unicode; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 520 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 521 | if (size < 0) { |
| 522 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 523 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 524 | return NULL; |
| 525 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 526 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 527 | /* 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] | 528 | some optimizations which share commonly used objects. |
| 529 | Also, this means the input must be UTF-8, so fall back to the |
| 530 | UTF-8 decoder at the end. */ |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 531 | if (u != NULL) { |
| 532 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 533 | /* Optimization for empty strings */ |
| 534 | if (size == 0 && unicode_empty != NULL) { |
| 535 | Py_INCREF(unicode_empty); |
| 536 | return (PyObject *)unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 537 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 538 | |
| 539 | /* Single characters are shared when using this constructor. |
| 540 | Restrict to ASCII, since the input must be UTF-8. */ |
| 541 | if (size == 1 && Py_CHARMASK(*u) < 128) { |
| 542 | unicode = unicode_latin1[Py_CHARMASK(*u)]; |
| 543 | if (!unicode) { |
| 544 | unicode = _PyUnicode_New(1); |
| 545 | if (!unicode) |
| 546 | return NULL; |
| 547 | unicode->str[0] = Py_CHARMASK(*u); |
| 548 | unicode_latin1[Py_CHARMASK(*u)] = unicode; |
| 549 | } |
| 550 | Py_INCREF(unicode); |
| 551 | return (PyObject *)unicode; |
| 552 | } |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 553 | |
| 554 | return PyUnicode_DecodeUTF8(u, size, NULL); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Walter Dörwald | 5550731 | 2007-05-18 13:12:10 +0000 | [diff] [blame] | 557 | unicode = _PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 558 | if (!unicode) |
| 559 | return NULL; |
| 560 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 561 | return (PyObject *)unicode; |
| 562 | } |
| 563 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 564 | PyObject *PyUnicode_FromString(const char *u) |
| 565 | { |
| 566 | size_t size = strlen(u); |
| 567 | if (size > PY_SSIZE_T_MAX) { |
| 568 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 569 | return NULL; |
| 570 | } |
| 571 | |
| 572 | return PyUnicode_FromStringAndSize(u, size); |
| 573 | } |
| 574 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 575 | #ifdef HAVE_WCHAR_H |
| 576 | |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 577 | #if (Py_UNICODE_SIZE == 2) && defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
| 578 | # define CONVERT_WCHAR_TO_SURROGATES |
| 579 | #endif |
| 580 | |
| 581 | #ifdef CONVERT_WCHAR_TO_SURROGATES |
| 582 | |
| 583 | /* Here sizeof(wchar_t) is 4 but Py_UNICODE_SIZE == 2, so we need |
| 584 | to convert from UTF32 to UTF16. */ |
| 585 | |
| 586 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
| 587 | Py_ssize_t size) |
| 588 | { |
| 589 | PyUnicodeObject *unicode; |
| 590 | register Py_ssize_t i; |
| 591 | Py_ssize_t alloc; |
| 592 | const wchar_t *orig_w; |
| 593 | |
| 594 | if (w == NULL) { |
| 595 | if (size == 0) |
| 596 | return PyUnicode_FromStringAndSize(NULL, 0); |
| 597 | PyErr_BadInternalCall(); |
| 598 | return NULL; |
| 599 | } |
| 600 | |
| 601 | if (size == -1) { |
| 602 | size = wcslen(w); |
| 603 | } |
| 604 | |
| 605 | alloc = size; |
| 606 | orig_w = w; |
| 607 | for (i = size; i > 0; i--) { |
| 608 | if (*w > 0xFFFF) |
| 609 | alloc++; |
| 610 | w++; |
| 611 | } |
| 612 | w = orig_w; |
| 613 | unicode = _PyUnicode_New(alloc); |
| 614 | if (!unicode) |
| 615 | return NULL; |
| 616 | |
| 617 | /* Copy the wchar_t data into the new object */ |
| 618 | { |
| 619 | register Py_UNICODE *u; |
| 620 | u = PyUnicode_AS_UNICODE(unicode); |
| 621 | for (i = size; i > 0; i--) { |
| 622 | if (*w > 0xFFFF) { |
| 623 | wchar_t ordinal = *w++; |
| 624 | ordinal -= 0x10000; |
| 625 | *u++ = 0xD800 | (ordinal >> 10); |
| 626 | *u++ = 0xDC00 | (ordinal & 0x3FF); |
| 627 | } |
| 628 | else |
| 629 | *u++ = *w++; |
| 630 | } |
| 631 | } |
| 632 | return (PyObject *)unicode; |
| 633 | } |
| 634 | |
| 635 | #else |
| 636 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 637 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 638 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 639 | { |
| 640 | PyUnicodeObject *unicode; |
| 641 | |
| 642 | if (w == NULL) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 643 | if (size == 0) |
| 644 | return PyUnicode_FromStringAndSize(NULL, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 645 | PyErr_BadInternalCall(); |
| 646 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 647 | } |
| 648 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 649 | if (size == -1) { |
| 650 | size = wcslen(w); |
| 651 | } |
| 652 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 653 | unicode = _PyUnicode_New(size); |
| 654 | if (!unicode) |
| 655 | return NULL; |
| 656 | |
| 657 | /* Copy the wchar_t data into the new object */ |
Daniel Stutzbach | 8515eae | 2010-08-24 21:57:33 +0000 | [diff] [blame] | 658 | #if Py_UNICODE_SIZE == SIZEOF_WCHAR_T |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 659 | memcpy(unicode->str, w, size * sizeof(wchar_t)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 660 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 661 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 662 | register Py_UNICODE *u; |
| 663 | register Py_ssize_t i; |
| 664 | u = PyUnicode_AS_UNICODE(unicode); |
| 665 | for (i = size; i > 0; i--) |
| 666 | *u++ = *w++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 667 | } |
| 668 | #endif |
| 669 | |
| 670 | return (PyObject *)unicode; |
| 671 | } |
| 672 | |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 673 | #endif /* CONVERT_WCHAR_TO_SURROGATES */ |
| 674 | |
| 675 | #undef CONVERT_WCHAR_TO_SURROGATES |
| 676 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 677 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 678 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 679 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 680 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 681 | *fmt++ = '%'; |
| 682 | if (width) { |
| 683 | if (zeropad) |
| 684 | *fmt++ = '0'; |
| 685 | fmt += sprintf(fmt, "%d", width); |
| 686 | } |
| 687 | if (precision) |
| 688 | fmt += sprintf(fmt, ".%d", precision); |
| 689 | if (longflag) |
| 690 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 691 | else if (longlongflag) { |
| 692 | /* longlongflag should only ever be nonzero on machines with |
| 693 | HAVE_LONG_LONG defined */ |
| 694 | #ifdef HAVE_LONG_LONG |
| 695 | char *f = PY_FORMAT_LONG_LONG; |
| 696 | while (*f) |
| 697 | *fmt++ = *f++; |
| 698 | #else |
| 699 | /* we shouldn't ever get here */ |
| 700 | assert(0); |
| 701 | *fmt++ = 'l'; |
| 702 | #endif |
| 703 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 704 | else if (size_tflag) { |
| 705 | char *f = PY_FORMAT_SIZE_T; |
| 706 | while (*f) |
| 707 | *fmt++ = *f++; |
| 708 | } |
| 709 | *fmt++ = c; |
| 710 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 713 | #define appendstring(string) {for (copy = string;*copy;) *s++ = *copy++;} |
| 714 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 715 | /* size of fixed-size buffer for formatting single arguments */ |
| 716 | #define ITEM_BUFFER_LEN 21 |
| 717 | /* maximum number of characters required for output of %ld. 21 characters |
| 718 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 719 | #define MAX_LONG_CHARS 21 |
| 720 | /* maximum number of characters required for output of %lld. |
| 721 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 722 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 723 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 724 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 725 | PyObject * |
| 726 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 727 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 728 | va_list count; |
| 729 | Py_ssize_t callcount = 0; |
| 730 | PyObject **callresults = NULL; |
| 731 | PyObject **callresult = NULL; |
| 732 | Py_ssize_t n = 0; |
| 733 | int width = 0; |
| 734 | int precision = 0; |
| 735 | int zeropad; |
| 736 | const char* f; |
| 737 | Py_UNICODE *s; |
| 738 | PyObject *string; |
| 739 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 740 | char buffer[ITEM_BUFFER_LEN+1]; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 741 | /* use abuffer instead of buffer, if we need more space |
| 742 | * (which can happen if there's a format specifier with width). */ |
| 743 | char *abuffer = NULL; |
| 744 | char *realbuffer; |
| 745 | Py_ssize_t abuffersize = 0; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 746 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 747 | const char *copy; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 748 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 749 | Py_VA_COPY(count, vargs); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 750 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 751 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 752 | * PyUnicode_DecodeUTF8() for these objects once during step 3 and put the |
| 753 | * result in an array) */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 754 | for (f = format; *f; f++) { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 755 | if (*f == '%') { |
| 756 | if (*(f+1)=='%') |
| 757 | continue; |
| 758 | if (*(f+1)=='S' || *(f+1)=='R' || *(f+1)=='A') |
| 759 | ++callcount; |
| 760 | while (ISDIGIT((unsigned)*f)) |
| 761 | width = (width*10) + *f++ - '0'; |
| 762 | while (*++f && *f != '%' && !ISALPHA((unsigned)*f)) |
| 763 | ; |
| 764 | if (*f == 's') |
| 765 | ++callcount; |
| 766 | } |
Benjamin Peterson | 9be0b2e | 2010-09-12 03:40:54 +0000 | [diff] [blame] | 767 | else if (128 <= (unsigned char)*f) { |
| 768 | PyErr_Format(PyExc_ValueError, |
| 769 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
Victor Stinner | 4c7db31 | 2010-09-12 07:51:18 +0000 | [diff] [blame] | 770 | "string, got a non-ASCII byte: 0x%02x", |
Benjamin Peterson | 9be0b2e | 2010-09-12 03:40:54 +0000 | [diff] [blame] | 771 | (unsigned char)*f); |
Benjamin Peterson | d4ac96a | 2010-09-12 16:40:53 +0000 | [diff] [blame] | 772 | return NULL; |
Benjamin Peterson | 9be0b2e | 2010-09-12 03:40:54 +0000 | [diff] [blame] | 773 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 774 | } |
| 775 | /* step 2: allocate memory for the results of |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 776 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 777 | if (callcount) { |
| 778 | callresults = PyObject_Malloc(sizeof(PyObject *)*callcount); |
| 779 | if (!callresults) { |
| 780 | PyErr_NoMemory(); |
| 781 | return NULL; |
| 782 | } |
| 783 | callresult = callresults; |
| 784 | } |
| 785 | /* step 3: figure out how large a buffer we need */ |
| 786 | for (f = format; *f; f++) { |
| 787 | if (*f == '%') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 788 | #ifdef HAVE_LONG_LONG |
| 789 | int longlongflag = 0; |
| 790 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 791 | const char* p = f; |
| 792 | width = 0; |
| 793 | while (ISDIGIT((unsigned)*f)) |
| 794 | width = (width*10) + *f++ - '0'; |
| 795 | while (*++f && *f != '%' && !ISALPHA((unsigned)*f)) |
| 796 | ; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 797 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 798 | /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
| 799 | * they don't affect the amount of space we reserve. |
| 800 | */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 801 | if (*f == 'l') { |
| 802 | if (f[1] == 'd' || f[1] == 'u') { |
| 803 | ++f; |
| 804 | } |
| 805 | #ifdef HAVE_LONG_LONG |
| 806 | else if (f[1] == 'l' && |
| 807 | (f[2] == 'd' || f[2] == 'u')) { |
| 808 | longlongflag = 1; |
| 809 | f += 2; |
| 810 | } |
| 811 | #endif |
| 812 | } |
| 813 | else if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 814 | ++f; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 815 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 816 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 817 | switch (*f) { |
| 818 | case 'c': |
| 819 | (void)va_arg(count, int); |
| 820 | /* fall through... */ |
| 821 | case '%': |
| 822 | n++; |
| 823 | break; |
| 824 | case 'd': case 'u': case 'i': case 'x': |
| 825 | (void) va_arg(count, int); |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 826 | #ifdef HAVE_LONG_LONG |
| 827 | if (longlongflag) { |
| 828 | if (width < MAX_LONG_LONG_CHARS) |
| 829 | width = MAX_LONG_LONG_CHARS; |
| 830 | } |
| 831 | else |
| 832 | #endif |
| 833 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 834 | including sign. Decimal takes the most space. This |
| 835 | isn't enough for octal. If a width is specified we |
| 836 | need more (which we allocate later). */ |
| 837 | if (width < MAX_LONG_CHARS) |
| 838 | width = MAX_LONG_CHARS; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 839 | n += width; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 840 | /* XXX should allow for large precision here too. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 841 | if (abuffersize < width) |
| 842 | abuffersize = width; |
| 843 | break; |
| 844 | case 's': |
| 845 | { |
| 846 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 847 | const char *s = va_arg(count, const char*); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 848 | PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); |
| 849 | if (!str) |
| 850 | goto fail; |
| 851 | n += PyUnicode_GET_SIZE(str); |
| 852 | /* Remember the str and switch to the next slot */ |
| 853 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 854 | break; |
| 855 | } |
| 856 | case 'U': |
| 857 | { |
| 858 | PyObject *obj = va_arg(count, PyObject *); |
| 859 | assert(obj && PyUnicode_Check(obj)); |
| 860 | n += PyUnicode_GET_SIZE(obj); |
| 861 | break; |
| 862 | } |
| 863 | case 'V': |
| 864 | { |
| 865 | PyObject *obj = va_arg(count, PyObject *); |
| 866 | const char *str = va_arg(count, const char *); |
| 867 | assert(obj || str); |
| 868 | assert(!obj || PyUnicode_Check(obj)); |
| 869 | if (obj) |
| 870 | n += PyUnicode_GET_SIZE(obj); |
| 871 | else |
| 872 | n += strlen(str); |
| 873 | break; |
| 874 | } |
| 875 | case 'S': |
| 876 | { |
| 877 | PyObject *obj = va_arg(count, PyObject *); |
| 878 | PyObject *str; |
| 879 | assert(obj); |
| 880 | str = PyObject_Str(obj); |
| 881 | if (!str) |
| 882 | goto fail; |
| 883 | n += PyUnicode_GET_SIZE(str); |
| 884 | /* Remember the str and switch to the next slot */ |
| 885 | *callresult++ = str; |
| 886 | break; |
| 887 | } |
| 888 | case 'R': |
| 889 | { |
| 890 | PyObject *obj = va_arg(count, PyObject *); |
| 891 | PyObject *repr; |
| 892 | assert(obj); |
| 893 | repr = PyObject_Repr(obj); |
| 894 | if (!repr) |
| 895 | goto fail; |
| 896 | n += PyUnicode_GET_SIZE(repr); |
| 897 | /* Remember the repr and switch to the next slot */ |
| 898 | *callresult++ = repr; |
| 899 | break; |
| 900 | } |
| 901 | case 'A': |
| 902 | { |
| 903 | PyObject *obj = va_arg(count, PyObject *); |
| 904 | PyObject *ascii; |
| 905 | assert(obj); |
| 906 | ascii = PyObject_ASCII(obj); |
| 907 | if (!ascii) |
| 908 | goto fail; |
| 909 | n += PyUnicode_GET_SIZE(ascii); |
| 910 | /* Remember the repr and switch to the next slot */ |
| 911 | *callresult++ = ascii; |
| 912 | break; |
| 913 | } |
| 914 | case 'p': |
| 915 | (void) va_arg(count, int); |
| 916 | /* maximum 64-bit pointer representation: |
| 917 | * 0xffffffffffffffff |
| 918 | * so 19 characters is enough. |
| 919 | * XXX I count 18 -- what's the extra for? |
| 920 | */ |
| 921 | n += 19; |
| 922 | break; |
| 923 | default: |
| 924 | /* if we stumble upon an unknown |
| 925 | formatting code, copy the rest of |
| 926 | the format string to the output |
| 927 | string. (we cannot just skip the |
| 928 | code, since there's no way to know |
| 929 | what's in the argument list) */ |
| 930 | n += strlen(p); |
| 931 | goto expand; |
| 932 | } |
| 933 | } else |
| 934 | n++; |
| 935 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 936 | expand: |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 937 | if (abuffersize > ITEM_BUFFER_LEN) { |
| 938 | /* add 1 for sprintf's trailing null byte */ |
| 939 | abuffer = PyObject_Malloc(abuffersize + 1); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 940 | if (!abuffer) { |
| 941 | PyErr_NoMemory(); |
| 942 | goto fail; |
| 943 | } |
| 944 | realbuffer = abuffer; |
| 945 | } |
| 946 | else |
| 947 | realbuffer = buffer; |
| 948 | /* step 4: fill the buffer */ |
| 949 | /* Since we've analyzed how much space we need for the worst case, |
| 950 | we don't have to resize the string. |
| 951 | There can be no errors beyond this point. */ |
| 952 | string = PyUnicode_FromUnicode(NULL, n); |
| 953 | if (!string) |
| 954 | goto fail; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 955 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 956 | s = PyUnicode_AS_UNICODE(string); |
| 957 | callresult = callresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 958 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 959 | for (f = format; *f; f++) { |
| 960 | if (*f == '%') { |
| 961 | const char* p = f++; |
| 962 | int longflag = 0; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 963 | int longlongflag = 0; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 964 | int size_tflag = 0; |
| 965 | zeropad = (*f == '0'); |
| 966 | /* parse the width.precision part */ |
| 967 | width = 0; |
| 968 | while (ISDIGIT((unsigned)*f)) |
| 969 | width = (width*10) + *f++ - '0'; |
| 970 | precision = 0; |
| 971 | if (*f == '.') { |
| 972 | f++; |
| 973 | while (ISDIGIT((unsigned)*f)) |
| 974 | precision = (precision*10) + *f++ - '0'; |
| 975 | } |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 976 | /* Handle %ld, %lu, %lld and %llu. */ |
| 977 | if (*f == 'l') { |
| 978 | if (f[1] == 'd' || f[1] == 'u') { |
| 979 | longflag = 1; |
| 980 | ++f; |
| 981 | } |
| 982 | #ifdef HAVE_LONG_LONG |
| 983 | else if (f[1] == 'l' && |
| 984 | (f[2] == 'd' || f[2] == 'u')) { |
| 985 | longlongflag = 1; |
| 986 | f += 2; |
| 987 | } |
| 988 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 989 | } |
| 990 | /* handle the size_t flag. */ |
| 991 | if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
| 992 | size_tflag = 1; |
| 993 | ++f; |
| 994 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 995 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 996 | switch (*f) { |
| 997 | case 'c': |
| 998 | *s++ = va_arg(vargs, int); |
| 999 | break; |
| 1000 | case 'd': |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1001 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 1002 | width, precision, 'd'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1003 | if (longflag) |
| 1004 | sprintf(realbuffer, fmt, va_arg(vargs, long)); |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1005 | #ifdef HAVE_LONG_LONG |
| 1006 | else if (longlongflag) |
| 1007 | sprintf(realbuffer, fmt, va_arg(vargs, PY_LONG_LONG)); |
| 1008 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1009 | else if (size_tflag) |
| 1010 | sprintf(realbuffer, fmt, va_arg(vargs, Py_ssize_t)); |
| 1011 | else |
| 1012 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 1013 | appendstring(realbuffer); |
| 1014 | break; |
| 1015 | case 'u': |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1016 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 1017 | width, precision, 'u'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1018 | if (longflag) |
| 1019 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned long)); |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1020 | #ifdef HAVE_LONG_LONG |
| 1021 | else if (longlongflag) |
| 1022 | sprintf(realbuffer, fmt, va_arg(vargs, |
| 1023 | unsigned PY_LONG_LONG)); |
| 1024 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1025 | else if (size_tflag) |
| 1026 | sprintf(realbuffer, fmt, va_arg(vargs, size_t)); |
| 1027 | else |
| 1028 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned int)); |
| 1029 | appendstring(realbuffer); |
| 1030 | break; |
| 1031 | case 'i': |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1032 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'i'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1033 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 1034 | appendstring(realbuffer); |
| 1035 | break; |
| 1036 | case 'x': |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1037 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1038 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 1039 | appendstring(realbuffer); |
| 1040 | break; |
| 1041 | case 's': |
| 1042 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1043 | /* unused, since we already have the result */ |
| 1044 | (void) va_arg(vargs, char *); |
| 1045 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(*callresult), |
| 1046 | PyUnicode_GET_SIZE(*callresult)); |
| 1047 | s += PyUnicode_GET_SIZE(*callresult); |
| 1048 | /* We're done with the unicode()/repr() => forget it */ |
| 1049 | Py_DECREF(*callresult); |
| 1050 | /* switch to next unicode()/repr() result */ |
| 1051 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1052 | break; |
| 1053 | } |
| 1054 | case 'U': |
| 1055 | { |
| 1056 | PyObject *obj = va_arg(vargs, PyObject *); |
| 1057 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 1058 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 1059 | s += size; |
| 1060 | break; |
| 1061 | } |
| 1062 | case 'V': |
| 1063 | { |
| 1064 | PyObject *obj = va_arg(vargs, PyObject *); |
| 1065 | const char *str = va_arg(vargs, const char *); |
| 1066 | if (obj) { |
| 1067 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 1068 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 1069 | s += size; |
| 1070 | } else { |
| 1071 | appendstring(str); |
| 1072 | } |
| 1073 | break; |
| 1074 | } |
| 1075 | case 'S': |
| 1076 | case 'R': |
| 1077 | { |
| 1078 | Py_UNICODE *ucopy; |
| 1079 | Py_ssize_t usize; |
| 1080 | Py_ssize_t upos; |
| 1081 | /* unused, since we already have the result */ |
| 1082 | (void) va_arg(vargs, PyObject *); |
| 1083 | ucopy = PyUnicode_AS_UNICODE(*callresult); |
| 1084 | usize = PyUnicode_GET_SIZE(*callresult); |
| 1085 | for (upos = 0; upos<usize;) |
| 1086 | *s++ = ucopy[upos++]; |
| 1087 | /* We're done with the unicode()/repr() => forget it */ |
| 1088 | Py_DECREF(*callresult); |
| 1089 | /* switch to next unicode()/repr() result */ |
| 1090 | ++callresult; |
| 1091 | break; |
| 1092 | } |
| 1093 | case 'p': |
| 1094 | sprintf(buffer, "%p", va_arg(vargs, void*)); |
| 1095 | /* %p is ill-defined: ensure leading 0x. */ |
| 1096 | if (buffer[1] == 'X') |
| 1097 | buffer[1] = 'x'; |
| 1098 | else if (buffer[1] != 'x') { |
| 1099 | memmove(buffer+2, buffer, strlen(buffer)+1); |
| 1100 | buffer[0] = '0'; |
| 1101 | buffer[1] = 'x'; |
| 1102 | } |
| 1103 | appendstring(buffer); |
| 1104 | break; |
| 1105 | case '%': |
| 1106 | *s++ = '%'; |
| 1107 | break; |
| 1108 | default: |
| 1109 | appendstring(p); |
| 1110 | goto end; |
| 1111 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 1112 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 1113 | else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1114 | *s++ = *f; |
| 1115 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1116 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1117 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1118 | if (callresults) |
| 1119 | PyObject_Free(callresults); |
| 1120 | if (abuffer) |
| 1121 | PyObject_Free(abuffer); |
| 1122 | PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string)); |
| 1123 | return string; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1124 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1125 | if (callresults) { |
| 1126 | PyObject **callresult2 = callresults; |
| 1127 | while (callresult2 < callresult) { |
| 1128 | Py_DECREF(*callresult2); |
| 1129 | ++callresult2; |
| 1130 | } |
| 1131 | PyObject_Free(callresults); |
| 1132 | } |
| 1133 | if (abuffer) |
| 1134 | PyObject_Free(abuffer); |
| 1135 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | #undef appendstring |
| 1139 | |
| 1140 | PyObject * |
| 1141 | PyUnicode_FromFormat(const char *format, ...) |
| 1142 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1143 | PyObject* ret; |
| 1144 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1145 | |
| 1146 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1147 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1148 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1149 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1150 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1151 | ret = PyUnicode_FromFormatV(format, vargs); |
| 1152 | va_end(vargs); |
| 1153 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1156 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 1157 | convert a Unicode object to a wide character string. |
| 1158 | |
| 1159 | - If w is NULL: return the number of wide characters (including the nul |
| 1160 | character) required to convert the unicode object. Ignore size argument. |
| 1161 | |
| 1162 | - Otherwise: return the number of wide characters (excluding the nul |
| 1163 | character) written into w. Write at most size wide characters (including |
| 1164 | the nul character). */ |
| 1165 | static Py_ssize_t |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1166 | unicode_aswidechar(PyUnicodeObject *unicode, |
| 1167 | wchar_t *w, |
| 1168 | Py_ssize_t size) |
| 1169 | { |
| 1170 | #if Py_UNICODE_SIZE == SIZEOF_WCHAR_T |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1171 | Py_ssize_t res; |
| 1172 | if (w != NULL) { |
| 1173 | res = PyUnicode_GET_SIZE(unicode); |
| 1174 | if (size > res) |
| 1175 | size = res + 1; |
| 1176 | else |
| 1177 | res = size; |
| 1178 | memcpy(w, unicode->str, size * sizeof(wchar_t)); |
| 1179 | return res; |
| 1180 | } |
| 1181 | else |
| 1182 | return PyUnicode_GET_SIZE(unicode) + 1; |
| 1183 | #elif Py_UNICODE_SIZE == 2 && SIZEOF_WCHAR_T == 4 |
| 1184 | register const Py_UNICODE *u; |
| 1185 | const Py_UNICODE *uend; |
| 1186 | const wchar_t *worig, *wend; |
| 1187 | Py_ssize_t nchar; |
| 1188 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1189 | u = PyUnicode_AS_UNICODE(unicode); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1190 | uend = u + PyUnicode_GET_SIZE(unicode); |
| 1191 | if (w != NULL) { |
| 1192 | worig = w; |
| 1193 | wend = w + size; |
| 1194 | while (u != uend && w != wend) { |
| 1195 | if (0xD800 <= u[0] && u[0] <= 0xDBFF |
| 1196 | && 0xDC00 <= u[1] && u[1] <= 0xDFFF) |
| 1197 | { |
| 1198 | *w = (((u[0] & 0x3FF) << 10) | (u[1] & 0x3FF)) + 0x10000; |
| 1199 | u += 2; |
| 1200 | } |
| 1201 | else { |
| 1202 | *w = *u; |
| 1203 | u++; |
| 1204 | } |
| 1205 | w++; |
| 1206 | } |
| 1207 | if (w != wend) |
| 1208 | *w = L'\0'; |
| 1209 | return w - worig; |
| 1210 | } |
| 1211 | else { |
| 1212 | nchar = 1; /* nul character at the end */ |
| 1213 | while (u != uend) { |
| 1214 | if (0xD800 <= u[0] && u[0] <= 0xDBFF |
| 1215 | && 0xDC00 <= u[1] && u[1] <= 0xDFFF) |
| 1216 | u += 2; |
| 1217 | else |
| 1218 | u++; |
| 1219 | nchar++; |
| 1220 | } |
| 1221 | } |
| 1222 | return nchar; |
| 1223 | #elif Py_UNICODE_SIZE == 4 && SIZEOF_WCHAR_T == 2 |
| 1224 | register Py_UNICODE *u, *uend, ordinal; |
| 1225 | register Py_ssize_t i; |
| 1226 | wchar_t *worig, *wend; |
| 1227 | Py_ssize_t nchar; |
| 1228 | |
| 1229 | u = PyUnicode_AS_UNICODE(unicode); |
| 1230 | uend = u + PyUnicode_GET_SIZE(u); |
| 1231 | if (w != NULL) { |
| 1232 | worig = w; |
| 1233 | wend = w + size; |
| 1234 | while (u != uend && w != wend) { |
| 1235 | ordinal = *u; |
| 1236 | if (ordinal > 0xffff) { |
| 1237 | ordinal -= 0x10000; |
| 1238 | *w++ = 0xD800 | (ordinal >> 10); |
| 1239 | *w++ = 0xDC00 | (ordinal & 0x3FF); |
| 1240 | } |
| 1241 | else |
| 1242 | *w++ = ordinal; |
| 1243 | u++; |
| 1244 | } |
| 1245 | if (w != wend) |
| 1246 | *w = 0; |
| 1247 | return w - worig; |
| 1248 | } |
| 1249 | else { |
| 1250 | nchar = 1; /* nul character */ |
| 1251 | while (u != uend) { |
| 1252 | if (*u > 0xffff) |
| 1253 | nchar += 2; |
| 1254 | else |
| 1255 | nchar++; |
| 1256 | u++; |
| 1257 | } |
| 1258 | return nchar; |
| 1259 | } |
| 1260 | #else |
| 1261 | # error "unsupported wchar_t and Py_UNICODE sizes, see issue #8670" |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1262 | #endif |
| 1263 | } |
| 1264 | |
| 1265 | Py_ssize_t |
| 1266 | PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
| 1267 | wchar_t *w, |
| 1268 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1269 | { |
| 1270 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1271 | PyErr_BadInternalCall(); |
| 1272 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1273 | } |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1274 | return unicode_aswidechar(unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1275 | } |
| 1276 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1277 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 1278 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1279 | Py_ssize_t *size) |
| 1280 | { |
| 1281 | wchar_t* buffer; |
| 1282 | Py_ssize_t buflen; |
| 1283 | |
| 1284 | if (unicode == NULL) { |
| 1285 | PyErr_BadInternalCall(); |
| 1286 | return NULL; |
| 1287 | } |
| 1288 | |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 1289 | buflen = unicode_aswidechar((PyUnicodeObject *)unicode, NULL, 0); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1290 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1291 | PyErr_NoMemory(); |
| 1292 | return NULL; |
| 1293 | } |
| 1294 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1295 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 1296 | if (buffer == NULL) { |
| 1297 | PyErr_NoMemory(); |
| 1298 | return NULL; |
| 1299 | } |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 1300 | buflen = unicode_aswidechar((PyUnicodeObject *)unicode, buffer, buflen); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1301 | if (size != NULL) |
| 1302 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1303 | return buffer; |
| 1304 | } |
| 1305 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1306 | #endif |
| 1307 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1308 | PyObject *PyUnicode_FromOrdinal(int ordinal) |
| 1309 | { |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1310 | Py_UNICODE s[2]; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1311 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1312 | if (ordinal < 0 || ordinal > 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1313 | PyErr_SetString(PyExc_ValueError, |
| 1314 | "chr() arg not in range(0x110000)"); |
| 1315 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1316 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1317 | |
| 1318 | #ifndef Py_UNICODE_WIDE |
| 1319 | if (ordinal > 0xffff) { |
| 1320 | ordinal -= 0x10000; |
| 1321 | s[0] = 0xD800 | (ordinal >> 10); |
| 1322 | s[1] = 0xDC00 | (ordinal & 0x3FF); |
| 1323 | return PyUnicode_FromUnicode(s, 2); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1324 | } |
| 1325 | #endif |
| 1326 | |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 1327 | s[0] = (Py_UNICODE)ordinal; |
| 1328 | return PyUnicode_FromUnicode(s, 1); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1331 | PyObject *PyUnicode_FromObject(register PyObject *obj) |
| 1332 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1333 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1334 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1335 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1336 | Py_INCREF(obj); |
| 1337 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1338 | } |
| 1339 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1340 | /* For a Unicode subtype that's not a Unicode object, |
| 1341 | return a true Unicode object with the same data. */ |
| 1342 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 1343 | PyUnicode_GET_SIZE(obj)); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1344 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1345 | PyErr_Format(PyExc_TypeError, |
| 1346 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1347 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1348 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1352 | const char *encoding, |
| 1353 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1354 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 1355 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1356 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1357 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1358 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1359 | PyErr_BadInternalCall(); |
| 1360 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1361 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1362 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 1363 | /* Decoding bytes objects is the most common case and should be fast */ |
| 1364 | if (PyBytes_Check(obj)) { |
| 1365 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 1366 | Py_INCREF(unicode_empty); |
| 1367 | v = (PyObject *) unicode_empty; |
| 1368 | } |
| 1369 | else { |
| 1370 | v = PyUnicode_Decode( |
| 1371 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 1372 | encoding, errors); |
| 1373 | } |
| 1374 | return v; |
| 1375 | } |
| 1376 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1377 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1378 | PyErr_SetString(PyExc_TypeError, |
| 1379 | "decoding str is not supported"); |
| 1380 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1381 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1382 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 1383 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 1384 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 1385 | PyErr_Format(PyExc_TypeError, |
| 1386 | "coercing to str: need bytes, bytearray " |
| 1387 | "or buffer-like object, %.80s found", |
| 1388 | Py_TYPE(obj)->tp_name); |
| 1389 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1390 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1391 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 1392 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1393 | Py_INCREF(unicode_empty); |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 1394 | v = (PyObject *) unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1395 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1396 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 1397 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 1398 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 1399 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1400 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 1403 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1404 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 1405 | 1 on success. */ |
| 1406 | static int |
| 1407 | normalize_encoding(const char *encoding, |
| 1408 | char *lower, |
| 1409 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1410 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1411 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 1412 | char *l; |
| 1413 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1414 | |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1415 | e = encoding; |
| 1416 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 1417 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1418 | while (*e) { |
| 1419 | if (l == l_end) |
| 1420 | return 0; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1421 | if (ISUPPER(*e)) { |
| 1422 | *l++ = TOLOWER(*e++); |
| 1423 | } |
| 1424 | else if (*e == '_') { |
| 1425 | *l++ = '-'; |
| 1426 | e++; |
| 1427 | } |
| 1428 | else { |
| 1429 | *l++ = *e++; |
| 1430 | } |
| 1431 | } |
| 1432 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1433 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | PyObject *PyUnicode_Decode(const char *s, |
| 1437 | Py_ssize_t size, |
| 1438 | const char *encoding, |
| 1439 | const char *errors) |
| 1440 | { |
| 1441 | PyObject *buffer = NULL, *unicode; |
| 1442 | Py_buffer info; |
| 1443 | char lower[11]; /* Enough for any encoding shortcut */ |
| 1444 | |
| 1445 | if (encoding == NULL) |
| 1446 | encoding = PyUnicode_GetDefaultEncoding(); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1447 | |
| 1448 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1449 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
| 1450 | if (strcmp(lower, "utf-8") == 0) |
| 1451 | return PyUnicode_DecodeUTF8(s, size, errors); |
| 1452 | else if ((strcmp(lower, "latin-1") == 0) || |
| 1453 | (strcmp(lower, "iso-8859-1") == 0)) |
| 1454 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1455 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1456 | else if (strcmp(lower, "mbcs") == 0) |
| 1457 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1458 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1459 | else if (strcmp(lower, "ascii") == 0) |
| 1460 | return PyUnicode_DecodeASCII(s, size, errors); |
| 1461 | else if (strcmp(lower, "utf-16") == 0) |
| 1462 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 1463 | else if (strcmp(lower, "utf-32") == 0) |
| 1464 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 1465 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1466 | |
| 1467 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1468 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 1469 | 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] | 1470 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 1471 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1472 | if (buffer == NULL) |
| 1473 | goto onError; |
| 1474 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 1475 | if (unicode == NULL) |
| 1476 | goto onError; |
| 1477 | if (!PyUnicode_Check(unicode)) { |
| 1478 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1479 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1480 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1481 | Py_DECREF(unicode); |
| 1482 | goto onError; |
| 1483 | } |
| 1484 | Py_DECREF(buffer); |
| 1485 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1486 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1487 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1488 | Py_XDECREF(buffer); |
| 1489 | return NULL; |
| 1490 | } |
| 1491 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1492 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 1493 | const char *encoding, |
| 1494 | const char *errors) |
| 1495 | { |
| 1496 | PyObject *v; |
| 1497 | |
| 1498 | if (!PyUnicode_Check(unicode)) { |
| 1499 | PyErr_BadArgument(); |
| 1500 | goto onError; |
| 1501 | } |
| 1502 | |
| 1503 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1504 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1505 | |
| 1506 | /* Decode via the codec registry */ |
| 1507 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1508 | if (v == NULL) |
| 1509 | goto onError; |
| 1510 | return v; |
| 1511 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1512 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1513 | return NULL; |
| 1514 | } |
| 1515 | |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1516 | PyObject *PyUnicode_AsDecodedUnicode(PyObject *unicode, |
| 1517 | const char *encoding, |
| 1518 | const char *errors) |
| 1519 | { |
| 1520 | PyObject *v; |
| 1521 | |
| 1522 | if (!PyUnicode_Check(unicode)) { |
| 1523 | PyErr_BadArgument(); |
| 1524 | goto onError; |
| 1525 | } |
| 1526 | |
| 1527 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1528 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1529 | |
| 1530 | /* Decode via the codec registry */ |
| 1531 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1532 | if (v == NULL) |
| 1533 | goto onError; |
| 1534 | if (!PyUnicode_Check(v)) { |
| 1535 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1536 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1537 | Py_TYPE(v)->tp_name); |
| 1538 | Py_DECREF(v); |
| 1539 | goto onError; |
| 1540 | } |
| 1541 | return v; |
| 1542 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1543 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1544 | return NULL; |
| 1545 | } |
| 1546 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1547 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1548 | Py_ssize_t size, |
| 1549 | const char *encoding, |
| 1550 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1551 | { |
| 1552 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1553 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1554 | unicode = PyUnicode_FromUnicode(s, size); |
| 1555 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1556 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1557 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 1558 | Py_DECREF(unicode); |
| 1559 | return v; |
| 1560 | } |
| 1561 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1562 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 1563 | const char *encoding, |
| 1564 | const char *errors) |
| 1565 | { |
| 1566 | PyObject *v; |
| 1567 | |
| 1568 | if (!PyUnicode_Check(unicode)) { |
| 1569 | PyErr_BadArgument(); |
| 1570 | goto onError; |
| 1571 | } |
| 1572 | |
| 1573 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1574 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1575 | |
| 1576 | /* Encode via the codec registry */ |
| 1577 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1578 | if (v == NULL) |
| 1579 | goto onError; |
| 1580 | return v; |
| 1581 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1582 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1583 | return NULL; |
| 1584 | } |
| 1585 | |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 1586 | PyObject *PyUnicode_EncodeFSDefault(PyObject *unicode) |
| 1587 | { |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 1588 | if (Py_FileSystemDefaultEncoding) { |
| 1589 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 1590 | if (strcmp(Py_FileSystemDefaultEncoding, "mbcs") == 0) |
| 1591 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 1592 | PyUnicode_GET_SIZE(unicode), |
| 1593 | NULL); |
| 1594 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 1595 | return PyUnicode_AsEncodedString(unicode, |
| 1596 | Py_FileSystemDefaultEncoding, |
| 1597 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 1598 | } |
| 1599 | else { |
| 1600 | /* if you change the default encoding, update also |
| 1601 | PyUnicode_DecodeFSDefaultAndSize() and redecode_filenames() */ |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 1602 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Victor Stinner | 3119ed7 | 2010-08-18 22:26:50 +0000 | [diff] [blame] | 1603 | PyUnicode_GET_SIZE(unicode), |
| 1604 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 1605 | } |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1608 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 1609 | const char *encoding, |
| 1610 | const char *errors) |
| 1611 | { |
| 1612 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 1613 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1614 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1615 | if (!PyUnicode_Check(unicode)) { |
| 1616 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1617 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1618 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1619 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1620 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1621 | encoding = PyUnicode_GetDefaultEncoding(); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1622 | |
| 1623 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1624 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
| 1625 | if (strcmp(lower, "utf-8") == 0) |
| 1626 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 1627 | PyUnicode_GET_SIZE(unicode), |
| 1628 | errors); |
| 1629 | else if ((strcmp(lower, "latin-1") == 0) || |
| 1630 | (strcmp(lower, "iso-8859-1") == 0)) |
| 1631 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 1632 | PyUnicode_GET_SIZE(unicode), |
| 1633 | errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1634 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1635 | else if (strcmp(lower, "mbcs") == 0) |
| 1636 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 1637 | PyUnicode_GET_SIZE(unicode), |
| 1638 | errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1639 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1640 | else if (strcmp(lower, "ascii") == 0) |
| 1641 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 1642 | PyUnicode_GET_SIZE(unicode), |
| 1643 | errors); |
| 1644 | } |
Victor Stinner | 59e62db | 2010-05-15 13:14:32 +0000 | [diff] [blame] | 1645 | /* During bootstrap, we may need to find the encodings |
| 1646 | package, to load the file system encoding, and require the |
| 1647 | file system encoding in order to load the encodings |
| 1648 | package. |
Christian Heimes | 6a27efa | 2008-10-30 21:48:26 +0000 | [diff] [blame] | 1649 | |
Victor Stinner | 59e62db | 2010-05-15 13:14:32 +0000 | [diff] [blame] | 1650 | Break out of this dependency by assuming that the path to |
| 1651 | the encodings module is ASCII-only. XXX could try wcstombs |
| 1652 | instead, if the file system encoding is the locale's |
| 1653 | encoding. */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1654 | if (Py_FileSystemDefaultEncoding && |
Victor Stinner | 59e62db | 2010-05-15 13:14:32 +0000 | [diff] [blame] | 1655 | strcmp(encoding, Py_FileSystemDefaultEncoding) == 0 && |
| 1656 | !PyThreadState_GET()->interp->codecs_initialized) |
| 1657 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 1658 | PyUnicode_GET_SIZE(unicode), |
| 1659 | errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1660 | |
| 1661 | /* Encode via the codec registry */ |
| 1662 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1663 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1664 | return NULL; |
| 1665 | |
| 1666 | /* The normal path */ |
| 1667 | if (PyBytes_Check(v)) |
| 1668 | return v; |
| 1669 | |
| 1670 | /* 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] | 1671 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 1672 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1673 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 1674 | |
| 1675 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 1676 | "encoder %s returned bytearray instead of bytes", |
| 1677 | encoding); |
| 1678 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1679 | Py_DECREF(v); |
| 1680 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1681 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1682 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1683 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 1684 | Py_DECREF(v); |
| 1685 | return b; |
| 1686 | } |
| 1687 | |
| 1688 | PyErr_Format(PyExc_TypeError, |
| 1689 | "encoder did not return a bytes object (type=%.400s)", |
| 1690 | Py_TYPE(v)->tp_name); |
| 1691 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1692 | return NULL; |
| 1693 | } |
| 1694 | |
| 1695 | PyObject *PyUnicode_AsEncodedUnicode(PyObject *unicode, |
| 1696 | const char *encoding, |
| 1697 | const char *errors) |
| 1698 | { |
| 1699 | PyObject *v; |
| 1700 | |
| 1701 | if (!PyUnicode_Check(unicode)) { |
| 1702 | PyErr_BadArgument(); |
| 1703 | goto onError; |
| 1704 | } |
| 1705 | |
| 1706 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1707 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1708 | |
| 1709 | /* Encode via the codec registry */ |
| 1710 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1711 | if (v == NULL) |
| 1712 | goto onError; |
| 1713 | if (!PyUnicode_Check(v)) { |
| 1714 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1715 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1716 | Py_TYPE(v)->tp_name); |
| 1717 | Py_DECREF(v); |
| 1718 | goto onError; |
| 1719 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1720 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1721 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1722 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1723 | return NULL; |
| 1724 | } |
| 1725 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1726 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1727 | const char *errors) |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1728 | { |
| 1729 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1730 | if (v) |
| 1731 | return v; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1732 | if (errors != NULL) |
| 1733 | Py_FatalError("non-NULL encoding in _PyUnicode_AsDefaultEncodedString"); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1734 | v = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Guido van Rossum | 0661009 | 2007-08-16 21:02:22 +0000 | [diff] [blame] | 1735 | PyUnicode_GET_SIZE(unicode), |
| 1736 | NULL); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1737 | if (!v) |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1738 | return NULL; |
Guido van Rossum | e7a0d39 | 2007-07-12 07:53:00 +0000 | [diff] [blame] | 1739 | ((PyUnicodeObject *)unicode)->defenc = v; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1740 | return v; |
| 1741 | } |
| 1742 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1743 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1744 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1745 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1746 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 1747 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1748 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1749 | PyObject* |
| 1750 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 1751 | { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1752 | /* During the early bootstrapping process, Py_FileSystemDefaultEncoding |
| 1753 | can be undefined. If it is case, decode using UTF-8. The following assumes |
| 1754 | that Py_FileSystemDefaultEncoding is set to a built-in encoding during the |
| 1755 | bootstrapping process where the codecs aren't ready yet. |
| 1756 | */ |
| 1757 | if (Py_FileSystemDefaultEncoding) { |
| 1758 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1759 | if (strcmp(Py_FileSystemDefaultEncoding, "mbcs") == 0) { |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 1760 | return PyUnicode_DecodeMBCS(s, size, NULL); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1761 | } |
| 1762 | #elif defined(__APPLE__) |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1763 | if (strcmp(Py_FileSystemDefaultEncoding, "utf-8") == 0) { |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 1764 | return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1765 | } |
| 1766 | #endif |
| 1767 | return PyUnicode_Decode(s, size, |
| 1768 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 1769 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1770 | } |
| 1771 | else { |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 1772 | /* if you change the default encoding, update also |
| 1773 | PyUnicode_EncodeFSDefault() and redecode_filenames() */ |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 1774 | return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1775 | } |
| 1776 | } |
| 1777 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1778 | |
| 1779 | int |
| 1780 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 1781 | { |
| 1782 | PyObject *output = NULL; |
| 1783 | Py_ssize_t size; |
| 1784 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 1785 | if (arg == NULL) { |
| 1786 | Py_DECREF(*(PyObject**)addr); |
| 1787 | return 1; |
| 1788 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 1789 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1790 | output = arg; |
| 1791 | Py_INCREF(output); |
| 1792 | } |
| 1793 | else { |
| 1794 | arg = PyUnicode_FromObject(arg); |
| 1795 | if (!arg) |
| 1796 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 1797 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1798 | Py_DECREF(arg); |
| 1799 | if (!output) |
| 1800 | return 0; |
| 1801 | if (!PyBytes_Check(output)) { |
| 1802 | Py_DECREF(output); |
| 1803 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 1804 | return 0; |
| 1805 | } |
| 1806 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 1807 | size = PyBytes_GET_SIZE(output); |
| 1808 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1809 | if (size != strlen(data)) { |
| 1810 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 1811 | Py_DECREF(output); |
| 1812 | return 0; |
| 1813 | } |
| 1814 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 1815 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
| 1818 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 1819 | int |
| 1820 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 1821 | { |
| 1822 | PyObject *output = NULL; |
| 1823 | Py_ssize_t size; |
| 1824 | void *data; |
| 1825 | if (arg == NULL) { |
| 1826 | Py_DECREF(*(PyObject**)addr); |
| 1827 | return 1; |
| 1828 | } |
| 1829 | if (PyUnicode_Check(arg)) { |
| 1830 | output = arg; |
| 1831 | Py_INCREF(output); |
| 1832 | } |
| 1833 | else { |
| 1834 | arg = PyBytes_FromObject(arg); |
| 1835 | if (!arg) |
| 1836 | return 0; |
| 1837 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 1838 | PyBytes_GET_SIZE(arg)); |
| 1839 | Py_DECREF(arg); |
| 1840 | if (!output) |
| 1841 | return 0; |
| 1842 | if (!PyUnicode_Check(output)) { |
| 1843 | Py_DECREF(output); |
| 1844 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 1845 | return 0; |
| 1846 | } |
| 1847 | } |
| 1848 | size = PyUnicode_GET_SIZE(output); |
| 1849 | data = PyUnicode_AS_UNICODE(output); |
| 1850 | if (size != Py_UNICODE_strlen(data)) { |
| 1851 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 1852 | Py_DECREF(output); |
| 1853 | return 0; |
| 1854 | } |
| 1855 | *(PyObject**)addr = output; |
| 1856 | return Py_CLEANUP_SUPPORTED; |
| 1857 | } |
| 1858 | |
| 1859 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1860 | char* |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1861 | _PyUnicode_AsStringAndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1862 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1863 | PyObject *bytes; |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 1864 | if (!PyUnicode_Check(unicode)) { |
| 1865 | PyErr_BadArgument(); |
| 1866 | return NULL; |
| 1867 | } |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1868 | bytes = _PyUnicode_AsDefaultEncodedString(unicode, NULL); |
| 1869 | if (bytes == NULL) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1870 | return NULL; |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1871 | if (psize != NULL) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1872 | *psize = PyBytes_GET_SIZE(bytes); |
| 1873 | return PyBytes_AS_STRING(bytes); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1874 | } |
| 1875 | |
| 1876 | char* |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1877 | _PyUnicode_AsString(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1878 | { |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1879 | return _PyUnicode_AsStringAndSize(unicode, NULL); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1880 | } |
| 1881 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1882 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 1883 | { |
| 1884 | if (!PyUnicode_Check(unicode)) { |
| 1885 | PyErr_BadArgument(); |
| 1886 | goto onError; |
| 1887 | } |
| 1888 | return PyUnicode_AS_UNICODE(unicode); |
| 1889 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1890 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1891 | return NULL; |
| 1892 | } |
| 1893 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1894 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1895 | { |
| 1896 | if (!PyUnicode_Check(unicode)) { |
| 1897 | PyErr_BadArgument(); |
| 1898 | goto onError; |
| 1899 | } |
| 1900 | return PyUnicode_GET_SIZE(unicode); |
| 1901 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1902 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1903 | return -1; |
| 1904 | } |
| 1905 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 1906 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1907 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 1908 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1909 | } |
| 1910 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 1911 | /* create or adjust a UnicodeDecodeError */ |
| 1912 | static void |
| 1913 | make_decode_exception(PyObject **exceptionObject, |
| 1914 | const char *encoding, |
| 1915 | const char *input, Py_ssize_t length, |
| 1916 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 1917 | const char *reason) |
| 1918 | { |
| 1919 | if (*exceptionObject == NULL) { |
| 1920 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 1921 | encoding, input, length, startpos, endpos, reason); |
| 1922 | } |
| 1923 | else { |
| 1924 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 1925 | goto onError; |
| 1926 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 1927 | goto onError; |
| 1928 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 1929 | goto onError; |
| 1930 | } |
| 1931 | return; |
| 1932 | |
| 1933 | onError: |
| 1934 | Py_DECREF(*exceptionObject); |
| 1935 | *exceptionObject = NULL; |
| 1936 | } |
| 1937 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1938 | /* error handling callback helper: |
| 1939 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 1940 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1941 | and adjust various state variables. |
| 1942 | return 0 on success, -1 on error |
| 1943 | */ |
| 1944 | |
| 1945 | static |
| 1946 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1947 | const char *encoding, const char *reason, |
| 1948 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 1949 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 1950 | PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1951 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1952 | 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] | 1953 | |
| 1954 | PyObject *restuple = NULL; |
| 1955 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1956 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1957 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1958 | Py_ssize_t requiredsize; |
| 1959 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1960 | Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1961 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1962 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1963 | int res = -1; |
| 1964 | |
| 1965 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1966 | *errorHandler = PyCodec_LookupError(errors); |
| 1967 | if (*errorHandler == NULL) |
| 1968 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1969 | } |
| 1970 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 1971 | make_decode_exception(exceptionObject, |
| 1972 | encoding, |
| 1973 | *input, *inend - *input, |
| 1974 | *startinpos, *endinpos, |
| 1975 | reason); |
| 1976 | if (*exceptionObject == NULL) |
| 1977 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1978 | |
| 1979 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 1980 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1981 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1982 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 1983 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1984 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1985 | } |
| 1986 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1987 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1988 | |
| 1989 | /* Copy back the bytes variables, which might have been modified by the |
| 1990 | callback */ |
| 1991 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 1992 | if (!inputobj) |
| 1993 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1994 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1995 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1996 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1997 | *input = PyBytes_AS_STRING(inputobj); |
| 1998 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1999 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 2000 | /* we can DECREF safely, as the exception has another reference, |
| 2001 | so the object won't go away. */ |
| 2002 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2003 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2004 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2005 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 2006 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2007 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 2008 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 2009 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2010 | |
| 2011 | /* need more space? (at least enough for what we |
| 2012 | have+the replacement+the rest of the string (starting |
| 2013 | at the new input position), so we won't have to check space |
| 2014 | when there are no errors in the rest of the string) */ |
| 2015 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 2016 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 2017 | requiredsize = *outpos + repsize + insize-newpos; |
| 2018 | if (requiredsize > outsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2019 | if (requiredsize<2*outsize) |
| 2020 | requiredsize = 2*outsize; |
| 2021 | if (_PyUnicode_Resize(output, requiredsize) < 0) |
| 2022 | goto onError; |
| 2023 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2024 | } |
| 2025 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2026 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2027 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 2028 | *outptr += repsize; |
| 2029 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2030 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2031 | /* we made it! */ |
| 2032 | res = 0; |
| 2033 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2034 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2035 | Py_XDECREF(restuple); |
| 2036 | return res; |
| 2037 | } |
| 2038 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2039 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 2040 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2041 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 2042 | |
| 2043 | /* Three simple macros defining base-64. */ |
| 2044 | |
| 2045 | /* Is c a base-64 character? */ |
| 2046 | |
| 2047 | #define IS_BASE64(c) \ |
| 2048 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 2049 | ((c) >= 'a' && (c) <= 'z') || \ |
| 2050 | ((c) >= '0' && (c) <= '9') || \ |
| 2051 | (c) == '+' || (c) == '/') |
| 2052 | |
| 2053 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 2054 | |
| 2055 | #define FROM_BASE64(c) \ |
| 2056 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 2057 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 2058 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 2059 | (c) == '+' ? 62 : 63) |
| 2060 | |
| 2061 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 2062 | |
| 2063 | #define TO_BASE64(n) \ |
| 2064 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 2065 | |
| 2066 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 2067 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 2068 | * byte not decoding to itself is the + which begins a base64 |
| 2069 | * string. */ |
| 2070 | |
| 2071 | #define DECODE_DIRECT(c) \ |
| 2072 | ((c) <= 127 && (c) != '+') |
| 2073 | |
| 2074 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 2075 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 2076 | * the above). See RFC2152. This array identifies these different |
| 2077 | * sets: |
| 2078 | * 0 : "Set D" |
| 2079 | * alphanumeric and '(),-./:? |
| 2080 | * 1 : "Set O" |
| 2081 | * !"#$%&*;<=>@[]^_`{|} |
| 2082 | * 2 : "whitespace" |
| 2083 | * ht nl cr sp |
| 2084 | * 3 : special (must be base64 encoded) |
| 2085 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 2086 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2087 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2088 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2089 | char utf7_category[128] = { |
| 2090 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 2091 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 2092 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 2093 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 2094 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 2095 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 2096 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 2097 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 2098 | /* @ A B C D E F G H I J K L M N O */ |
| 2099 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2100 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 2101 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 2102 | /* ` a b c d e f g h i j k l m n o */ |
| 2103 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2104 | /* p q r s t u v w x y z { | } ~ del */ |
| 2105 | 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] | 2106 | }; |
| 2107 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2108 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 2109 | * answer depends on whether we are encoding set O as itself, and also |
| 2110 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 2111 | * clear that the answers to these questions vary between |
| 2112 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 2113 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2114 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 2115 | ((c) < 128 && (c) > 0 && \ |
| 2116 | ((utf7_category[(c)] == 0) || \ |
| 2117 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 2118 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2119 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2120 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2121 | Py_ssize_t size, |
| 2122 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2123 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2124 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 2125 | } |
| 2126 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2127 | /* The decoder. The only state we preserve is our read position, |
| 2128 | * i.e. how many characters we have consumed. So if we end in the |
| 2129 | * middle of a shift sequence we have to back off the read position |
| 2130 | * and the output to the beginning of the sequence, otherwise we lose |
| 2131 | * all the shift state (seen bits, number of bits seen, high |
| 2132 | * surrogate). */ |
| 2133 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2134 | PyObject *PyUnicode_DecodeUTF7Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2135 | Py_ssize_t size, |
| 2136 | const char *errors, |
| 2137 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2138 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2139 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2140 | Py_ssize_t startinpos; |
| 2141 | Py_ssize_t endinpos; |
| 2142 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2143 | const char *e; |
| 2144 | PyUnicodeObject *unicode; |
| 2145 | Py_UNICODE *p; |
| 2146 | const char *errmsg = ""; |
| 2147 | int inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2148 | Py_UNICODE *shiftOutStart; |
| 2149 | unsigned int base64bits = 0; |
| 2150 | unsigned long base64buffer = 0; |
| 2151 | Py_UNICODE surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2152 | PyObject *errorHandler = NULL; |
| 2153 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2154 | |
| 2155 | unicode = _PyUnicode_New(size); |
| 2156 | if (!unicode) |
| 2157 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2158 | if (size == 0) { |
| 2159 | if (consumed) |
| 2160 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2161 | return (PyObject *)unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2162 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2163 | |
| 2164 | p = unicode->str; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2165 | shiftOutStart = p; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2166 | e = s + size; |
| 2167 | |
| 2168 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2169 | Py_UNICODE ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2170 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 2171 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2172 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2173 | if (inShift) { /* in a base-64 section */ |
| 2174 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 2175 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 2176 | base64bits += 6; |
| 2177 | s++; |
| 2178 | if (base64bits >= 16) { |
| 2179 | /* we have enough bits for a UTF-16 value */ |
| 2180 | Py_UNICODE outCh = (Py_UNICODE) |
| 2181 | (base64buffer >> (base64bits-16)); |
| 2182 | base64bits -= 16; |
| 2183 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 2184 | if (surrogate) { |
| 2185 | /* expecting a second surrogate */ |
| 2186 | if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 2187 | #ifdef Py_UNICODE_WIDE |
| 2188 | *p++ = (((surrogate & 0x3FF)<<10) |
| 2189 | | (outCh & 0x3FF)) + 0x10000; |
| 2190 | #else |
| 2191 | *p++ = surrogate; |
| 2192 | *p++ = outCh; |
| 2193 | #endif |
| 2194 | surrogate = 0; |
| 2195 | } |
| 2196 | else { |
| 2197 | surrogate = 0; |
| 2198 | errmsg = "second surrogate missing"; |
| 2199 | goto utf7Error; |
| 2200 | } |
| 2201 | } |
| 2202 | else if (outCh >= 0xD800 && outCh <= 0xDBFF) { |
| 2203 | /* first surrogate */ |
| 2204 | surrogate = outCh; |
| 2205 | } |
| 2206 | else if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 2207 | errmsg = "unexpected second surrogate"; |
| 2208 | goto utf7Error; |
| 2209 | } |
| 2210 | else { |
| 2211 | *p++ = outCh; |
| 2212 | } |
| 2213 | } |
| 2214 | } |
| 2215 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2216 | inShift = 0; |
| 2217 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2218 | if (surrogate) { |
| 2219 | errmsg = "second surrogate missing at end of shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2220 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2221 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2222 | if (base64bits > 0) { /* left-over bits */ |
| 2223 | if (base64bits >= 6) { |
| 2224 | /* We've seen at least one base-64 character */ |
| 2225 | errmsg = "partial character in shift sequence"; |
| 2226 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2227 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2228 | else { |
| 2229 | /* Some bits remain; they should be zero */ |
| 2230 | if (base64buffer != 0) { |
| 2231 | errmsg = "non-zero padding bits in shift sequence"; |
| 2232 | goto utf7Error; |
| 2233 | } |
| 2234 | } |
| 2235 | } |
| 2236 | if (ch != '-') { |
| 2237 | /* '-' is absorbed; other terminating |
| 2238 | characters are preserved */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2239 | *p++ = ch; |
| 2240 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2241 | } |
| 2242 | } |
| 2243 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2244 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2245 | s++; /* consume '+' */ |
| 2246 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2247 | s++; |
| 2248 | *p++ = '+'; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2249 | } |
| 2250 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2251 | inShift = 1; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2252 | shiftOutStart = p; |
| 2253 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2254 | } |
| 2255 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2256 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2257 | *p++ = ch; |
| 2258 | s++; |
| 2259 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2260 | else { |
| 2261 | startinpos = s-starts; |
| 2262 | s++; |
| 2263 | errmsg = "unexpected special character"; |
| 2264 | goto utf7Error; |
| 2265 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2266 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2267 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2268 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2269 | endinpos = s-starts; |
| 2270 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2271 | errors, &errorHandler, |
| 2272 | "utf7", errmsg, |
| 2273 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2274 | &unicode, &outpos, &p)) |
| 2275 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2276 | } |
| 2277 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2278 | /* end of string */ |
| 2279 | |
| 2280 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 2281 | /* if we're in an inconsistent state, that's an error */ |
| 2282 | if (surrogate || |
| 2283 | (base64bits >= 6) || |
| 2284 | (base64bits > 0 && base64buffer != 0)) { |
| 2285 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2286 | endinpos = size; |
| 2287 | if (unicode_decode_call_errorhandler( |
| 2288 | errors, &errorHandler, |
| 2289 | "utf7", "unterminated shift sequence", |
| 2290 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2291 | &unicode, &outpos, &p)) |
| 2292 | goto onError; |
| 2293 | if (s < e) |
| 2294 | goto restart; |
| 2295 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2296 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2297 | |
| 2298 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2299 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2300 | if (inShift) { |
| 2301 | p = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2302 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2303 | } |
| 2304 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2305 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2306 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2307 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2308 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2309 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2310 | goto onError; |
| 2311 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2312 | Py_XDECREF(errorHandler); |
| 2313 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2314 | return (PyObject *)unicode; |
| 2315 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2316 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2317 | Py_XDECREF(errorHandler); |
| 2318 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2319 | Py_DECREF(unicode); |
| 2320 | return NULL; |
| 2321 | } |
| 2322 | |
| 2323 | |
| 2324 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2325 | Py_ssize_t size, |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2326 | int base64SetO, |
| 2327 | int base64WhiteSpace, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2328 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2329 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2330 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2331 | /* It might be possible to tighten this worst case */ |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 2332 | Py_ssize_t allocated = 8 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2333 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2334 | Py_ssize_t i = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2335 | unsigned int base64bits = 0; |
| 2336 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2337 | char * out; |
| 2338 | char * start; |
| 2339 | |
| 2340 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2341 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2342 | |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 2343 | if (allocated / 8 != size) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2344 | return PyErr_NoMemory(); |
| 2345 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2346 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2347 | if (v == NULL) |
| 2348 | return NULL; |
| 2349 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2350 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2351 | for (;i < size; ++i) { |
| 2352 | Py_UNICODE ch = s[i]; |
| 2353 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2354 | if (inShift) { |
| 2355 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 2356 | /* shifting out */ |
| 2357 | if (base64bits) { /* output remaining bits */ |
| 2358 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 2359 | base64buffer = 0; |
| 2360 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2361 | } |
| 2362 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2363 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 2364 | so no '-' is required, except if the character is itself a '-' */ |
| 2365 | if (IS_BASE64(ch) || ch == '-') { |
| 2366 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2367 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2368 | *out++ = (char) ch; |
| 2369 | } |
| 2370 | else { |
| 2371 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2372 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2373 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2374 | else { /* not in a shift sequence */ |
| 2375 | if (ch == '+') { |
| 2376 | *out++ = '+'; |
| 2377 | *out++ = '-'; |
| 2378 | } |
| 2379 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 2380 | *out++ = (char) ch; |
| 2381 | } |
| 2382 | else { |
| 2383 | *out++ = '+'; |
| 2384 | inShift = 1; |
| 2385 | goto encode_char; |
| 2386 | } |
| 2387 | } |
| 2388 | continue; |
| 2389 | encode_char: |
| 2390 | #ifdef Py_UNICODE_WIDE |
| 2391 | if (ch >= 0x10000) { |
| 2392 | /* code first surrogate */ |
| 2393 | base64bits += 16; |
| 2394 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 2395 | while (base64bits >= 6) { |
| 2396 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 2397 | base64bits -= 6; |
| 2398 | } |
| 2399 | /* prepare second surrogate */ |
| 2400 | ch = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2401 | } |
| 2402 | #endif |
| 2403 | base64bits += 16; |
| 2404 | base64buffer = (base64buffer << 16) | ch; |
| 2405 | while (base64bits >= 6) { |
| 2406 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 2407 | base64bits -= 6; |
| 2408 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 2409 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2410 | if (base64bits) |
| 2411 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 2412 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2413 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2414 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 2415 | return NULL; |
| 2416 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2417 | } |
| 2418 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2419 | #undef IS_BASE64 |
| 2420 | #undef FROM_BASE64 |
| 2421 | #undef TO_BASE64 |
| 2422 | #undef DECODE_DIRECT |
| 2423 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2424 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2425 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 2426 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2427 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2428 | char utf8_code_length[256] = { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2429 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 2430 | illegal prefix. See RFC 3629 for details */ |
| 2431 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 2432 | 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] | 2433 | 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] | 2434 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2435 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2436 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2437 | 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] | 2438 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 2439 | 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] | 2440 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2441 | 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] | 2442 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 2443 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 2444 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 2445 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 2446 | 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] | 2447 | }; |
| 2448 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2449 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2450 | Py_ssize_t size, |
| 2451 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2452 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2453 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 2454 | } |
| 2455 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2456 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 2457 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 2458 | |
| 2459 | /* Mask to quickly check whether a C 'long' contains a |
| 2460 | non-ASCII, UTF8-encoded char. */ |
| 2461 | #if (SIZEOF_LONG == 8) |
| 2462 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 2463 | #elif (SIZEOF_LONG == 4) |
| 2464 | # define ASCII_CHAR_MASK 0x80808080L |
| 2465 | #else |
| 2466 | # error C 'long' size should be either 4 or 8! |
| 2467 | #endif |
| 2468 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2469 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2470 | Py_ssize_t size, |
| 2471 | const char *errors, |
| 2472 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2473 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2474 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2475 | int n; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2476 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2477 | Py_ssize_t startinpos; |
| 2478 | Py_ssize_t endinpos; |
| 2479 | Py_ssize_t outpos; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2480 | const char *e, *aligned_end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2481 | PyUnicodeObject *unicode; |
| 2482 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2483 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2484 | PyObject *errorHandler = NULL; |
| 2485 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2486 | |
| 2487 | /* Note: size will always be longer than the resulting Unicode |
| 2488 | character count */ |
| 2489 | unicode = _PyUnicode_New(size); |
| 2490 | if (!unicode) |
| 2491 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2492 | if (size == 0) { |
| 2493 | if (consumed) |
| 2494 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2495 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2496 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2497 | |
| 2498 | /* Unpack UTF-8 encoded data */ |
| 2499 | p = unicode->str; |
| 2500 | e = s + size; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2501 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2502 | |
| 2503 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2504 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2505 | |
| 2506 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2507 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 2508 | input will consist of an overwhelming majority of ASCII |
| 2509 | characters, we try to optimize for this case by checking |
| 2510 | as many characters as a C 'long' can contain. |
| 2511 | First, check if we can do an aligned read, as most CPUs have |
| 2512 | a penalty for unaligned reads. |
| 2513 | */ |
| 2514 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 2515 | /* Help register allocation */ |
| 2516 | register const char *_s = s; |
| 2517 | register Py_UNICODE *_p = p; |
| 2518 | while (_s < aligned_end) { |
| 2519 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 2520 | and do a fast unrolled copy if it only contains ASCII |
| 2521 | characters. */ |
| 2522 | unsigned long data = *(unsigned long *) _s; |
| 2523 | if (data & ASCII_CHAR_MASK) |
| 2524 | break; |
| 2525 | _p[0] = (unsigned char) _s[0]; |
| 2526 | _p[1] = (unsigned char) _s[1]; |
| 2527 | _p[2] = (unsigned char) _s[2]; |
| 2528 | _p[3] = (unsigned char) _s[3]; |
| 2529 | #if (SIZEOF_LONG == 8) |
| 2530 | _p[4] = (unsigned char) _s[4]; |
| 2531 | _p[5] = (unsigned char) _s[5]; |
| 2532 | _p[6] = (unsigned char) _s[6]; |
| 2533 | _p[7] = (unsigned char) _s[7]; |
| 2534 | #endif |
| 2535 | _s += SIZEOF_LONG; |
| 2536 | _p += SIZEOF_LONG; |
| 2537 | } |
| 2538 | s = _s; |
| 2539 | p = _p; |
| 2540 | if (s == e) |
| 2541 | break; |
| 2542 | ch = (unsigned char)*s; |
| 2543 | } |
| 2544 | } |
| 2545 | |
| 2546 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2547 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2548 | s++; |
| 2549 | continue; |
| 2550 | } |
| 2551 | |
| 2552 | n = utf8_code_length[ch]; |
| 2553 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2554 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2555 | if (consumed) |
| 2556 | break; |
| 2557 | else { |
| 2558 | errmsg = "unexpected end of data"; |
| 2559 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2560 | endinpos = startinpos+1; |
| 2561 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 2562 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2563 | goto utf8Error; |
| 2564 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2565 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2566 | |
| 2567 | switch (n) { |
| 2568 | |
| 2569 | case 0: |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2570 | errmsg = "invalid start byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2571 | startinpos = s-starts; |
| 2572 | endinpos = startinpos+1; |
| 2573 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2574 | |
| 2575 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2576 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2577 | startinpos = s-starts; |
| 2578 | endinpos = startinpos+1; |
| 2579 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2580 | |
| 2581 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2582 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2583 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2584 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2585 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2586 | goto utf8Error; |
| 2587 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2588 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2589 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 2590 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2591 | break; |
| 2592 | |
| 2593 | case 3: |
Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 2594 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 2595 | will result in surrogates in range d800-dfff. Surrogates are |
| 2596 | not valid UTF-8 so they are rejected. |
| 2597 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 2598 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2599 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2600 | (s[2] & 0xc0) != 0x80 || |
| 2601 | ((unsigned char)s[0] == 0xE0 && |
| 2602 | (unsigned char)s[1] < 0xA0) || |
| 2603 | ((unsigned char)s[0] == 0xED && |
| 2604 | (unsigned char)s[1] > 0x9F)) { |
| 2605 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2606 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2607 | endinpos = startinpos + 1; |
| 2608 | |
| 2609 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 2610 | continuation byte is s[2], so increment endinpos by 1, |
| 2611 | if not, s[1] is invalid and endinpos doesn't need to |
| 2612 | be incremented. */ |
| 2613 | if ((s[1] & 0xC0) == 0x80) |
| 2614 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2615 | goto utf8Error; |
| 2616 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2617 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2618 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
| 2619 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2620 | break; |
| 2621 | |
| 2622 | case 4: |
| 2623 | if ((s[1] & 0xc0) != 0x80 || |
| 2624 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2625 | (s[3] & 0xc0) != 0x80 || |
| 2626 | ((unsigned char)s[0] == 0xF0 && |
| 2627 | (unsigned char)s[1] < 0x90) || |
| 2628 | ((unsigned char)s[0] == 0xF4 && |
| 2629 | (unsigned char)s[1] > 0x8F)) { |
| 2630 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2631 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2632 | endinpos = startinpos + 1; |
| 2633 | if ((s[1] & 0xC0) == 0x80) { |
| 2634 | endinpos++; |
| 2635 | if ((s[2] & 0xC0) == 0x80) |
| 2636 | endinpos++; |
| 2637 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2638 | goto utf8Error; |
| 2639 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2640 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2641 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 2642 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 2643 | |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2644 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2645 | *p++ = (Py_UNICODE)ch; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2646 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2647 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2648 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2649 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 2650 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2651 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2652 | /* high surrogate = top 10 bits added to D800 */ |
| 2653 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2654 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2655 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2656 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2657 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2658 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2659 | } |
| 2660 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2661 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2662 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2663 | utf8Error: |
| 2664 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2665 | if (unicode_decode_call_errorhandler( |
| 2666 | errors, &errorHandler, |
| 2667 | "utf8", errmsg, |
| 2668 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2669 | &unicode, &outpos, &p)) |
| 2670 | goto onError; |
| 2671 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2672 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2673 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2674 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2675 | |
| 2676 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2677 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2678 | goto onError; |
| 2679 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2680 | Py_XDECREF(errorHandler); |
| 2681 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2682 | return (PyObject *)unicode; |
| 2683 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2684 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2685 | Py_XDECREF(errorHandler); |
| 2686 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2687 | Py_DECREF(unicode); |
| 2688 | return NULL; |
| 2689 | } |
| 2690 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2691 | #undef ASCII_CHAR_MASK |
| 2692 | |
| 2693 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2694 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 2695 | and allocate exactly as much space needed at the end. Else allocate the |
| 2696 | maximum possible needed (4 result bytes per Unicode character), and return |
| 2697 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2698 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 2699 | PyObject * |
| 2700 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2701 | Py_ssize_t size, |
| 2702 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2703 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2704 | #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] | 2705 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2706 | Py_ssize_t i; /* index into s of next input byte */ |
| 2707 | PyObject *result; /* result string object */ |
| 2708 | char *p; /* next free byte in output buffer */ |
| 2709 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 2710 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2711 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2712 | PyObject *errorHandler = NULL; |
| 2713 | PyObject *exc = NULL; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2714 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2715 | assert(s != NULL); |
| 2716 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2717 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2718 | if (size <= MAX_SHORT_UNICHARS) { |
| 2719 | /* Write into the stack buffer; nallocated can't overflow. |
| 2720 | * At the end, we'll allocate exactly as much heap space as it |
| 2721 | * turns out we need. |
| 2722 | */ |
| 2723 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2724 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2725 | p = stackbuf; |
| 2726 | } |
| 2727 | else { |
| 2728 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 2729 | nallocated = size * 4; |
| 2730 | if (nallocated / 4 != size) /* overflow! */ |
| 2731 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2732 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2733 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2734 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2735 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2736 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2737 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2738 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2739 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2740 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2741 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2742 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2743 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2744 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2745 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2746 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 2747 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 2748 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2749 | } else if (0xD800 <= ch && ch <= 0xDFFF) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2750 | #ifndef Py_UNICODE_WIDE |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2751 | /* Special case: check for high and low surrogate */ |
| 2752 | if (ch <= 0xDBFF && i != size && 0xDC00 <= s[i] && s[i] <= 0xDFFF) { |
| 2753 | Py_UCS4 ch2 = s[i]; |
| 2754 | /* Combine the two surrogates to form a UCS4 value */ |
| 2755 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
| 2756 | i++; |
| 2757 | |
| 2758 | /* Encode UCS4 Unicode ordinals */ |
| 2759 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2760 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2761 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2762 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2763 | } else { |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 2764 | #endif |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2765 | Py_ssize_t newpos; |
| 2766 | PyObject *rep; |
| 2767 | Py_ssize_t repsize, k; |
| 2768 | rep = unicode_encode_call_errorhandler |
| 2769 | (errors, &errorHandler, "utf-8", "surrogates not allowed", |
| 2770 | s, size, &exc, i-1, i, &newpos); |
| 2771 | if (!rep) |
| 2772 | goto error; |
| 2773 | |
| 2774 | if (PyBytes_Check(rep)) |
| 2775 | repsize = PyBytes_GET_SIZE(rep); |
| 2776 | else |
| 2777 | repsize = PyUnicode_GET_SIZE(rep); |
| 2778 | |
| 2779 | if (repsize > 4) { |
| 2780 | Py_ssize_t offset; |
| 2781 | |
| 2782 | if (result == NULL) |
| 2783 | offset = p - stackbuf; |
| 2784 | else |
| 2785 | offset = p - PyBytes_AS_STRING(result); |
| 2786 | |
| 2787 | if (nallocated > PY_SSIZE_T_MAX - repsize + 4) { |
| 2788 | /* integer overflow */ |
| 2789 | PyErr_NoMemory(); |
| 2790 | goto error; |
| 2791 | } |
| 2792 | nallocated += repsize - 4; |
| 2793 | if (result != NULL) { |
| 2794 | if (_PyBytes_Resize(&result, nallocated) < 0) |
| 2795 | goto error; |
| 2796 | } else { |
| 2797 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
| 2798 | if (result == NULL) |
| 2799 | goto error; |
| 2800 | Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset); |
| 2801 | } |
| 2802 | p = PyBytes_AS_STRING(result) + offset; |
| 2803 | } |
| 2804 | |
| 2805 | if (PyBytes_Check(rep)) { |
| 2806 | char *prep = PyBytes_AS_STRING(rep); |
| 2807 | for(k = repsize; k > 0; k--) |
| 2808 | *p++ = *prep++; |
| 2809 | } else /* rep is unicode */ { |
| 2810 | Py_UNICODE *prep = PyUnicode_AS_UNICODE(rep); |
| 2811 | Py_UNICODE c; |
| 2812 | |
| 2813 | for(k=0; k<repsize; k++) { |
| 2814 | c = prep[k]; |
| 2815 | if (0x80 <= c) { |
| 2816 | raise_encode_exception(&exc, "utf-8", s, size, |
| 2817 | i-1, i, "surrogates not allowed"); |
| 2818 | goto error; |
| 2819 | } |
| 2820 | *p++ = (char)prep[k]; |
| 2821 | } |
| 2822 | } |
| 2823 | Py_DECREF(rep); |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 2824 | #ifndef Py_UNICODE_WIDE |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2825 | } |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 2826 | #endif |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2827 | } else if (ch < 0x10000) { |
| 2828 | *p++ = (char)(0xe0 | (ch >> 12)); |
| 2829 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2830 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2831 | } else /* ch >= 0x10000 */ { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2832 | /* Encode UCS4 Unicode ordinals */ |
| 2833 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2834 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 2835 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2836 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2837 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2838 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 2839 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2840 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2841 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2842 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2843 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2844 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2845 | } |
| 2846 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 2847 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2848 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2849 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2850 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2851 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2852 | Py_XDECREF(errorHandler); |
| 2853 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2854 | return result; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2855 | error: |
| 2856 | Py_XDECREF(errorHandler); |
| 2857 | Py_XDECREF(exc); |
| 2858 | Py_XDECREF(result); |
| 2859 | return NULL; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2860 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2861 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2862 | } |
| 2863 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2864 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 2865 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2866 | if (!PyUnicode_Check(unicode)) { |
| 2867 | PyErr_BadArgument(); |
| 2868 | return NULL; |
| 2869 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 2870 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2871 | PyUnicode_GET_SIZE(unicode), |
| 2872 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2873 | } |
| 2874 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2875 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 2876 | |
| 2877 | PyObject * |
| 2878 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2879 | Py_ssize_t size, |
| 2880 | const char *errors, |
| 2881 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2882 | { |
| 2883 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 2884 | } |
| 2885 | |
| 2886 | PyObject * |
| 2887 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2888 | Py_ssize_t size, |
| 2889 | const char *errors, |
| 2890 | int *byteorder, |
| 2891 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2892 | { |
| 2893 | const char *starts = s; |
| 2894 | Py_ssize_t startinpos; |
| 2895 | Py_ssize_t endinpos; |
| 2896 | Py_ssize_t outpos; |
| 2897 | PyUnicodeObject *unicode; |
| 2898 | Py_UNICODE *p; |
| 2899 | #ifndef Py_UNICODE_WIDE |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 2900 | int pairs = 0; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 2901 | const unsigned char *qq; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2902 | #else |
| 2903 | const int pairs = 0; |
| 2904 | #endif |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 2905 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2906 | int bo = 0; /* assume native ordering by default */ |
| 2907 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2908 | /* Offsets from q for retrieving bytes in the right order. */ |
| 2909 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2910 | int iorder[] = {0, 1, 2, 3}; |
| 2911 | #else |
| 2912 | int iorder[] = {3, 2, 1, 0}; |
| 2913 | #endif |
| 2914 | PyObject *errorHandler = NULL; |
| 2915 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 2916 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2917 | q = (unsigned char *)s; |
| 2918 | e = q + size; |
| 2919 | |
| 2920 | if (byteorder) |
| 2921 | bo = *byteorder; |
| 2922 | |
| 2923 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2924 | byte order setting accordingly. In native mode, the leading BOM |
| 2925 | mark is skipped, in all other modes, it is copied to the output |
| 2926 | stream as-is (giving a ZWNBSP character). */ |
| 2927 | if (bo == 0) { |
| 2928 | if (size >= 4) { |
| 2929 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2930 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2931 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2932 | if (bom == 0x0000FEFF) { |
| 2933 | q += 4; |
| 2934 | bo = -1; |
| 2935 | } |
| 2936 | else if (bom == 0xFFFE0000) { |
| 2937 | q += 4; |
| 2938 | bo = 1; |
| 2939 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2940 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2941 | if (bom == 0x0000FEFF) { |
| 2942 | q += 4; |
| 2943 | bo = 1; |
| 2944 | } |
| 2945 | else if (bom == 0xFFFE0000) { |
| 2946 | q += 4; |
| 2947 | bo = -1; |
| 2948 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2949 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2950 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2951 | } |
| 2952 | |
| 2953 | if (bo == -1) { |
| 2954 | /* force LE */ |
| 2955 | iorder[0] = 0; |
| 2956 | iorder[1] = 1; |
| 2957 | iorder[2] = 2; |
| 2958 | iorder[3] = 3; |
| 2959 | } |
| 2960 | else if (bo == 1) { |
| 2961 | /* force BE */ |
| 2962 | iorder[0] = 3; |
| 2963 | iorder[1] = 2; |
| 2964 | iorder[2] = 1; |
| 2965 | iorder[3] = 0; |
| 2966 | } |
| 2967 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 2968 | /* On narrow builds we split characters outside the BMP into two |
| 2969 | codepoints => count how much extra space we need. */ |
| 2970 | #ifndef Py_UNICODE_WIDE |
| 2971 | for (qq = q; qq < e; qq += 4) |
| 2972 | if (qq[iorder[2]] != 0 || qq[iorder[3]] != 0) |
| 2973 | pairs++; |
| 2974 | #endif |
| 2975 | |
| 2976 | /* This might be one to much, because of a BOM */ |
| 2977 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 2978 | if (!unicode) |
| 2979 | return NULL; |
| 2980 | if (size == 0) |
| 2981 | return (PyObject *)unicode; |
| 2982 | |
| 2983 | /* Unpack UTF-32 encoded data */ |
| 2984 | p = unicode->str; |
| 2985 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2986 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2987 | Py_UCS4 ch; |
| 2988 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 2989 | if (e-q<4) { |
| 2990 | if (consumed) |
| 2991 | break; |
| 2992 | errmsg = "truncated data"; |
| 2993 | startinpos = ((const char *)q)-starts; |
| 2994 | endinpos = ((const char *)e)-starts; |
| 2995 | goto utf32Error; |
| 2996 | /* The remaining input chars are ignored if the callback |
| 2997 | chooses to skip the input */ |
| 2998 | } |
| 2999 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 3000 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3001 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3002 | if (ch >= 0x110000) |
| 3003 | { |
| 3004 | errmsg = "codepoint not in range(0x110000)"; |
| 3005 | startinpos = ((const char *)q)-starts; |
| 3006 | endinpos = startinpos+4; |
| 3007 | goto utf32Error; |
| 3008 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3009 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3010 | if (ch >= 0x10000) |
| 3011 | { |
| 3012 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 3013 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 3014 | } |
| 3015 | else |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3016 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3017 | *p++ = ch; |
| 3018 | q += 4; |
| 3019 | continue; |
| 3020 | utf32Error: |
| 3021 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 3022 | if (unicode_decode_call_errorhandler( |
| 3023 | errors, &errorHandler, |
| 3024 | "utf32", errmsg, |
| 3025 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
| 3026 | &unicode, &outpos, &p)) |
| 3027 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3028 | } |
| 3029 | |
| 3030 | if (byteorder) |
| 3031 | *byteorder = bo; |
| 3032 | |
| 3033 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3034 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3035 | |
| 3036 | /* Adjust length */ |
| 3037 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
| 3038 | goto onError; |
| 3039 | |
| 3040 | Py_XDECREF(errorHandler); |
| 3041 | Py_XDECREF(exc); |
| 3042 | return (PyObject *)unicode; |
| 3043 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3044 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3045 | Py_DECREF(unicode); |
| 3046 | Py_XDECREF(errorHandler); |
| 3047 | Py_XDECREF(exc); |
| 3048 | return NULL; |
| 3049 | } |
| 3050 | |
| 3051 | PyObject * |
| 3052 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3053 | Py_ssize_t size, |
| 3054 | const char *errors, |
| 3055 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3056 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3057 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3058 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3059 | Py_ssize_t nsize, bytesize; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3060 | #ifndef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3061 | Py_ssize_t i, pairs; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3062 | #else |
| 3063 | const int pairs = 0; |
| 3064 | #endif |
| 3065 | /* Offsets from p for storing byte pairs in the right order. */ |
| 3066 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3067 | int iorder[] = {0, 1, 2, 3}; |
| 3068 | #else |
| 3069 | int iorder[] = {3, 2, 1, 0}; |
| 3070 | #endif |
| 3071 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3072 | #define STORECHAR(CH) \ |
| 3073 | do { \ |
| 3074 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 3075 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 3076 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 3077 | p[iorder[0]] = (CH) & 0xff; \ |
| 3078 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3079 | } while(0) |
| 3080 | |
| 3081 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 3082 | so we need less space. */ |
| 3083 | #ifndef Py_UNICODE_WIDE |
| 3084 | for (i = pairs = 0; i < size-1; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3085 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 3086 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 3087 | pairs++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3088 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3089 | nsize = (size - pairs + (byteorder == 0)); |
| 3090 | bytesize = nsize * 4; |
| 3091 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3092 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3093 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3094 | if (v == NULL) |
| 3095 | return NULL; |
| 3096 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3097 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3098 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3099 | STORECHAR(0xFEFF); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3100 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3101 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3102 | |
| 3103 | if (byteorder == -1) { |
| 3104 | /* force LE */ |
| 3105 | iorder[0] = 0; |
| 3106 | iorder[1] = 1; |
| 3107 | iorder[2] = 2; |
| 3108 | iorder[3] = 3; |
| 3109 | } |
| 3110 | else if (byteorder == 1) { |
| 3111 | /* force BE */ |
| 3112 | iorder[0] = 3; |
| 3113 | iorder[1] = 2; |
| 3114 | iorder[2] = 1; |
| 3115 | iorder[3] = 0; |
| 3116 | } |
| 3117 | |
| 3118 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3119 | Py_UCS4 ch = *s++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3120 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3121 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 3122 | Py_UCS4 ch2 = *s; |
| 3123 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 3124 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 3125 | s++; |
| 3126 | size--; |
| 3127 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3128 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3129 | #endif |
| 3130 | STORECHAR(ch); |
| 3131 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3132 | |
| 3133 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3134 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3135 | #undef STORECHAR |
| 3136 | } |
| 3137 | |
| 3138 | PyObject *PyUnicode_AsUTF32String(PyObject *unicode) |
| 3139 | { |
| 3140 | if (!PyUnicode_Check(unicode)) { |
| 3141 | PyErr_BadArgument(); |
| 3142 | return NULL; |
| 3143 | } |
| 3144 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3145 | PyUnicode_GET_SIZE(unicode), |
| 3146 | NULL, |
| 3147 | 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3148 | } |
| 3149 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3150 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 3151 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3152 | PyObject * |
| 3153 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3154 | Py_ssize_t size, |
| 3155 | const char *errors, |
| 3156 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3157 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3158 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 3159 | } |
| 3160 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3161 | /* Two masks for fast checking of whether a C 'long' may contain |
| 3162 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 3163 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 3164 | rare in most input. |
| 3165 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 3166 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3167 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3168 | #if (SIZEOF_LONG == 8) |
| 3169 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 3170 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 3171 | #elif (SIZEOF_LONG == 4) |
| 3172 | # define FAST_CHAR_MASK 0x80008000L |
| 3173 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 3174 | #else |
| 3175 | # error C 'long' size should be either 4 or 8! |
| 3176 | #endif |
| 3177 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3178 | PyObject * |
| 3179 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3180 | Py_ssize_t size, |
| 3181 | const char *errors, |
| 3182 | int *byteorder, |
| 3183 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3184 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3185 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3186 | Py_ssize_t startinpos; |
| 3187 | Py_ssize_t endinpos; |
| 3188 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3189 | PyUnicodeObject *unicode; |
| 3190 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3191 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3192 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3193 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 3194 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3195 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 3196 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3197 | int ihi = 1, ilo = 0; |
| 3198 | #else |
| 3199 | int ihi = 0, ilo = 1; |
| 3200 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3201 | PyObject *errorHandler = NULL; |
| 3202 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3203 | |
| 3204 | /* Note: size will always be longer than the resulting Unicode |
| 3205 | character count */ |
| 3206 | unicode = _PyUnicode_New(size); |
| 3207 | if (!unicode) |
| 3208 | return NULL; |
| 3209 | if (size == 0) |
| 3210 | return (PyObject *)unicode; |
| 3211 | |
| 3212 | /* Unpack UTF-16 encoded data */ |
| 3213 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3214 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3215 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3216 | |
| 3217 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3218 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3219 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3220 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 3221 | byte order setting accordingly. In native mode, the leading BOM |
| 3222 | mark is skipped, in all other modes, it is copied to the output |
| 3223 | stream as-is (giving a ZWNBSP character). */ |
| 3224 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3225 | if (size >= 2) { |
| 3226 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3227 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3228 | if (bom == 0xFEFF) { |
| 3229 | q += 2; |
| 3230 | bo = -1; |
| 3231 | } |
| 3232 | else if (bom == 0xFFFE) { |
| 3233 | q += 2; |
| 3234 | bo = 1; |
| 3235 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3236 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3237 | if (bom == 0xFEFF) { |
| 3238 | q += 2; |
| 3239 | bo = 1; |
| 3240 | } |
| 3241 | else if (bom == 0xFFFE) { |
| 3242 | q += 2; |
| 3243 | bo = -1; |
| 3244 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3245 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3246 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3247 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3248 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3249 | if (bo == -1) { |
| 3250 | /* force LE */ |
| 3251 | ihi = 1; |
| 3252 | ilo = 0; |
| 3253 | } |
| 3254 | else if (bo == 1) { |
| 3255 | /* force BE */ |
| 3256 | ihi = 0; |
| 3257 | ilo = 1; |
| 3258 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3259 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3260 | native_ordering = ilo < ihi; |
| 3261 | #else |
| 3262 | native_ordering = ilo > ihi; |
| 3263 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3264 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3265 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3266 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3267 | Py_UNICODE ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3268 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 3269 | reads are more expensive, better to defer to another iteration. */ |
| 3270 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 3271 | /* Fast path for runs of non-surrogate chars. */ |
| 3272 | register const unsigned char *_q = q; |
| 3273 | Py_UNICODE *_p = p; |
| 3274 | if (native_ordering) { |
| 3275 | /* Native ordering is simple: as long as the input cannot |
| 3276 | possibly contain a surrogate char, do an unrolled copy |
| 3277 | of several 16-bit code points to the target object. |
| 3278 | The non-surrogate check is done on several input bytes |
| 3279 | at a time (as many as a C 'long' can contain). */ |
| 3280 | while (_q < aligned_end) { |
| 3281 | unsigned long data = * (unsigned long *) _q; |
| 3282 | if (data & FAST_CHAR_MASK) |
| 3283 | break; |
| 3284 | _p[0] = ((unsigned short *) _q)[0]; |
| 3285 | _p[1] = ((unsigned short *) _q)[1]; |
| 3286 | #if (SIZEOF_LONG == 8) |
| 3287 | _p[2] = ((unsigned short *) _q)[2]; |
| 3288 | _p[3] = ((unsigned short *) _q)[3]; |
| 3289 | #endif |
| 3290 | _q += SIZEOF_LONG; |
| 3291 | _p += SIZEOF_LONG / 2; |
| 3292 | } |
| 3293 | } |
| 3294 | else { |
| 3295 | /* Byteswapped ordering is similar, but we must decompose |
| 3296 | the copy bytewise, and take care of zero'ing out the |
| 3297 | upper bytes if the target object is in 32-bit units |
| 3298 | (that is, in UCS-4 builds). */ |
| 3299 | while (_q < aligned_end) { |
| 3300 | unsigned long data = * (unsigned long *) _q; |
| 3301 | if (data & SWAPPED_FAST_CHAR_MASK) |
| 3302 | break; |
| 3303 | /* Zero upper bytes in UCS-4 builds */ |
| 3304 | #if (Py_UNICODE_SIZE > 2) |
| 3305 | _p[0] = 0; |
| 3306 | _p[1] = 0; |
| 3307 | #if (SIZEOF_LONG == 8) |
| 3308 | _p[2] = 0; |
| 3309 | _p[3] = 0; |
| 3310 | #endif |
| 3311 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 3312 | /* Issue #4916; UCS-4 builds on big endian machines must |
| 3313 | fill the two last bytes of each 4-byte unit. */ |
| 3314 | #if (!defined(BYTEORDER_IS_LITTLE_ENDIAN) && Py_UNICODE_SIZE > 2) |
| 3315 | # define OFF 2 |
| 3316 | #else |
| 3317 | # define OFF 0 |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3318 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 3319 | ((unsigned char *) _p)[OFF + 1] = _q[0]; |
| 3320 | ((unsigned char *) _p)[OFF + 0] = _q[1]; |
| 3321 | ((unsigned char *) _p)[OFF + 1 + Py_UNICODE_SIZE] = _q[2]; |
| 3322 | ((unsigned char *) _p)[OFF + 0 + Py_UNICODE_SIZE] = _q[3]; |
| 3323 | #if (SIZEOF_LONG == 8) |
| 3324 | ((unsigned char *) _p)[OFF + 1 + 2 * Py_UNICODE_SIZE] = _q[4]; |
| 3325 | ((unsigned char *) _p)[OFF + 0 + 2 * Py_UNICODE_SIZE] = _q[5]; |
| 3326 | ((unsigned char *) _p)[OFF + 1 + 3 * Py_UNICODE_SIZE] = _q[6]; |
| 3327 | ((unsigned char *) _p)[OFF + 0 + 3 * Py_UNICODE_SIZE] = _q[7]; |
| 3328 | #endif |
| 3329 | #undef OFF |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3330 | _q += SIZEOF_LONG; |
| 3331 | _p += SIZEOF_LONG / 2; |
| 3332 | } |
| 3333 | } |
| 3334 | p = _p; |
| 3335 | q = _q; |
| 3336 | if (q >= e) |
| 3337 | break; |
| 3338 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3339 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3340 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3341 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3342 | |
| 3343 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 3344 | *p++ = ch; |
| 3345 | continue; |
| 3346 | } |
| 3347 | |
| 3348 | /* UTF-16 code pair: */ |
| 3349 | if (q > e) { |
| 3350 | errmsg = "unexpected end of data"; |
| 3351 | startinpos = (((const char *)q) - 2) - starts; |
| 3352 | endinpos = ((const char *)e) + 1 - starts; |
| 3353 | goto utf16Error; |
| 3354 | } |
| 3355 | if (0xD800 <= ch && ch <= 0xDBFF) { |
| 3356 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 3357 | q += 2; |
| 3358 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 3359 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3360 | *p++ = ch; |
| 3361 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3362 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3363 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3364 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3365 | continue; |
| 3366 | } |
| 3367 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3368 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3369 | startinpos = (((const char *)q)-4)-starts; |
| 3370 | endinpos = startinpos+2; |
| 3371 | goto utf16Error; |
| 3372 | } |
| 3373 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3374 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3375 | errmsg = "illegal encoding"; |
| 3376 | startinpos = (((const char *)q)-2)-starts; |
| 3377 | endinpos = startinpos+2; |
| 3378 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3379 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3380 | utf16Error: |
| 3381 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 3382 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3383 | errors, |
| 3384 | &errorHandler, |
| 3385 | "utf16", errmsg, |
| 3386 | &starts, |
| 3387 | (const char **)&e, |
| 3388 | &startinpos, |
| 3389 | &endinpos, |
| 3390 | &exc, |
| 3391 | (const char **)&q, |
| 3392 | &unicode, |
| 3393 | &outpos, |
| 3394 | &p)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3395 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3396 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3397 | /* remaining byte at the end? (size should be even) */ |
| 3398 | if (e == q) { |
| 3399 | if (!consumed) { |
| 3400 | errmsg = "truncated data"; |
| 3401 | startinpos = ((const char *)q) - starts; |
| 3402 | endinpos = ((const char *)e) + 1 - starts; |
| 3403 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 3404 | if (unicode_decode_call_errorhandler( |
| 3405 | errors, |
| 3406 | &errorHandler, |
| 3407 | "utf16", errmsg, |
| 3408 | &starts, |
| 3409 | (const char **)&e, |
| 3410 | &startinpos, |
| 3411 | &endinpos, |
| 3412 | &exc, |
| 3413 | (const char **)&q, |
| 3414 | &unicode, |
| 3415 | &outpos, |
| 3416 | &p)) |
| 3417 | goto onError; |
| 3418 | /* The remaining input chars are ignored if the callback |
| 3419 | chooses to skip the input */ |
| 3420 | } |
| 3421 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3422 | |
| 3423 | if (byteorder) |
| 3424 | *byteorder = bo; |
| 3425 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3426 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3427 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3428 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3429 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 3430 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3431 | goto onError; |
| 3432 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3433 | Py_XDECREF(errorHandler); |
| 3434 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3435 | return (PyObject *)unicode; |
| 3436 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3437 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3438 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3439 | Py_XDECREF(errorHandler); |
| 3440 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3441 | return NULL; |
| 3442 | } |
| 3443 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3444 | #undef FAST_CHAR_MASK |
| 3445 | #undef SWAPPED_FAST_CHAR_MASK |
| 3446 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3447 | PyObject * |
| 3448 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3449 | Py_ssize_t size, |
| 3450 | const char *errors, |
| 3451 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3452 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3453 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3454 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3455 | Py_ssize_t nsize, bytesize; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3456 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3457 | Py_ssize_t i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3458 | #else |
| 3459 | const int pairs = 0; |
| 3460 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3461 | /* Offsets from p for storing byte pairs in the right order. */ |
| 3462 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3463 | int ihi = 1, ilo = 0; |
| 3464 | #else |
| 3465 | int ihi = 0, ilo = 1; |
| 3466 | #endif |
| 3467 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3468 | #define STORECHAR(CH) \ |
| 3469 | do { \ |
| 3470 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 3471 | p[ilo] = (CH) & 0xff; \ |
| 3472 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3473 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3474 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3475 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3476 | for (i = pairs = 0; i < size; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3477 | if (s[i] >= 0x10000) |
| 3478 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3479 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3480 | /* 2 * (size + pairs + (byteorder == 0)) */ |
| 3481 | if (size > PY_SSIZE_T_MAX || |
| 3482 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3483 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3484 | nsize = size + pairs + (byteorder == 0); |
| 3485 | bytesize = nsize * 2; |
| 3486 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3487 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3488 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3489 | if (v == NULL) |
| 3490 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3491 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3492 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3493 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3494 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 3495 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3496 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3497 | |
| 3498 | if (byteorder == -1) { |
| 3499 | /* force LE */ |
| 3500 | ihi = 1; |
| 3501 | ilo = 0; |
| 3502 | } |
| 3503 | else if (byteorder == 1) { |
| 3504 | /* force BE */ |
| 3505 | ihi = 0; |
| 3506 | ilo = 1; |
| 3507 | } |
| 3508 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3509 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3510 | Py_UNICODE ch = *s++; |
| 3511 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3512 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3513 | if (ch >= 0x10000) { |
| 3514 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 3515 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 3516 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3517 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3518 | STORECHAR(ch); |
| 3519 | if (ch2) |
| 3520 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3521 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3522 | |
| 3523 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3524 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3525 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3526 | } |
| 3527 | |
| 3528 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 3529 | { |
| 3530 | if (!PyUnicode_Check(unicode)) { |
| 3531 | PyErr_BadArgument(); |
| 3532 | return NULL; |
| 3533 | } |
| 3534 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3535 | PyUnicode_GET_SIZE(unicode), |
| 3536 | NULL, |
| 3537 | 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3538 | } |
| 3539 | |
| 3540 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 3541 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 3542 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 3543 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3544 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3545 | Py_ssize_t size, |
| 3546 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3547 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3548 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3549 | Py_ssize_t startinpos; |
| 3550 | Py_ssize_t endinpos; |
| 3551 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3552 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3553 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3554 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3555 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3556 | char* message; |
| 3557 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3558 | PyObject *errorHandler = NULL; |
| 3559 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3560 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3561 | /* Escaped strings will always be longer than the resulting |
| 3562 | 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] | 3563 | length after conversion to the true value. |
| 3564 | (but if the error callback returns a long replacement string |
| 3565 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3566 | v = _PyUnicode_New(size); |
| 3567 | if (v == NULL) |
| 3568 | goto onError; |
| 3569 | if (size == 0) |
| 3570 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3571 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3572 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3573 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3574 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3575 | while (s < end) { |
| 3576 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 3577 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3578 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3579 | |
| 3580 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3581 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3582 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3583 | continue; |
| 3584 | } |
| 3585 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3586 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3587 | /* \ - Escapes */ |
| 3588 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3589 | c = *s++; |
| 3590 | if (s > end) |
| 3591 | c = '\0'; /* Invalid after \ */ |
| 3592 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3593 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3594 | /* \x escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3595 | case '\n': break; |
| 3596 | case '\\': *p++ = '\\'; break; |
| 3597 | case '\'': *p++ = '\''; break; |
| 3598 | case '\"': *p++ = '\"'; break; |
| 3599 | case 'b': *p++ = '\b'; break; |
| 3600 | case 'f': *p++ = '\014'; break; /* FF */ |
| 3601 | case 't': *p++ = '\t'; break; |
| 3602 | case 'n': *p++ = '\n'; break; |
| 3603 | case 'r': *p++ = '\r'; break; |
| 3604 | case 'v': *p++ = '\013'; break; /* VT */ |
| 3605 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 3606 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3607 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3608 | case '0': case '1': case '2': case '3': |
| 3609 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3610 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3611 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3612 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3613 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3614 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3615 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3616 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3617 | break; |
| 3618 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3619 | /* hex escapes */ |
| 3620 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3621 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3622 | digits = 2; |
| 3623 | message = "truncated \\xXX escape"; |
| 3624 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3625 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3626 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3627 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3628 | digits = 4; |
| 3629 | message = "truncated \\uXXXX escape"; |
| 3630 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3631 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3632 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3633 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3634 | digits = 8; |
| 3635 | message = "truncated \\UXXXXXXXX escape"; |
| 3636 | hexescape: |
| 3637 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3638 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3639 | if (s+digits>end) { |
| 3640 | endinpos = size; |
| 3641 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3642 | errors, &errorHandler, |
| 3643 | "unicodeescape", "end of string in escape sequence", |
| 3644 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3645 | &v, &outpos, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3646 | goto onError; |
| 3647 | goto nextByte; |
| 3648 | } |
| 3649 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3650 | c = (unsigned char) s[i]; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3651 | if (!ISXDIGIT(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3652 | endinpos = (s+i+1)-starts; |
| 3653 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3654 | errors, &errorHandler, |
| 3655 | "unicodeescape", message, |
| 3656 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3657 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3658 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3659 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3660 | } |
| 3661 | chr = (chr<<4) & ~0xF; |
| 3662 | if (c >= '0' && c <= '9') |
| 3663 | chr += c - '0'; |
| 3664 | else if (c >= 'a' && c <= 'f') |
| 3665 | chr += 10 + c - 'a'; |
| 3666 | else |
| 3667 | chr += 10 + c - 'A'; |
| 3668 | } |
| 3669 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 3670 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3671 | /* _decoding_error will have already written into the |
| 3672 | target buffer. */ |
| 3673 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3674 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3675 | /* when we get here, chr is a 32-bit unicode character */ |
| 3676 | if (chr <= 0xffff) |
| 3677 | /* UCS-2 character */ |
| 3678 | *p++ = (Py_UNICODE) chr; |
| 3679 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3680 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3681 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 3682 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3683 | *p++ = chr; |
| 3684 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3685 | chr -= 0x10000L; |
| 3686 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 3687 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3688 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3689 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3690 | endinpos = s-starts; |
| 3691 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3692 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3693 | errors, &errorHandler, |
| 3694 | "unicodeescape", "illegal Unicode character", |
| 3695 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3696 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3697 | goto onError; |
| 3698 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3699 | break; |
| 3700 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3701 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3702 | case 'N': |
| 3703 | message = "malformed \\N character escape"; |
| 3704 | if (ucnhash_CAPI == NULL) { |
| 3705 | /* load the unicode data module */ |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3706 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import(PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3707 | if (ucnhash_CAPI == NULL) |
| 3708 | goto ucnhashError; |
| 3709 | } |
| 3710 | if (*s == '{') { |
| 3711 | const char *start = s+1; |
| 3712 | /* look for the closing brace */ |
| 3713 | while (*s != '}' && s < end) |
| 3714 | s++; |
| 3715 | if (s > start && s < end && *s == '}') { |
| 3716 | /* found a name. look it up in the unicode database */ |
| 3717 | message = "unknown Unicode character name"; |
| 3718 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 3719 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3720 | goto store; |
| 3721 | } |
| 3722 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3723 | endinpos = s-starts; |
| 3724 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3725 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3726 | errors, &errorHandler, |
| 3727 | "unicodeescape", message, |
| 3728 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3729 | &v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3730 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3731 | break; |
| 3732 | |
| 3733 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3734 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3735 | message = "\\ at end of string"; |
| 3736 | s--; |
| 3737 | endinpos = s-starts; |
| 3738 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3739 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3740 | errors, &errorHandler, |
| 3741 | "unicodeescape", message, |
| 3742 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3743 | &v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3744 | goto onError; |
| 3745 | } |
| 3746 | else { |
| 3747 | *p++ = '\\'; |
| 3748 | *p++ = (unsigned char)s[-1]; |
| 3749 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3750 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3751 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3752 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3753 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3754 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3755 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3756 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 3757 | Py_XDECREF(errorHandler); |
| 3758 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3759 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3760 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3761 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 3762 | PyErr_SetString( |
| 3763 | PyExc_UnicodeError, |
| 3764 | "\\N escapes not supported (can't load unicodedata module)" |
| 3765 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3766 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3767 | Py_XDECREF(errorHandler); |
| 3768 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 3769 | return NULL; |
| 3770 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3771 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3772 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3773 | Py_XDECREF(errorHandler); |
| 3774 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3775 | return NULL; |
| 3776 | } |
| 3777 | |
| 3778 | /* Return a Unicode-Escape string version of the Unicode object. |
| 3779 | |
| 3780 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 3781 | appropriate. |
| 3782 | |
| 3783 | */ |
| 3784 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3785 | Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3786 | Py_ssize_t size, |
| 3787 | Py_UNICODE ch) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3788 | { |
| 3789 | /* like wcschr, but doesn't stop at NULL characters */ |
| 3790 | |
| 3791 | while (size-- > 0) { |
| 3792 | if (*s == ch) |
| 3793 | return s; |
| 3794 | s++; |
| 3795 | } |
| 3796 | |
| 3797 | return NULL; |
| 3798 | } |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 3799 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3800 | static const char *hexdigits = "0123456789abcdef"; |
| 3801 | |
| 3802 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3803 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3804 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3805 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3806 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3807 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3808 | #ifdef Py_UNICODE_WIDE |
| 3809 | const Py_ssize_t expandsize = 10; |
| 3810 | #else |
| 3811 | const Py_ssize_t expandsize = 6; |
| 3812 | #endif |
| 3813 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3814 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 3815 | better to choose a different scheme. Perhaps scan the |
| 3816 | first N-chars of the string and allocate based on that size. |
| 3817 | */ |
| 3818 | /* Initial allocation is based on the longest-possible unichr |
| 3819 | escape. |
| 3820 | |
| 3821 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 3822 | unichr, so in this case it's the longest unichr escape. In |
| 3823 | narrow (UTF-16) builds this is five chars per source unichr |
| 3824 | since there are two unichrs in the surrogate pair, so in narrow |
| 3825 | (UTF-16) builds it's not the longest unichr escape. |
| 3826 | |
| 3827 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 3828 | so in the narrow (UTF-16) build case it's the longest unichr |
| 3829 | escape. |
| 3830 | */ |
| 3831 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3832 | if (size == 0) |
| 3833 | return PyBytes_FromStringAndSize(NULL, 0); |
| 3834 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3835 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3836 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3837 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3838 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3839 | 2 |
| 3840 | + expandsize*size |
| 3841 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3842 | if (repr == NULL) |
| 3843 | return NULL; |
| 3844 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3845 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3846 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3847 | while (size-- > 0) { |
| 3848 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3849 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3850 | /* Escape backslashes */ |
| 3851 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3852 | *p++ = '\\'; |
| 3853 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3854 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3855 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3856 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 3857 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3858 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 3859 | else if (ch >= 0x10000) { |
| 3860 | *p++ = '\\'; |
| 3861 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3862 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 3863 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 3864 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 3865 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 3866 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 3867 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 3868 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 3869 | *p++ = hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3870 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3871 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3872 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3873 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 3874 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 3875 | Py_UNICODE ch2; |
| 3876 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3877 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3878 | ch2 = *s++; |
| 3879 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 3880 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3881 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 3882 | *p++ = '\\'; |
| 3883 | *p++ = 'U'; |
| 3884 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 3885 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 3886 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 3887 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 3888 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 3889 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 3890 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 3891 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 3892 | continue; |
| 3893 | } |
| 3894 | /* Fall through: isolated surrogates are copied as-is */ |
| 3895 | s--; |
| 3896 | size++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3897 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3898 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3899 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3900 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3901 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3902 | *p++ = '\\'; |
| 3903 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3904 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 3905 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 3906 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 3907 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3908 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3909 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3910 | /* Map special whitespace to '\t', \n', '\r' */ |
| 3911 | else if (ch == '\t') { |
| 3912 | *p++ = '\\'; |
| 3913 | *p++ = 't'; |
| 3914 | } |
| 3915 | else if (ch == '\n') { |
| 3916 | *p++ = '\\'; |
| 3917 | *p++ = 'n'; |
| 3918 | } |
| 3919 | else if (ch == '\r') { |
| 3920 | *p++ = '\\'; |
| 3921 | *p++ = 'r'; |
| 3922 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3923 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3924 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 3925 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3926 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3927 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3928 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 3929 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3930 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3931 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3932 | /* Copy everything else as-is */ |
| 3933 | else |
| 3934 | *p++ = (char) ch; |
| 3935 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3936 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3937 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 3938 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 3939 | return NULL; |
| 3940 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3941 | } |
| 3942 | |
Alexandre Vassalotti | 2056bed | 2008-12-27 19:46:35 +0000 | [diff] [blame] | 3943 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3944 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3945 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3946 | if (!PyUnicode_Check(unicode)) { |
| 3947 | PyErr_BadArgument(); |
| 3948 | return NULL; |
| 3949 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3950 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3951 | PyUnicode_GET_SIZE(unicode)); |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3952 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3953 | } |
| 3954 | |
| 3955 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 3956 | |
| 3957 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3958 | Py_ssize_t size, |
| 3959 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3960 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3961 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3962 | Py_ssize_t startinpos; |
| 3963 | Py_ssize_t endinpos; |
| 3964 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3965 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3966 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3967 | const char *end; |
| 3968 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3969 | PyObject *errorHandler = NULL; |
| 3970 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3971 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3972 | /* Escaped strings will always be longer than the resulting |
| 3973 | 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] | 3974 | length after conversion to the true value. (But decoding error |
| 3975 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3976 | v = _PyUnicode_New(size); |
| 3977 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3978 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3979 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3980 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3981 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3982 | end = s + size; |
| 3983 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3984 | unsigned char c; |
| 3985 | Py_UCS4 x; |
| 3986 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3987 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3988 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3989 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3990 | if (*s != '\\') { |
| 3991 | *p++ = (unsigned char)*s++; |
| 3992 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3993 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3994 | startinpos = s-starts; |
| 3995 | |
| 3996 | /* \u-escapes are only interpreted iff the number of leading |
| 3997 | backslashes if odd */ |
| 3998 | bs = s; |
| 3999 | for (;s < end;) { |
| 4000 | if (*s != '\\') |
| 4001 | break; |
| 4002 | *p++ = (unsigned char)*s++; |
| 4003 | } |
| 4004 | if (((s - bs) & 1) == 0 || |
| 4005 | s >= end || |
| 4006 | (*s != 'u' && *s != 'U')) { |
| 4007 | continue; |
| 4008 | } |
| 4009 | p--; |
| 4010 | count = *s=='u' ? 4 : 8; |
| 4011 | s++; |
| 4012 | |
| 4013 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
| 4014 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4015 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 4016 | c = (unsigned char)*s; |
| 4017 | if (!ISXDIGIT(c)) { |
| 4018 | endinpos = s-starts; |
| 4019 | if (unicode_decode_call_errorhandler( |
| 4020 | errors, &errorHandler, |
| 4021 | "rawunicodeescape", "truncated \\uXXXX", |
| 4022 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 4023 | &v, &outpos, &p)) |
| 4024 | goto onError; |
| 4025 | goto nextByte; |
| 4026 | } |
| 4027 | x = (x<<4) & ~0xF; |
| 4028 | if (c >= '0' && c <= '9') |
| 4029 | x += c - '0'; |
| 4030 | else if (c >= 'a' && c <= 'f') |
| 4031 | x += 10 + c - 'a'; |
| 4032 | else |
| 4033 | x += 10 + c - 'A'; |
| 4034 | } |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4035 | if (x <= 0xffff) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4036 | /* UCS-2 character */ |
| 4037 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4038 | else if (x <= 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4039 | /* UCS-4 character. Either store directly, or as |
| 4040 | surrogate pair. */ |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4041 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4042 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4043 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4044 | x -= 0x10000L; |
| 4045 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 4046 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4047 | #endif |
| 4048 | } else { |
| 4049 | endinpos = s-starts; |
| 4050 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4051 | if (unicode_decode_call_errorhandler( |
| 4052 | errors, &errorHandler, |
| 4053 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4054 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 4055 | &v, &outpos, &p)) |
| 4056 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4057 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4058 | nextByte: |
| 4059 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4060 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4061 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4062 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4063 | Py_XDECREF(errorHandler); |
| 4064 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4065 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4066 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4067 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4068 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4069 | Py_XDECREF(errorHandler); |
| 4070 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4071 | return NULL; |
| 4072 | } |
| 4073 | |
| 4074 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4075 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4076 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4077 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4078 | char *p; |
| 4079 | char *q; |
| 4080 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4081 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4082 | const Py_ssize_t expandsize = 10; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4083 | #else |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4084 | const Py_ssize_t expandsize = 6; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4085 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4086 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4087 | if (size > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4088 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4089 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4090 | repr = PyBytes_FromStringAndSize(NULL, expandsize * size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4091 | if (repr == NULL) |
| 4092 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 4093 | if (size == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4094 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4095 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4096 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4097 | while (size-- > 0) { |
| 4098 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4099 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4100 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 4101 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4102 | *p++ = '\\'; |
| 4103 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 4104 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 4105 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 4106 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 4107 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 4108 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 4109 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 4110 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 4111 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4112 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4113 | else |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4114 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4115 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 4116 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 4117 | Py_UNICODE ch2; |
| 4118 | Py_UCS4 ucs; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4119 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4120 | ch2 = *s++; |
| 4121 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 4122 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4123 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 4124 | *p++ = '\\'; |
| 4125 | *p++ = 'U'; |
| 4126 | *p++ = hexdigits[(ucs >> 28) & 0xf]; |
| 4127 | *p++ = hexdigits[(ucs >> 24) & 0xf]; |
| 4128 | *p++ = hexdigits[(ucs >> 20) & 0xf]; |
| 4129 | *p++ = hexdigits[(ucs >> 16) & 0xf]; |
| 4130 | *p++ = hexdigits[(ucs >> 12) & 0xf]; |
| 4131 | *p++ = hexdigits[(ucs >> 8) & 0xf]; |
| 4132 | *p++ = hexdigits[(ucs >> 4) & 0xf]; |
| 4133 | *p++ = hexdigits[ucs & 0xf]; |
| 4134 | continue; |
| 4135 | } |
| 4136 | /* Fall through: isolated surrogates are copied as-is */ |
| 4137 | s--; |
| 4138 | size++; |
| 4139 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4140 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4141 | /* Map 16-bit characters to '\uxxxx' */ |
| 4142 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4143 | *p++ = '\\'; |
| 4144 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 4145 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 4146 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 4147 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 4148 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4149 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4150 | /* Copy everything else as-is */ |
| 4151 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4152 | *p++ = (char) ch; |
| 4153 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4154 | size = p - q; |
| 4155 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4156 | assert(size > 0); |
| 4157 | if (_PyBytes_Resize(&repr, size) < 0) |
| 4158 | return NULL; |
| 4159 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4160 | } |
| 4161 | |
| 4162 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 4163 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 4164 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4165 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 4166 | PyErr_BadArgument(); |
| 4167 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4168 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 4169 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 4170 | PyUnicode_GET_SIZE(unicode)); |
| 4171 | |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 4172 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4173 | } |
| 4174 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4175 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 4176 | |
| 4177 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4178 | Py_ssize_t size, |
| 4179 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4180 | { |
| 4181 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4182 | Py_ssize_t startinpos; |
| 4183 | Py_ssize_t endinpos; |
| 4184 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4185 | PyUnicodeObject *v; |
| 4186 | Py_UNICODE *p; |
| 4187 | const char *end; |
| 4188 | const char *reason; |
| 4189 | PyObject *errorHandler = NULL; |
| 4190 | PyObject *exc = NULL; |
| 4191 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 4192 | #ifdef Py_UNICODE_WIDE |
| 4193 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 4194 | #endif |
| 4195 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4196 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4197 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 4198 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4199 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4200 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4201 | return (PyObject *)v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4202 | p = PyUnicode_AS_UNICODE(v); |
| 4203 | end = s + size; |
| 4204 | |
| 4205 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4206 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4207 | /* We have to sanity check the raw data, otherwise doom looms for |
| 4208 | some malformed UCS-4 data. */ |
| 4209 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4210 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4211 | *p > unimax || *p < 0 || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4212 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4213 | end-s < Py_UNICODE_SIZE |
| 4214 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4215 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4216 | startinpos = s - starts; |
| 4217 | if (end-s < Py_UNICODE_SIZE) { |
| 4218 | endinpos = end-starts; |
| 4219 | reason = "truncated input"; |
| 4220 | } |
| 4221 | else { |
| 4222 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 4223 | reason = "illegal code point (> 0x10FFFF)"; |
| 4224 | } |
| 4225 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 4226 | if (unicode_decode_call_errorhandler( |
| 4227 | errors, &errorHandler, |
| 4228 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4229 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 4230 | &v, &outpos, &p)) { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4231 | goto onError; |
| 4232 | } |
| 4233 | } |
| 4234 | else { |
| 4235 | p++; |
| 4236 | s += Py_UNICODE_SIZE; |
| 4237 | } |
| 4238 | } |
| 4239 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4240 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4241 | goto onError; |
| 4242 | Py_XDECREF(errorHandler); |
| 4243 | Py_XDECREF(exc); |
| 4244 | return (PyObject *)v; |
| 4245 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4246 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4247 | Py_XDECREF(v); |
| 4248 | Py_XDECREF(errorHandler); |
| 4249 | Py_XDECREF(exc); |
| 4250 | return NULL; |
| 4251 | } |
| 4252 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4253 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 4254 | |
| 4255 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4256 | Py_ssize_t size, |
| 4257 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4258 | { |
| 4259 | PyUnicodeObject *v; |
| 4260 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4261 | const char *e, *unrolled_end; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4262 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4263 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4264 | if (size == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4265 | Py_UNICODE r = *(unsigned char*)s; |
| 4266 | return PyUnicode_FromUnicode(&r, 1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4267 | } |
| 4268 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4269 | v = _PyUnicode_New(size); |
| 4270 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4271 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4272 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4273 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4274 | p = PyUnicode_AS_UNICODE(v); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4275 | e = s + size; |
| 4276 | /* Unrolling the copy makes it much faster by reducing the looping |
| 4277 | overhead. This is similar to what many memcpy() implementations do. */ |
| 4278 | unrolled_end = e - 4; |
| 4279 | while (s < unrolled_end) { |
| 4280 | p[0] = (unsigned char) s[0]; |
| 4281 | p[1] = (unsigned char) s[1]; |
| 4282 | p[2] = (unsigned char) s[2]; |
| 4283 | p[3] = (unsigned char) s[3]; |
| 4284 | s += 4; |
| 4285 | p += 4; |
| 4286 | } |
| 4287 | while (s < e) |
| 4288 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4289 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4290 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4291 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4292 | Py_XDECREF(v); |
| 4293 | return NULL; |
| 4294 | } |
| 4295 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4296 | /* create or adjust a UnicodeEncodeError */ |
| 4297 | static void make_encode_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4298 | const char *encoding, |
| 4299 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4300 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4301 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4302 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4303 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4304 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 4305 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4306 | } |
| 4307 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4308 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 4309 | goto onError; |
| 4310 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 4311 | goto onError; |
| 4312 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 4313 | goto onError; |
| 4314 | return; |
| 4315 | onError: |
| 4316 | Py_DECREF(*exceptionObject); |
| 4317 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4318 | } |
| 4319 | } |
| 4320 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4321 | /* raises a UnicodeEncodeError */ |
| 4322 | static void raise_encode_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4323 | const char *encoding, |
| 4324 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4325 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4326 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4327 | { |
| 4328 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4329 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4330 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4331 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4332 | } |
| 4333 | |
| 4334 | /* error handling callback helper: |
| 4335 | build arguments, call the callback and check the arguments, |
| 4336 | put the result into newpos and return the replacement string, which |
| 4337 | has to be freed by the caller */ |
| 4338 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4339 | PyObject **errorHandler, |
| 4340 | const char *encoding, const char *reason, |
| 4341 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 4342 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4343 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4344 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4345 | 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] | 4346 | |
| 4347 | PyObject *restuple; |
| 4348 | PyObject *resunicode; |
| 4349 | |
| 4350 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4351 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4352 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4353 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4354 | } |
| 4355 | |
| 4356 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4357 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4358 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4359 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4360 | |
| 4361 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4362 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4363 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4364 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4365 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4366 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4367 | Py_DECREF(restuple); |
| 4368 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4369 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4370 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4371 | &resunicode, newpos)) { |
| 4372 | Py_DECREF(restuple); |
| 4373 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4374 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4375 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 4376 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 4377 | Py_DECREF(restuple); |
| 4378 | return NULL; |
| 4379 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4380 | if (*newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4381 | *newpos = size+*newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4382 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4383 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 4384 | Py_DECREF(restuple); |
| 4385 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4386 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4387 | Py_INCREF(resunicode); |
| 4388 | Py_DECREF(restuple); |
| 4389 | return resunicode; |
| 4390 | } |
| 4391 | |
| 4392 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4393 | Py_ssize_t size, |
| 4394 | const char *errors, |
| 4395 | int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4396 | { |
| 4397 | /* output object */ |
| 4398 | PyObject *res; |
| 4399 | /* pointers to the beginning and end+1 of input */ |
| 4400 | const Py_UNICODE *startp = p; |
| 4401 | const Py_UNICODE *endp = p + size; |
| 4402 | /* pointer to the beginning of the unencodable characters */ |
| 4403 | /* const Py_UNICODE *badp = NULL; */ |
| 4404 | /* pointer into the output */ |
| 4405 | char *str; |
| 4406 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4407 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4408 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 4409 | 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] | 4410 | PyObject *errorHandler = NULL; |
| 4411 | PyObject *exc = NULL; |
| 4412 | /* the following variable is used for caching string comparisons |
| 4413 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 4414 | int known_errorHandler = -1; |
| 4415 | |
| 4416 | /* allocate enough for a simple encoding without |
| 4417 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4418 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4419 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4420 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4421 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4422 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4423 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4424 | ressize = size; |
| 4425 | |
| 4426 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4427 | Py_UNICODE c = *p; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4428 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4429 | /* can we encode this? */ |
| 4430 | if (c<limit) { |
| 4431 | /* no overflow check, because we know that the space is enough */ |
| 4432 | *str++ = (char)c; |
| 4433 | ++p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4434 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4435 | else { |
| 4436 | Py_ssize_t unicodepos = p-startp; |
| 4437 | Py_ssize_t requiredsize; |
| 4438 | PyObject *repunicode; |
| 4439 | Py_ssize_t repsize; |
| 4440 | Py_ssize_t newpos; |
| 4441 | Py_ssize_t respos; |
| 4442 | Py_UNICODE *uni2; |
| 4443 | /* startpos for collecting unencodable chars */ |
| 4444 | const Py_UNICODE *collstart = p; |
| 4445 | const Py_UNICODE *collend = p; |
| 4446 | /* find all unecodable characters */ |
| 4447 | while ((collend < endp) && ((*collend)>=limit)) |
| 4448 | ++collend; |
| 4449 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 4450 | if (known_errorHandler==-1) { |
| 4451 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4452 | known_errorHandler = 1; |
| 4453 | else if (!strcmp(errors, "replace")) |
| 4454 | known_errorHandler = 2; |
| 4455 | else if (!strcmp(errors, "ignore")) |
| 4456 | known_errorHandler = 3; |
| 4457 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4458 | known_errorHandler = 4; |
| 4459 | else |
| 4460 | known_errorHandler = 0; |
| 4461 | } |
| 4462 | switch (known_errorHandler) { |
| 4463 | case 1: /* strict */ |
| 4464 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 4465 | goto onError; |
| 4466 | case 2: /* replace */ |
| 4467 | while (collstart++<collend) |
| 4468 | *str++ = '?'; /* fall through */ |
| 4469 | case 3: /* ignore */ |
| 4470 | p = collend; |
| 4471 | break; |
| 4472 | case 4: /* xmlcharrefreplace */ |
| 4473 | respos = str - PyBytes_AS_STRING(res); |
| 4474 | /* determine replacement size (temporarily (mis)uses p) */ |
| 4475 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 4476 | if (*p<10) |
| 4477 | repsize += 2+1+1; |
| 4478 | else if (*p<100) |
| 4479 | repsize += 2+2+1; |
| 4480 | else if (*p<1000) |
| 4481 | repsize += 2+3+1; |
| 4482 | else if (*p<10000) |
| 4483 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 4484 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4485 | else |
| 4486 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 4487 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4488 | else if (*p<100000) |
| 4489 | repsize += 2+5+1; |
| 4490 | else if (*p<1000000) |
| 4491 | repsize += 2+6+1; |
| 4492 | else |
| 4493 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4494 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4495 | } |
| 4496 | requiredsize = respos+repsize+(endp-collend); |
| 4497 | if (requiredsize > ressize) { |
| 4498 | if (requiredsize<2*ressize) |
| 4499 | requiredsize = 2*ressize; |
| 4500 | if (_PyBytes_Resize(&res, requiredsize)) |
| 4501 | goto onError; |
| 4502 | str = PyBytes_AS_STRING(res) + respos; |
| 4503 | ressize = requiredsize; |
| 4504 | } |
| 4505 | /* generate replacement (temporarily (mis)uses p) */ |
| 4506 | for (p = collstart; p < collend; ++p) { |
| 4507 | str += sprintf(str, "&#%d;", (int)*p); |
| 4508 | } |
| 4509 | p = collend; |
| 4510 | break; |
| 4511 | default: |
| 4512 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 4513 | encoding, reason, startp, size, &exc, |
| 4514 | collstart-startp, collend-startp, &newpos); |
| 4515 | if (repunicode == NULL) |
| 4516 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4517 | if (PyBytes_Check(repunicode)) { |
| 4518 | /* Directly copy bytes result to output. */ |
| 4519 | repsize = PyBytes_Size(repunicode); |
| 4520 | if (repsize > 1) { |
| 4521 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 4522 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4523 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 4524 | Py_DECREF(repunicode); |
| 4525 | goto onError; |
| 4526 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 4527 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4528 | ressize += repsize-1; |
| 4529 | } |
| 4530 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 4531 | str += repsize; |
| 4532 | p = startp + newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4533 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4534 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4535 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4536 | /* need more space? (at least enough for what we |
| 4537 | have+the replacement+the rest of the string, so |
| 4538 | we won't have to check space for encodable characters) */ |
| 4539 | respos = str - PyBytes_AS_STRING(res); |
| 4540 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4541 | requiredsize = respos+repsize+(endp-collend); |
| 4542 | if (requiredsize > ressize) { |
| 4543 | if (requiredsize<2*ressize) |
| 4544 | requiredsize = 2*ressize; |
| 4545 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 4546 | Py_DECREF(repunicode); |
| 4547 | goto onError; |
| 4548 | } |
| 4549 | str = PyBytes_AS_STRING(res) + respos; |
| 4550 | ressize = requiredsize; |
| 4551 | } |
| 4552 | /* check if there is anything unencodable in the replacement |
| 4553 | and copy it to the output */ |
| 4554 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 4555 | c = *uni2; |
| 4556 | if (c >= limit) { |
| 4557 | raise_encode_exception(&exc, encoding, startp, size, |
| 4558 | unicodepos, unicodepos+1, reason); |
| 4559 | Py_DECREF(repunicode); |
| 4560 | goto onError; |
| 4561 | } |
| 4562 | *str = (char)c; |
| 4563 | } |
| 4564 | p = startp + newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4565 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4566 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4567 | } |
| 4568 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4569 | /* Resize if we allocated to much */ |
| 4570 | size = str - PyBytes_AS_STRING(res); |
| 4571 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 4572 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4573 | if (_PyBytes_Resize(&res, size) < 0) |
| 4574 | goto onError; |
| 4575 | } |
| 4576 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4577 | Py_XDECREF(errorHandler); |
| 4578 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4579 | return res; |
| 4580 | |
| 4581 | onError: |
| 4582 | Py_XDECREF(res); |
| 4583 | Py_XDECREF(errorHandler); |
| 4584 | Py_XDECREF(exc); |
| 4585 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4586 | } |
| 4587 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4588 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4589 | Py_ssize_t size, |
| 4590 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4591 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4592 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4593 | } |
| 4594 | |
| 4595 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 4596 | { |
| 4597 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4598 | PyErr_BadArgument(); |
| 4599 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4600 | } |
| 4601 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4602 | PyUnicode_GET_SIZE(unicode), |
| 4603 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4604 | } |
| 4605 | |
| 4606 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 4607 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4608 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4609 | Py_ssize_t size, |
| 4610 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4611 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4612 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4613 | PyUnicodeObject *v; |
| 4614 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4615 | Py_ssize_t startinpos; |
| 4616 | Py_ssize_t endinpos; |
| 4617 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4618 | const char *e; |
| 4619 | PyObject *errorHandler = NULL; |
| 4620 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4621 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4622 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4623 | if (size == 1 && *(unsigned char*)s < 128) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4624 | Py_UNICODE r = *(unsigned char*)s; |
| 4625 | return PyUnicode_FromUnicode(&r, 1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4626 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4627 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4628 | v = _PyUnicode_New(size); |
| 4629 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4630 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4631 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4632 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4633 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4634 | e = s + size; |
| 4635 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4636 | register unsigned char c = (unsigned char)*s; |
| 4637 | if (c < 128) { |
| 4638 | *p++ = c; |
| 4639 | ++s; |
| 4640 | } |
| 4641 | else { |
| 4642 | startinpos = s-starts; |
| 4643 | endinpos = startinpos + 1; |
| 4644 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
| 4645 | if (unicode_decode_call_errorhandler( |
| 4646 | errors, &errorHandler, |
| 4647 | "ascii", "ordinal not in range(128)", |
| 4648 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 4649 | &v, &outpos, &p)) |
| 4650 | goto onError; |
| 4651 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4652 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 4653 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4654 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 4655 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4656 | Py_XDECREF(errorHandler); |
| 4657 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4658 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4659 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4660 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4661 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4662 | Py_XDECREF(errorHandler); |
| 4663 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4664 | return NULL; |
| 4665 | } |
| 4666 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4667 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4668 | Py_ssize_t size, |
| 4669 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4670 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4671 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4672 | } |
| 4673 | |
| 4674 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 4675 | { |
| 4676 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4677 | PyErr_BadArgument(); |
| 4678 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4679 | } |
| 4680 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4681 | PyUnicode_GET_SIZE(unicode), |
| 4682 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4683 | } |
| 4684 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4685 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4686 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4687 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4688 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 4689 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4690 | #define NEED_RETRY |
| 4691 | #endif |
| 4692 | |
| 4693 | /* XXX This code is limited to "true" double-byte encodings, as |
| 4694 | a) it assumes an incomplete character consists of a single byte, and |
| 4695 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4696 | encodings, see IsDBCSLeadByteEx documentation. */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4697 | |
| 4698 | static int is_dbcs_lead_byte(const char *s, int offset) |
| 4699 | { |
| 4700 | const char *curr = s + offset; |
| 4701 | |
| 4702 | if (IsDBCSLeadByte(*curr)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4703 | const char *prev = CharPrev(s, curr); |
| 4704 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4705 | } |
| 4706 | return 0; |
| 4707 | } |
| 4708 | |
| 4709 | /* |
| 4710 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 4711 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 4712 | */ |
| 4713 | static int decode_mbcs(PyUnicodeObject **v, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4714 | const char *s, /* MBCS string */ |
| 4715 | int size, /* sizeof MBCS string */ |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4716 | int final, |
| 4717 | const char *errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4718 | { |
| 4719 | Py_UNICODE *p; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4720 | Py_ssize_t n; |
| 4721 | DWORD usize; |
| 4722 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4723 | |
| 4724 | assert(size >= 0); |
| 4725 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4726 | /* check and handle 'errors' arg */ |
| 4727 | if (errors==NULL || strcmp(errors, "strict")==0) |
| 4728 | flags = MB_ERR_INVALID_CHARS; |
| 4729 | else if (strcmp(errors, "ignore")==0) |
| 4730 | flags = 0; |
| 4731 | else { |
| 4732 | PyErr_Format(PyExc_ValueError, |
| 4733 | "mbcs encoding does not support errors='%s'", |
| 4734 | errors); |
| 4735 | return -1; |
| 4736 | } |
| 4737 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4738 | /* Skip trailing lead-byte unless 'final' is set */ |
| 4739 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4740 | --size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4741 | |
| 4742 | /* First get the size of the result */ |
| 4743 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4744 | usize = MultiByteToWideChar(CP_ACP, flags, s, size, NULL, 0); |
| 4745 | if (usize==0) |
| 4746 | goto mbcs_decode_error; |
| 4747 | } else |
| 4748 | usize = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4749 | |
| 4750 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4751 | /* Create unicode object */ |
| 4752 | *v = _PyUnicode_New(usize); |
| 4753 | if (*v == NULL) |
| 4754 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4755 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4756 | } |
| 4757 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4758 | /* Extend unicode object */ |
| 4759 | n = PyUnicode_GET_SIZE(*v); |
| 4760 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 4761 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4762 | } |
| 4763 | |
| 4764 | /* Do the conversion */ |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4765 | if (usize > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4766 | p = PyUnicode_AS_UNICODE(*v) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4767 | if (0 == MultiByteToWideChar(CP_ACP, flags, s, size, p, usize)) { |
| 4768 | goto mbcs_decode_error; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4769 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4770 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4771 | return size; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4772 | |
| 4773 | mbcs_decode_error: |
| 4774 | /* If the last error was ERROR_NO_UNICODE_TRANSLATION, then |
| 4775 | we raise a UnicodeDecodeError - else it is a 'generic' |
| 4776 | windows error |
| 4777 | */ |
| 4778 | if (GetLastError()==ERROR_NO_UNICODE_TRANSLATION) { |
| 4779 | /* Ideally, we should get reason from FormatMessage - this |
| 4780 | is the Windows 2000 English version of the message |
| 4781 | */ |
| 4782 | PyObject *exc = NULL; |
| 4783 | const char *reason = "No mapping for the Unicode character exists " |
| 4784 | "in the target multi-byte code page."; |
| 4785 | make_decode_exception(&exc, "mbcs", s, size, 0, 0, reason); |
| 4786 | if (exc != NULL) { |
| 4787 | PyCodec_StrictErrors(exc); |
| 4788 | Py_DECREF(exc); |
| 4789 | } |
| 4790 | } else { |
| 4791 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4792 | } |
| 4793 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4794 | } |
| 4795 | |
| 4796 | PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4797 | Py_ssize_t size, |
| 4798 | const char *errors, |
| 4799 | Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4800 | { |
| 4801 | PyUnicodeObject *v = NULL; |
| 4802 | int done; |
| 4803 | |
| 4804 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4805 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4806 | |
| 4807 | #ifdef NEED_RETRY |
| 4808 | retry: |
| 4809 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4810 | done = decode_mbcs(&v, s, INT_MAX, 0, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4811 | else |
| 4812 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4813 | done = decode_mbcs(&v, s, (int)size, !consumed, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4814 | |
| 4815 | if (done < 0) { |
| 4816 | Py_XDECREF(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4817 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4818 | } |
| 4819 | |
| 4820 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4821 | *consumed += done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4822 | |
| 4823 | #ifdef NEED_RETRY |
| 4824 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4825 | s += done; |
| 4826 | size -= done; |
| 4827 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4828 | } |
| 4829 | #endif |
| 4830 | |
| 4831 | return (PyObject *)v; |
| 4832 | } |
| 4833 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4834 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4835 | Py_ssize_t size, |
| 4836 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4837 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4838 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 4839 | } |
| 4840 | |
| 4841 | /* |
| 4842 | * Convert unicode into string object (MBCS). |
| 4843 | * Returns 0 if succeed, -1 otherwise. |
| 4844 | */ |
| 4845 | static int encode_mbcs(PyObject **repr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4846 | const Py_UNICODE *p, /* unicode */ |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4847 | int size, /* size of unicode */ |
| 4848 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4849 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4850 | BOOL usedDefaultChar = FALSE; |
| 4851 | BOOL *pusedDefaultChar; |
| 4852 | int mbcssize; |
| 4853 | Py_ssize_t n; |
| 4854 | PyObject *exc = NULL; |
| 4855 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4856 | |
| 4857 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4858 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4859 | /* check and handle 'errors' arg */ |
| 4860 | if (errors==NULL || strcmp(errors, "strict")==0) { |
| 4861 | flags = WC_NO_BEST_FIT_CHARS; |
| 4862 | pusedDefaultChar = &usedDefaultChar; |
| 4863 | } else if (strcmp(errors, "replace")==0) { |
| 4864 | flags = 0; |
| 4865 | pusedDefaultChar = NULL; |
| 4866 | } else { |
| 4867 | PyErr_Format(PyExc_ValueError, |
| 4868 | "mbcs encoding does not support errors='%s'", |
| 4869 | errors); |
| 4870 | return -1; |
| 4871 | } |
| 4872 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4873 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4874 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4875 | mbcssize = WideCharToMultiByte(CP_ACP, flags, p, size, NULL, 0, |
| 4876 | NULL, pusedDefaultChar); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4877 | if (mbcssize == 0) { |
| 4878 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4879 | return -1; |
| 4880 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4881 | /* If we used a default char, then we failed! */ |
| 4882 | if (pusedDefaultChar && *pusedDefaultChar) |
| 4883 | goto mbcs_encode_error; |
| 4884 | } else { |
| 4885 | mbcssize = 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4886 | } |
| 4887 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4888 | if (*repr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4889 | /* Create string object */ |
| 4890 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); |
| 4891 | if (*repr == NULL) |
| 4892 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4893 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4894 | } |
| 4895 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4896 | /* Extend string object */ |
| 4897 | n = PyBytes_Size(*repr); |
| 4898 | if (_PyBytes_Resize(repr, n + mbcssize) < 0) |
| 4899 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4900 | } |
| 4901 | |
| 4902 | /* Do the conversion */ |
| 4903 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4904 | char *s = PyBytes_AS_STRING(*repr) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4905 | if (0 == WideCharToMultiByte(CP_ACP, flags, p, size, s, mbcssize, |
| 4906 | NULL, pusedDefaultChar)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4907 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4908 | return -1; |
| 4909 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4910 | if (pusedDefaultChar && *pusedDefaultChar) |
| 4911 | goto mbcs_encode_error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4912 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4913 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4914 | |
| 4915 | mbcs_encode_error: |
| 4916 | raise_encode_exception(&exc, "mbcs", p, size, 0, 0, "invalid character"); |
| 4917 | Py_XDECREF(exc); |
| 4918 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4919 | } |
| 4920 | |
| 4921 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4922 | Py_ssize_t size, |
| 4923 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4924 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4925 | PyObject *repr = NULL; |
| 4926 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 4927 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4928 | #ifdef NEED_RETRY |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4929 | retry: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4930 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4931 | ret = encode_mbcs(&repr, p, INT_MAX, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4932 | else |
| 4933 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4934 | ret = encode_mbcs(&repr, p, (int)size, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4935 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4936 | if (ret < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4937 | Py_XDECREF(repr); |
| 4938 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4939 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4940 | |
| 4941 | #ifdef NEED_RETRY |
| 4942 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4943 | p += INT_MAX; |
| 4944 | size -= INT_MAX; |
| 4945 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4946 | } |
| 4947 | #endif |
| 4948 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4949 | return repr; |
| 4950 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4951 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 4952 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 4953 | { |
| 4954 | if (!PyUnicode_Check(unicode)) { |
| 4955 | PyErr_BadArgument(); |
| 4956 | return NULL; |
| 4957 | } |
| 4958 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4959 | PyUnicode_GET_SIZE(unicode), |
| 4960 | NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 4961 | } |
| 4962 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4963 | #undef NEED_RETRY |
| 4964 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4965 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4966 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4967 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 4968 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4969 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4970 | Py_ssize_t size, |
| 4971 | PyObject *mapping, |
| 4972 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4973 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4974 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4975 | Py_ssize_t startinpos; |
| 4976 | Py_ssize_t endinpos; |
| 4977 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4978 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4979 | PyUnicodeObject *v; |
| 4980 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4981 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4982 | PyObject *errorHandler = NULL; |
| 4983 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4984 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4985 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4986 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4987 | /* Default to Latin-1 */ |
| 4988 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4989 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4990 | |
| 4991 | v = _PyUnicode_New(size); |
| 4992 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4993 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4994 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4995 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4996 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4997 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4998 | if (PyUnicode_CheckExact(mapping)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4999 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 5000 | maplen = PyUnicode_GET_SIZE(mapping); |
| 5001 | while (s < e) { |
| 5002 | unsigned char ch = *s; |
| 5003 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5004 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5005 | if (ch < maplen) |
| 5006 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5007 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5008 | if (x == 0xfffe) { |
| 5009 | /* undefined mapping */ |
| 5010 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 5011 | startinpos = s-starts; |
| 5012 | endinpos = startinpos+1; |
| 5013 | if (unicode_decode_call_errorhandler( |
| 5014 | errors, &errorHandler, |
| 5015 | "charmap", "character maps to <undefined>", |
| 5016 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 5017 | &v, &outpos, &p)) { |
| 5018 | goto onError; |
| 5019 | } |
| 5020 | continue; |
| 5021 | } |
| 5022 | *p++ = x; |
| 5023 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5024 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 5025 | } |
| 5026 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5027 | while (s < e) { |
| 5028 | unsigned char ch = *s; |
| 5029 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 5030 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5031 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 5032 | w = PyLong_FromLong((long)ch); |
| 5033 | if (w == NULL) |
| 5034 | goto onError; |
| 5035 | x = PyObject_GetItem(mapping, w); |
| 5036 | Py_DECREF(w); |
| 5037 | if (x == NULL) { |
| 5038 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 5039 | /* No mapping found means: mapping is undefined. */ |
| 5040 | PyErr_Clear(); |
| 5041 | x = Py_None; |
| 5042 | Py_INCREF(x); |
| 5043 | } else |
| 5044 | goto onError; |
| 5045 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5046 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5047 | /* Apply mapping */ |
| 5048 | if (PyLong_Check(x)) { |
| 5049 | long value = PyLong_AS_LONG(x); |
| 5050 | if (value < 0 || value > 65535) { |
| 5051 | PyErr_SetString(PyExc_TypeError, |
| 5052 | "character mapping must be in range(65536)"); |
| 5053 | Py_DECREF(x); |
| 5054 | goto onError; |
| 5055 | } |
| 5056 | *p++ = (Py_UNICODE)value; |
| 5057 | } |
| 5058 | else if (x == Py_None) { |
| 5059 | /* undefined mapping */ |
| 5060 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 5061 | startinpos = s-starts; |
| 5062 | endinpos = startinpos+1; |
| 5063 | if (unicode_decode_call_errorhandler( |
| 5064 | errors, &errorHandler, |
| 5065 | "charmap", "character maps to <undefined>", |
| 5066 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 5067 | &v, &outpos, &p)) { |
| 5068 | Py_DECREF(x); |
| 5069 | goto onError; |
| 5070 | } |
| 5071 | Py_DECREF(x); |
| 5072 | continue; |
| 5073 | } |
| 5074 | else if (PyUnicode_Check(x)) { |
| 5075 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5076 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5077 | if (targetsize == 1) |
| 5078 | /* 1-1 mapping */ |
| 5079 | *p++ = *PyUnicode_AS_UNICODE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5080 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5081 | else if (targetsize > 1) { |
| 5082 | /* 1-n mapping */ |
| 5083 | if (targetsize > extrachars) { |
| 5084 | /* resize first */ |
| 5085 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 5086 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 5087 | (targetsize << 2); |
| 5088 | extrachars += needed; |
| 5089 | /* XXX overflow detection missing */ |
| 5090 | if (_PyUnicode_Resize(&v, |
| 5091 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 5092 | Py_DECREF(x); |
| 5093 | goto onError; |
| 5094 | } |
| 5095 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 5096 | } |
| 5097 | Py_UNICODE_COPY(p, |
| 5098 | PyUnicode_AS_UNICODE(x), |
| 5099 | targetsize); |
| 5100 | p += targetsize; |
| 5101 | extrachars -= targetsize; |
| 5102 | } |
| 5103 | /* 1-0 mapping: skip the character */ |
| 5104 | } |
| 5105 | else { |
| 5106 | /* wrong return value */ |
| 5107 | PyErr_SetString(PyExc_TypeError, |
| 5108 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5109 | Py_DECREF(x); |
| 5110 | goto onError; |
| 5111 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5112 | Py_DECREF(x); |
| 5113 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5114 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5115 | } |
| 5116 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5117 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 5118 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5119 | Py_XDECREF(errorHandler); |
| 5120 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5121 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5122 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5123 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5124 | Py_XDECREF(errorHandler); |
| 5125 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5126 | Py_XDECREF(v); |
| 5127 | return NULL; |
| 5128 | } |
| 5129 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5130 | /* Charmap encoding: the lookup table */ |
| 5131 | |
| 5132 | struct encoding_map{ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5133 | PyObject_HEAD |
| 5134 | unsigned char level1[32]; |
| 5135 | int count2, count3; |
| 5136 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5137 | }; |
| 5138 | |
| 5139 | static PyObject* |
| 5140 | encoding_map_size(PyObject *obj, PyObject* args) |
| 5141 | { |
| 5142 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5143 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5144 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5145 | } |
| 5146 | |
| 5147 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5148 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5149 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 5150 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5151 | }; |
| 5152 | |
| 5153 | static void |
| 5154 | encoding_map_dealloc(PyObject* o) |
| 5155 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5156 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5157 | } |
| 5158 | |
| 5159 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5160 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5161 | "EncodingMap", /*tp_name*/ |
| 5162 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 5163 | 0, /*tp_itemsize*/ |
| 5164 | /* methods */ |
| 5165 | encoding_map_dealloc, /*tp_dealloc*/ |
| 5166 | 0, /*tp_print*/ |
| 5167 | 0, /*tp_getattr*/ |
| 5168 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 5169 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5170 | 0, /*tp_repr*/ |
| 5171 | 0, /*tp_as_number*/ |
| 5172 | 0, /*tp_as_sequence*/ |
| 5173 | 0, /*tp_as_mapping*/ |
| 5174 | 0, /*tp_hash*/ |
| 5175 | 0, /*tp_call*/ |
| 5176 | 0, /*tp_str*/ |
| 5177 | 0, /*tp_getattro*/ |
| 5178 | 0, /*tp_setattro*/ |
| 5179 | 0, /*tp_as_buffer*/ |
| 5180 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 5181 | 0, /*tp_doc*/ |
| 5182 | 0, /*tp_traverse*/ |
| 5183 | 0, /*tp_clear*/ |
| 5184 | 0, /*tp_richcompare*/ |
| 5185 | 0, /*tp_weaklistoffset*/ |
| 5186 | 0, /*tp_iter*/ |
| 5187 | 0, /*tp_iternext*/ |
| 5188 | encoding_map_methods, /*tp_methods*/ |
| 5189 | 0, /*tp_members*/ |
| 5190 | 0, /*tp_getset*/ |
| 5191 | 0, /*tp_base*/ |
| 5192 | 0, /*tp_dict*/ |
| 5193 | 0, /*tp_descr_get*/ |
| 5194 | 0, /*tp_descr_set*/ |
| 5195 | 0, /*tp_dictoffset*/ |
| 5196 | 0, /*tp_init*/ |
| 5197 | 0, /*tp_alloc*/ |
| 5198 | 0, /*tp_new*/ |
| 5199 | 0, /*tp_free*/ |
| 5200 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5201 | }; |
| 5202 | |
| 5203 | PyObject* |
| 5204 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 5205 | { |
| 5206 | Py_UNICODE *decode; |
| 5207 | PyObject *result; |
| 5208 | struct encoding_map *mresult; |
| 5209 | int i; |
| 5210 | int need_dict = 0; |
| 5211 | unsigned char level1[32]; |
| 5212 | unsigned char level2[512]; |
| 5213 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 5214 | int count2 = 0, count3 = 0; |
| 5215 | |
| 5216 | if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { |
| 5217 | PyErr_BadArgument(); |
| 5218 | return NULL; |
| 5219 | } |
| 5220 | decode = PyUnicode_AS_UNICODE(string); |
| 5221 | memset(level1, 0xFF, sizeof level1); |
| 5222 | memset(level2, 0xFF, sizeof level2); |
| 5223 | |
| 5224 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 5225 | or if there are non-BMP characters, we need to use |
| 5226 | a mapping dictionary. */ |
| 5227 | if (decode[0] != 0) |
| 5228 | need_dict = 1; |
| 5229 | for (i = 1; i < 256; i++) { |
| 5230 | int l1, l2; |
| 5231 | if (decode[i] == 0 |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5232 | #ifdef Py_UNICODE_WIDE |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5233 | || decode[i] > 0xFFFF |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5234 | #endif |
| 5235 | ) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5236 | need_dict = 1; |
| 5237 | break; |
| 5238 | } |
| 5239 | if (decode[i] == 0xFFFE) |
| 5240 | /* unmapped character */ |
| 5241 | continue; |
| 5242 | l1 = decode[i] >> 11; |
| 5243 | l2 = decode[i] >> 7; |
| 5244 | if (level1[l1] == 0xFF) |
| 5245 | level1[l1] = count2++; |
| 5246 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5247 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5248 | } |
| 5249 | |
| 5250 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 5251 | need_dict = 1; |
| 5252 | |
| 5253 | if (need_dict) { |
| 5254 | PyObject *result = PyDict_New(); |
| 5255 | PyObject *key, *value; |
| 5256 | if (!result) |
| 5257 | return NULL; |
| 5258 | for (i = 0; i < 256; i++) { |
| 5259 | key = value = NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5260 | key = PyLong_FromLong(decode[i]); |
| 5261 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5262 | if (!key || !value) |
| 5263 | goto failed1; |
| 5264 | if (PyDict_SetItem(result, key, value) == -1) |
| 5265 | goto failed1; |
| 5266 | Py_DECREF(key); |
| 5267 | Py_DECREF(value); |
| 5268 | } |
| 5269 | return result; |
| 5270 | failed1: |
| 5271 | Py_XDECREF(key); |
| 5272 | Py_XDECREF(value); |
| 5273 | Py_DECREF(result); |
| 5274 | return NULL; |
| 5275 | } |
| 5276 | |
| 5277 | /* Create a three-level trie */ |
| 5278 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 5279 | 16*count2 + 128*count3 - 1); |
| 5280 | if (!result) |
| 5281 | return PyErr_NoMemory(); |
| 5282 | PyObject_Init(result, &EncodingMapType); |
| 5283 | mresult = (struct encoding_map*)result; |
| 5284 | mresult->count2 = count2; |
| 5285 | mresult->count3 = count3; |
| 5286 | mlevel1 = mresult->level1; |
| 5287 | mlevel2 = mresult->level23; |
| 5288 | mlevel3 = mresult->level23 + 16*count2; |
| 5289 | memcpy(mlevel1, level1, 32); |
| 5290 | memset(mlevel2, 0xFF, 16*count2); |
| 5291 | memset(mlevel3, 0, 128*count3); |
| 5292 | count3 = 0; |
| 5293 | for (i = 1; i < 256; i++) { |
| 5294 | int o1, o2, o3, i2, i3; |
| 5295 | if (decode[i] == 0xFFFE) |
| 5296 | /* unmapped character */ |
| 5297 | continue; |
| 5298 | o1 = decode[i]>>11; |
| 5299 | o2 = (decode[i]>>7) & 0xF; |
| 5300 | i2 = 16*mlevel1[o1] + o2; |
| 5301 | if (mlevel2[i2] == 0xFF) |
| 5302 | mlevel2[i2] = count3++; |
| 5303 | o3 = decode[i] & 0x7F; |
| 5304 | i3 = 128*mlevel2[i2] + o3; |
| 5305 | mlevel3[i3] = i; |
| 5306 | } |
| 5307 | return result; |
| 5308 | } |
| 5309 | |
| 5310 | static int |
| 5311 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 5312 | { |
| 5313 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 5314 | int l1 = c>>11; |
| 5315 | int l2 = (c>>7) & 0xF; |
| 5316 | int l3 = c & 0x7F; |
| 5317 | int i; |
| 5318 | |
| 5319 | #ifdef Py_UNICODE_WIDE |
| 5320 | if (c > 0xFFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5321 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5322 | } |
| 5323 | #endif |
| 5324 | if (c == 0) |
| 5325 | return 0; |
| 5326 | /* level 1*/ |
| 5327 | i = map->level1[l1]; |
| 5328 | if (i == 0xFF) { |
| 5329 | return -1; |
| 5330 | } |
| 5331 | /* level 2*/ |
| 5332 | i = map->level23[16*i+l2]; |
| 5333 | if (i == 0xFF) { |
| 5334 | return -1; |
| 5335 | } |
| 5336 | /* level 3 */ |
| 5337 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 5338 | if (i == 0) { |
| 5339 | return -1; |
| 5340 | } |
| 5341 | return i; |
| 5342 | } |
| 5343 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5344 | /* Lookup the character ch in the mapping. If the character |
| 5345 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 5346 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5347 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5348 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5349 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5350 | PyObject *x; |
| 5351 | |
| 5352 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5353 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5354 | x = PyObject_GetItem(mapping, w); |
| 5355 | Py_DECREF(w); |
| 5356 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5357 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 5358 | /* No mapping found means: mapping is undefined. */ |
| 5359 | PyErr_Clear(); |
| 5360 | x = Py_None; |
| 5361 | Py_INCREF(x); |
| 5362 | return x; |
| 5363 | } else |
| 5364 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5365 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 5366 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5367 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5368 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5369 | long value = PyLong_AS_LONG(x); |
| 5370 | if (value < 0 || value > 255) { |
| 5371 | PyErr_SetString(PyExc_TypeError, |
| 5372 | "character mapping must be in range(256)"); |
| 5373 | Py_DECREF(x); |
| 5374 | return NULL; |
| 5375 | } |
| 5376 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5377 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5378 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5379 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5380 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5381 | /* wrong return value */ |
| 5382 | PyErr_Format(PyExc_TypeError, |
| 5383 | "character mapping must return integer, bytes or None, not %.400s", |
| 5384 | x->ob_type->tp_name); |
| 5385 | Py_DECREF(x); |
| 5386 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5387 | } |
| 5388 | } |
| 5389 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5390 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5391 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5392 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5393 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 5394 | /* exponentially overallocate to minimize reallocations */ |
| 5395 | if (requiredsize < 2*outsize) |
| 5396 | requiredsize = 2*outsize; |
| 5397 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 5398 | return -1; |
| 5399 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5400 | } |
| 5401 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5402 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5403 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5404 | }charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5405 | /* 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] | 5406 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5407 | space is available. Return a new reference to the object that |
| 5408 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 5409 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 5410 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5411 | static |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5412 | charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5413 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5414 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5415 | PyObject *rep; |
| 5416 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5417 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5418 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5419 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5420 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5421 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5422 | if (res == -1) |
| 5423 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5424 | if (outsize<requiredsize) |
| 5425 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 5426 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5427 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5428 | outstart[(*outpos)++] = (char)res; |
| 5429 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5430 | } |
| 5431 | |
| 5432 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5433 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5434 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5435 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5436 | Py_DECREF(rep); |
| 5437 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5438 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5439 | if (PyLong_Check(rep)) { |
| 5440 | Py_ssize_t requiredsize = *outpos+1; |
| 5441 | if (outsize<requiredsize) |
| 5442 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 5443 | Py_DECREF(rep); |
| 5444 | return enc_EXCEPTION; |
| 5445 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5446 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5447 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5448 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5449 | else { |
| 5450 | const char *repchars = PyBytes_AS_STRING(rep); |
| 5451 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 5452 | Py_ssize_t requiredsize = *outpos+repsize; |
| 5453 | if (outsize<requiredsize) |
| 5454 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 5455 | Py_DECREF(rep); |
| 5456 | return enc_EXCEPTION; |
| 5457 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5458 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5459 | memcpy(outstart + *outpos, repchars, repsize); |
| 5460 | *outpos += repsize; |
| 5461 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5462 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5463 | Py_DECREF(rep); |
| 5464 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5465 | } |
| 5466 | |
| 5467 | /* handle an error in PyUnicode_EncodeCharmap |
| 5468 | Return 0 on success, -1 on error */ |
| 5469 | static |
| 5470 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5471 | 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] | 5472 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 5473 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5474 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5475 | { |
| 5476 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5477 | Py_ssize_t repsize; |
| 5478 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5479 | Py_UNICODE *uni2; |
| 5480 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5481 | Py_ssize_t collstartpos = *inpos; |
| 5482 | Py_ssize_t collendpos = *inpos+1; |
| 5483 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5484 | char *encoding = "charmap"; |
| 5485 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5486 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5487 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5488 | /* find all unencodable characters */ |
| 5489 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5490 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5491 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5492 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 5493 | if (res != -1) |
| 5494 | break; |
| 5495 | ++collendpos; |
| 5496 | continue; |
| 5497 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5498 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5499 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 5500 | if (rep==NULL) |
| 5501 | return -1; |
| 5502 | else if (rep!=Py_None) { |
| 5503 | Py_DECREF(rep); |
| 5504 | break; |
| 5505 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5506 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5507 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5508 | } |
| 5509 | /* cache callback name lookup |
| 5510 | * (if not done yet, i.e. it's the first error) */ |
| 5511 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5512 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5513 | *known_errorHandler = 1; |
| 5514 | else if (!strcmp(errors, "replace")) |
| 5515 | *known_errorHandler = 2; |
| 5516 | else if (!strcmp(errors, "ignore")) |
| 5517 | *known_errorHandler = 3; |
| 5518 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5519 | *known_errorHandler = 4; |
| 5520 | else |
| 5521 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5522 | } |
| 5523 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5524 | case 1: /* strict */ |
| 5525 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5526 | return -1; |
| 5527 | case 2: /* replace */ |
| 5528 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5529 | x = charmapencode_output('?', mapping, res, respos); |
| 5530 | if (x==enc_EXCEPTION) { |
| 5531 | return -1; |
| 5532 | } |
| 5533 | else if (x==enc_FAILED) { |
| 5534 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5535 | return -1; |
| 5536 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5537 | } |
| 5538 | /* fall through */ |
| 5539 | case 3: /* ignore */ |
| 5540 | *inpos = collendpos; |
| 5541 | break; |
| 5542 | case 4: /* xmlcharrefreplace */ |
| 5543 | /* generate replacement (temporarily (mis)uses p) */ |
| 5544 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5545 | char buffer[2+29+1+1]; |
| 5546 | char *cp; |
| 5547 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 5548 | for (cp = buffer; *cp; ++cp) { |
| 5549 | x = charmapencode_output(*cp, mapping, res, respos); |
| 5550 | if (x==enc_EXCEPTION) |
| 5551 | return -1; |
| 5552 | else if (x==enc_FAILED) { |
| 5553 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5554 | return -1; |
| 5555 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5556 | } |
| 5557 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5558 | *inpos = collendpos; |
| 5559 | break; |
| 5560 | default: |
| 5561 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5562 | encoding, reason, p, size, exceptionObject, |
| 5563 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5564 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5565 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5566 | if (PyBytes_Check(repunicode)) { |
| 5567 | /* Directly copy bytes result to output. */ |
| 5568 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 5569 | Py_ssize_t requiredsize; |
| 5570 | repsize = PyBytes_Size(repunicode); |
| 5571 | requiredsize = *respos + repsize; |
| 5572 | if (requiredsize > outsize) |
| 5573 | /* Make room for all additional bytes. */ |
| 5574 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 5575 | Py_DECREF(repunicode); |
| 5576 | return -1; |
| 5577 | } |
| 5578 | memcpy(PyBytes_AsString(*res) + *respos, |
| 5579 | PyBytes_AsString(repunicode), repsize); |
| 5580 | *respos += repsize; |
| 5581 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5582 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5583 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5584 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5585 | /* generate replacement */ |
| 5586 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5587 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5588 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 5589 | if (x==enc_EXCEPTION) { |
| 5590 | return -1; |
| 5591 | } |
| 5592 | else if (x==enc_FAILED) { |
| 5593 | Py_DECREF(repunicode); |
| 5594 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5595 | return -1; |
| 5596 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5597 | } |
| 5598 | *inpos = newpos; |
| 5599 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5600 | } |
| 5601 | return 0; |
| 5602 | } |
| 5603 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5604 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5605 | Py_ssize_t size, |
| 5606 | PyObject *mapping, |
| 5607 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5608 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5609 | /* output object */ |
| 5610 | PyObject *res = NULL; |
| 5611 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5612 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5613 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5614 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5615 | PyObject *errorHandler = NULL; |
| 5616 | PyObject *exc = NULL; |
| 5617 | /* the following variable is used for caching string comparisons |
| 5618 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 5619 | * 3=ignore, 4=xmlcharrefreplace */ |
| 5620 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5621 | |
| 5622 | /* Default to Latin-1 */ |
| 5623 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5624 | return PyUnicode_EncodeLatin1(p, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5625 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5626 | /* allocate enough for a simple encoding without |
| 5627 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5628 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5629 | if (res == NULL) |
| 5630 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 5631 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5632 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5633 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5634 | while (inpos<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5635 | /* try to encode it */ |
| 5636 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 5637 | if (x==enc_EXCEPTION) /* error */ |
| 5638 | goto onError; |
| 5639 | if (x==enc_FAILED) { /* unencodable character */ |
| 5640 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 5641 | &exc, |
| 5642 | &known_errorHandler, &errorHandler, errors, |
| 5643 | &res, &respos)) { |
| 5644 | goto onError; |
| 5645 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5646 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5647 | else |
| 5648 | /* done with this character => adjust input position */ |
| 5649 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5650 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5651 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5652 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5653 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5654 | if (_PyBytes_Resize(&res, respos) < 0) |
| 5655 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5656 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5657 | Py_XDECREF(exc); |
| 5658 | Py_XDECREF(errorHandler); |
| 5659 | return res; |
| 5660 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5661 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5662 | Py_XDECREF(res); |
| 5663 | Py_XDECREF(exc); |
| 5664 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5665 | return NULL; |
| 5666 | } |
| 5667 | |
| 5668 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5669 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5670 | { |
| 5671 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5672 | PyErr_BadArgument(); |
| 5673 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5674 | } |
| 5675 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5676 | PyUnicode_GET_SIZE(unicode), |
| 5677 | mapping, |
| 5678 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5679 | } |
| 5680 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5681 | /* create or adjust a UnicodeTranslateError */ |
| 5682 | static void make_translate_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5683 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5684 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5685 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5686 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5687 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5688 | *exceptionObject = PyUnicodeTranslateError_Create( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5689 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5690 | } |
| 5691 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5692 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 5693 | goto onError; |
| 5694 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 5695 | goto onError; |
| 5696 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 5697 | goto onError; |
| 5698 | return; |
| 5699 | onError: |
| 5700 | Py_DECREF(*exceptionObject); |
| 5701 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5702 | } |
| 5703 | } |
| 5704 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5705 | /* raises a UnicodeTranslateError */ |
| 5706 | static void raise_translate_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5707 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5708 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5709 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5710 | { |
| 5711 | make_translate_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5712 | unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5713 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5714 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5715 | } |
| 5716 | |
| 5717 | /* error handling callback helper: |
| 5718 | build arguments, call the callback and check the arguments, |
| 5719 | put the result into newpos and return the replacement string, which |
| 5720 | has to be freed by the caller */ |
| 5721 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5722 | PyObject **errorHandler, |
| 5723 | const char *reason, |
| 5724 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 5725 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5726 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5727 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 5728 | 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] | 5729 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5730 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5731 | PyObject *restuple; |
| 5732 | PyObject *resunicode; |
| 5733 | |
| 5734 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5735 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5736 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5737 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5738 | } |
| 5739 | |
| 5740 | make_translate_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5741 | unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5742 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5743 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5744 | |
| 5745 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5746 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5747 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5748 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5749 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 5750 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5751 | Py_DECREF(restuple); |
| 5752 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5753 | } |
| 5754 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5755 | &resunicode, &i_newpos)) { |
| 5756 | Py_DECREF(restuple); |
| 5757 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5758 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5759 | if (i_newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5760 | *newpos = size+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5761 | else |
| 5762 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5763 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5764 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 5765 | Py_DECREF(restuple); |
| 5766 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5767 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5768 | Py_INCREF(resunicode); |
| 5769 | Py_DECREF(restuple); |
| 5770 | return resunicode; |
| 5771 | } |
| 5772 | |
| 5773 | /* Lookup the character ch in the mapping and put the result in result, |
| 5774 | which must be decrefed by the caller. |
| 5775 | Return 0 on success, -1 on error */ |
| 5776 | static |
| 5777 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 5778 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5779 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5780 | PyObject *x; |
| 5781 | |
| 5782 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5783 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5784 | x = PyObject_GetItem(mapping, w); |
| 5785 | Py_DECREF(w); |
| 5786 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5787 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 5788 | /* No mapping found means: use 1:1 mapping. */ |
| 5789 | PyErr_Clear(); |
| 5790 | *result = NULL; |
| 5791 | return 0; |
| 5792 | } else |
| 5793 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5794 | } |
| 5795 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5796 | *result = x; |
| 5797 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5798 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5799 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5800 | long value = PyLong_AS_LONG(x); |
| 5801 | long max = PyUnicode_GetMax(); |
| 5802 | if (value < 0 || value > max) { |
| 5803 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 5804 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5805 | Py_DECREF(x); |
| 5806 | return -1; |
| 5807 | } |
| 5808 | *result = x; |
| 5809 | return 0; |
| 5810 | } |
| 5811 | else if (PyUnicode_Check(x)) { |
| 5812 | *result = x; |
| 5813 | return 0; |
| 5814 | } |
| 5815 | else { |
| 5816 | /* wrong return value */ |
| 5817 | PyErr_SetString(PyExc_TypeError, |
| 5818 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5819 | Py_DECREF(x); |
| 5820 | return -1; |
| 5821 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5822 | } |
| 5823 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5824 | if not reallocate and adjust various state variables. |
| 5825 | Return 0 on success, -1 on error */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5826 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5827 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5828 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5829 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5830 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5831 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5832 | /* remember old output position */ |
| 5833 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
| 5834 | /* exponentially overallocate to minimize reallocations */ |
| 5835 | if (requiredsize < 2 * oldsize) |
| 5836 | requiredsize = 2 * oldsize; |
| 5837 | if (PyUnicode_Resize(outobj, requiredsize) < 0) |
| 5838 | return -1; |
| 5839 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5840 | } |
| 5841 | return 0; |
| 5842 | } |
| 5843 | /* lookup the character, put the result in the output string and adjust |
| 5844 | various state variables. Return a new reference to the object that |
| 5845 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 5846 | undefined (in which case no character was written). |
| 5847 | The called must decref result. |
| 5848 | Return 0 on success, -1 on error. */ |
| 5849 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5850 | int charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5851 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
| 5852 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5853 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5854 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5855 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5856 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5857 | /* not found => default to 1:1 mapping */ |
| 5858 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5859 | } |
| 5860 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5861 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5862 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5863 | /* no overflow check, because we know that the space is enough */ |
| 5864 | *(*outp)++ = (Py_UNICODE)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5865 | } |
| 5866 | else if (PyUnicode_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5867 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
| 5868 | if (repsize==1) { |
| 5869 | /* no overflow check, because we know that the space is enough */ |
| 5870 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 5871 | } |
| 5872 | else if (repsize!=0) { |
| 5873 | /* more than one character */ |
| 5874 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
| 5875 | (insize - (curinp-startinp)) + |
| 5876 | repsize - 1; |
| 5877 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
| 5878 | return -1; |
| 5879 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 5880 | *outp += repsize; |
| 5881 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5882 | } |
| 5883 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5884 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5885 | return 0; |
| 5886 | } |
| 5887 | |
| 5888 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5889 | Py_ssize_t size, |
| 5890 | PyObject *mapping, |
| 5891 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5892 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5893 | /* output object */ |
| 5894 | PyObject *res = NULL; |
| 5895 | /* pointers to the beginning and end+1 of input */ |
| 5896 | const Py_UNICODE *startp = p; |
| 5897 | const Py_UNICODE *endp = p + size; |
| 5898 | /* pointer into the output */ |
| 5899 | Py_UNICODE *str; |
| 5900 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5901 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5902 | char *reason = "character maps to <undefined>"; |
| 5903 | PyObject *errorHandler = NULL; |
| 5904 | PyObject *exc = NULL; |
| 5905 | /* the following variable is used for caching string comparisons |
| 5906 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 5907 | * 3=ignore, 4=xmlcharrefreplace */ |
| 5908 | int known_errorHandler = -1; |
| 5909 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5910 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5911 | PyErr_BadArgument(); |
| 5912 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5913 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5914 | |
| 5915 | /* allocate enough for a simple 1:1 translation without |
| 5916 | replacements, if we need more, we'll resize */ |
| 5917 | res = PyUnicode_FromUnicode(NULL, size); |
| 5918 | if (res == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5919 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5920 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5921 | return res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5922 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5923 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5924 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5925 | /* try to encode it */ |
| 5926 | PyObject *x = NULL; |
| 5927 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
| 5928 | Py_XDECREF(x); |
| 5929 | goto onError; |
| 5930 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5931 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5932 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 5933 | ++p; |
| 5934 | else { /* untranslatable character */ |
| 5935 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 5936 | Py_ssize_t repsize; |
| 5937 | Py_ssize_t newpos; |
| 5938 | Py_UNICODE *uni2; |
| 5939 | /* startpos for collecting untranslatable chars */ |
| 5940 | const Py_UNICODE *collstart = p; |
| 5941 | const Py_UNICODE *collend = p+1; |
| 5942 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5943 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5944 | /* find all untranslatable characters */ |
| 5945 | while (collend < endp) { |
| 5946 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
| 5947 | goto onError; |
| 5948 | Py_XDECREF(x); |
| 5949 | if (x!=Py_None) |
| 5950 | break; |
| 5951 | ++collend; |
| 5952 | } |
| 5953 | /* cache callback name lookup |
| 5954 | * (if not done yet, i.e. it's the first error) */ |
| 5955 | if (known_errorHandler==-1) { |
| 5956 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5957 | known_errorHandler = 1; |
| 5958 | else if (!strcmp(errors, "replace")) |
| 5959 | known_errorHandler = 2; |
| 5960 | else if (!strcmp(errors, "ignore")) |
| 5961 | known_errorHandler = 3; |
| 5962 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5963 | known_errorHandler = 4; |
| 5964 | else |
| 5965 | known_errorHandler = 0; |
| 5966 | } |
| 5967 | switch (known_errorHandler) { |
| 5968 | case 1: /* strict */ |
| 5969 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5970 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5971 | case 2: /* replace */ |
| 5972 | /* No need to check for space, this is a 1:1 replacement */ |
| 5973 | for (coll = collstart; coll<collend; ++coll) |
| 5974 | *str++ = '?'; |
| 5975 | /* fall through */ |
| 5976 | case 3: /* ignore */ |
| 5977 | p = collend; |
| 5978 | break; |
| 5979 | case 4: /* xmlcharrefreplace */ |
| 5980 | /* generate replacement (temporarily (mis)uses p) */ |
| 5981 | for (p = collstart; p < collend; ++p) { |
| 5982 | char buffer[2+29+1+1]; |
| 5983 | char *cp; |
| 5984 | sprintf(buffer, "&#%d;", (int)*p); |
| 5985 | if (charmaptranslate_makespace(&res, &str, |
| 5986 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 5987 | goto onError; |
| 5988 | for (cp = buffer; *cp; ++cp) |
| 5989 | *str++ = *cp; |
| 5990 | } |
| 5991 | p = collend; |
| 5992 | break; |
| 5993 | default: |
| 5994 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 5995 | reason, startp, size, &exc, |
| 5996 | collstart-startp, collend-startp, &newpos); |
| 5997 | if (repunicode == NULL) |
| 5998 | goto onError; |
| 5999 | /* generate replacement */ |
| 6000 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 6001 | if (charmaptranslate_makespace(&res, &str, |
| 6002 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 6003 | Py_DECREF(repunicode); |
| 6004 | goto onError; |
| 6005 | } |
| 6006 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 6007 | *str++ = *uni2; |
| 6008 | p = startp + newpos; |
| 6009 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6010 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6011 | } |
| 6012 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6013 | /* Resize if we allocated to much */ |
| 6014 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 6015 | if (respos<PyUnicode_GET_SIZE(res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6016 | if (PyUnicode_Resize(&res, respos) < 0) |
| 6017 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6018 | } |
| 6019 | Py_XDECREF(exc); |
| 6020 | Py_XDECREF(errorHandler); |
| 6021 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6022 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6023 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6024 | Py_XDECREF(res); |
| 6025 | Py_XDECREF(exc); |
| 6026 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6027 | return NULL; |
| 6028 | } |
| 6029 | |
| 6030 | PyObject *PyUnicode_Translate(PyObject *str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6031 | PyObject *mapping, |
| 6032 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6033 | { |
| 6034 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6035 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6036 | str = PyUnicode_FromObject(str); |
| 6037 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6038 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6039 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6040 | PyUnicode_GET_SIZE(str), |
| 6041 | mapping, |
| 6042 | errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6043 | Py_DECREF(str); |
| 6044 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6045 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6046 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6047 | Py_XDECREF(str); |
| 6048 | return NULL; |
| 6049 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6050 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6051 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 6052 | |
| 6053 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6054 | Py_ssize_t length, |
| 6055 | char *output, |
| 6056 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6057 | { |
| 6058 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6059 | PyObject *errorHandler = NULL; |
| 6060 | PyObject *exc = NULL; |
| 6061 | const char *encoding = "decimal"; |
| 6062 | const char *reason = "invalid decimal Unicode string"; |
| 6063 | /* the following variable is used for caching string comparisons |
| 6064 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6065 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6066 | |
| 6067 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6068 | PyErr_BadArgument(); |
| 6069 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6070 | } |
| 6071 | |
| 6072 | p = s; |
| 6073 | end = s + length; |
| 6074 | while (p < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6075 | register Py_UNICODE ch = *p; |
| 6076 | int decimal; |
| 6077 | PyObject *repunicode; |
| 6078 | Py_ssize_t repsize; |
| 6079 | Py_ssize_t newpos; |
| 6080 | Py_UNICODE *uni2; |
| 6081 | Py_UNICODE *collstart; |
| 6082 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6083 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6084 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6085 | *output++ = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6086 | ++p; |
| 6087 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6088 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6089 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 6090 | if (decimal >= 0) { |
| 6091 | *output++ = '0' + decimal; |
| 6092 | ++p; |
| 6093 | continue; |
| 6094 | } |
| 6095 | if (0 < ch && ch < 256) { |
| 6096 | *output++ = (char)ch; |
| 6097 | ++p; |
| 6098 | continue; |
| 6099 | } |
| 6100 | /* All other characters are considered unencodable */ |
| 6101 | collstart = p; |
| 6102 | collend = p+1; |
| 6103 | while (collend < end) { |
| 6104 | if ((0 < *collend && *collend < 256) || |
| 6105 | !Py_UNICODE_ISSPACE(*collend) || |
| 6106 | Py_UNICODE_TODECIMAL(*collend)) |
| 6107 | break; |
| 6108 | } |
| 6109 | /* cache callback name lookup |
| 6110 | * (if not done yet, i.e. it's the first error) */ |
| 6111 | if (known_errorHandler==-1) { |
| 6112 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6113 | known_errorHandler = 1; |
| 6114 | else if (!strcmp(errors, "replace")) |
| 6115 | known_errorHandler = 2; |
| 6116 | else if (!strcmp(errors, "ignore")) |
| 6117 | known_errorHandler = 3; |
| 6118 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6119 | known_errorHandler = 4; |
| 6120 | else |
| 6121 | known_errorHandler = 0; |
| 6122 | } |
| 6123 | switch (known_errorHandler) { |
| 6124 | case 1: /* strict */ |
| 6125 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 6126 | goto onError; |
| 6127 | case 2: /* replace */ |
| 6128 | for (p = collstart; p < collend; ++p) |
| 6129 | *output++ = '?'; |
| 6130 | /* fall through */ |
| 6131 | case 3: /* ignore */ |
| 6132 | p = collend; |
| 6133 | break; |
| 6134 | case 4: /* xmlcharrefreplace */ |
| 6135 | /* generate replacement (temporarily (mis)uses p) */ |
| 6136 | for (p = collstart; p < collend; ++p) |
| 6137 | output += sprintf(output, "&#%d;", (int)*p); |
| 6138 | p = collend; |
| 6139 | break; |
| 6140 | default: |
| 6141 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 6142 | encoding, reason, s, length, &exc, |
| 6143 | collstart-s, collend-s, &newpos); |
| 6144 | if (repunicode == NULL) |
| 6145 | goto onError; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6146 | if (!PyUnicode_Check(repunicode)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6147 | /* Byte results not supported, since they have no decimal property. */ |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6148 | PyErr_SetString(PyExc_TypeError, "error handler should return unicode"); |
| 6149 | Py_DECREF(repunicode); |
| 6150 | goto onError; |
| 6151 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6152 | /* generate replacement */ |
| 6153 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 6154 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 6155 | Py_UNICODE ch = *uni2; |
| 6156 | if (Py_UNICODE_ISSPACE(ch)) |
| 6157 | *output++ = ' '; |
| 6158 | else { |
| 6159 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 6160 | if (decimal >= 0) |
| 6161 | *output++ = '0' + decimal; |
| 6162 | else if (0 < ch && ch < 256) |
| 6163 | *output++ = (char)ch; |
| 6164 | else { |
| 6165 | Py_DECREF(repunicode); |
| 6166 | raise_encode_exception(&exc, encoding, |
| 6167 | s, length, collstart-s, collend-s, reason); |
| 6168 | goto onError; |
| 6169 | } |
| 6170 | } |
| 6171 | } |
| 6172 | p = s + newpos; |
| 6173 | Py_DECREF(repunicode); |
| 6174 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6175 | } |
| 6176 | /* 0-terminate the output string */ |
| 6177 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6178 | Py_XDECREF(exc); |
| 6179 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6180 | return 0; |
| 6181 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6182 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6183 | Py_XDECREF(exc); |
| 6184 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6185 | return -1; |
| 6186 | } |
| 6187 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6188 | /* --- Helpers ------------------------------------------------------------ */ |
| 6189 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 6190 | #include "stringlib/unicodedefs.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6191 | #include "stringlib/fastsearch.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6192 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6193 | #include "stringlib/count.h" |
| 6194 | #include "stringlib/find.h" |
| 6195 | #include "stringlib/partition.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6196 | #include "stringlib/split.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6197 | |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 6198 | #define _Py_InsertThousandsGrouping _PyUnicode_InsertThousandsGrouping |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame] | 6199 | #define _Py_InsertThousandsGroupingLocale _PyUnicode_InsertThousandsGroupingLocale |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 6200 | #include "stringlib/localeutil.h" |
| 6201 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6202 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6203 | #define ADJUST_INDICES(start, end, len) \ |
| 6204 | if (end > len) \ |
| 6205 | end = len; \ |
| 6206 | else if (end < 0) { \ |
| 6207 | end += len; \ |
| 6208 | if (end < 0) \ |
| 6209 | end = 0; \ |
| 6210 | } \ |
| 6211 | if (start < 0) { \ |
| 6212 | start += len; \ |
| 6213 | if (start < 0) \ |
| 6214 | start = 0; \ |
| 6215 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6216 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6217 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6218 | PyObject *substr, |
| 6219 | Py_ssize_t start, |
| 6220 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6221 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6222 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6223 | PyUnicodeObject* str_obj; |
| 6224 | PyUnicodeObject* sub_obj; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6225 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6226 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
| 6227 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6228 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6229 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
| 6230 | if (!sub_obj) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6231 | Py_DECREF(str_obj); |
| 6232 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6233 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6234 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6235 | ADJUST_INDICES(start, end, str_obj->length); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6236 | result = stringlib_count( |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6237 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length, |
| 6238 | PY_SSIZE_T_MAX |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6239 | ); |
| 6240 | |
| 6241 | Py_DECREF(sub_obj); |
| 6242 | Py_DECREF(str_obj); |
| 6243 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6244 | return result; |
| 6245 | } |
| 6246 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6247 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6248 | PyObject *sub, |
| 6249 | Py_ssize_t start, |
| 6250 | Py_ssize_t end, |
| 6251 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6252 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6253 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6254 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6255 | str = PyUnicode_FromObject(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6256 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6257 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6258 | sub = PyUnicode_FromObject(sub); |
| 6259 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6260 | Py_DECREF(str); |
| 6261 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6262 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6263 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6264 | if (direction > 0) |
| 6265 | result = stringlib_find_slice( |
| 6266 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 6267 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 6268 | start, end |
| 6269 | ); |
| 6270 | else |
| 6271 | result = stringlib_rfind_slice( |
| 6272 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 6273 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 6274 | start, end |
| 6275 | ); |
| 6276 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6277 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6278 | Py_DECREF(sub); |
| 6279 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6280 | return result; |
| 6281 | } |
| 6282 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6283 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6284 | int tailmatch(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6285 | PyUnicodeObject *substring, |
| 6286 | Py_ssize_t start, |
| 6287 | Py_ssize_t end, |
| 6288 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6289 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6290 | if (substring->length == 0) |
| 6291 | return 1; |
| 6292 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6293 | ADJUST_INDICES(start, end, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6294 | end -= substring->length; |
| 6295 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6296 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6297 | |
| 6298 | if (direction > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6299 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 6300 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6301 | } else { |
| 6302 | if (Py_UNICODE_MATCH(self, start, substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6303 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6304 | } |
| 6305 | |
| 6306 | return 0; |
| 6307 | } |
| 6308 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6309 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6310 | PyObject *substr, |
| 6311 | Py_ssize_t start, |
| 6312 | Py_ssize_t end, |
| 6313 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6314 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6315 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6316 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6317 | str = PyUnicode_FromObject(str); |
| 6318 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6319 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6320 | substr = PyUnicode_FromObject(substr); |
| 6321 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6322 | Py_DECREF(str); |
| 6323 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6324 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6325 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6326 | result = tailmatch((PyUnicodeObject *)str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6327 | (PyUnicodeObject *)substr, |
| 6328 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6329 | Py_DECREF(str); |
| 6330 | Py_DECREF(substr); |
| 6331 | return result; |
| 6332 | } |
| 6333 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6334 | /* Apply fixfct filter to the Unicode object self and return a |
| 6335 | reference to the modified object */ |
| 6336 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6337 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6338 | PyObject *fixup(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6339 | int (*fixfct)(PyUnicodeObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6340 | { |
| 6341 | |
| 6342 | PyUnicodeObject *u; |
| 6343 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6344 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6345 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6346 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6347 | |
| 6348 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6349 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6350 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6351 | /* fixfct should return TRUE if it modified the buffer. If |
| 6352 | FALSE, return a reference to the original buffer instead |
| 6353 | (to save space, not time) */ |
| 6354 | Py_INCREF(self); |
| 6355 | Py_DECREF(u); |
| 6356 | return (PyObject*) self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6357 | } |
| 6358 | return (PyObject*) u; |
| 6359 | } |
| 6360 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6361 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6362 | int fixupper(PyUnicodeObject *self) |
| 6363 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6364 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6365 | Py_UNICODE *s = self->str; |
| 6366 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6367 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6368 | while (len-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6369 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6370 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6371 | ch = Py_UNICODE_TOUPPER(*s); |
| 6372 | if (ch != *s) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6373 | status = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6374 | *s = ch; |
| 6375 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6376 | s++; |
| 6377 | } |
| 6378 | |
| 6379 | return status; |
| 6380 | } |
| 6381 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6382 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6383 | int fixlower(PyUnicodeObject *self) |
| 6384 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6385 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6386 | Py_UNICODE *s = self->str; |
| 6387 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6388 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6389 | while (len-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6390 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6391 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6392 | ch = Py_UNICODE_TOLOWER(*s); |
| 6393 | if (ch != *s) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6394 | status = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6395 | *s = ch; |
| 6396 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6397 | s++; |
| 6398 | } |
| 6399 | |
| 6400 | return status; |
| 6401 | } |
| 6402 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6403 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6404 | int fixswapcase(PyUnicodeObject *self) |
| 6405 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6406 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6407 | Py_UNICODE *s = self->str; |
| 6408 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6409 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6410 | while (len-- > 0) { |
| 6411 | if (Py_UNICODE_ISUPPER(*s)) { |
| 6412 | *s = Py_UNICODE_TOLOWER(*s); |
| 6413 | status = 1; |
| 6414 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 6415 | *s = Py_UNICODE_TOUPPER(*s); |
| 6416 | status = 1; |
| 6417 | } |
| 6418 | s++; |
| 6419 | } |
| 6420 | |
| 6421 | return status; |
| 6422 | } |
| 6423 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6424 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6425 | int fixcapitalize(PyUnicodeObject *self) |
| 6426 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6427 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6428 | Py_UNICODE *s = self->str; |
| 6429 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6430 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6431 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6432 | return 0; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6433 | if (Py_UNICODE_ISLOWER(*s)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6434 | *s = Py_UNICODE_TOUPPER(*s); |
| 6435 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6436 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6437 | s++; |
| 6438 | while (--len > 0) { |
| 6439 | if (Py_UNICODE_ISUPPER(*s)) { |
| 6440 | *s = Py_UNICODE_TOLOWER(*s); |
| 6441 | status = 1; |
| 6442 | } |
| 6443 | s++; |
| 6444 | } |
| 6445 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6446 | } |
| 6447 | |
| 6448 | static |
| 6449 | int fixtitle(PyUnicodeObject *self) |
| 6450 | { |
| 6451 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6452 | register Py_UNICODE *e; |
| 6453 | int previous_is_cased; |
| 6454 | |
| 6455 | /* Shortcut for single character strings */ |
| 6456 | if (PyUnicode_GET_SIZE(self) == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6457 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 6458 | if (*p != ch) { |
| 6459 | *p = ch; |
| 6460 | return 1; |
| 6461 | } |
| 6462 | else |
| 6463 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6464 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6465 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6466 | e = p + PyUnicode_GET_SIZE(self); |
| 6467 | previous_is_cased = 0; |
| 6468 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6469 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6470 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6471 | if (previous_is_cased) |
| 6472 | *p = Py_UNICODE_TOLOWER(ch); |
| 6473 | else |
| 6474 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6475 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6476 | if (Py_UNICODE_ISLOWER(ch) || |
| 6477 | Py_UNICODE_ISUPPER(ch) || |
| 6478 | Py_UNICODE_ISTITLE(ch)) |
| 6479 | previous_is_cased = 1; |
| 6480 | else |
| 6481 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6482 | } |
| 6483 | return 1; |
| 6484 | } |
| 6485 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6486 | PyObject * |
| 6487 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6488 | { |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 6489 | const Py_UNICODE blank = ' '; |
| 6490 | const Py_UNICODE *sep = ␣ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6491 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6492 | PyUnicodeObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6493 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 6494 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6495 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 6496 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6497 | PyObject *item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6498 | Py_ssize_t sz, i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6499 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6500 | fseq = PySequence_Fast(seq, ""); |
| 6501 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6502 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6503 | } |
| 6504 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6505 | /* NOTE: the following code can't call back into Python code, |
| 6506 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 6507 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6508 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6509 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 6510 | /* If empty sequence, return u"". */ |
| 6511 | if (seqlen == 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6512 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 6513 | goto Done; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6514 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6515 | items = PySequence_Fast_ITEMS(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6516 | /* If singleton sequence with an exact Unicode, return that. */ |
| 6517 | if (seqlen == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6518 | item = items[0]; |
| 6519 | if (PyUnicode_CheckExact(item)) { |
| 6520 | Py_INCREF(item); |
| 6521 | res = (PyUnicodeObject *)item; |
| 6522 | goto Done; |
| 6523 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6524 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6525 | else { |
| 6526 | /* Set up sep and seplen */ |
| 6527 | if (separator == NULL) { |
| 6528 | sep = ␣ |
| 6529 | seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6530 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6531 | else { |
| 6532 | if (!PyUnicode_Check(separator)) { |
| 6533 | PyErr_Format(PyExc_TypeError, |
| 6534 | "separator: expected str instance," |
| 6535 | " %.80s found", |
| 6536 | Py_TYPE(separator)->tp_name); |
| 6537 | goto onError; |
| 6538 | } |
| 6539 | sep = PyUnicode_AS_UNICODE(separator); |
| 6540 | seplen = PyUnicode_GET_SIZE(separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6541 | } |
| 6542 | } |
| 6543 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6544 | /* There are at least two things to join, or else we have a subclass |
| 6545 | * of str in the sequence. |
| 6546 | * Do a pre-pass to figure out the total amount of space we'll |
| 6547 | * need (sz), and see whether all argument are strings. |
| 6548 | */ |
| 6549 | sz = 0; |
| 6550 | for (i = 0; i < seqlen; i++) { |
| 6551 | const Py_ssize_t old_sz = sz; |
| 6552 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6553 | if (!PyUnicode_Check(item)) { |
| 6554 | PyErr_Format(PyExc_TypeError, |
| 6555 | "sequence item %zd: expected str instance," |
| 6556 | " %.80s found", |
| 6557 | i, Py_TYPE(item)->tp_name); |
| 6558 | goto onError; |
| 6559 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6560 | sz += PyUnicode_GET_SIZE(item); |
| 6561 | if (i != 0) |
| 6562 | sz += seplen; |
| 6563 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 6564 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6565 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6566 | goto onError; |
| 6567 | } |
| 6568 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6569 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6570 | res = _PyUnicode_New(sz); |
| 6571 | if (res == NULL) |
| 6572 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 6573 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6574 | /* Catenate everything. */ |
| 6575 | res_p = PyUnicode_AS_UNICODE(res); |
| 6576 | for (i = 0; i < seqlen; ++i) { |
| 6577 | Py_ssize_t itemlen; |
| 6578 | item = items[i]; |
| 6579 | itemlen = PyUnicode_GET_SIZE(item); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6580 | /* Copy item, and maybe the separator. */ |
| 6581 | if (i) { |
| 6582 | Py_UNICODE_COPY(res_p, sep, seplen); |
| 6583 | res_p += seplen; |
| 6584 | } |
| 6585 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
| 6586 | res_p += itemlen; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6587 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6588 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6589 | Done: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6590 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6591 | return (PyObject *)res; |
| 6592 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6593 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6594 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6595 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6596 | return NULL; |
| 6597 | } |
| 6598 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6599 | static |
| 6600 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6601 | Py_ssize_t left, |
| 6602 | Py_ssize_t right, |
| 6603 | Py_UNICODE fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6604 | { |
| 6605 | PyUnicodeObject *u; |
| 6606 | |
| 6607 | if (left < 0) |
| 6608 | left = 0; |
| 6609 | if (right < 0) |
| 6610 | right = 0; |
| 6611 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6612 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6613 | Py_INCREF(self); |
| 6614 | return self; |
| 6615 | } |
| 6616 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6617 | if (left > PY_SSIZE_T_MAX - self->length || |
| 6618 | right > PY_SSIZE_T_MAX - (left + self->length)) { |
| 6619 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 6620 | return NULL; |
| 6621 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6622 | u = _PyUnicode_New(left + self->length + right); |
| 6623 | if (u) { |
| 6624 | if (left) |
| 6625 | Py_UNICODE_FILL(u->str, fill, left); |
| 6626 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 6627 | if (right) |
| 6628 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 6629 | } |
| 6630 | |
| 6631 | return u; |
| 6632 | } |
| 6633 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6634 | PyObject *PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6635 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6636 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6637 | |
| 6638 | string = PyUnicode_FromObject(string); |
| 6639 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6640 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6641 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6642 | list = stringlib_splitlines( |
| 6643 | (PyObject*) string, PyUnicode_AS_UNICODE(string), |
| 6644 | PyUnicode_GET_SIZE(string), keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6645 | |
| 6646 | Py_DECREF(string); |
| 6647 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6648 | } |
| 6649 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6650 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6651 | PyObject *split(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6652 | PyUnicodeObject *substring, |
| 6653 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6654 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6655 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6656 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6657 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6658 | if (substring == NULL) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6659 | return stringlib_split_whitespace( |
| 6660 | (PyObject*) self, self->str, self->length, maxcount |
| 6661 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6662 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6663 | return stringlib_split( |
| 6664 | (PyObject*) self, self->str, self->length, |
| 6665 | substring->str, substring->length, |
| 6666 | maxcount |
| 6667 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6668 | } |
| 6669 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6670 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6671 | PyObject *rsplit(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6672 | PyUnicodeObject *substring, |
| 6673 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6674 | { |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6675 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6676 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6677 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6678 | if (substring == NULL) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6679 | return stringlib_rsplit_whitespace( |
| 6680 | (PyObject*) self, self->str, self->length, maxcount |
| 6681 | ); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6682 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6683 | return stringlib_rsplit( |
| 6684 | (PyObject*) self, self->str, self->length, |
| 6685 | substring->str, substring->length, |
| 6686 | maxcount |
| 6687 | ); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6688 | } |
| 6689 | |
| 6690 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6691 | PyObject *replace(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6692 | PyUnicodeObject *str1, |
| 6693 | PyUnicodeObject *str2, |
| 6694 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6695 | { |
| 6696 | PyUnicodeObject *u; |
| 6697 | |
| 6698 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6699 | maxcount = PY_SSIZE_T_MAX; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6700 | else if (maxcount == 0 || self->length == 0) |
| 6701 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6702 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6703 | if (str1->length == str2->length) { |
Antoine Pitrou | cbfdee3 | 2010-01-13 08:58:08 +0000 | [diff] [blame] | 6704 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6705 | /* same length */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6706 | if (str1->length == 0) |
| 6707 | goto nothing; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6708 | if (str1->length == 1) { |
| 6709 | /* replace characters */ |
| 6710 | Py_UNICODE u1, u2; |
| 6711 | if (!findchar(self->str, self->length, str1->str[0])) |
| 6712 | goto nothing; |
| 6713 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6714 | if (!u) |
| 6715 | return NULL; |
| 6716 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6717 | u1 = str1->str[0]; |
| 6718 | u2 = str2->str[0]; |
| 6719 | for (i = 0; i < u->length; i++) |
| 6720 | if (u->str[i] == u1) { |
| 6721 | if (--maxcount < 0) |
| 6722 | break; |
| 6723 | u->str[i] = u2; |
| 6724 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6725 | } else { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6726 | i = stringlib_find( |
| 6727 | self->str, self->length, str1->str, str1->length, 0 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6728 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6729 | if (i < 0) |
| 6730 | goto nothing; |
| 6731 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6732 | if (!u) |
| 6733 | return NULL; |
| 6734 | Py_UNICODE_COPY(u->str, self->str, self->length); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6735 | |
| 6736 | /* change everything in-place, starting with this one */ |
| 6737 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 6738 | i += str1->length; |
| 6739 | |
| 6740 | while ( --maxcount > 0) { |
| 6741 | i = stringlib_find(self->str+i, self->length-i, |
| 6742 | str1->str, str1->length, |
| 6743 | i); |
| 6744 | if (i == -1) |
| 6745 | break; |
| 6746 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 6747 | i += str1->length; |
| 6748 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6749 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6750 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6751 | |
| 6752 | Py_ssize_t n, i, j, e; |
| 6753 | Py_ssize_t product, new_size, delta; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6754 | Py_UNICODE *p; |
| 6755 | |
| 6756 | /* replace strings */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6757 | n = stringlib_count(self->str, self->length, str1->str, str1->length, |
| 6758 | maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6759 | if (n == 0) |
| 6760 | goto nothing; |
| 6761 | /* new_size = self->length + n * (str2->length - str1->length)); */ |
| 6762 | delta = (str2->length - str1->length); |
| 6763 | if (delta == 0) { |
| 6764 | new_size = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6765 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6766 | product = n * (str2->length - str1->length); |
| 6767 | if ((product / (str2->length - str1->length)) != n) { |
| 6768 | PyErr_SetString(PyExc_OverflowError, |
| 6769 | "replace string is too long"); |
| 6770 | return NULL; |
| 6771 | } |
| 6772 | new_size = self->length + product; |
| 6773 | if (new_size < 0) { |
| 6774 | PyErr_SetString(PyExc_OverflowError, |
| 6775 | "replace string is too long"); |
| 6776 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6777 | } |
| 6778 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6779 | u = _PyUnicode_New(new_size); |
| 6780 | if (!u) |
| 6781 | return NULL; |
| 6782 | i = 0; |
| 6783 | p = u->str; |
| 6784 | e = self->length - str1->length; |
| 6785 | if (str1->length > 0) { |
| 6786 | while (n-- > 0) { |
| 6787 | /* look for next match */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6788 | j = stringlib_find(self->str+i, self->length-i, |
| 6789 | str1->str, str1->length, |
| 6790 | i); |
| 6791 | if (j == -1) |
| 6792 | break; |
| 6793 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6794 | /* copy unchanged part [i:j] */ |
| 6795 | Py_UNICODE_COPY(p, self->str+i, j-i); |
| 6796 | p += j - i; |
| 6797 | } |
| 6798 | /* copy substitution string */ |
| 6799 | if (str2->length > 0) { |
| 6800 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6801 | p += str2->length; |
| 6802 | } |
| 6803 | i = j + str1->length; |
| 6804 | } |
| 6805 | if (i < self->length) |
| 6806 | /* copy tail [i:] */ |
| 6807 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6808 | } else { |
| 6809 | /* interleave */ |
| 6810 | while (n > 0) { |
| 6811 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6812 | p += str2->length; |
| 6813 | if (--n <= 0) |
| 6814 | break; |
| 6815 | *p++ = self->str[i++]; |
| 6816 | } |
| 6817 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6818 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6819 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6820 | return (PyObject *) u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6821 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6822 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6823 | /* nothing to replace; return original string (when possible) */ |
| 6824 | if (PyUnicode_CheckExact(self)) { |
| 6825 | Py_INCREF(self); |
| 6826 | return (PyObject *) self; |
| 6827 | } |
| 6828 | return PyUnicode_FromUnicode(self->str, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6829 | } |
| 6830 | |
| 6831 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 6832 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6833 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6834 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6835 | \n\ |
| 6836 | 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] | 6837 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6838 | |
| 6839 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6840 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6841 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6842 | return fixup(self, fixtitle); |
| 6843 | } |
| 6844 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6845 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6846 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6847 | \n\ |
| 6848 | 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] | 6849 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6850 | |
| 6851 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6852 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6853 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6854 | return fixup(self, fixcapitalize); |
| 6855 | } |
| 6856 | |
| 6857 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6858 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6859 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6860 | \n\ |
| 6861 | 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] | 6862 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6863 | |
| 6864 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6865 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6866 | { |
| 6867 | PyObject *list; |
| 6868 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6869 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6870 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6871 | /* Split into words */ |
| 6872 | list = split(self, NULL, -1); |
| 6873 | if (!list) |
| 6874 | return NULL; |
| 6875 | |
| 6876 | /* Capitalize each word */ |
| 6877 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 6878 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6879 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6880 | if (item == NULL) |
| 6881 | goto onError; |
| 6882 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 6883 | PyList_SET_ITEM(list, i, item); |
| 6884 | } |
| 6885 | |
| 6886 | /* Join the words to form a new string */ |
| 6887 | item = PyUnicode_Join(NULL, list); |
| 6888 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6889 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6890 | Py_DECREF(list); |
| 6891 | return (PyObject *)item; |
| 6892 | } |
| 6893 | #endif |
| 6894 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6895 | /* Argument converter. Coerces to a single unicode character */ |
| 6896 | |
| 6897 | static int |
| 6898 | convert_uc(PyObject *obj, void *addr) |
| 6899 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6900 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 6901 | PyObject *uniobj; |
| 6902 | Py_UNICODE *unistr; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6903 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6904 | uniobj = PyUnicode_FromObject(obj); |
| 6905 | if (uniobj == NULL) { |
| 6906 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6907 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6908 | return 0; |
| 6909 | } |
| 6910 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 6911 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6912 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6913 | Py_DECREF(uniobj); |
| 6914 | return 0; |
| 6915 | } |
| 6916 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 6917 | *fillcharloc = unistr[0]; |
| 6918 | Py_DECREF(uniobj); |
| 6919 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6920 | } |
| 6921 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6922 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6923 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6924 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 6925 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6926 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6927 | |
| 6928 | static PyObject * |
| 6929 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 6930 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6931 | Py_ssize_t marg, left; |
| 6932 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6933 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6934 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 6935 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6936 | return NULL; |
| 6937 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6938 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6939 | Py_INCREF(self); |
| 6940 | return (PyObject*) self; |
| 6941 | } |
| 6942 | |
| 6943 | marg = width - self->length; |
| 6944 | left = marg / 2 + (marg & width & 1); |
| 6945 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6946 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6947 | } |
| 6948 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6949 | #if 0 |
| 6950 | |
| 6951 | /* This code should go into some future Unicode collation support |
| 6952 | module. The basic comparison should compare ordinals on a naive |
Georg Brandl | c6c3178 | 2009-06-08 13:41:29 +0000 | [diff] [blame] | 6953 | basis (this is what Java does and thus Jython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6954 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6955 | /* speedy UTF-16 code point order comparison */ |
| 6956 | /* gleaned from: */ |
| 6957 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 6958 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6959 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6960 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6961 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6962 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 6963 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6964 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6965 | }; |
| 6966 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6967 | static int |
| 6968 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6969 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6970 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6971 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6972 | Py_UNICODE *s1 = str1->str; |
| 6973 | Py_UNICODE *s2 = str2->str; |
| 6974 | |
| 6975 | len1 = str1->length; |
| 6976 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6977 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6978 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6979 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6980 | |
| 6981 | c1 = *s1++; |
| 6982 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6983 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6984 | if (c1 > (1<<11) * 26) |
| 6985 | c1 += utf16Fixup[c1>>11]; |
| 6986 | if (c2 > (1<<11) * 26) |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6987 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6988 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6989 | |
| 6990 | if (c1 != c2) |
| 6991 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6992 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6993 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6994 | } |
| 6995 | |
| 6996 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6997 | } |
| 6998 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6999 | #else |
| 7000 | |
| 7001 | static int |
| 7002 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 7003 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7004 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 7005 | |
| 7006 | Py_UNICODE *s1 = str1->str; |
| 7007 | Py_UNICODE *s2 = str2->str; |
| 7008 | |
| 7009 | len1 = str1->length; |
| 7010 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7011 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 7012 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7013 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 7014 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 7015 | c1 = *s1++; |
| 7016 | c2 = *s2++; |
| 7017 | |
| 7018 | if (c1 != c2) |
| 7019 | return (c1 < c2) ? -1 : 1; |
| 7020 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 7021 | len1--; len2--; |
| 7022 | } |
| 7023 | |
| 7024 | return (len1 < len2) ? -1 : (len1 != len2); |
| 7025 | } |
| 7026 | |
| 7027 | #endif |
| 7028 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7029 | int PyUnicode_Compare(PyObject *left, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7030 | PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7031 | { |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 7032 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) |
| 7033 | return unicode_compare((PyUnicodeObject *)left, |
| 7034 | (PyUnicodeObject *)right); |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 7035 | PyErr_Format(PyExc_TypeError, |
| 7036 | "Can't compare %.100s and %.100s", |
| 7037 | left->ob_type->tp_name, |
| 7038 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7039 | return -1; |
| 7040 | } |
| 7041 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 7042 | int |
| 7043 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 7044 | { |
| 7045 | int i; |
| 7046 | Py_UNICODE *id; |
| 7047 | assert(PyUnicode_Check(uni)); |
| 7048 | id = PyUnicode_AS_UNICODE(uni); |
| 7049 | /* Compare Unicode string and source character set string */ |
| 7050 | for (i = 0; id[i] && str[i]; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7051 | if (id[i] != str[i]) |
| 7052 | return ((int)id[i] < (int)str[i]) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 7053 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 7054 | to C strings identical up to that point. */ |
Benjamin Peterson | a23831f | 2010-04-25 21:54:00 +0000 | [diff] [blame] | 7055 | if (PyUnicode_GET_SIZE(uni) != i || id[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7056 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 7057 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7058 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 7059 | return 0; |
| 7060 | } |
| 7061 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 7062 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7063 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7064 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 7065 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 7066 | PyObject *PyUnicode_RichCompare(PyObject *left, |
| 7067 | PyObject *right, |
| 7068 | int op) |
| 7069 | { |
| 7070 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7071 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 7072 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 7073 | PyObject *v; |
| 7074 | if (((PyUnicodeObject *) left)->length != |
| 7075 | ((PyUnicodeObject *) right)->length) { |
| 7076 | if (op == Py_EQ) { |
| 7077 | Py_INCREF(Py_False); |
| 7078 | return Py_False; |
| 7079 | } |
| 7080 | if (op == Py_NE) { |
| 7081 | Py_INCREF(Py_True); |
| 7082 | return Py_True; |
| 7083 | } |
| 7084 | } |
| 7085 | if (left == right) |
| 7086 | result = 0; |
| 7087 | else |
| 7088 | result = unicode_compare((PyUnicodeObject *)left, |
| 7089 | (PyUnicodeObject *)right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7090 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 7091 | /* Convert the return value to a Boolean */ |
| 7092 | switch (op) { |
| 7093 | case Py_EQ: |
| 7094 | v = TEST_COND(result == 0); |
| 7095 | break; |
| 7096 | case Py_NE: |
| 7097 | v = TEST_COND(result != 0); |
| 7098 | break; |
| 7099 | case Py_LE: |
| 7100 | v = TEST_COND(result <= 0); |
| 7101 | break; |
| 7102 | case Py_GE: |
| 7103 | v = TEST_COND(result >= 0); |
| 7104 | break; |
| 7105 | case Py_LT: |
| 7106 | v = TEST_COND(result == -1); |
| 7107 | break; |
| 7108 | case Py_GT: |
| 7109 | v = TEST_COND(result == 1); |
| 7110 | break; |
| 7111 | default: |
| 7112 | PyErr_BadArgument(); |
| 7113 | return NULL; |
| 7114 | } |
| 7115 | Py_INCREF(v); |
| 7116 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 7117 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7118 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 7119 | Py_INCREF(Py_NotImplemented); |
| 7120 | return Py_NotImplemented; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 7121 | } |
| 7122 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7123 | int PyUnicode_Contains(PyObject *container, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7124 | PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7125 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7126 | PyObject *str, *sub; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7127 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7128 | |
| 7129 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7130 | sub = PyUnicode_FromObject(element); |
| 7131 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7132 | PyErr_Format(PyExc_TypeError, |
| 7133 | "'in <string>' requires string as left operand, not %s", |
| 7134 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7135 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7136 | } |
| 7137 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7138 | str = PyUnicode_FromObject(container); |
| 7139 | if (!str) { |
| 7140 | Py_DECREF(sub); |
| 7141 | return -1; |
| 7142 | } |
| 7143 | |
| 7144 | result = stringlib_contains_obj(str, sub); |
| 7145 | |
| 7146 | Py_DECREF(str); |
| 7147 | Py_DECREF(sub); |
| 7148 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7149 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7150 | } |
| 7151 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7152 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 7153 | |
| 7154 | PyObject *PyUnicode_Concat(PyObject *left, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7155 | PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7156 | { |
| 7157 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 7158 | |
| 7159 | /* Coerce the two arguments */ |
| 7160 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 7161 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7162 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7163 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 7164 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7165 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7166 | |
| 7167 | /* Shortcuts */ |
| 7168 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7169 | Py_DECREF(v); |
| 7170 | return (PyObject *)u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7171 | } |
| 7172 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7173 | Py_DECREF(u); |
| 7174 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7175 | } |
| 7176 | |
| 7177 | /* Concat the two Unicode strings */ |
| 7178 | w = _PyUnicode_New(u->length + v->length); |
| 7179 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7180 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7181 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 7182 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 7183 | |
| 7184 | Py_DECREF(u); |
| 7185 | Py_DECREF(v); |
| 7186 | return (PyObject *)w; |
| 7187 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7188 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7189 | Py_XDECREF(u); |
| 7190 | Py_XDECREF(v); |
| 7191 | return NULL; |
| 7192 | } |
| 7193 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7194 | void |
| 7195 | PyUnicode_Append(PyObject **pleft, PyObject *right) |
| 7196 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7197 | PyObject *new; |
| 7198 | if (*pleft == NULL) |
| 7199 | return; |
| 7200 | if (right == NULL || !PyUnicode_Check(*pleft)) { |
| 7201 | Py_DECREF(*pleft); |
| 7202 | *pleft = NULL; |
| 7203 | return; |
| 7204 | } |
| 7205 | new = PyUnicode_Concat(*pleft, right); |
| 7206 | Py_DECREF(*pleft); |
| 7207 | *pleft = new; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7208 | } |
| 7209 | |
| 7210 | void |
| 7211 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 7212 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7213 | PyUnicode_Append(pleft, right); |
| 7214 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7215 | } |
| 7216 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7217 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7218 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7219 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7220 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7221 | 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] | 7222 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7223 | |
| 7224 | static PyObject * |
| 7225 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 7226 | { |
| 7227 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7228 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7229 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7230 | PyObject *result; |
| 7231 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 7232 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7233 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7234 | return NULL; |
| 7235 | |
| 7236 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7237 | (PyObject *)substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7238 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7239 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7240 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 7241 | ADJUST_INDICES(start, end, self->length); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7242 | result = PyLong_FromSsize_t( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7243 | stringlib_count(self->str + start, end - start, |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 7244 | substring->str, substring->length, |
| 7245 | PY_SSIZE_T_MAX) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7246 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7247 | |
| 7248 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7249 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7250 | return result; |
| 7251 | } |
| 7252 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7253 | PyDoc_STRVAR(encode__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7254 | "S.encode([encoding[, errors]]) -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7255 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7256 | Encode S using the codec registered for encoding. encoding defaults\n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7257 | to the default encoding. errors may be given to set a different error\n\ |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7258 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7259 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 7260 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 7261 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7262 | |
| 7263 | static PyObject * |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 7264 | unicode_encode(PyUnicodeObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7265 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 7266 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7267 | char *encoding = NULL; |
| 7268 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7269 | PyObject *v; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 7270 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 7271 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 7272 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7273 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 7274 | v = PyUnicode_AsEncodedString((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 7275 | if (v == NULL) |
| 7276 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7277 | if (!PyBytes_Check(v)) { |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7278 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 7279 | "encoder did not return a bytes object " |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7280 | "(type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7281 | Py_TYPE(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7282 | Py_DECREF(v); |
| 7283 | return NULL; |
| 7284 | } |
| 7285 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 7286 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7287 | onError: |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 7288 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7289 | } |
| 7290 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7291 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7292 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7293 | \n\ |
| 7294 | 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] | 7295 | 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] | 7296 | |
| 7297 | static PyObject* |
| 7298 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 7299 | { |
| 7300 | Py_UNICODE *e; |
| 7301 | Py_UNICODE *p; |
| 7302 | Py_UNICODE *q; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7303 | Py_UNICODE *qe; |
| 7304 | Py_ssize_t i, j, incr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7305 | PyUnicodeObject *u; |
| 7306 | int tabsize = 8; |
| 7307 | |
| 7308 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7309 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7310 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 7311 | /* First pass: determine size of output string */ |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7312 | i = 0; /* chars up to and including most recent \n or \r */ |
| 7313 | j = 0; /* chars since most recent \n or \r (use in tab calculations) */ |
| 7314 | e = self->str + self->length; /* end of input */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7315 | for (p = self->str; p < e; p++) |
| 7316 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7317 | if (tabsize > 0) { |
| 7318 | incr = tabsize - (j % tabsize); /* cannot overflow */ |
| 7319 | if (j > PY_SSIZE_T_MAX - incr) |
| 7320 | goto overflow1; |
| 7321 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7322 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7323 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7324 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7325 | if (j > PY_SSIZE_T_MAX - 1) |
| 7326 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7327 | j++; |
| 7328 | if (*p == '\n' || *p == '\r') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7329 | if (i > PY_SSIZE_T_MAX - j) |
| 7330 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7331 | i += j; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7332 | j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7333 | } |
| 7334 | } |
| 7335 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7336 | if (i > PY_SSIZE_T_MAX - j) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7337 | goto overflow1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 7338 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7339 | /* Second pass: create output string and fill it */ |
| 7340 | u = _PyUnicode_New(i + j); |
| 7341 | if (!u) |
| 7342 | return NULL; |
| 7343 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7344 | j = 0; /* same as in first pass */ |
| 7345 | q = u->str; /* next output char */ |
| 7346 | qe = u->str + u->length; /* end of output */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7347 | |
| 7348 | for (p = self->str; p < e; p++) |
| 7349 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7350 | if (tabsize > 0) { |
| 7351 | i = tabsize - (j % tabsize); |
| 7352 | j += i; |
| 7353 | while (i--) { |
| 7354 | if (q >= qe) |
| 7355 | goto overflow2; |
| 7356 | *q++ = ' '; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7357 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7358 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7359 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7360 | else { |
| 7361 | if (q >= qe) |
| 7362 | goto overflow2; |
| 7363 | *q++ = *p; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7364 | j++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7365 | if (*p == '\n' || *p == '\r') |
| 7366 | j = 0; |
| 7367 | } |
| 7368 | |
| 7369 | return (PyObject*) u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7370 | |
| 7371 | overflow2: |
| 7372 | Py_DECREF(u); |
| 7373 | overflow1: |
| 7374 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 7375 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7376 | } |
| 7377 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7378 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7379 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7380 | \n\ |
| 7381 | Return the lowest index in S where substring sub is found,\n\ |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 7382 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7383 | arguments start and end are interpreted as in slice notation.\n\ |
| 7384 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7385 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7386 | |
| 7387 | static PyObject * |
| 7388 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 7389 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7390 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7391 | Py_ssize_t start; |
| 7392 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7393 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7394 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7395 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7396 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7397 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7398 | result = stringlib_find_slice( |
| 7399 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7400 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7401 | start, end |
| 7402 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7403 | |
| 7404 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7405 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7406 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7407 | } |
| 7408 | |
| 7409 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7410 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7411 | { |
| 7412 | if (index < 0 || index >= self->length) { |
| 7413 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 7414 | return NULL; |
| 7415 | } |
| 7416 | |
| 7417 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 7418 | } |
| 7419 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7420 | /* Believe it or not, this produces the same value for ASCII strings |
| 7421 | as string_hash(). */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7422 | static long |
Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 7423 | unicode_hash(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7424 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7425 | Py_ssize_t len; |
| 7426 | Py_UNICODE *p; |
| 7427 | long x; |
| 7428 | |
| 7429 | if (self->hash != -1) |
| 7430 | return self->hash; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7431 | len = Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7432 | p = self->str; |
| 7433 | x = *p << 7; |
| 7434 | while (--len >= 0) |
| 7435 | x = (1000003*x) ^ *p++; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7436 | x ^= Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7437 | if (x == -1) |
| 7438 | x = -2; |
| 7439 | self->hash = x; |
| 7440 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7441 | } |
| 7442 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7443 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7444 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7445 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7446 | 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] | 7447 | |
| 7448 | static PyObject * |
| 7449 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 7450 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7451 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7452 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7453 | Py_ssize_t start; |
| 7454 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7455 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7456 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7457 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7458 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7459 | result = stringlib_find_slice( |
| 7460 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7461 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7462 | start, end |
| 7463 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7464 | |
| 7465 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7466 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7467 | if (result < 0) { |
| 7468 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 7469 | return NULL; |
| 7470 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7471 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7472 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7473 | } |
| 7474 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7475 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7476 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7477 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7478 | 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] | 7479 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7480 | |
| 7481 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7482 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7483 | { |
| 7484 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7485 | register const Py_UNICODE *e; |
| 7486 | int cased; |
| 7487 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7488 | /* Shortcut for single character strings */ |
| 7489 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7490 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7491 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7492 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7493 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7494 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7495 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7496 | e = p + PyUnicode_GET_SIZE(self); |
| 7497 | cased = 0; |
| 7498 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7499 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7500 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7501 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 7502 | return PyBool_FromLong(0); |
| 7503 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 7504 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7505 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7506 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7507 | } |
| 7508 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7509 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7510 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7511 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7512 | 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] | 7513 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7514 | |
| 7515 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7516 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7517 | { |
| 7518 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7519 | register const Py_UNICODE *e; |
| 7520 | int cased; |
| 7521 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7522 | /* Shortcut for single character strings */ |
| 7523 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7524 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7525 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7526 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7527 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7528 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7529 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7530 | e = p + PyUnicode_GET_SIZE(self); |
| 7531 | cased = 0; |
| 7532 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7533 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7534 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7535 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 7536 | return PyBool_FromLong(0); |
| 7537 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 7538 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7539 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7540 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7541 | } |
| 7542 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7543 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7544 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7545 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7546 | Return True if S is a titlecased string and there is at least one\n\ |
| 7547 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 7548 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 7549 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7550 | |
| 7551 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7552 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7553 | { |
| 7554 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7555 | register const Py_UNICODE *e; |
| 7556 | int cased, previous_is_cased; |
| 7557 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7558 | /* Shortcut for single character strings */ |
| 7559 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7560 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 7561 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7562 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7563 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7564 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7565 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7566 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7567 | e = p + PyUnicode_GET_SIZE(self); |
| 7568 | cased = 0; |
| 7569 | previous_is_cased = 0; |
| 7570 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7571 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7572 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7573 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 7574 | if (previous_is_cased) |
| 7575 | return PyBool_FromLong(0); |
| 7576 | previous_is_cased = 1; |
| 7577 | cased = 1; |
| 7578 | } |
| 7579 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 7580 | if (!previous_is_cased) |
| 7581 | return PyBool_FromLong(0); |
| 7582 | previous_is_cased = 1; |
| 7583 | cased = 1; |
| 7584 | } |
| 7585 | else |
| 7586 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7587 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7588 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7589 | } |
| 7590 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7591 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7592 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7593 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7594 | Return True if all characters in S are whitespace\n\ |
| 7595 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7596 | |
| 7597 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7598 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7599 | { |
| 7600 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7601 | register const Py_UNICODE *e; |
| 7602 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7603 | /* Shortcut for single character strings */ |
| 7604 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7605 | Py_UNICODE_ISSPACE(*p)) |
| 7606 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7607 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7608 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7609 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7610 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7611 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7612 | e = p + PyUnicode_GET_SIZE(self); |
| 7613 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7614 | if (!Py_UNICODE_ISSPACE(*p)) |
| 7615 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7616 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7617 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7618 | } |
| 7619 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7620 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7621 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7622 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7623 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7624 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7625 | |
| 7626 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7627 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7628 | { |
| 7629 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7630 | register const Py_UNICODE *e; |
| 7631 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7632 | /* Shortcut for single character strings */ |
| 7633 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7634 | Py_UNICODE_ISALPHA(*p)) |
| 7635 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7636 | |
| 7637 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7638 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7639 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7640 | |
| 7641 | e = p + PyUnicode_GET_SIZE(self); |
| 7642 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7643 | if (!Py_UNICODE_ISALPHA(*p)) |
| 7644 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7645 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7646 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7647 | } |
| 7648 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7649 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7650 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7651 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7652 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7653 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7654 | |
| 7655 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7656 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7657 | { |
| 7658 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7659 | register const Py_UNICODE *e; |
| 7660 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7661 | /* Shortcut for single character strings */ |
| 7662 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7663 | Py_UNICODE_ISALNUM(*p)) |
| 7664 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7665 | |
| 7666 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7667 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7668 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7669 | |
| 7670 | e = p + PyUnicode_GET_SIZE(self); |
| 7671 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7672 | if (!Py_UNICODE_ISALNUM(*p)) |
| 7673 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7674 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7675 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7676 | } |
| 7677 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7678 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7679 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7680 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7681 | 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] | 7682 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7683 | |
| 7684 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7685 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7686 | { |
| 7687 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7688 | register const Py_UNICODE *e; |
| 7689 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7690 | /* Shortcut for single character strings */ |
| 7691 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7692 | Py_UNICODE_ISDECIMAL(*p)) |
| 7693 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7694 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7695 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7696 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7697 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7698 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7699 | e = p + PyUnicode_GET_SIZE(self); |
| 7700 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7701 | if (!Py_UNICODE_ISDECIMAL(*p)) |
| 7702 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7703 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7704 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7705 | } |
| 7706 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7707 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7708 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7709 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7710 | Return True if all characters in S are digits\n\ |
| 7711 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7712 | |
| 7713 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7714 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7715 | { |
| 7716 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7717 | register const Py_UNICODE *e; |
| 7718 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7719 | /* Shortcut for single character strings */ |
| 7720 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7721 | Py_UNICODE_ISDIGIT(*p)) |
| 7722 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7723 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7724 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7725 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7726 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7727 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7728 | e = p + PyUnicode_GET_SIZE(self); |
| 7729 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7730 | if (!Py_UNICODE_ISDIGIT(*p)) |
| 7731 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7732 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7733 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7734 | } |
| 7735 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7736 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7737 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7738 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7739 | 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] | 7740 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7741 | |
| 7742 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7743 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7744 | { |
| 7745 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7746 | register const Py_UNICODE *e; |
| 7747 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7748 | /* Shortcut for single character strings */ |
| 7749 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7750 | Py_UNICODE_ISNUMERIC(*p)) |
| 7751 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7752 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7753 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7754 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7755 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7756 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7757 | e = p + PyUnicode_GET_SIZE(self); |
| 7758 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7759 | if (!Py_UNICODE_ISNUMERIC(*p)) |
| 7760 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7761 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7762 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7763 | } |
| 7764 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7765 | int |
| 7766 | PyUnicode_IsIdentifier(PyObject *self) |
| 7767 | { |
| 7768 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE((PyUnicodeObject*)self); |
| 7769 | register const Py_UNICODE *e; |
| 7770 | |
| 7771 | /* Special case for empty strings */ |
| 7772 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7773 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7774 | |
| 7775 | /* PEP 3131 says that the first character must be in |
| 7776 | XID_Start and subsequent characters in XID_Continue, |
| 7777 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7778 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7779 | letters, digits, underscore). However, given the current |
| 7780 | definition of XID_Start and XID_Continue, it is sufficient |
| 7781 | to check just for these, except that _ must be allowed |
| 7782 | as starting an identifier. */ |
| 7783 | if (!_PyUnicode_IsXidStart(*p) && *p != 0x5F /* LOW LINE */) |
| 7784 | return 0; |
| 7785 | |
| 7786 | e = p + PyUnicode_GET_SIZE(self); |
| 7787 | for (p++; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7788 | if (!_PyUnicode_IsXidContinue(*p)) |
| 7789 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7790 | } |
| 7791 | return 1; |
| 7792 | } |
| 7793 | |
| 7794 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7795 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7796 | \n\ |
| 7797 | Return True if S is a valid identifier according\n\ |
| 7798 | to the language definition."); |
| 7799 | |
| 7800 | static PyObject* |
| 7801 | unicode_isidentifier(PyObject *self) |
| 7802 | { |
| 7803 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 7804 | } |
| 7805 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7806 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7807 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7808 | \n\ |
| 7809 | Return True if all characters in S are considered\n\ |
| 7810 | printable in repr() or S is empty, False otherwise."); |
| 7811 | |
| 7812 | static PyObject* |
| 7813 | unicode_isprintable(PyObject *self) |
| 7814 | { |
| 7815 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7816 | register const Py_UNICODE *e; |
| 7817 | |
| 7818 | /* Shortcut for single character strings */ |
| 7819 | if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISPRINTABLE(*p)) { |
| 7820 | Py_RETURN_TRUE; |
| 7821 | } |
| 7822 | |
| 7823 | e = p + PyUnicode_GET_SIZE(self); |
| 7824 | for (; p < e; p++) { |
| 7825 | if (!Py_UNICODE_ISPRINTABLE(*p)) { |
| 7826 | Py_RETURN_FALSE; |
| 7827 | } |
| 7828 | } |
| 7829 | Py_RETURN_TRUE; |
| 7830 | } |
| 7831 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7832 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 7833 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7834 | \n\ |
| 7835 | 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] | 7836 | iterable. The separator between elements is S."); |
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_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7840 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7841 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7842 | } |
| 7843 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7844 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7845 | unicode_length(PyUnicodeObject *self) |
| 7846 | { |
| 7847 | return self->length; |
| 7848 | } |
| 7849 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7850 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7851 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7852 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 7853 | 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] | 7854 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7855 | |
| 7856 | static PyObject * |
| 7857 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 7858 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7859 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7860 | Py_UNICODE fillchar = ' '; |
| 7861 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7862 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7863 | return NULL; |
| 7864 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7865 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7866 | Py_INCREF(self); |
| 7867 | return (PyObject*) self; |
| 7868 | } |
| 7869 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7870 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7871 | } |
| 7872 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7873 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7874 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7875 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7876 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7877 | |
| 7878 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7879 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7880 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7881 | return fixup(self, fixlower); |
| 7882 | } |
| 7883 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7884 | #define LEFTSTRIP 0 |
| 7885 | #define RIGHTSTRIP 1 |
| 7886 | #define BOTHSTRIP 2 |
| 7887 | |
| 7888 | /* Arrays indexed by above */ |
| 7889 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 7890 | |
| 7891 | #define STRIPNAME(i) (stripformat[i]+3) |
| 7892 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7893 | /* externally visible for str.strip(unicode) */ |
| 7894 | PyObject * |
| 7895 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 7896 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7897 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 7898 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
| 7899 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
| 7900 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 7901 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7902 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7903 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7904 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7905 | i = 0; |
| 7906 | if (striptype != RIGHTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7907 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 7908 | i++; |
| 7909 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7910 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7911 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7912 | j = len; |
| 7913 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7914 | do { |
| 7915 | j--; |
| 7916 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 7917 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7918 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7919 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7920 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7921 | Py_INCREF(self); |
| 7922 | return (PyObject*)self; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7923 | } |
| 7924 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7925 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7926 | } |
| 7927 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7928 | |
| 7929 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7930 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7931 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7932 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 7933 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7934 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7935 | i = 0; |
| 7936 | if (striptype != RIGHTSTRIP) { |
| 7937 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 7938 | i++; |
| 7939 | } |
| 7940 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7941 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7942 | j = len; |
| 7943 | if (striptype != LEFTSTRIP) { |
| 7944 | do { |
| 7945 | j--; |
| 7946 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 7947 | j++; |
| 7948 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7949 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7950 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 7951 | Py_INCREF(self); |
| 7952 | return (PyObject*)self; |
| 7953 | } |
| 7954 | else |
| 7955 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7956 | } |
| 7957 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7958 | |
| 7959 | static PyObject * |
| 7960 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 7961 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7962 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7963 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7964 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 7965 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7966 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7967 | if (sep != NULL && sep != Py_None) { |
| 7968 | if (PyUnicode_Check(sep)) |
| 7969 | return _PyUnicode_XStrip(self, striptype, sep); |
| 7970 | else { |
| 7971 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7972 | "%s arg must be None or str", |
| 7973 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7974 | return NULL; |
| 7975 | } |
| 7976 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7977 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7978 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7979 | } |
| 7980 | |
| 7981 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7982 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7983 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7984 | \n\ |
| 7985 | Return a copy of the string S with leading and trailing\n\ |
| 7986 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7987 | 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] | 7988 | |
| 7989 | static PyObject * |
| 7990 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 7991 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7992 | if (PyTuple_GET_SIZE(args) == 0) |
| 7993 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 7994 | else |
| 7995 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7996 | } |
| 7997 | |
| 7998 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7999 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8000 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8001 | \n\ |
| 8002 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8003 | 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] | 8004 | |
| 8005 | static PyObject * |
| 8006 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 8007 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8008 | if (PyTuple_GET_SIZE(args) == 0) |
| 8009 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 8010 | else |
| 8011 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8012 | } |
| 8013 | |
| 8014 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8015 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8016 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8017 | \n\ |
| 8018 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8019 | 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] | 8020 | |
| 8021 | static PyObject * |
| 8022 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 8023 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8024 | if (PyTuple_GET_SIZE(args) == 0) |
| 8025 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 8026 | else |
| 8027 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8028 | } |
| 8029 | |
| 8030 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8031 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8032 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8033 | { |
| 8034 | PyUnicodeObject *u; |
| 8035 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8036 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 8037 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8038 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 8039 | if (len < 1) { |
| 8040 | Py_INCREF(unicode_empty); |
| 8041 | return (PyObject *)unicode_empty; |
| 8042 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8043 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 8044 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8045 | /* no repeat, return original string */ |
| 8046 | Py_INCREF(str); |
| 8047 | return (PyObject*) str; |
| 8048 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 8049 | |
| 8050 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 8051 | * needed doesn't overflow size_t |
| 8052 | */ |
| 8053 | nchars = len * str->length; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 8054 | if (nchars / len != str->length) { |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 8055 | PyErr_SetString(PyExc_OverflowError, |
| 8056 | "repeated string is too long"); |
| 8057 | return NULL; |
| 8058 | } |
| 8059 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 8060 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 8061 | PyErr_SetString(PyExc_OverflowError, |
| 8062 | "repeated string is too long"); |
| 8063 | return NULL; |
| 8064 | } |
| 8065 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8066 | if (!u) |
| 8067 | return NULL; |
| 8068 | |
| 8069 | p = u->str; |
| 8070 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 8071 | if (str->length == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8072 | Py_UNICODE_FILL(p, str->str[0], len); |
| 8073 | } else { |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 8074 | Py_ssize_t done = str->length; /* number of characters copied this far */ |
| 8075 | Py_UNICODE_COPY(p, str->str, str->length); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8076 | while (done < nchars) { |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 8077 | Py_ssize_t n = (done <= nchars-done) ? done : nchars-done; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8078 | Py_UNICODE_COPY(p+done, p, n); |
| 8079 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8080 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8081 | } |
| 8082 | |
| 8083 | return (PyObject*) u; |
| 8084 | } |
| 8085 | |
| 8086 | PyObject *PyUnicode_Replace(PyObject *obj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8087 | PyObject *subobj, |
| 8088 | PyObject *replobj, |
| 8089 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8090 | { |
| 8091 | PyObject *self; |
| 8092 | PyObject *str1; |
| 8093 | PyObject *str2; |
| 8094 | PyObject *result; |
| 8095 | |
| 8096 | self = PyUnicode_FromObject(obj); |
| 8097 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8098 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8099 | str1 = PyUnicode_FromObject(subobj); |
| 8100 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8101 | Py_DECREF(self); |
| 8102 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8103 | } |
| 8104 | str2 = PyUnicode_FromObject(replobj); |
| 8105 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8106 | Py_DECREF(self); |
| 8107 | Py_DECREF(str1); |
| 8108 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8109 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8110 | result = replace((PyUnicodeObject *)self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8111 | (PyUnicodeObject *)str1, |
| 8112 | (PyUnicodeObject *)str2, |
| 8113 | maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8114 | Py_DECREF(self); |
| 8115 | Py_DECREF(str1); |
| 8116 | Py_DECREF(str2); |
| 8117 | return result; |
| 8118 | } |
| 8119 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8120 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 8121 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8122 | \n\ |
| 8123 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 8124 | old replaced by new. If the optional argument count is\n\ |
| 8125 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8126 | |
| 8127 | static PyObject* |
| 8128 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 8129 | { |
| 8130 | PyUnicodeObject *str1; |
| 8131 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8132 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8133 | PyObject *result; |
| 8134 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8135 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8136 | return NULL; |
| 8137 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 8138 | if (str1 == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8139 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8140 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 8141 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8142 | Py_DECREF(str1); |
| 8143 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 8144 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8145 | |
| 8146 | result = replace(self, str1, str2, maxcount); |
| 8147 | |
| 8148 | Py_DECREF(str1); |
| 8149 | Py_DECREF(str2); |
| 8150 | return result; |
| 8151 | } |
| 8152 | |
| 8153 | static |
| 8154 | PyObject *unicode_repr(PyObject *unicode) |
| 8155 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8156 | PyObject *repr; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8157 | Py_UNICODE *p; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8158 | Py_UNICODE *s = PyUnicode_AS_UNICODE(unicode); |
| 8159 | Py_ssize_t size = PyUnicode_GET_SIZE(unicode); |
| 8160 | |
| 8161 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 8162 | better to choose a different scheme. Perhaps scan the |
| 8163 | first N-chars of the string and allocate based on that size. |
| 8164 | */ |
| 8165 | /* Initial allocation is based on the longest-possible unichr |
| 8166 | escape. |
| 8167 | |
| 8168 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 8169 | unichr, so in this case it's the longest unichr escape. In |
| 8170 | narrow (UTF-16) builds this is five chars per source unichr |
| 8171 | since there are two unichrs in the surrogate pair, so in narrow |
| 8172 | (UTF-16) builds it's not the longest unichr escape. |
| 8173 | |
| 8174 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 8175 | so in the narrow (UTF-16) build case it's the longest unichr |
| 8176 | escape. |
| 8177 | */ |
| 8178 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8179 | repr = PyUnicode_FromUnicode(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8180 | 2 /* quotes */ |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8181 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8182 | + 10*size |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8183 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8184 | + 6*size |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8185 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8186 | + 1); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8187 | if (repr == NULL) |
| 8188 | return NULL; |
| 8189 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8190 | p = PyUnicode_AS_UNICODE(repr); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8191 | |
| 8192 | /* Add quote */ |
| 8193 | *p++ = (findchar(s, size, '\'') && |
| 8194 | !findchar(s, size, '"')) ? '"' : '\''; |
| 8195 | while (size-- > 0) { |
| 8196 | Py_UNICODE ch = *s++; |
| 8197 | |
| 8198 | /* Escape quotes and backslashes */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8199 | if ((ch == PyUnicode_AS_UNICODE(repr)[0]) || (ch == '\\')) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8200 | *p++ = '\\'; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8201 | *p++ = ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8202 | continue; |
| 8203 | } |
| 8204 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8205 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8206 | if (ch == '\t') { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8207 | *p++ = '\\'; |
| 8208 | *p++ = 't'; |
| 8209 | } |
| 8210 | else if (ch == '\n') { |
| 8211 | *p++ = '\\'; |
| 8212 | *p++ = 'n'; |
| 8213 | } |
| 8214 | else if (ch == '\r') { |
| 8215 | *p++ = '\\'; |
| 8216 | *p++ = 'r'; |
| 8217 | } |
| 8218 | |
| 8219 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8220 | else if (ch < ' ' || ch == 0x7F) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8221 | *p++ = '\\'; |
| 8222 | *p++ = 'x'; |
| 8223 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 8224 | *p++ = hexdigits[ch & 0x000F]; |
| 8225 | } |
| 8226 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8227 | /* Copy ASCII characters as-is */ |
| 8228 | else if (ch < 0x7F) { |
| 8229 | *p++ = ch; |
| 8230 | } |
| 8231 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8232 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8233 | else { |
| 8234 | Py_UCS4 ucs = ch; |
| 8235 | |
| 8236 | #ifndef Py_UNICODE_WIDE |
| 8237 | Py_UNICODE ch2 = 0; |
| 8238 | /* Get code point from surrogate pair */ |
| 8239 | if (size > 0) { |
| 8240 | ch2 = *s; |
| 8241 | if (ch >= 0xD800 && ch < 0xDC00 && ch2 >= 0xDC00 |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8242 | && ch2 <= 0xDFFF) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8243 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8244 | + 0x00010000; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8245 | s++; |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8246 | size--; |
| 8247 | } |
| 8248 | } |
| 8249 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8250 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8251 | (categories Z* and C* except ASCII space) |
| 8252 | */ |
| 8253 | if (!Py_UNICODE_ISPRINTABLE(ucs)) { |
| 8254 | /* Map 8-bit characters to '\xhh' */ |
| 8255 | if (ucs <= 0xff) { |
| 8256 | *p++ = '\\'; |
| 8257 | *p++ = 'x'; |
| 8258 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 8259 | *p++ = hexdigits[ch & 0x000F]; |
| 8260 | } |
| 8261 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 8262 | else if (ucs >= 0x10000) { |
| 8263 | *p++ = '\\'; |
| 8264 | *p++ = 'U'; |
| 8265 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 8266 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 8267 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 8268 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 8269 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 8270 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 8271 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 8272 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 8273 | } |
| 8274 | /* Map 16-bit characters to '\uxxxx' */ |
| 8275 | else { |
| 8276 | *p++ = '\\'; |
| 8277 | *p++ = 'u'; |
| 8278 | *p++ = hexdigits[(ucs >> 12) & 0x000F]; |
| 8279 | *p++ = hexdigits[(ucs >> 8) & 0x000F]; |
| 8280 | *p++ = hexdigits[(ucs >> 4) & 0x000F]; |
| 8281 | *p++ = hexdigits[ucs & 0x000F]; |
| 8282 | } |
| 8283 | } |
| 8284 | /* Copy characters as-is */ |
| 8285 | else { |
| 8286 | *p++ = ch; |
| 8287 | #ifndef Py_UNICODE_WIDE |
| 8288 | if (ucs >= 0x10000) |
| 8289 | *p++ = ch2; |
| 8290 | #endif |
| 8291 | } |
| 8292 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8293 | } |
| 8294 | /* Add quote */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8295 | *p++ = PyUnicode_AS_UNICODE(repr)[0]; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8296 | |
| 8297 | *p = '\0'; |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 8298 | PyUnicode_Resize(&repr, p - PyUnicode_AS_UNICODE(repr)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8299 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8300 | } |
| 8301 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8302 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8303 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8304 | \n\ |
| 8305 | Return the highest index in S where substring sub is found,\n\ |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 8306 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8307 | arguments start and end are interpreted as in slice notation.\n\ |
| 8308 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8309 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8310 | |
| 8311 | static PyObject * |
| 8312 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 8313 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8314 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8315 | Py_ssize_t start; |
| 8316 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8317 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8318 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8319 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8320 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8321 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8322 | result = stringlib_rfind_slice( |
| 8323 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 8324 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 8325 | start, end |
| 8326 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8327 | |
| 8328 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8329 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8330 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8331 | } |
| 8332 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8333 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8334 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8335 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8336 | 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] | 8337 | |
| 8338 | static PyObject * |
| 8339 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 8340 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8341 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8342 | Py_ssize_t start; |
| 8343 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8344 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8345 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8346 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8347 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8348 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8349 | result = stringlib_rfind_slice( |
| 8350 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 8351 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 8352 | start, end |
| 8353 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8354 | |
| 8355 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8356 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8357 | if (result < 0) { |
| 8358 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 8359 | return NULL; |
| 8360 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8361 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8362 | } |
| 8363 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8364 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8365 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8366 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 8367 | 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] | 8368 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8369 | |
| 8370 | static PyObject * |
| 8371 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 8372 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8373 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 8374 | Py_UNICODE fillchar = ' '; |
| 8375 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8376 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8377 | return NULL; |
| 8378 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 8379 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8380 | Py_INCREF(self); |
| 8381 | return (PyObject*) self; |
| 8382 | } |
| 8383 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 8384 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8385 | } |
| 8386 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8387 | PyObject *PyUnicode_Split(PyObject *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8388 | PyObject *sep, |
| 8389 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8390 | { |
| 8391 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8392 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8393 | s = PyUnicode_FromObject(s); |
| 8394 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8395 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8396 | if (sep != NULL) { |
| 8397 | sep = PyUnicode_FromObject(sep); |
| 8398 | if (sep == NULL) { |
| 8399 | Py_DECREF(s); |
| 8400 | return NULL; |
| 8401 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8402 | } |
| 8403 | |
| 8404 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 8405 | |
| 8406 | Py_DECREF(s); |
| 8407 | Py_XDECREF(sep); |
| 8408 | return result; |
| 8409 | } |
| 8410 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8411 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8412 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8413 | \n\ |
| 8414 | Return a list of the words in S, using sep as the\n\ |
| 8415 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 8416 | 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] | 8417 | whitespace string is a separator and empty strings are\n\ |
| 8418 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8419 | |
| 8420 | static PyObject* |
| 8421 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 8422 | { |
| 8423 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8424 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8425 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8426 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8427 | return NULL; |
| 8428 | |
| 8429 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8430 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8431 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8432 | return split(self, (PyUnicodeObject *)substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8433 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8434 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8435 | } |
| 8436 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8437 | PyObject * |
| 8438 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 8439 | { |
| 8440 | PyObject* str_obj; |
| 8441 | PyObject* sep_obj; |
| 8442 | PyObject* out; |
| 8443 | |
| 8444 | str_obj = PyUnicode_FromObject(str_in); |
| 8445 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8446 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8447 | sep_obj = PyUnicode_FromObject(sep_in); |
| 8448 | if (!sep_obj) { |
| 8449 | Py_DECREF(str_obj); |
| 8450 | return NULL; |
| 8451 | } |
| 8452 | |
| 8453 | out = stringlib_partition( |
| 8454 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 8455 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 8456 | ); |
| 8457 | |
| 8458 | Py_DECREF(sep_obj); |
| 8459 | Py_DECREF(str_obj); |
| 8460 | |
| 8461 | return out; |
| 8462 | } |
| 8463 | |
| 8464 | |
| 8465 | PyObject * |
| 8466 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 8467 | { |
| 8468 | PyObject* str_obj; |
| 8469 | PyObject* sep_obj; |
| 8470 | PyObject* out; |
| 8471 | |
| 8472 | str_obj = PyUnicode_FromObject(str_in); |
| 8473 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8474 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8475 | sep_obj = PyUnicode_FromObject(sep_in); |
| 8476 | if (!sep_obj) { |
| 8477 | Py_DECREF(str_obj); |
| 8478 | return NULL; |
| 8479 | } |
| 8480 | |
| 8481 | out = stringlib_rpartition( |
| 8482 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 8483 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 8484 | ); |
| 8485 | |
| 8486 | Py_DECREF(sep_obj); |
| 8487 | Py_DECREF(str_obj); |
| 8488 | |
| 8489 | return out; |
| 8490 | } |
| 8491 | |
| 8492 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8493 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8494 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8495 | 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] | 8496 | 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] | 8497 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8498 | |
| 8499 | static PyObject* |
| 8500 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
| 8501 | { |
| 8502 | return PyUnicode_Partition((PyObject *)self, separator); |
| 8503 | } |
| 8504 | |
| 8505 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 8506 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8507 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8508 | 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] | 8509 | 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] | 8510 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8511 | |
| 8512 | static PyObject* |
| 8513 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 8514 | { |
| 8515 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 8516 | } |
| 8517 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8518 | PyObject *PyUnicode_RSplit(PyObject *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8519 | PyObject *sep, |
| 8520 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8521 | { |
| 8522 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8523 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8524 | s = PyUnicode_FromObject(s); |
| 8525 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8526 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8527 | if (sep != NULL) { |
| 8528 | sep = PyUnicode_FromObject(sep); |
| 8529 | if (sep == NULL) { |
| 8530 | Py_DECREF(s); |
| 8531 | return NULL; |
| 8532 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8533 | } |
| 8534 | |
| 8535 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 8536 | |
| 8537 | Py_DECREF(s); |
| 8538 | Py_XDECREF(sep); |
| 8539 | return result; |
| 8540 | } |
| 8541 | |
| 8542 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8543 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8544 | \n\ |
| 8545 | Return a list of the words in S, using sep as the\n\ |
| 8546 | delimiter string, starting at the end of the string and\n\ |
| 8547 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 8548 | splits are done. If sep is not specified, any whitespace string\n\ |
| 8549 | is a separator."); |
| 8550 | |
| 8551 | static PyObject* |
| 8552 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 8553 | { |
| 8554 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8555 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8556 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8557 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8558 | return NULL; |
| 8559 | |
| 8560 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8561 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8562 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8563 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8564 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8565 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8566 | } |
| 8567 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8568 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8569 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8570 | \n\ |
| 8571 | 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] | 8572 | 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] | 8573 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8574 | |
| 8575 | static PyObject* |
| 8576 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 8577 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8578 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8579 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8580 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8581 | return NULL; |
| 8582 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8583 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8584 | } |
| 8585 | |
| 8586 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 8587 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8588 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 8589 | if (PyUnicode_CheckExact(self)) { |
| 8590 | Py_INCREF(self); |
| 8591 | return self; |
| 8592 | } else |
| 8593 | /* Subtype -- return genuine unicode string with the same value. */ |
| 8594 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(self), |
| 8595 | PyUnicode_GET_SIZE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8596 | } |
| 8597 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8598 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8599 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8600 | \n\ |
| 8601 | 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] | 8602 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8603 | |
| 8604 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8605 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8606 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8607 | return fixup(self, fixswapcase); |
| 8608 | } |
| 8609 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8610 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8611 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8612 | \n\ |
| 8613 | Return a translation table usable for str.translate().\n\ |
| 8614 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 8615 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8616 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8617 | If there are two arguments, they must be strings of equal length, and\n\ |
| 8618 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 8619 | character at the same position in y. If there is a third argument, it\n\ |
| 8620 | must be a string, whose characters will be mapped to None in the result."); |
| 8621 | |
| 8622 | static PyObject* |
| 8623 | unicode_maketrans(PyUnicodeObject *null, PyObject *args) |
| 8624 | { |
| 8625 | PyObject *x, *y = NULL, *z = NULL; |
| 8626 | PyObject *new = NULL, *key, *value; |
| 8627 | Py_ssize_t i = 0; |
| 8628 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8629 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8630 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 8631 | return NULL; |
| 8632 | new = PyDict_New(); |
| 8633 | if (!new) |
| 8634 | return NULL; |
| 8635 | if (y != NULL) { |
| 8636 | /* x must be a string too, of equal length */ |
| 8637 | Py_ssize_t ylen = PyUnicode_GET_SIZE(y); |
| 8638 | if (!PyUnicode_Check(x)) { |
| 8639 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 8640 | "be a string if there is a second argument"); |
| 8641 | goto err; |
| 8642 | } |
| 8643 | if (PyUnicode_GET_SIZE(x) != ylen) { |
| 8644 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 8645 | "arguments must have equal length"); |
| 8646 | goto err; |
| 8647 | } |
| 8648 | /* create entries for translating chars in x to those in y */ |
| 8649 | for (i = 0; i < PyUnicode_GET_SIZE(x); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8650 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(x)[i]); |
| 8651 | value = PyLong_FromLong(PyUnicode_AS_UNICODE(y)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8652 | if (!key || !value) |
| 8653 | goto err; |
| 8654 | res = PyDict_SetItem(new, key, value); |
| 8655 | Py_DECREF(key); |
| 8656 | Py_DECREF(value); |
| 8657 | if (res < 0) |
| 8658 | goto err; |
| 8659 | } |
| 8660 | /* create entries for deleting chars in z */ |
| 8661 | if (z != NULL) { |
| 8662 | for (i = 0; i < PyUnicode_GET_SIZE(z); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8663 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(z)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8664 | if (!key) |
| 8665 | goto err; |
| 8666 | res = PyDict_SetItem(new, key, Py_None); |
| 8667 | Py_DECREF(key); |
| 8668 | if (res < 0) |
| 8669 | goto err; |
| 8670 | } |
| 8671 | } |
| 8672 | } else { |
| 8673 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 8674 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8675 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 8676 | "to maketrans it must be a dict"); |
| 8677 | goto err; |
| 8678 | } |
| 8679 | /* copy entries into the new dict, converting string keys to int keys */ |
| 8680 | while (PyDict_Next(x, &i, &key, &value)) { |
| 8681 | if (PyUnicode_Check(key)) { |
| 8682 | /* convert string keys to integer keys */ |
| 8683 | PyObject *newkey; |
| 8684 | if (PyUnicode_GET_SIZE(key) != 1) { |
| 8685 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 8686 | "table must be of length 1"); |
| 8687 | goto err; |
| 8688 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8689 | newkey = PyLong_FromLong(PyUnicode_AS_UNICODE(key)[0]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8690 | if (!newkey) |
| 8691 | goto err; |
| 8692 | res = PyDict_SetItem(new, newkey, value); |
| 8693 | Py_DECREF(newkey); |
| 8694 | if (res < 0) |
| 8695 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8696 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8697 | /* just keep integer keys */ |
| 8698 | if (PyDict_SetItem(new, key, value) < 0) |
| 8699 | goto err; |
| 8700 | } else { |
| 8701 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 8702 | "be strings or integers"); |
| 8703 | goto err; |
| 8704 | } |
| 8705 | } |
| 8706 | } |
| 8707 | return new; |
| 8708 | err: |
| 8709 | Py_DECREF(new); |
| 8710 | return NULL; |
| 8711 | } |
| 8712 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8713 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8714 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8715 | \n\ |
| 8716 | Return a copy of the string S, where all characters have been mapped\n\ |
| 8717 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8718 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 8719 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 8720 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8721 | |
| 8722 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8723 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8724 | { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8725 | return PyUnicode_TranslateCharmap(self->str, self->length, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8726 | } |
| 8727 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8728 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8729 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8730 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8731 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8732 | |
| 8733 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8734 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8735 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8736 | return fixup(self, fixupper); |
| 8737 | } |
| 8738 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8739 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8740 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8741 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 8742 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 8743 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8744 | |
| 8745 | static PyObject * |
| 8746 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 8747 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8748 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8749 | PyUnicodeObject *u; |
| 8750 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8751 | Py_ssize_t width; |
| 8752 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8753 | return NULL; |
| 8754 | |
| 8755 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 8756 | if (PyUnicode_CheckExact(self)) { |
| 8757 | Py_INCREF(self); |
| 8758 | return (PyObject*) self; |
| 8759 | } |
| 8760 | else |
| 8761 | return PyUnicode_FromUnicode( |
| 8762 | PyUnicode_AS_UNICODE(self), |
| 8763 | PyUnicode_GET_SIZE(self) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8764 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8765 | } |
| 8766 | |
| 8767 | fill = width - self->length; |
| 8768 | |
| 8769 | u = pad(self, fill, 0, '0'); |
| 8770 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8771 | if (u == NULL) |
| 8772 | return NULL; |
| 8773 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8774 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 8775 | /* move sign to beginning of string */ |
| 8776 | u->str[0] = u->str[fill]; |
| 8777 | u->str[fill] = '0'; |
| 8778 | } |
| 8779 | |
| 8780 | return (PyObject*) u; |
| 8781 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8782 | |
| 8783 | #if 0 |
| 8784 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8785 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8786 | { |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 8787 | return PyLong_FromLong(numfree); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8788 | } |
| 8789 | #endif |
| 8790 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8791 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8792 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8793 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 8794 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 8795 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8796 | With optional end, stop comparing S at that position.\n\ |
| 8797 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8798 | |
| 8799 | static PyObject * |
| 8800 | unicode_startswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8801 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8802 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8803 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8804 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8805 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8806 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8807 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8808 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8809 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8810 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 8811 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8812 | if (PyTuple_Check(subobj)) { |
| 8813 | Py_ssize_t i; |
| 8814 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 8815 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8816 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8817 | if (substring == NULL) |
| 8818 | return NULL; |
| 8819 | result = tailmatch(self, substring, start, end, -1); |
| 8820 | Py_DECREF(substring); |
| 8821 | if (result) { |
| 8822 | Py_RETURN_TRUE; |
| 8823 | } |
| 8824 | } |
| 8825 | /* nothing matched */ |
| 8826 | Py_RETURN_FALSE; |
| 8827 | } |
| 8828 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8829 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8830 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8831 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8832 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8833 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8834 | } |
| 8835 | |
| 8836 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8837 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8838 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8839 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 8840 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 8841 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8842 | With optional end, stop comparing S at that position.\n\ |
| 8843 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8844 | |
| 8845 | static PyObject * |
| 8846 | unicode_endswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8847 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8848 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8849 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8850 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8851 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8852 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8853 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8854 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8855 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8856 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 8857 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8858 | if (PyTuple_Check(subobj)) { |
| 8859 | Py_ssize_t i; |
| 8860 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 8861 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8862 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8863 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8864 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8865 | result = tailmatch(self, substring, start, end, +1); |
| 8866 | Py_DECREF(substring); |
| 8867 | if (result) { |
| 8868 | Py_RETURN_TRUE; |
| 8869 | } |
| 8870 | } |
| 8871 | Py_RETURN_FALSE; |
| 8872 | } |
| 8873 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8874 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8875 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8876 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8877 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8878 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8879 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8880 | } |
| 8881 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8882 | #include "stringlib/string_format.h" |
| 8883 | |
| 8884 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8885 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8886 | \n\ |
| 8887 | "); |
| 8888 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 8889 | static PyObject * |
| 8890 | unicode__format__(PyObject* self, PyObject* args) |
| 8891 | { |
| 8892 | PyObject *format_spec; |
| 8893 | |
| 8894 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 8895 | return NULL; |
| 8896 | |
| 8897 | return _PyUnicode_FormatAdvanced(self, |
| 8898 | PyUnicode_AS_UNICODE(format_spec), |
| 8899 | PyUnicode_GET_SIZE(format_spec)); |
| 8900 | } |
| 8901 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8902 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8903 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8904 | \n\ |
| 8905 | "); |
| 8906 | |
| 8907 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8908 | unicode__sizeof__(PyUnicodeObject *v) |
| 8909 | { |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 8910 | return PyLong_FromSsize_t(sizeof(PyUnicodeObject) + |
| 8911 | sizeof(Py_UNICODE) * (v->length + 1)); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8912 | } |
| 8913 | |
| 8914 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8915 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8916 | |
| 8917 | static PyObject * |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8918 | unicode_getnewargs(PyUnicodeObject *v) |
| 8919 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8920 | return Py_BuildValue("(u#)", v->str, v->length); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8921 | } |
| 8922 | |
| 8923 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8924 | static PyMethodDef unicode_methods[] = { |
| 8925 | |
| 8926 | /* Order is according to common usage: often used methods should |
| 8927 | appear first, since lookup is done sequentially. */ |
| 8928 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 8929 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8930 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 8931 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8932 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8933 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 8934 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 8935 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 8936 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 8937 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 8938 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 8939 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8940 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8941 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 8942 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 8943 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8944 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8945 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 8946 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 8947 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8948 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8949 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8950 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8951 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8952 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 8953 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 8954 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 8955 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 8956 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 8957 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 8958 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 8959 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 8960 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 8961 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 8962 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 8963 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 8964 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 8965 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 8966 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8967 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8968 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 8969 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 8970 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Eric Smith | f6db409 | 2007-08-27 23:52:26 +0000 | [diff] [blame] | 8971 | {"_formatter_field_name_split", (PyCFunction) formatter_field_name_split, METH_NOARGS}, |
| 8972 | {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8973 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 8974 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8975 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8976 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8977 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8978 | #endif |
| 8979 | |
| 8980 | #if 0 |
| 8981 | /* This one is just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8982 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8983 | #endif |
| 8984 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8985 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8986 | {NULL, NULL} |
| 8987 | }; |
| 8988 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8989 | static PyObject * |
| 8990 | unicode_mod(PyObject *v, PyObject *w) |
| 8991 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8992 | if (!PyUnicode_Check(v)) { |
| 8993 | Py_INCREF(Py_NotImplemented); |
| 8994 | return Py_NotImplemented; |
| 8995 | } |
| 8996 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8997 | } |
| 8998 | |
| 8999 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9000 | 0, /*nb_add*/ |
| 9001 | 0, /*nb_subtract*/ |
| 9002 | 0, /*nb_multiply*/ |
| 9003 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 9004 | }; |
| 9005 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9006 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9007 | (lenfunc) unicode_length, /* sq_length */ |
| 9008 | PyUnicode_Concat, /* sq_concat */ |
| 9009 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 9010 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 9011 | 0, /* sq_slice */ |
| 9012 | 0, /* sq_ass_item */ |
| 9013 | 0, /* sq_ass_slice */ |
| 9014 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9015 | }; |
| 9016 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9017 | static PyObject* |
| 9018 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 9019 | { |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9020 | if (PyIndex_Check(item)) { |
| 9021 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9022 | if (i == -1 && PyErr_Occurred()) |
| 9023 | return NULL; |
| 9024 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 9025 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9026 | return unicode_getitem(self, i); |
| 9027 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9028 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9029 | Py_UNICODE* source_buf; |
| 9030 | Py_UNICODE* result_buf; |
| 9031 | PyObject* result; |
| 9032 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 9033 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9034 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9035 | return NULL; |
| 9036 | } |
| 9037 | |
| 9038 | if (slicelength <= 0) { |
| 9039 | return PyUnicode_FromUnicode(NULL, 0); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 9040 | } else if (start == 0 && step == 1 && slicelength == self->length && |
| 9041 | PyUnicode_CheckExact(self)) { |
| 9042 | Py_INCREF(self); |
| 9043 | return (PyObject *)self; |
| 9044 | } else if (step == 1) { |
| 9045 | return PyUnicode_FromUnicode(self->str + start, slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9046 | } else { |
| 9047 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 9048 | result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* |
| 9049 | sizeof(Py_UNICODE)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9050 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9051 | if (result_buf == NULL) |
| 9052 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9053 | |
| 9054 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 9055 | result_buf[i] = source_buf[cur]; |
| 9056 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9057 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9058 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 9059 | PyObject_FREE(result_buf); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9060 | return result; |
| 9061 | } |
| 9062 | } else { |
| 9063 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 9064 | return NULL; |
| 9065 | } |
| 9066 | } |
| 9067 | |
| 9068 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9069 | (lenfunc)unicode_length, /* mp_length */ |
| 9070 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 9071 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9072 | }; |
| 9073 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9074 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9075 | /* Helpers for PyUnicode_Format() */ |
| 9076 | |
| 9077 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9078 | 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] | 9079 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9080 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9081 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9082 | (*p_argidx)++; |
| 9083 | if (arglen < 0) |
| 9084 | return args; |
| 9085 | else |
| 9086 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9087 | } |
| 9088 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9089 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9090 | return NULL; |
| 9091 | } |
| 9092 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9093 | /* 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] | 9094 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9095 | static PyObject * |
| 9096 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9097 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9098 | char *p; |
| 9099 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9100 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9101 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9102 | x = PyFloat_AsDouble(v); |
| 9103 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9104 | return NULL; |
| 9105 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9106 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9107 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 9108 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 9109 | p = PyOS_double_to_string(x, type, prec, |
| 9110 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9111 | if (p == NULL) |
| 9112 | return NULL; |
| 9113 | result = PyUnicode_FromStringAndSize(p, strlen(p)); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 9114 | PyMem_Free(p); |
| 9115 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9116 | } |
| 9117 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9118 | static PyObject* |
| 9119 | formatlong(PyObject *val, int flags, int prec, int type) |
| 9120 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9121 | char *buf; |
| 9122 | int len; |
| 9123 | PyObject *str; /* temporary string object. */ |
| 9124 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9125 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9126 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 9127 | if (!str) |
| 9128 | return NULL; |
| 9129 | result = PyUnicode_FromStringAndSize(buf, len); |
| 9130 | Py_DECREF(str); |
| 9131 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9132 | } |
| 9133 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9134 | static int |
| 9135 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9136 | size_t buflen, |
| 9137 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9138 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 9139 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 9140 | if (PyUnicode_Check(v)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9141 | if (PyUnicode_GET_SIZE(v) == 1) { |
| 9142 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
| 9143 | buf[1] = '\0'; |
| 9144 | return 1; |
| 9145 | } |
| 9146 | #ifndef Py_UNICODE_WIDE |
| 9147 | if (PyUnicode_GET_SIZE(v) == 2) { |
| 9148 | /* Decode a valid surrogate pair */ |
| 9149 | int c0 = PyUnicode_AS_UNICODE(v)[0]; |
| 9150 | int c1 = PyUnicode_AS_UNICODE(v)[1]; |
| 9151 | if (0xD800 <= c0 && c0 <= 0xDBFF && |
| 9152 | 0xDC00 <= c1 && c1 <= 0xDFFF) { |
| 9153 | buf[0] = c0; |
| 9154 | buf[1] = c1; |
| 9155 | buf[2] = '\0'; |
| 9156 | return 2; |
| 9157 | } |
| 9158 | } |
| 9159 | #endif |
| 9160 | goto onError; |
| 9161 | } |
| 9162 | else { |
| 9163 | /* Integer input truncated to a character */ |
| 9164 | long x; |
| 9165 | x = PyLong_AsLong(v); |
| 9166 | if (x == -1 && PyErr_Occurred()) |
| 9167 | goto onError; |
| 9168 | |
| 9169 | if (x < 0 || x > 0x10ffff) { |
| 9170 | PyErr_SetString(PyExc_OverflowError, |
| 9171 | "%c arg not in range(0x110000)"); |
| 9172 | return -1; |
| 9173 | } |
| 9174 | |
| 9175 | #ifndef Py_UNICODE_WIDE |
| 9176 | if (x > 0xffff) { |
| 9177 | x -= 0x10000; |
| 9178 | buf[0] = (Py_UNICODE)(0xD800 | (x >> 10)); |
| 9179 | buf[1] = (Py_UNICODE)(0xDC00 | (x & 0x3FF)); |
| 9180 | return 2; |
| 9181 | } |
| 9182 | #endif |
| 9183 | buf[0] = (Py_UNICODE) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9184 | buf[1] = '\0'; |
| 9185 | return 1; |
| 9186 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 9187 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9188 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 9189 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9190 | "%c requires int or char"); |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 9191 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9192 | } |
| 9193 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9194 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9195 | 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] | 9196 | */ |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9197 | #define FORMATBUFLEN (size_t)10 |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9198 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9199 | PyObject *PyUnicode_Format(PyObject *format, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9200 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9201 | { |
| 9202 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9203 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9204 | int args_owned = 0; |
| 9205 | PyUnicodeObject *result = NULL; |
| 9206 | PyObject *dict = NULL; |
| 9207 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9208 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9209 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9210 | PyErr_BadInternalCall(); |
| 9211 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9212 | } |
| 9213 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 9214 | if (uformat == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9215 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9216 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 9217 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 9218 | |
| 9219 | reslen = rescnt = fmtcnt + 100; |
| 9220 | result = _PyUnicode_New(reslen); |
| 9221 | if (result == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9222 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9223 | res = PyUnicode_AS_UNICODE(result); |
| 9224 | |
| 9225 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9226 | arglen = PyTuple_Size(args); |
| 9227 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9228 | } |
| 9229 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9230 | arglen = -1; |
| 9231 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9232 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 9233 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 9234 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9235 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9236 | |
| 9237 | while (--fmtcnt >= 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9238 | if (*fmt != '%') { |
| 9239 | if (--rescnt < 0) { |
| 9240 | rescnt = fmtcnt + 100; |
| 9241 | reslen += rescnt; |
| 9242 | if (_PyUnicode_Resize(&result, reslen) < 0) |
| 9243 | goto onError; |
| 9244 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 9245 | --rescnt; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9246 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9247 | *res++ = *fmt++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9248 | } |
| 9249 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9250 | /* Got a format specifier */ |
| 9251 | int flags = 0; |
| 9252 | Py_ssize_t width = -1; |
| 9253 | int prec = -1; |
| 9254 | Py_UNICODE c = '\0'; |
| 9255 | Py_UNICODE fill; |
| 9256 | int isnumok; |
| 9257 | PyObject *v = NULL; |
| 9258 | PyObject *temp = NULL; |
| 9259 | Py_UNICODE *pbuf; |
| 9260 | Py_UNICODE sign; |
| 9261 | Py_ssize_t len; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9262 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For formatchar() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9263 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9264 | fmt++; |
| 9265 | if (*fmt == '(') { |
| 9266 | Py_UNICODE *keystart; |
| 9267 | Py_ssize_t keylen; |
| 9268 | PyObject *key; |
| 9269 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 9270 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9271 | if (dict == NULL) { |
| 9272 | PyErr_SetString(PyExc_TypeError, |
| 9273 | "format requires a mapping"); |
| 9274 | goto onError; |
| 9275 | } |
| 9276 | ++fmt; |
| 9277 | --fmtcnt; |
| 9278 | keystart = fmt; |
| 9279 | /* Skip over balanced parentheses */ |
| 9280 | while (pcount > 0 && --fmtcnt >= 0) { |
| 9281 | if (*fmt == ')') |
| 9282 | --pcount; |
| 9283 | else if (*fmt == '(') |
| 9284 | ++pcount; |
| 9285 | fmt++; |
| 9286 | } |
| 9287 | keylen = fmt - keystart - 1; |
| 9288 | if (fmtcnt < 0 || pcount > 0) { |
| 9289 | PyErr_SetString(PyExc_ValueError, |
| 9290 | "incomplete format key"); |
| 9291 | goto onError; |
| 9292 | } |
| 9293 | #if 0 |
| 9294 | /* keys are converted to strings using UTF-8 and |
| 9295 | then looked up since Python uses strings to hold |
| 9296 | variables names etc. in its namespaces and we |
| 9297 | wouldn't want to break common idioms. */ |
| 9298 | key = PyUnicode_EncodeUTF8(keystart, |
| 9299 | keylen, |
| 9300 | NULL); |
| 9301 | #else |
| 9302 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 9303 | #endif |
| 9304 | if (key == NULL) |
| 9305 | goto onError; |
| 9306 | if (args_owned) { |
| 9307 | Py_DECREF(args); |
| 9308 | args_owned = 0; |
| 9309 | } |
| 9310 | args = PyObject_GetItem(dict, key); |
| 9311 | Py_DECREF(key); |
| 9312 | if (args == NULL) { |
| 9313 | goto onError; |
| 9314 | } |
| 9315 | args_owned = 1; |
| 9316 | arglen = -1; |
| 9317 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9318 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9319 | while (--fmtcnt >= 0) { |
| 9320 | switch (c = *fmt++) { |
| 9321 | case '-': flags |= F_LJUST; continue; |
| 9322 | case '+': flags |= F_SIGN; continue; |
| 9323 | case ' ': flags |= F_BLANK; continue; |
| 9324 | case '#': flags |= F_ALT; continue; |
| 9325 | case '0': flags |= F_ZERO; continue; |
| 9326 | } |
| 9327 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9328 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9329 | if (c == '*') { |
| 9330 | v = getnextarg(args, arglen, &argidx); |
| 9331 | if (v == NULL) |
| 9332 | goto onError; |
| 9333 | if (!PyLong_Check(v)) { |
| 9334 | PyErr_SetString(PyExc_TypeError, |
| 9335 | "* wants int"); |
| 9336 | goto onError; |
| 9337 | } |
| 9338 | width = PyLong_AsLong(v); |
| 9339 | if (width == -1 && PyErr_Occurred()) |
| 9340 | goto onError; |
| 9341 | if (width < 0) { |
| 9342 | flags |= F_LJUST; |
| 9343 | width = -width; |
| 9344 | } |
| 9345 | if (--fmtcnt >= 0) |
| 9346 | c = *fmt++; |
| 9347 | } |
| 9348 | else if (c >= '0' && c <= '9') { |
| 9349 | width = c - '0'; |
| 9350 | while (--fmtcnt >= 0) { |
| 9351 | c = *fmt++; |
| 9352 | if (c < '0' || c > '9') |
| 9353 | break; |
| 9354 | if ((width*10) / 10 != width) { |
| 9355 | PyErr_SetString(PyExc_ValueError, |
| 9356 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9357 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9358 | } |
| 9359 | width = width*10 + (c - '0'); |
| 9360 | } |
| 9361 | } |
| 9362 | if (c == '.') { |
| 9363 | prec = 0; |
| 9364 | if (--fmtcnt >= 0) |
| 9365 | c = *fmt++; |
| 9366 | if (c == '*') { |
| 9367 | v = getnextarg(args, arglen, &argidx); |
| 9368 | if (v == NULL) |
| 9369 | goto onError; |
| 9370 | if (!PyLong_Check(v)) { |
| 9371 | PyErr_SetString(PyExc_TypeError, |
| 9372 | "* wants int"); |
| 9373 | goto onError; |
| 9374 | } |
| 9375 | prec = PyLong_AsLong(v); |
| 9376 | if (prec == -1 && PyErr_Occurred()) |
| 9377 | goto onError; |
| 9378 | if (prec < 0) |
| 9379 | prec = 0; |
| 9380 | if (--fmtcnt >= 0) |
| 9381 | c = *fmt++; |
| 9382 | } |
| 9383 | else if (c >= '0' && c <= '9') { |
| 9384 | prec = c - '0'; |
| 9385 | while (--fmtcnt >= 0) { |
Stefan Krah | 99212f6 | 2010-07-19 17:58:26 +0000 | [diff] [blame] | 9386 | c = *fmt++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9387 | if (c < '0' || c > '9') |
| 9388 | break; |
| 9389 | if ((prec*10) / 10 != prec) { |
| 9390 | PyErr_SetString(PyExc_ValueError, |
| 9391 | "prec too big"); |
| 9392 | goto onError; |
| 9393 | } |
| 9394 | prec = prec*10 + (c - '0'); |
| 9395 | } |
| 9396 | } |
| 9397 | } /* prec */ |
| 9398 | if (fmtcnt >= 0) { |
| 9399 | if (c == 'h' || c == 'l' || c == 'L') { |
| 9400 | if (--fmtcnt >= 0) |
| 9401 | c = *fmt++; |
| 9402 | } |
| 9403 | } |
| 9404 | if (fmtcnt < 0) { |
| 9405 | PyErr_SetString(PyExc_ValueError, |
| 9406 | "incomplete format"); |
| 9407 | goto onError; |
| 9408 | } |
| 9409 | if (c != '%') { |
| 9410 | v = getnextarg(args, arglen, &argidx); |
| 9411 | if (v == NULL) |
| 9412 | goto onError; |
| 9413 | } |
| 9414 | sign = 0; |
| 9415 | fill = ' '; |
| 9416 | switch (c) { |
| 9417 | |
| 9418 | case '%': |
| 9419 | pbuf = formatbuf; |
| 9420 | /* presume that buffer length is at least 1 */ |
| 9421 | pbuf[0] = '%'; |
| 9422 | len = 1; |
| 9423 | break; |
| 9424 | |
| 9425 | case 's': |
| 9426 | case 'r': |
| 9427 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 9428 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9429 | temp = v; |
| 9430 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9431 | } |
| 9432 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9433 | if (c == 's') |
| 9434 | temp = PyObject_Str(v); |
| 9435 | else if (c == 'r') |
| 9436 | temp = PyObject_Repr(v); |
| 9437 | else |
| 9438 | temp = PyObject_ASCII(v); |
| 9439 | if (temp == NULL) |
| 9440 | goto onError; |
| 9441 | if (PyUnicode_Check(temp)) |
| 9442 | /* nothing to do */; |
| 9443 | else { |
| 9444 | Py_DECREF(temp); |
| 9445 | PyErr_SetString(PyExc_TypeError, |
| 9446 | "%s argument has non-string str()"); |
| 9447 | goto onError; |
| 9448 | } |
| 9449 | } |
| 9450 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9451 | len = PyUnicode_GET_SIZE(temp); |
| 9452 | if (prec >= 0 && len > prec) |
| 9453 | len = prec; |
| 9454 | break; |
| 9455 | |
| 9456 | case 'i': |
| 9457 | case 'd': |
| 9458 | case 'u': |
| 9459 | case 'o': |
| 9460 | case 'x': |
| 9461 | case 'X': |
| 9462 | if (c == 'i') |
| 9463 | c = 'd'; |
| 9464 | isnumok = 0; |
| 9465 | if (PyNumber_Check(v)) { |
| 9466 | PyObject *iobj=NULL; |
| 9467 | |
| 9468 | if (PyLong_Check(v)) { |
| 9469 | iobj = v; |
| 9470 | Py_INCREF(iobj); |
| 9471 | } |
| 9472 | else { |
| 9473 | iobj = PyNumber_Long(v); |
| 9474 | } |
| 9475 | if (iobj!=NULL) { |
| 9476 | if (PyLong_Check(iobj)) { |
| 9477 | isnumok = 1; |
| 9478 | temp = formatlong(iobj, flags, prec, c); |
| 9479 | Py_DECREF(iobj); |
| 9480 | if (!temp) |
| 9481 | goto onError; |
| 9482 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9483 | len = PyUnicode_GET_SIZE(temp); |
| 9484 | sign = 1; |
| 9485 | } |
| 9486 | else { |
| 9487 | Py_DECREF(iobj); |
| 9488 | } |
| 9489 | } |
| 9490 | } |
| 9491 | if (!isnumok) { |
| 9492 | PyErr_Format(PyExc_TypeError, |
| 9493 | "%%%c format: a number is required, " |
| 9494 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 9495 | goto onError; |
| 9496 | } |
| 9497 | if (flags & F_ZERO) |
| 9498 | fill = '0'; |
| 9499 | break; |
| 9500 | |
| 9501 | case 'e': |
| 9502 | case 'E': |
| 9503 | case 'f': |
| 9504 | case 'F': |
| 9505 | case 'g': |
| 9506 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9507 | temp = formatfloat(v, flags, prec, c); |
| 9508 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9509 | goto onError; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9510 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9511 | len = PyUnicode_GET_SIZE(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9512 | sign = 1; |
| 9513 | if (flags & F_ZERO) |
| 9514 | fill = '0'; |
| 9515 | break; |
| 9516 | |
| 9517 | case 'c': |
| 9518 | pbuf = formatbuf; |
| 9519 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
| 9520 | if (len < 0) |
| 9521 | goto onError; |
| 9522 | break; |
| 9523 | |
| 9524 | default: |
| 9525 | PyErr_Format(PyExc_ValueError, |
| 9526 | "unsupported format character '%c' (0x%x) " |
| 9527 | "at index %zd", |
| 9528 | (31<=c && c<=126) ? (char)c : '?', |
| 9529 | (int)c, |
| 9530 | (Py_ssize_t)(fmt - 1 - |
| 9531 | PyUnicode_AS_UNICODE(uformat))); |
| 9532 | goto onError; |
| 9533 | } |
| 9534 | if (sign) { |
| 9535 | if (*pbuf == '-' || *pbuf == '+') { |
| 9536 | sign = *pbuf++; |
| 9537 | len--; |
| 9538 | } |
| 9539 | else if (flags & F_SIGN) |
| 9540 | sign = '+'; |
| 9541 | else if (flags & F_BLANK) |
| 9542 | sign = ' '; |
| 9543 | else |
| 9544 | sign = 0; |
| 9545 | } |
| 9546 | if (width < len) |
| 9547 | width = len; |
| 9548 | if (rescnt - (sign != 0) < width) { |
| 9549 | reslen -= rescnt; |
| 9550 | rescnt = width + fmtcnt + 100; |
| 9551 | reslen += rescnt; |
| 9552 | if (reslen < 0) { |
| 9553 | Py_XDECREF(temp); |
| 9554 | PyErr_NoMemory(); |
| 9555 | goto onError; |
| 9556 | } |
| 9557 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 9558 | Py_XDECREF(temp); |
| 9559 | goto onError; |
| 9560 | } |
| 9561 | res = PyUnicode_AS_UNICODE(result) |
| 9562 | + reslen - rescnt; |
| 9563 | } |
| 9564 | if (sign) { |
| 9565 | if (fill != ' ') |
| 9566 | *res++ = sign; |
| 9567 | rescnt--; |
| 9568 | if (width > len) |
| 9569 | width--; |
| 9570 | } |
| 9571 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
| 9572 | assert(pbuf[0] == '0'); |
| 9573 | assert(pbuf[1] == c); |
| 9574 | if (fill != ' ') { |
| 9575 | *res++ = *pbuf++; |
| 9576 | *res++ = *pbuf++; |
| 9577 | } |
| 9578 | rescnt -= 2; |
| 9579 | width -= 2; |
| 9580 | if (width < 0) |
| 9581 | width = 0; |
| 9582 | len -= 2; |
| 9583 | } |
| 9584 | if (width > len && !(flags & F_LJUST)) { |
| 9585 | do { |
| 9586 | --rescnt; |
| 9587 | *res++ = fill; |
| 9588 | } while (--width > len); |
| 9589 | } |
| 9590 | if (fill == ' ') { |
| 9591 | if (sign) |
| 9592 | *res++ = sign; |
| 9593 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
| 9594 | assert(pbuf[0] == '0'); |
| 9595 | assert(pbuf[1] == c); |
| 9596 | *res++ = *pbuf++; |
| 9597 | *res++ = *pbuf++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9598 | } |
| 9599 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9600 | Py_UNICODE_COPY(res, pbuf, len); |
| 9601 | res += len; |
| 9602 | rescnt -= len; |
| 9603 | while (--width >= len) { |
| 9604 | --rescnt; |
| 9605 | *res++ = ' '; |
| 9606 | } |
| 9607 | if (dict && (argidx < arglen) && c != '%') { |
| 9608 | PyErr_SetString(PyExc_TypeError, |
| 9609 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9610 | Py_XDECREF(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9611 | goto onError; |
| 9612 | } |
| 9613 | Py_XDECREF(temp); |
| 9614 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9615 | } /* until end */ |
| 9616 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9617 | PyErr_SetString(PyExc_TypeError, |
| 9618 | "not all arguments converted during string formatting"); |
| 9619 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9620 | } |
| 9621 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9622 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9623 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9624 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9625 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9626 | } |
| 9627 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9628 | return (PyObject *)result; |
| 9629 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9630 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9631 | Py_XDECREF(result); |
| 9632 | Py_DECREF(uformat); |
| 9633 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9634 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9635 | } |
| 9636 | return NULL; |
| 9637 | } |
| 9638 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 9639 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9640 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 9641 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9642 | static PyObject * |
| 9643 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9644 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9645 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9646 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 9647 | char *encoding = NULL; |
| 9648 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9649 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9650 | if (type != &PyUnicode_Type) |
| 9651 | return unicode_subtype_new(type, args, kwds); |
| 9652 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9653 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9654 | return NULL; |
| 9655 | if (x == NULL) |
| 9656 | return (PyObject *)_PyUnicode_New(0); |
| 9657 | if (encoding == NULL && errors == NULL) |
| 9658 | return PyObject_Str(x); |
| 9659 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9660 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9661 | } |
| 9662 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9663 | static PyObject * |
| 9664 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9665 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9666 | PyUnicodeObject *tmp, *pnew; |
| 9667 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9668 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9669 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 9670 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 9671 | if (tmp == NULL) |
| 9672 | return NULL; |
| 9673 | assert(PyUnicode_Check(tmp)); |
| 9674 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
| 9675 | if (pnew == NULL) { |
| 9676 | Py_DECREF(tmp); |
| 9677 | return NULL; |
| 9678 | } |
| 9679 | pnew->str = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1)); |
| 9680 | if (pnew->str == NULL) { |
| 9681 | _Py_ForgetReference((PyObject *)pnew); |
| 9682 | PyObject_Del(pnew); |
| 9683 | Py_DECREF(tmp); |
| 9684 | return PyErr_NoMemory(); |
| 9685 | } |
| 9686 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 9687 | pnew->length = n; |
| 9688 | pnew->hash = tmp->hash; |
| 9689 | Py_DECREF(tmp); |
| 9690 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9691 | } |
| 9692 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9693 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9694 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9695 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 9696 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 9697 | encoding defaults to the current default string encoding.\n\ |
| 9698 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9699 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9700 | static PyObject *unicode_iter(PyObject *seq); |
| 9701 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9702 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 9703 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9704 | "str", /* tp_name */ |
| 9705 | sizeof(PyUnicodeObject), /* tp_size */ |
| 9706 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9707 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9708 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 9709 | 0, /* tp_print */ |
| 9710 | 0, /* tp_getattr */ |
| 9711 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 9712 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9713 | unicode_repr, /* tp_repr */ |
| 9714 | &unicode_as_number, /* tp_as_number */ |
| 9715 | &unicode_as_sequence, /* tp_as_sequence */ |
| 9716 | &unicode_as_mapping, /* tp_as_mapping */ |
| 9717 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 9718 | 0, /* tp_call*/ |
| 9719 | (reprfunc) unicode_str, /* tp_str */ |
| 9720 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9721 | 0, /* tp_setattro */ |
| 9722 | 0, /* tp_as_buffer */ |
| 9723 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9724 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9725 | unicode_doc, /* tp_doc */ |
| 9726 | 0, /* tp_traverse */ |
| 9727 | 0, /* tp_clear */ |
| 9728 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 9729 | 0, /* tp_weaklistoffset */ |
| 9730 | unicode_iter, /* tp_iter */ |
| 9731 | 0, /* tp_iternext */ |
| 9732 | unicode_methods, /* tp_methods */ |
| 9733 | 0, /* tp_members */ |
| 9734 | 0, /* tp_getset */ |
| 9735 | &PyBaseObject_Type, /* tp_base */ |
| 9736 | 0, /* tp_dict */ |
| 9737 | 0, /* tp_descr_get */ |
| 9738 | 0, /* tp_descr_set */ |
| 9739 | 0, /* tp_dictoffset */ |
| 9740 | 0, /* tp_init */ |
| 9741 | 0, /* tp_alloc */ |
| 9742 | unicode_new, /* tp_new */ |
| 9743 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9744 | }; |
| 9745 | |
| 9746 | /* Initialize the Unicode implementation */ |
| 9747 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9748 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9749 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9750 | int i; |
| 9751 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9752 | /* XXX - move this array to unicodectype.c ? */ |
| 9753 | Py_UNICODE linebreak[] = { |
| 9754 | 0x000A, /* LINE FEED */ |
| 9755 | 0x000D, /* CARRIAGE RETURN */ |
| 9756 | 0x001C, /* FILE SEPARATOR */ |
| 9757 | 0x001D, /* GROUP SEPARATOR */ |
| 9758 | 0x001E, /* RECORD SEPARATOR */ |
| 9759 | 0x0085, /* NEXT LINE */ |
| 9760 | 0x2028, /* LINE SEPARATOR */ |
| 9761 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 9762 | }; |
| 9763 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 9764 | /* Init the implementation */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 9765 | free_list = NULL; |
| 9766 | numfree = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9767 | unicode_empty = _PyUnicode_New(0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9768 | if (!unicode_empty) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9769 | return; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9770 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9771 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9772 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 9773 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9774 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9775 | |
| 9776 | /* initialize the linebreak bloom filter */ |
| 9777 | bloom_linebreak = make_bloom_mask( |
| 9778 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 9779 | ); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9780 | |
| 9781 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9782 | } |
| 9783 | |
| 9784 | /* Finalize the Unicode implementation */ |
| 9785 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9786 | int |
| 9787 | PyUnicode_ClearFreeList(void) |
| 9788 | { |
| 9789 | int freelist_size = numfree; |
| 9790 | PyUnicodeObject *u; |
| 9791 | |
| 9792 | for (u = free_list; u != NULL;) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9793 | PyUnicodeObject *v = u; |
| 9794 | u = *(PyUnicodeObject **)u; |
| 9795 | if (v->str) |
| 9796 | PyObject_DEL(v->str); |
| 9797 | Py_XDECREF(v->defenc); |
| 9798 | PyObject_Del(v); |
| 9799 | numfree--; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9800 | } |
| 9801 | free_list = NULL; |
| 9802 | assert(numfree == 0); |
| 9803 | return freelist_size; |
| 9804 | } |
| 9805 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9806 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9807 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9808 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9809 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9810 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 9811 | Py_XDECREF(unicode_empty); |
| 9812 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 9813 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9814 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9815 | if (unicode_latin1[i]) { |
| 9816 | Py_DECREF(unicode_latin1[i]); |
| 9817 | unicode_latin1[i] = NULL; |
| 9818 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9819 | } |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9820 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9821 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9822 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9823 | void |
| 9824 | PyUnicode_InternInPlace(PyObject **p) |
| 9825 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9826 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 9827 | PyObject *t; |
| 9828 | if (s == NULL || !PyUnicode_Check(s)) |
| 9829 | Py_FatalError( |
| 9830 | "PyUnicode_InternInPlace: unicode strings only please!"); |
| 9831 | /* If it's a subclass, we don't really know what putting |
| 9832 | it in the interned dict might do. */ |
| 9833 | if (!PyUnicode_CheckExact(s)) |
| 9834 | return; |
| 9835 | if (PyUnicode_CHECK_INTERNED(s)) |
| 9836 | return; |
| 9837 | if (interned == NULL) { |
| 9838 | interned = PyDict_New(); |
| 9839 | if (interned == NULL) { |
| 9840 | PyErr_Clear(); /* Don't leave an exception */ |
| 9841 | return; |
| 9842 | } |
| 9843 | } |
| 9844 | /* It might be that the GetItem call fails even |
| 9845 | though the key is present in the dictionary, |
| 9846 | namely when this happens during a stack overflow. */ |
| 9847 | Py_ALLOW_RECURSION |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9848 | t = PyDict_GetItem(interned, (PyObject *)s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9849 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9850 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9851 | if (t) { |
| 9852 | Py_INCREF(t); |
| 9853 | Py_DECREF(*p); |
| 9854 | *p = t; |
| 9855 | return; |
| 9856 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9857 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9858 | PyThreadState_GET()->recursion_critical = 1; |
| 9859 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 9860 | PyErr_Clear(); |
| 9861 | PyThreadState_GET()->recursion_critical = 0; |
| 9862 | return; |
| 9863 | } |
| 9864 | PyThreadState_GET()->recursion_critical = 0; |
| 9865 | /* The two references in interned are not counted by refcnt. |
| 9866 | The deallocator will take care of this */ |
| 9867 | Py_REFCNT(s) -= 2; |
| 9868 | PyUnicode_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9869 | } |
| 9870 | |
| 9871 | void |
| 9872 | PyUnicode_InternImmortal(PyObject **p) |
| 9873 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9874 | PyUnicode_InternInPlace(p); |
| 9875 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
| 9876 | PyUnicode_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL; |
| 9877 | Py_INCREF(*p); |
| 9878 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9879 | } |
| 9880 | |
| 9881 | PyObject * |
| 9882 | PyUnicode_InternFromString(const char *cp) |
| 9883 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9884 | PyObject *s = PyUnicode_FromString(cp); |
| 9885 | if (s == NULL) |
| 9886 | return NULL; |
| 9887 | PyUnicode_InternInPlace(&s); |
| 9888 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9889 | } |
| 9890 | |
| 9891 | void _Py_ReleaseInternedUnicodeStrings(void) |
| 9892 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9893 | PyObject *keys; |
| 9894 | PyUnicodeObject *s; |
| 9895 | Py_ssize_t i, n; |
| 9896 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9897 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9898 | if (interned == NULL || !PyDict_Check(interned)) |
| 9899 | return; |
| 9900 | keys = PyDict_Keys(interned); |
| 9901 | if (keys == NULL || !PyList_Check(keys)) { |
| 9902 | PyErr_Clear(); |
| 9903 | return; |
| 9904 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9905 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9906 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 9907 | detector, interned unicode strings are not forcibly deallocated; |
| 9908 | rather, we give them their stolen references back, and then clear |
| 9909 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9910 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9911 | n = PyList_GET_SIZE(keys); |
| 9912 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9913 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9914 | for (i = 0; i < n; i++) { |
| 9915 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
| 9916 | switch (s->state) { |
| 9917 | case SSTATE_NOT_INTERNED: |
| 9918 | /* XXX Shouldn't happen */ |
| 9919 | break; |
| 9920 | case SSTATE_INTERNED_IMMORTAL: |
| 9921 | Py_REFCNT(s) += 1; |
| 9922 | immortal_size += s->length; |
| 9923 | break; |
| 9924 | case SSTATE_INTERNED_MORTAL: |
| 9925 | Py_REFCNT(s) += 2; |
| 9926 | mortal_size += s->length; |
| 9927 | break; |
| 9928 | default: |
| 9929 | Py_FatalError("Inconsistent interned string state."); |
| 9930 | } |
| 9931 | s->state = SSTATE_NOT_INTERNED; |
| 9932 | } |
| 9933 | fprintf(stderr, "total size of all interned strings: " |
| 9934 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 9935 | "mortal/immortal\n", mortal_size, immortal_size); |
| 9936 | Py_DECREF(keys); |
| 9937 | PyDict_Clear(interned); |
| 9938 | Py_DECREF(interned); |
| 9939 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9940 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9941 | |
| 9942 | |
| 9943 | /********************* Unicode Iterator **************************/ |
| 9944 | |
| 9945 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9946 | PyObject_HEAD |
| 9947 | Py_ssize_t it_index; |
| 9948 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9949 | } unicodeiterobject; |
| 9950 | |
| 9951 | static void |
| 9952 | unicodeiter_dealloc(unicodeiterobject *it) |
| 9953 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9954 | _PyObject_GC_UNTRACK(it); |
| 9955 | Py_XDECREF(it->it_seq); |
| 9956 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9957 | } |
| 9958 | |
| 9959 | static int |
| 9960 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 9961 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9962 | Py_VISIT(it->it_seq); |
| 9963 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9964 | } |
| 9965 | |
| 9966 | static PyObject * |
| 9967 | unicodeiter_next(unicodeiterobject *it) |
| 9968 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9969 | PyUnicodeObject *seq; |
| 9970 | PyObject *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9971 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9972 | assert(it != NULL); |
| 9973 | seq = it->it_seq; |
| 9974 | if (seq == NULL) |
| 9975 | return NULL; |
| 9976 | assert(PyUnicode_Check(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9977 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9978 | if (it->it_index < PyUnicode_GET_SIZE(seq)) { |
| 9979 | item = PyUnicode_FromUnicode( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9980 | PyUnicode_AS_UNICODE(seq)+it->it_index, 1); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9981 | if (item != NULL) |
| 9982 | ++it->it_index; |
| 9983 | return item; |
| 9984 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9985 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9986 | Py_DECREF(seq); |
| 9987 | it->it_seq = NULL; |
| 9988 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9989 | } |
| 9990 | |
| 9991 | static PyObject * |
| 9992 | unicodeiter_len(unicodeiterobject *it) |
| 9993 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9994 | Py_ssize_t len = 0; |
| 9995 | if (it->it_seq) |
| 9996 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
| 9997 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9998 | } |
| 9999 | |
| 10000 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 10001 | |
| 10002 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10003 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10004 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10005 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10006 | }; |
| 10007 | |
| 10008 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10009 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 10010 | "str_iterator", /* tp_name */ |
| 10011 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 10012 | 0, /* tp_itemsize */ |
| 10013 | /* methods */ |
| 10014 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 10015 | 0, /* tp_print */ |
| 10016 | 0, /* tp_getattr */ |
| 10017 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 10018 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10019 | 0, /* tp_repr */ |
| 10020 | 0, /* tp_as_number */ |
| 10021 | 0, /* tp_as_sequence */ |
| 10022 | 0, /* tp_as_mapping */ |
| 10023 | 0, /* tp_hash */ |
| 10024 | 0, /* tp_call */ |
| 10025 | 0, /* tp_str */ |
| 10026 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 10027 | 0, /* tp_setattro */ |
| 10028 | 0, /* tp_as_buffer */ |
| 10029 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 10030 | 0, /* tp_doc */ |
| 10031 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 10032 | 0, /* tp_clear */ |
| 10033 | 0, /* tp_richcompare */ |
| 10034 | 0, /* tp_weaklistoffset */ |
| 10035 | PyObject_SelfIter, /* tp_iter */ |
| 10036 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 10037 | unicodeiter_methods, /* tp_methods */ |
| 10038 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10039 | }; |
| 10040 | |
| 10041 | static PyObject * |
| 10042 | unicode_iter(PyObject *seq) |
| 10043 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10044 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10045 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10046 | if (!PyUnicode_Check(seq)) { |
| 10047 | PyErr_BadInternalCall(); |
| 10048 | return NULL; |
| 10049 | } |
| 10050 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 10051 | if (it == NULL) |
| 10052 | return NULL; |
| 10053 | it->it_index = 0; |
| 10054 | Py_INCREF(seq); |
| 10055 | it->it_seq = (PyUnicodeObject *)seq; |
| 10056 | _PyObject_GC_TRACK(it); |
| 10057 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10058 | } |
| 10059 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10060 | size_t |
| 10061 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 10062 | { |
| 10063 | int res = 0; |
| 10064 | while(*u++) |
| 10065 | res++; |
| 10066 | return res; |
| 10067 | } |
| 10068 | |
| 10069 | Py_UNICODE* |
| 10070 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 10071 | { |
| 10072 | Py_UNICODE *u = s1; |
| 10073 | while ((*u++ = *s2++)); |
| 10074 | return s1; |
| 10075 | } |
| 10076 | |
| 10077 | Py_UNICODE* |
| 10078 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 10079 | { |
| 10080 | Py_UNICODE *u = s1; |
| 10081 | while ((*u++ = *s2++)) |
| 10082 | if (n-- == 0) |
| 10083 | break; |
| 10084 | return s1; |
| 10085 | } |
| 10086 | |
Victor Stinner | c4eb765 | 2010-09-01 23:43:50 +0000 | [diff] [blame] | 10087 | Py_UNICODE* |
| 10088 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 10089 | { |
| 10090 | Py_UNICODE *u1 = s1; |
| 10091 | u1 += Py_UNICODE_strlen(u1); |
| 10092 | Py_UNICODE_strcpy(u1, s2); |
| 10093 | return s1; |
| 10094 | } |
| 10095 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10096 | int |
| 10097 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 10098 | { |
| 10099 | while (*s1 && *s2 && *s1 == *s2) |
| 10100 | s1++, s2++; |
| 10101 | if (*s1 && *s2) |
| 10102 | return (*s1 < *s2) ? -1 : +1; |
| 10103 | if (*s1) |
| 10104 | return 1; |
| 10105 | if (*s2) |
| 10106 | return -1; |
| 10107 | return 0; |
| 10108 | } |
| 10109 | |
Victor Stinner | ef8d95c | 2010-08-16 22:03:11 +0000 | [diff] [blame] | 10110 | int |
| 10111 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 10112 | { |
| 10113 | register Py_UNICODE u1, u2; |
| 10114 | for (; n != 0; n--) { |
| 10115 | u1 = *s1; |
| 10116 | u2 = *s2; |
| 10117 | if (u1 != u2) |
| 10118 | return (u1 < u2) ? -1 : +1; |
| 10119 | if (u1 == '\0') |
| 10120 | return 0; |
| 10121 | s1++; |
| 10122 | s2++; |
| 10123 | } |
| 10124 | return 0; |
| 10125 | } |
| 10126 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10127 | Py_UNICODE* |
| 10128 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 10129 | { |
| 10130 | const Py_UNICODE *p; |
| 10131 | for (p = s; *p; p++) |
| 10132 | if (*p == c) |
| 10133 | return (Py_UNICODE*)p; |
| 10134 | return NULL; |
| 10135 | } |
| 10136 | |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 10137 | Py_UNICODE* |
| 10138 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 10139 | { |
| 10140 | const Py_UNICODE *p; |
| 10141 | p = s + Py_UNICODE_strlen(s); |
| 10142 | while (p != s) { |
| 10143 | p--; |
| 10144 | if (*p == c) |
| 10145 | return (Py_UNICODE*)p; |
| 10146 | } |
| 10147 | return NULL; |
| 10148 | } |
| 10149 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 10150 | Py_UNICODE* |
Victor Stinner | 4640860 | 2010-09-03 16:18:00 +0000 | [diff] [blame] | 10151 | PyUnicode_AsUnicodeCopy(PyObject *object) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 10152 | { |
| 10153 | PyUnicodeObject *unicode = (PyUnicodeObject *)object; |
| 10154 | Py_UNICODE *copy; |
| 10155 | Py_ssize_t size; |
| 10156 | |
| 10157 | /* Ensure we won't overflow the size. */ |
| 10158 | if (PyUnicode_GET_SIZE(unicode) > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 10159 | PyErr_NoMemory(); |
| 10160 | return NULL; |
| 10161 | } |
| 10162 | size = PyUnicode_GET_SIZE(unicode) + 1; /* copy the nul character */ |
| 10163 | size *= sizeof(Py_UNICODE); |
| 10164 | copy = PyMem_Malloc(size); |
| 10165 | if (copy == NULL) { |
| 10166 | PyErr_NoMemory(); |
| 10167 | return NULL; |
| 10168 | } |
| 10169 | memcpy(copy, PyUnicode_AS_UNICODE(unicode), size); |
| 10170 | return copy; |
| 10171 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10172 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10173 | #ifdef __cplusplus |
| 10174 | } |
| 10175 | #endif |