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 | |
Marc-André Lemburg | d49e5b4 | 2000-06-30 14:58:20 +0000 | [diff] [blame] | 46 | #include "ucnhash.h" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 47 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 48 | #ifdef MS_WINDOWS |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 49 | #include <windows.h> |
| 50 | #endif |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 51 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 52 | /* Limit for the Unicode object free list */ |
| 53 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 54 | #define PyUnicode_MAXFREELIST 1024 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 55 | |
| 56 | /* Limit for the Unicode object free list stay alive optimization. |
| 57 | |
| 58 | The implementation will keep allocated Unicode memory intact for |
| 59 | all objects on the free list having a size less than this |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 60 | limit. This reduces malloc() overhead for small Unicode objects. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 61 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 62 | At worst this will result in PyUnicode_MAXFREELIST * |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 63 | (sizeof(PyUnicodeObject) + KEEPALIVE_SIZE_LIMIT + |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 64 | malloc()-overhead) bytes of unused garbage. |
| 65 | |
| 66 | Setting the limit to 0 effectively turns the feature off. |
| 67 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 68 | Note: This is an experimental feature ! If you get core dumps when |
| 69 | using Unicode objects, turn this feature off. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 70 | |
| 71 | */ |
| 72 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 73 | #define KEEPALIVE_SIZE_LIMIT 9 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 74 | |
| 75 | /* Endianness switches; defaults to little endian */ |
| 76 | |
| 77 | #ifdef WORDS_BIGENDIAN |
| 78 | # define BYTEORDER_IS_BIG_ENDIAN |
| 79 | #else |
| 80 | # define BYTEORDER_IS_LITTLE_ENDIAN |
| 81 | #endif |
| 82 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 83 | /* --- Globals ------------------------------------------------------------ |
| 84 | |
| 85 | The globals are initialized by the _PyUnicode_Init() API and should |
| 86 | not be used before calling that API. |
| 87 | |
| 88 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 89 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 90 | |
| 91 | #ifdef __cplusplus |
| 92 | extern "C" { |
| 93 | #endif |
| 94 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 95 | /* This dictionary holds all interned unicode strings. Note that references |
| 96 | to strings in this dictionary are *not* counted in the string's ob_refcnt. |
| 97 | When the interned string reaches a refcnt of 0 the string deallocation |
| 98 | function will delete the reference from this dictionary. |
| 99 | |
| 100 | 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] | 101 | 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] | 102 | */ |
| 103 | static PyObject *interned; |
| 104 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 105 | /* Free list for Unicode objects */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 106 | static PyUnicodeObject *free_list; |
| 107 | static int numfree; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 108 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 109 | /* The empty Unicode object is shared to improve performance. */ |
| 110 | static PyUnicodeObject *unicode_empty; |
| 111 | |
| 112 | /* Single character Unicode strings in the Latin-1 range are being |
| 113 | shared as well. */ |
| 114 | static PyUnicodeObject *unicode_latin1[256]; |
| 115 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 116 | /* Fast detection of the most frequent whitespace characters */ |
| 117 | const unsigned char _Py_ascii_whitespace[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 118 | 0, 0, 0, 0, 0, 0, 0, 0, |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 119 | /* case 0x0009: * CHARACTER TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 120 | /* case 0x000A: * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 121 | /* case 0x000B: * LINE TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 122 | /* case 0x000C: * FORM FEED */ |
| 123 | /* case 0x000D: * CARRIAGE RETURN */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 124 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 125 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 126 | /* case 0x001C: * FILE SEPARATOR */ |
| 127 | /* case 0x001D: * GROUP SEPARATOR */ |
| 128 | /* case 0x001E: * RECORD SEPARATOR */ |
| 129 | /* case 0x001F: * UNIT SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 130 | 0, 0, 0, 0, 1, 1, 1, 1, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 131 | /* case 0x0020: * SPACE */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 132 | 1, 0, 0, 0, 0, 0, 0, 0, |
| 133 | 0, 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, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 136 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 137 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 138 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 139 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 140 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 141 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 142 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 143 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 144 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 145 | }; |
| 146 | |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 147 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
| 148 | PyObject **errorHandler,const char *encoding, const char *reason, |
| 149 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 150 | Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos); |
| 151 | |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 152 | static void raise_encode_exception(PyObject **exceptionObject, |
| 153 | const char *encoding, |
| 154 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 155 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 156 | const char *reason); |
| 157 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 158 | /* Same for linebreaks */ |
| 159 | static unsigned char ascii_linebreak[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 160 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 161 | /* 0x000A, * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 162 | /* 0x000B, * LINE TABULATION */ |
| 163 | /* 0x000C, * FORM FEED */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 164 | /* 0x000D, * CARRIAGE RETURN */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 165 | 0, 0, 1, 1, 1, 1, 0, 0, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 166 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 167 | /* 0x001C, * FILE SEPARATOR */ |
| 168 | /* 0x001D, * GROUP SEPARATOR */ |
| 169 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 170 | 0, 0, 0, 0, 1, 1, 1, 0, |
| 171 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 172 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 173 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 174 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 175 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 176 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 177 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 178 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 179 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 180 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 181 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 182 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 183 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 187 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 188 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 189 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 190 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 191 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 192 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 193 | /* This is actually an illegal character, so it should |
| 194 | not be passed to unichr. */ |
| 195 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 196 | #endif |
| 197 | } |
| 198 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 199 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 200 | |
| 201 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 202 | to keep things simple, we use a single bitmask, using the least 5 |
| 203 | bits from each unicode characters as the bit index. */ |
| 204 | |
| 205 | /* the linebreak mask is set up by Unicode_Init below */ |
| 206 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 207 | #if LONG_BIT >= 128 |
| 208 | #define BLOOM_WIDTH 128 |
| 209 | #elif LONG_BIT >= 64 |
| 210 | #define BLOOM_WIDTH 64 |
| 211 | #elif LONG_BIT >= 32 |
| 212 | #define BLOOM_WIDTH 32 |
| 213 | #else |
| 214 | #error "LONG_BIT is smaller than 32" |
| 215 | #endif |
| 216 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 217 | #define BLOOM_MASK unsigned long |
| 218 | |
| 219 | static BLOOM_MASK bloom_linebreak; |
| 220 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 221 | #define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
| 222 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 223 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 224 | #define BLOOM_LINEBREAK(ch) \ |
| 225 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 226 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 227 | |
| 228 | Py_LOCAL_INLINE(BLOOM_MASK) make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len) |
| 229 | { |
| 230 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 231 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 232 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 233 | Py_ssize_t i; |
| 234 | |
| 235 | mask = 0; |
| 236 | for (i = 0; i < len; i++) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 237 | BLOOM_ADD(mask, ptr[i]); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 238 | |
| 239 | return mask; |
| 240 | } |
| 241 | |
| 242 | Py_LOCAL_INLINE(int) unicode_member(Py_UNICODE chr, Py_UNICODE* set, Py_ssize_t setlen) |
| 243 | { |
| 244 | Py_ssize_t i; |
| 245 | |
| 246 | for (i = 0; i < setlen; i++) |
| 247 | if (set[i] == chr) |
| 248 | return 1; |
| 249 | |
| 250 | return 0; |
| 251 | } |
| 252 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 253 | #define BLOOM_MEMBER(mask, chr, set, setlen) \ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 254 | BLOOM(mask, chr) && unicode_member(chr, set, setlen) |
| 255 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 256 | /* --- Unicode Object ----------------------------------------------------- */ |
| 257 | |
| 258 | static |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 259 | int unicode_resize(register PyUnicodeObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 260 | Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 261 | { |
| 262 | void *oldstr; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 263 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 264 | /* Shortcut if there's nothing much to do. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 265 | if (unicode->length == length) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 266 | goto reset; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 267 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 268 | /* Resizing shared object (unicode_empty or single character |
| 269 | objects) in-place is not allowed. Use PyUnicode_Resize() |
| 270 | instead ! */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 271 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 272 | if (unicode == unicode_empty || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 273 | (unicode->length == 1 && |
| 274 | unicode->str[0] < 256U && |
| 275 | unicode_latin1[unicode->str[0]] == unicode)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 276 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 277 | "can't resize shared str objects"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 278 | return -1; |
| 279 | } |
| 280 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 281 | /* We allocate one more byte to make sure the string is Ux0000 terminated. |
| 282 | The overallocation is also used by fastsearch, which assumes that it's |
| 283 | safe to look at str[length] (without making any assumptions about what |
| 284 | it contains). */ |
| 285 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 286 | oldstr = unicode->str; |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 287 | unicode->str = PyObject_REALLOC(unicode->str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 288 | sizeof(Py_UNICODE) * (length + 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 289 | if (!unicode->str) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 290 | unicode->str = (Py_UNICODE *)oldstr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 291 | PyErr_NoMemory(); |
| 292 | return -1; |
| 293 | } |
| 294 | unicode->str[length] = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 295 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 296 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 297 | reset: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 298 | /* Reset the object caches */ |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 299 | if (unicode->defenc) { |
Georg Brandl | 8ee604b | 2010-07-29 14:23:06 +0000 | [diff] [blame] | 300 | Py_CLEAR(unicode->defenc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 301 | } |
| 302 | unicode->hash = -1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 303 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | /* 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] | 308 | Ux0000 terminated; some code (e.g. new_identifier) |
| 309 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 310 | |
| 311 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 312 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 313 | |
| 314 | */ |
| 315 | |
| 316 | static |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 317 | PyUnicodeObject *_PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 318 | { |
| 319 | register PyUnicodeObject *unicode; |
| 320 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 321 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 322 | if (length == 0 && unicode_empty != NULL) { |
| 323 | Py_INCREF(unicode_empty); |
| 324 | return unicode_empty; |
| 325 | } |
| 326 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 327 | /* Ensure we won't overflow the size. */ |
| 328 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 329 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 330 | } |
| 331 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 332 | /* Unicode freelist & memory allocation */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 333 | if (free_list) { |
| 334 | unicode = free_list; |
| 335 | free_list = *(PyUnicodeObject **)unicode; |
| 336 | numfree--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 337 | if (unicode->str) { |
| 338 | /* Keep-Alive optimization: we only upsize the buffer, |
| 339 | never downsize it. */ |
| 340 | if ((unicode->length < length) && |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 341 | unicode_resize(unicode, length) < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 342 | PyObject_DEL(unicode->str); |
| 343 | unicode->str = NULL; |
| 344 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 345 | } |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 346 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 347 | size_t new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 348 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 349 | } |
| 350 | PyObject_INIT(unicode, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 351 | } |
| 352 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 353 | size_t new_size; |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 354 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 355 | if (unicode == NULL) |
| 356 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 357 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 358 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 361 | if (!unicode->str) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 362 | PyErr_NoMemory(); |
| 363 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 364 | } |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 365 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 366 | * the caller fails before initializing str -- unicode_resize() |
| 367 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 368 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 369 | * We don't want unicode_resize to read uninitialized memory in |
| 370 | * that case. |
| 371 | */ |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 372 | unicode->str[0] = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 373 | unicode->str[length] = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 374 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 375 | unicode->hash = -1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 376 | unicode->state = 0; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 377 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 378 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 379 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 380 | onError: |
Amaury Forgeot d'Arc | 7888d08 | 2008-08-01 01:06:32 +0000 | [diff] [blame] | 381 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
| 382 | _Py_DEC_REFTOTAL; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 383 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 384 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 385 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | static |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 389 | void unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 390 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 391 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 392 | case SSTATE_NOT_INTERNED: |
| 393 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 394 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 395 | case SSTATE_INTERNED_MORTAL: |
| 396 | /* revive dead object temporarily for DelItem */ |
| 397 | Py_REFCNT(unicode) = 3; |
| 398 | if (PyDict_DelItem(interned, (PyObject *)unicode) != 0) |
| 399 | Py_FatalError( |
| 400 | "deletion of interned string failed"); |
| 401 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 402 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 403 | case SSTATE_INTERNED_IMMORTAL: |
| 404 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 405 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 406 | default: |
| 407 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 410 | if (PyUnicode_CheckExact(unicode) && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 411 | numfree < PyUnicode_MAXFREELIST) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 412 | /* Keep-Alive optimization */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 413 | if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { |
| 414 | PyObject_DEL(unicode->str); |
| 415 | unicode->str = NULL; |
| 416 | unicode->length = 0; |
| 417 | } |
| 418 | if (unicode->defenc) { |
Georg Brandl | 8ee604b | 2010-07-29 14:23:06 +0000 | [diff] [blame] | 419 | Py_CLEAR(unicode->defenc); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 420 | } |
| 421 | /* Add to free list */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 422 | *(PyUnicodeObject **)unicode = free_list; |
| 423 | free_list = unicode; |
| 424 | numfree++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 425 | } |
| 426 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 427 | PyObject_DEL(unicode->str); |
| 428 | Py_XDECREF(unicode->defenc); |
| 429 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 433 | static |
| 434 | int _PyUnicode_Resize(PyUnicodeObject **unicode, Py_ssize_t length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 435 | { |
| 436 | register PyUnicodeObject *v; |
| 437 | |
| 438 | /* Argument checks */ |
| 439 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 440 | PyErr_BadInternalCall(); |
| 441 | return -1; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 442 | } |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 443 | v = *unicode; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 444 | if (v == NULL || !PyUnicode_Check(v) || Py_REFCNT(v) != 1 || length < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 445 | PyErr_BadInternalCall(); |
| 446 | return -1; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | /* Resizing unicode_empty and single character objects is not |
| 450 | possible since these are being shared. We simply return a fresh |
| 451 | copy with the same Unicode content. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 452 | if (v->length != length && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 453 | (v == unicode_empty || v->length == 1)) { |
| 454 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 455 | if (w == NULL) |
| 456 | return -1; |
| 457 | Py_UNICODE_COPY(w->str, v->str, |
| 458 | length < v->length ? length : v->length); |
| 459 | Py_DECREF(*unicode); |
| 460 | *unicode = w; |
| 461 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 465 | objects, since we can modify them in-place. */ |
| 466 | return unicode_resize(v, length); |
| 467 | } |
| 468 | |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 469 | int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) |
| 470 | { |
| 471 | return _PyUnicode_Resize((PyUnicodeObject **)unicode, length); |
| 472 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 473 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 474 | PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 475 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 476 | { |
| 477 | PyUnicodeObject *unicode; |
| 478 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 479 | /* If the Unicode data is known at construction time, we can apply |
| 480 | some optimizations which share commonly used objects. */ |
| 481 | if (u != NULL) { |
| 482 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 483 | /* Optimization for empty strings */ |
| 484 | if (size == 0 && unicode_empty != NULL) { |
| 485 | Py_INCREF(unicode_empty); |
| 486 | return (PyObject *)unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 487 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 488 | |
| 489 | /* Single character Unicode objects in the Latin-1 range are |
| 490 | shared when using this constructor */ |
| 491 | if (size == 1 && *u < 256) { |
| 492 | unicode = unicode_latin1[*u]; |
| 493 | if (!unicode) { |
| 494 | unicode = _PyUnicode_New(1); |
| 495 | if (!unicode) |
| 496 | return NULL; |
| 497 | unicode->str[0] = *u; |
| 498 | unicode_latin1[*u] = unicode; |
| 499 | } |
| 500 | Py_INCREF(unicode); |
| 501 | return (PyObject *)unicode; |
| 502 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 503 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 504 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 505 | unicode = _PyUnicode_New(size); |
| 506 | if (!unicode) |
| 507 | return NULL; |
| 508 | |
| 509 | /* Copy the Unicode data into the new object */ |
| 510 | if (u != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 511 | Py_UNICODE_COPY(unicode->str, u, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 512 | |
| 513 | return (PyObject *)unicode; |
| 514 | } |
| 515 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 516 | PyObject *PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 517 | { |
| 518 | PyUnicodeObject *unicode; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 519 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 520 | if (size < 0) { |
| 521 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 522 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 523 | return NULL; |
| 524 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 525 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 526 | /* 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] | 527 | some optimizations which share commonly used objects. |
| 528 | Also, this means the input must be UTF-8, so fall back to the |
| 529 | UTF-8 decoder at the end. */ |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 530 | if (u != NULL) { |
| 531 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 532 | /* Optimization for empty strings */ |
| 533 | if (size == 0 && unicode_empty != NULL) { |
| 534 | Py_INCREF(unicode_empty); |
| 535 | return (PyObject *)unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 536 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 537 | |
| 538 | /* Single characters are shared when using this constructor. |
| 539 | Restrict to ASCII, since the input must be UTF-8. */ |
| 540 | if (size == 1 && Py_CHARMASK(*u) < 128) { |
| 541 | unicode = unicode_latin1[Py_CHARMASK(*u)]; |
| 542 | if (!unicode) { |
| 543 | unicode = _PyUnicode_New(1); |
| 544 | if (!unicode) |
| 545 | return NULL; |
| 546 | unicode->str[0] = Py_CHARMASK(*u); |
| 547 | unicode_latin1[Py_CHARMASK(*u)] = unicode; |
| 548 | } |
| 549 | Py_INCREF(unicode); |
| 550 | return (PyObject *)unicode; |
| 551 | } |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 552 | |
| 553 | return PyUnicode_DecodeUTF8(u, size, NULL); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Walter Dörwald | 5550731 | 2007-05-18 13:12:10 +0000 | [diff] [blame] | 556 | unicode = _PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 557 | if (!unicode) |
| 558 | return NULL; |
| 559 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 560 | return (PyObject *)unicode; |
| 561 | } |
| 562 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 563 | PyObject *PyUnicode_FromString(const char *u) |
| 564 | { |
| 565 | size_t size = strlen(u); |
| 566 | if (size > PY_SSIZE_T_MAX) { |
| 567 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 568 | return NULL; |
| 569 | } |
| 570 | |
| 571 | return PyUnicode_FromStringAndSize(u, size); |
| 572 | } |
| 573 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 574 | #ifdef HAVE_WCHAR_H |
| 575 | |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 576 | #if (Py_UNICODE_SIZE == 2) && defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
| 577 | # define CONVERT_WCHAR_TO_SURROGATES |
| 578 | #endif |
| 579 | |
| 580 | #ifdef CONVERT_WCHAR_TO_SURROGATES |
| 581 | |
| 582 | /* Here sizeof(wchar_t) is 4 but Py_UNICODE_SIZE == 2, so we need |
| 583 | to convert from UTF32 to UTF16. */ |
| 584 | |
| 585 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
| 586 | Py_ssize_t size) |
| 587 | { |
| 588 | PyUnicodeObject *unicode; |
| 589 | register Py_ssize_t i; |
| 590 | Py_ssize_t alloc; |
| 591 | const wchar_t *orig_w; |
| 592 | |
| 593 | if (w == NULL) { |
| 594 | if (size == 0) |
| 595 | return PyUnicode_FromStringAndSize(NULL, 0); |
| 596 | PyErr_BadInternalCall(); |
| 597 | return NULL; |
| 598 | } |
| 599 | |
| 600 | if (size == -1) { |
| 601 | size = wcslen(w); |
| 602 | } |
| 603 | |
| 604 | alloc = size; |
| 605 | orig_w = w; |
| 606 | for (i = size; i > 0; i--) { |
| 607 | if (*w > 0xFFFF) |
| 608 | alloc++; |
| 609 | w++; |
| 610 | } |
| 611 | w = orig_w; |
| 612 | unicode = _PyUnicode_New(alloc); |
| 613 | if (!unicode) |
| 614 | return NULL; |
| 615 | |
| 616 | /* Copy the wchar_t data into the new object */ |
| 617 | { |
| 618 | register Py_UNICODE *u; |
| 619 | u = PyUnicode_AS_UNICODE(unicode); |
| 620 | for (i = size; i > 0; i--) { |
| 621 | if (*w > 0xFFFF) { |
| 622 | wchar_t ordinal = *w++; |
| 623 | ordinal -= 0x10000; |
| 624 | *u++ = 0xD800 | (ordinal >> 10); |
| 625 | *u++ = 0xDC00 | (ordinal & 0x3FF); |
| 626 | } |
| 627 | else |
| 628 | *u++ = *w++; |
| 629 | } |
| 630 | } |
| 631 | return (PyObject *)unicode; |
| 632 | } |
| 633 | |
| 634 | #else |
| 635 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 636 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 637 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 638 | { |
| 639 | PyUnicodeObject *unicode; |
| 640 | |
| 641 | if (w == NULL) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 642 | if (size == 0) |
| 643 | return PyUnicode_FromStringAndSize(NULL, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 644 | PyErr_BadInternalCall(); |
| 645 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 648 | if (size == -1) { |
| 649 | size = wcslen(w); |
| 650 | } |
| 651 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 652 | unicode = _PyUnicode_New(size); |
| 653 | if (!unicode) |
| 654 | return NULL; |
| 655 | |
| 656 | /* Copy the wchar_t data into the new object */ |
Daniel Stutzbach | 8515eae | 2010-08-24 21:57:33 +0000 | [diff] [blame] | 657 | #if Py_UNICODE_SIZE == SIZEOF_WCHAR_T |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 658 | memcpy(unicode->str, w, size * sizeof(wchar_t)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 659 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 660 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 661 | register Py_UNICODE *u; |
| 662 | register Py_ssize_t i; |
| 663 | u = PyUnicode_AS_UNICODE(unicode); |
| 664 | for (i = size; i > 0; i--) |
| 665 | *u++ = *w++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 666 | } |
| 667 | #endif |
| 668 | |
| 669 | return (PyObject *)unicode; |
| 670 | } |
| 671 | |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 672 | #endif /* CONVERT_WCHAR_TO_SURROGATES */ |
| 673 | |
| 674 | #undef CONVERT_WCHAR_TO_SURROGATES |
| 675 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 676 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 677 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 678 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 679 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 680 | *fmt++ = '%'; |
| 681 | if (width) { |
| 682 | if (zeropad) |
| 683 | *fmt++ = '0'; |
| 684 | fmt += sprintf(fmt, "%d", width); |
| 685 | } |
| 686 | if (precision) |
| 687 | fmt += sprintf(fmt, ".%d", precision); |
| 688 | if (longflag) |
| 689 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 690 | else if (longlongflag) { |
| 691 | /* longlongflag should only ever be nonzero on machines with |
| 692 | HAVE_LONG_LONG defined */ |
| 693 | #ifdef HAVE_LONG_LONG |
| 694 | char *f = PY_FORMAT_LONG_LONG; |
| 695 | while (*f) |
| 696 | *fmt++ = *f++; |
| 697 | #else |
| 698 | /* we shouldn't ever get here */ |
| 699 | assert(0); |
| 700 | *fmt++ = 'l'; |
| 701 | #endif |
| 702 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 703 | else if (size_tflag) { |
| 704 | char *f = PY_FORMAT_SIZE_T; |
| 705 | while (*f) |
| 706 | *fmt++ = *f++; |
| 707 | } |
| 708 | *fmt++ = c; |
| 709 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 710 | } |
| 711 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 712 | #define appendstring(string) {for (copy = string;*copy;) *s++ = *copy++;} |
| 713 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 714 | /* size of fixed-size buffer for formatting single arguments */ |
| 715 | #define ITEM_BUFFER_LEN 21 |
| 716 | /* maximum number of characters required for output of %ld. 21 characters |
| 717 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 718 | #define MAX_LONG_CHARS 21 |
| 719 | /* maximum number of characters required for output of %lld. |
| 720 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 721 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 722 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 723 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 724 | PyObject * |
| 725 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 726 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 727 | va_list count; |
| 728 | Py_ssize_t callcount = 0; |
| 729 | PyObject **callresults = NULL; |
| 730 | PyObject **callresult = NULL; |
| 731 | Py_ssize_t n = 0; |
| 732 | int width = 0; |
| 733 | int precision = 0; |
| 734 | int zeropad; |
| 735 | const char* f; |
| 736 | Py_UNICODE *s; |
| 737 | PyObject *string; |
| 738 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 739 | char buffer[ITEM_BUFFER_LEN+1]; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 740 | /* use abuffer instead of buffer, if we need more space |
| 741 | * (which can happen if there's a format specifier with width). */ |
| 742 | char *abuffer = NULL; |
| 743 | char *realbuffer; |
| 744 | Py_ssize_t abuffersize = 0; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 745 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 746 | const char *copy; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 747 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 748 | Py_VA_COPY(count, vargs); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 749 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 750 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 751 | * PyUnicode_DecodeUTF8() for these objects once during step 3 and put the |
| 752 | * result in an array) */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 753 | for (f = format; *f; f++) { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 754 | if (*f == '%') { |
| 755 | if (*(f+1)=='%') |
| 756 | continue; |
| 757 | if (*(f+1)=='S' || *(f+1)=='R' || *(f+1)=='A') |
| 758 | ++callcount; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 759 | while (Py_ISDIGIT((unsigned)*f)) |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 760 | width = (width*10) + *f++ - '0'; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 761 | while (*++f && *f != '%' && !Py_ISALPHA((unsigned)*f)) |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 762 | ; |
| 763 | if (*f == 's') |
| 764 | ++callcount; |
| 765 | } |
Benjamin Peterson | 9be0b2e | 2010-09-12 03:40:54 +0000 | [diff] [blame] | 766 | else if (128 <= (unsigned char)*f) { |
| 767 | PyErr_Format(PyExc_ValueError, |
| 768 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
Victor Stinner | 4c7db31 | 2010-09-12 07:51:18 +0000 | [diff] [blame] | 769 | "string, got a non-ASCII byte: 0x%02x", |
Benjamin Peterson | 9be0b2e | 2010-09-12 03:40:54 +0000 | [diff] [blame] | 770 | (unsigned char)*f); |
Benjamin Peterson | d4ac96a | 2010-09-12 16:40:53 +0000 | [diff] [blame] | 771 | return NULL; |
Benjamin Peterson | 9be0b2e | 2010-09-12 03:40:54 +0000 | [diff] [blame] | 772 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 773 | } |
| 774 | /* step 2: allocate memory for the results of |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 775 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 776 | if (callcount) { |
| 777 | callresults = PyObject_Malloc(sizeof(PyObject *)*callcount); |
| 778 | if (!callresults) { |
| 779 | PyErr_NoMemory(); |
| 780 | return NULL; |
| 781 | } |
| 782 | callresult = callresults; |
| 783 | } |
| 784 | /* step 3: figure out how large a buffer we need */ |
| 785 | for (f = format; *f; f++) { |
| 786 | if (*f == '%') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 787 | #ifdef HAVE_LONG_LONG |
| 788 | int longlongflag = 0; |
| 789 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 790 | const char* p = f; |
| 791 | width = 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 792 | while (Py_ISDIGIT((unsigned)*f)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 793 | width = (width*10) + *f++ - '0'; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 794 | while (*++f && *f != '%' && !Py_ISALPHA((unsigned)*f)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 795 | ; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 796 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 797 | /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
| 798 | * they don't affect the amount of space we reserve. |
| 799 | */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 800 | if (*f == 'l') { |
| 801 | if (f[1] == 'd' || f[1] == 'u') { |
| 802 | ++f; |
| 803 | } |
| 804 | #ifdef HAVE_LONG_LONG |
| 805 | else if (f[1] == 'l' && |
| 806 | (f[2] == 'd' || f[2] == 'u')) { |
| 807 | longlongflag = 1; |
| 808 | f += 2; |
| 809 | } |
| 810 | #endif |
| 811 | } |
| 812 | else if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 813 | ++f; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 814 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 815 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 816 | switch (*f) { |
| 817 | case 'c': |
| 818 | (void)va_arg(count, int); |
| 819 | /* fall through... */ |
| 820 | case '%': |
| 821 | n++; |
| 822 | break; |
| 823 | case 'd': case 'u': case 'i': case 'x': |
| 824 | (void) va_arg(count, int); |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 825 | #ifdef HAVE_LONG_LONG |
| 826 | if (longlongflag) { |
| 827 | if (width < MAX_LONG_LONG_CHARS) |
| 828 | width = MAX_LONG_LONG_CHARS; |
| 829 | } |
| 830 | else |
| 831 | #endif |
| 832 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 833 | including sign. Decimal takes the most space. This |
| 834 | isn't enough for octal. If a width is specified we |
| 835 | need more (which we allocate later). */ |
| 836 | if (width < MAX_LONG_CHARS) |
| 837 | width = MAX_LONG_CHARS; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 838 | n += width; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 839 | /* XXX should allow for large precision here too. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 840 | if (abuffersize < width) |
| 841 | abuffersize = width; |
| 842 | break; |
| 843 | case 's': |
| 844 | { |
| 845 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 846 | const char *s = va_arg(count, const char*); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 847 | PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); |
| 848 | if (!str) |
| 849 | goto fail; |
| 850 | n += PyUnicode_GET_SIZE(str); |
| 851 | /* Remember the str and switch to the next slot */ |
| 852 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 853 | break; |
| 854 | } |
| 855 | case 'U': |
| 856 | { |
| 857 | PyObject *obj = va_arg(count, PyObject *); |
| 858 | assert(obj && PyUnicode_Check(obj)); |
| 859 | n += PyUnicode_GET_SIZE(obj); |
| 860 | break; |
| 861 | } |
| 862 | case 'V': |
| 863 | { |
| 864 | PyObject *obj = va_arg(count, PyObject *); |
| 865 | const char *str = va_arg(count, const char *); |
| 866 | assert(obj || str); |
| 867 | assert(!obj || PyUnicode_Check(obj)); |
| 868 | if (obj) |
| 869 | n += PyUnicode_GET_SIZE(obj); |
| 870 | else |
| 871 | n += strlen(str); |
| 872 | break; |
| 873 | } |
| 874 | case 'S': |
| 875 | { |
| 876 | PyObject *obj = va_arg(count, PyObject *); |
| 877 | PyObject *str; |
| 878 | assert(obj); |
| 879 | str = PyObject_Str(obj); |
| 880 | if (!str) |
| 881 | goto fail; |
| 882 | n += PyUnicode_GET_SIZE(str); |
| 883 | /* Remember the str and switch to the next slot */ |
| 884 | *callresult++ = str; |
| 885 | break; |
| 886 | } |
| 887 | case 'R': |
| 888 | { |
| 889 | PyObject *obj = va_arg(count, PyObject *); |
| 890 | PyObject *repr; |
| 891 | assert(obj); |
| 892 | repr = PyObject_Repr(obj); |
| 893 | if (!repr) |
| 894 | goto fail; |
| 895 | n += PyUnicode_GET_SIZE(repr); |
| 896 | /* Remember the repr and switch to the next slot */ |
| 897 | *callresult++ = repr; |
| 898 | break; |
| 899 | } |
| 900 | case 'A': |
| 901 | { |
| 902 | PyObject *obj = va_arg(count, PyObject *); |
| 903 | PyObject *ascii; |
| 904 | assert(obj); |
| 905 | ascii = PyObject_ASCII(obj); |
| 906 | if (!ascii) |
| 907 | goto fail; |
| 908 | n += PyUnicode_GET_SIZE(ascii); |
| 909 | /* Remember the repr and switch to the next slot */ |
| 910 | *callresult++ = ascii; |
| 911 | break; |
| 912 | } |
| 913 | case 'p': |
| 914 | (void) va_arg(count, int); |
| 915 | /* maximum 64-bit pointer representation: |
| 916 | * 0xffffffffffffffff |
| 917 | * so 19 characters is enough. |
| 918 | * XXX I count 18 -- what's the extra for? |
| 919 | */ |
| 920 | n += 19; |
| 921 | break; |
| 922 | default: |
| 923 | /* if we stumble upon an unknown |
| 924 | formatting code, copy the rest of |
| 925 | the format string to the output |
| 926 | string. (we cannot just skip the |
| 927 | code, since there's no way to know |
| 928 | what's in the argument list) */ |
| 929 | n += strlen(p); |
| 930 | goto expand; |
| 931 | } |
| 932 | } else |
| 933 | n++; |
| 934 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 935 | expand: |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 936 | if (abuffersize > ITEM_BUFFER_LEN) { |
| 937 | /* add 1 for sprintf's trailing null byte */ |
| 938 | abuffer = PyObject_Malloc(abuffersize + 1); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 939 | if (!abuffer) { |
| 940 | PyErr_NoMemory(); |
| 941 | goto fail; |
| 942 | } |
| 943 | realbuffer = abuffer; |
| 944 | } |
| 945 | else |
| 946 | realbuffer = buffer; |
| 947 | /* step 4: fill the buffer */ |
| 948 | /* Since we've analyzed how much space we need for the worst case, |
| 949 | we don't have to resize the string. |
| 950 | There can be no errors beyond this point. */ |
| 951 | string = PyUnicode_FromUnicode(NULL, n); |
| 952 | if (!string) |
| 953 | goto fail; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 954 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 955 | s = PyUnicode_AS_UNICODE(string); |
| 956 | callresult = callresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 957 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 958 | for (f = format; *f; f++) { |
| 959 | if (*f == '%') { |
| 960 | const char* p = f++; |
| 961 | int longflag = 0; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 962 | int longlongflag = 0; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 963 | int size_tflag = 0; |
| 964 | zeropad = (*f == '0'); |
| 965 | /* parse the width.precision part */ |
| 966 | width = 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 967 | while (Py_ISDIGIT((unsigned)*f)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 968 | width = (width*10) + *f++ - '0'; |
| 969 | precision = 0; |
| 970 | if (*f == '.') { |
| 971 | f++; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 972 | while (Py_ISDIGIT((unsigned)*f)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 973 | precision = (precision*10) + *f++ - '0'; |
| 974 | } |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 975 | /* Handle %ld, %lu, %lld and %llu. */ |
| 976 | if (*f == 'l') { |
| 977 | if (f[1] == 'd' || f[1] == 'u') { |
| 978 | longflag = 1; |
| 979 | ++f; |
| 980 | } |
| 981 | #ifdef HAVE_LONG_LONG |
| 982 | else if (f[1] == 'l' && |
| 983 | (f[2] == 'd' || f[2] == 'u')) { |
| 984 | longlongflag = 1; |
| 985 | f += 2; |
| 986 | } |
| 987 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 988 | } |
| 989 | /* handle the size_t flag. */ |
| 990 | if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
| 991 | size_tflag = 1; |
| 992 | ++f; |
| 993 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 994 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 995 | switch (*f) { |
| 996 | case 'c': |
| 997 | *s++ = va_arg(vargs, int); |
| 998 | break; |
| 999 | case 'd': |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1000 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 1001 | width, precision, 'd'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1002 | if (longflag) |
| 1003 | sprintf(realbuffer, fmt, va_arg(vargs, long)); |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1004 | #ifdef HAVE_LONG_LONG |
| 1005 | else if (longlongflag) |
| 1006 | sprintf(realbuffer, fmt, va_arg(vargs, PY_LONG_LONG)); |
| 1007 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1008 | else if (size_tflag) |
| 1009 | sprintf(realbuffer, fmt, va_arg(vargs, Py_ssize_t)); |
| 1010 | else |
| 1011 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 1012 | appendstring(realbuffer); |
| 1013 | break; |
| 1014 | case 'u': |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1015 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 1016 | width, precision, 'u'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1017 | if (longflag) |
| 1018 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned long)); |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1019 | #ifdef HAVE_LONG_LONG |
| 1020 | else if (longlongflag) |
| 1021 | sprintf(realbuffer, fmt, va_arg(vargs, |
| 1022 | unsigned PY_LONG_LONG)); |
| 1023 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1024 | else if (size_tflag) |
| 1025 | sprintf(realbuffer, fmt, va_arg(vargs, size_t)); |
| 1026 | else |
| 1027 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned int)); |
| 1028 | appendstring(realbuffer); |
| 1029 | break; |
| 1030 | case 'i': |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1031 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'i'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1032 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 1033 | appendstring(realbuffer); |
| 1034 | break; |
| 1035 | case 'x': |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1036 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1037 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 1038 | appendstring(realbuffer); |
| 1039 | break; |
| 1040 | case 's': |
| 1041 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1042 | /* unused, since we already have the result */ |
| 1043 | (void) va_arg(vargs, char *); |
| 1044 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(*callresult), |
| 1045 | PyUnicode_GET_SIZE(*callresult)); |
| 1046 | s += PyUnicode_GET_SIZE(*callresult); |
| 1047 | /* We're done with the unicode()/repr() => forget it */ |
| 1048 | Py_DECREF(*callresult); |
| 1049 | /* switch to next unicode()/repr() result */ |
| 1050 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1051 | break; |
| 1052 | } |
| 1053 | case 'U': |
| 1054 | { |
| 1055 | PyObject *obj = va_arg(vargs, PyObject *); |
| 1056 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 1057 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 1058 | s += size; |
| 1059 | break; |
| 1060 | } |
| 1061 | case 'V': |
| 1062 | { |
| 1063 | PyObject *obj = va_arg(vargs, PyObject *); |
| 1064 | const char *str = va_arg(vargs, const char *); |
| 1065 | if (obj) { |
| 1066 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 1067 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 1068 | s += size; |
| 1069 | } else { |
| 1070 | appendstring(str); |
| 1071 | } |
| 1072 | break; |
| 1073 | } |
| 1074 | case 'S': |
| 1075 | case 'R': |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 1076 | case 'A': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 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; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 1421 | if (Py_ISUPPER(*e)) { |
| 1422 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 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 | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 1586 | PyObject * |
| 1587 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 1588 | { |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 1589 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 1590 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 1591 | PyUnicode_GET_SIZE(unicode), |
| 1592 | NULL); |
| 1593 | #elif defined(__APPLE__) |
| 1594 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 1595 | PyUnicode_GET_SIZE(unicode), |
| 1596 | "surrogateescape"); |
| 1597 | #else |
| 1598 | if (Py_FileSystemDefaultEncoding) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 1599 | return PyUnicode_AsEncodedString(unicode, |
| 1600 | Py_FileSystemDefaultEncoding, |
| 1601 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 1602 | } |
| 1603 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 1604 | /* locale encoding with surrogateescape */ |
| 1605 | wchar_t *wchar; |
| 1606 | char *bytes; |
| 1607 | PyObject *bytes_obj; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 1608 | size_t error_pos; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 1609 | |
| 1610 | wchar = PyUnicode_AsWideCharString(unicode, NULL); |
| 1611 | if (wchar == NULL) |
| 1612 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 1613 | bytes = _Py_wchar2char(wchar, &error_pos); |
| 1614 | if (bytes == NULL) { |
| 1615 | if (error_pos != (size_t)-1) { |
| 1616 | char *errmsg = strerror(errno); |
| 1617 | PyObject *exc = NULL; |
| 1618 | if (errmsg == NULL) |
| 1619 | errmsg = "Py_wchar2char() failed"; |
| 1620 | raise_encode_exception(&exc, |
| 1621 | "filesystemencoding", |
| 1622 | PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), |
| 1623 | error_pos, error_pos+1, |
| 1624 | errmsg); |
| 1625 | Py_XDECREF(exc); |
| 1626 | } |
| 1627 | else |
| 1628 | PyErr_NoMemory(); |
| 1629 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 1630 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 1631 | } |
| 1632 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 1633 | |
| 1634 | bytes_obj = PyBytes_FromString(bytes); |
| 1635 | PyMem_Free(bytes); |
| 1636 | return bytes_obj; |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 1637 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 1638 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 1639 | } |
| 1640 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1641 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 1642 | const char *encoding, |
| 1643 | const char *errors) |
| 1644 | { |
| 1645 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 1646 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1647 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1648 | if (!PyUnicode_Check(unicode)) { |
| 1649 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1650 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1651 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1652 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1653 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1654 | encoding = PyUnicode_GetDefaultEncoding(); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1655 | |
| 1656 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1657 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
| 1658 | if (strcmp(lower, "utf-8") == 0) |
| 1659 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 1660 | PyUnicode_GET_SIZE(unicode), |
| 1661 | errors); |
| 1662 | else if ((strcmp(lower, "latin-1") == 0) || |
| 1663 | (strcmp(lower, "iso-8859-1") == 0)) |
| 1664 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 1665 | PyUnicode_GET_SIZE(unicode), |
| 1666 | errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1667 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1668 | else if (strcmp(lower, "mbcs") == 0) |
| 1669 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 1670 | PyUnicode_GET_SIZE(unicode), |
| 1671 | errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1672 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1673 | else if (strcmp(lower, "ascii") == 0) |
| 1674 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 1675 | PyUnicode_GET_SIZE(unicode), |
| 1676 | errors); |
| 1677 | } |
Victor Stinner | 59e62db | 2010-05-15 13:14:32 +0000 | [diff] [blame] | 1678 | /* During bootstrap, we may need to find the encodings |
| 1679 | package, to load the file system encoding, and require the |
| 1680 | file system encoding in order to load the encodings |
| 1681 | package. |
Christian Heimes | 6a27efa | 2008-10-30 21:48:26 +0000 | [diff] [blame] | 1682 | |
Victor Stinner | 59e62db | 2010-05-15 13:14:32 +0000 | [diff] [blame] | 1683 | Break out of this dependency by assuming that the path to |
| 1684 | the encodings module is ASCII-only. XXX could try wcstombs |
| 1685 | instead, if the file system encoding is the locale's |
| 1686 | encoding. */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1687 | if (Py_FileSystemDefaultEncoding && |
Victor Stinner | 59e62db | 2010-05-15 13:14:32 +0000 | [diff] [blame] | 1688 | strcmp(encoding, Py_FileSystemDefaultEncoding) == 0 && |
| 1689 | !PyThreadState_GET()->interp->codecs_initialized) |
| 1690 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 1691 | PyUnicode_GET_SIZE(unicode), |
| 1692 | errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1693 | |
| 1694 | /* Encode via the codec registry */ |
| 1695 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1696 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1697 | return NULL; |
| 1698 | |
| 1699 | /* The normal path */ |
| 1700 | if (PyBytes_Check(v)) |
| 1701 | return v; |
| 1702 | |
| 1703 | /* 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] | 1704 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 1705 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1706 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 1707 | |
| 1708 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 1709 | "encoder %s returned bytearray instead of bytes", |
| 1710 | encoding); |
| 1711 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1712 | Py_DECREF(v); |
| 1713 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1714 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1715 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1716 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 1717 | Py_DECREF(v); |
| 1718 | return b; |
| 1719 | } |
| 1720 | |
| 1721 | PyErr_Format(PyExc_TypeError, |
| 1722 | "encoder did not return a bytes object (type=%.400s)", |
| 1723 | Py_TYPE(v)->tp_name); |
| 1724 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1725 | return NULL; |
| 1726 | } |
| 1727 | |
| 1728 | PyObject *PyUnicode_AsEncodedUnicode(PyObject *unicode, |
| 1729 | const char *encoding, |
| 1730 | const char *errors) |
| 1731 | { |
| 1732 | PyObject *v; |
| 1733 | |
| 1734 | if (!PyUnicode_Check(unicode)) { |
| 1735 | PyErr_BadArgument(); |
| 1736 | goto onError; |
| 1737 | } |
| 1738 | |
| 1739 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1740 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1741 | |
| 1742 | /* Encode via the codec registry */ |
| 1743 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1744 | if (v == NULL) |
| 1745 | goto onError; |
| 1746 | if (!PyUnicode_Check(v)) { |
| 1747 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1748 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1749 | Py_TYPE(v)->tp_name); |
| 1750 | Py_DECREF(v); |
| 1751 | goto onError; |
| 1752 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1753 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1754 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1755 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1756 | return NULL; |
| 1757 | } |
| 1758 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1759 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1760 | const char *errors) |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1761 | { |
| 1762 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1763 | if (v) |
| 1764 | return v; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1765 | if (errors != NULL) |
| 1766 | Py_FatalError("non-NULL encoding in _PyUnicode_AsDefaultEncodedString"); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1767 | v = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Guido van Rossum | 0661009 | 2007-08-16 21:02:22 +0000 | [diff] [blame] | 1768 | PyUnicode_GET_SIZE(unicode), |
| 1769 | NULL); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1770 | if (!v) |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1771 | return NULL; |
Guido van Rossum | e7a0d39 | 2007-07-12 07:53:00 +0000 | [diff] [blame] | 1772 | ((PyUnicodeObject *)unicode)->defenc = v; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1773 | return v; |
| 1774 | } |
| 1775 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1776 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1777 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1778 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1779 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 1780 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1781 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1782 | PyObject* |
| 1783 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 1784 | { |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 1785 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 1786 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 1787 | #elif defined(__APPLE__) |
| 1788 | return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); |
| 1789 | #else |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1790 | /* During the early bootstrapping process, Py_FileSystemDefaultEncoding |
| 1791 | can be undefined. If it is case, decode using UTF-8. The following assumes |
| 1792 | that Py_FileSystemDefaultEncoding is set to a built-in encoding during the |
| 1793 | bootstrapping process where the codecs aren't ready yet. |
| 1794 | */ |
| 1795 | if (Py_FileSystemDefaultEncoding) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1796 | return PyUnicode_Decode(s, size, |
| 1797 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 1798 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1799 | } |
| 1800 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 1801 | /* locale encoding with surrogateescape */ |
| 1802 | wchar_t *wchar; |
| 1803 | PyObject *unicode; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 1804 | size_t len; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 1805 | |
| 1806 | if (s[size] != '\0' || size != strlen(s)) { |
| 1807 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 1808 | return NULL; |
| 1809 | } |
| 1810 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 1811 | wchar = _Py_char2wchar(s, &len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 1812 | if (wchar == NULL) |
Victor Stinner | d5af0a5 | 2010-11-08 23:34:29 +0000 | [diff] [blame] | 1813 | return PyErr_NoMemory(); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 1814 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 1815 | unicode = PyUnicode_FromWideChar(wchar, len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 1816 | PyMem_Free(wchar); |
| 1817 | return unicode; |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1818 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 1819 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1820 | } |
| 1821 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1822 | |
| 1823 | int |
| 1824 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 1825 | { |
| 1826 | PyObject *output = NULL; |
| 1827 | Py_ssize_t size; |
| 1828 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 1829 | if (arg == NULL) { |
| 1830 | Py_DECREF(*(PyObject**)addr); |
| 1831 | return 1; |
| 1832 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 1833 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1834 | output = arg; |
| 1835 | Py_INCREF(output); |
| 1836 | } |
| 1837 | else { |
| 1838 | arg = PyUnicode_FromObject(arg); |
| 1839 | if (!arg) |
| 1840 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 1841 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1842 | Py_DECREF(arg); |
| 1843 | if (!output) |
| 1844 | return 0; |
| 1845 | if (!PyBytes_Check(output)) { |
| 1846 | Py_DECREF(output); |
| 1847 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 1848 | return 0; |
| 1849 | } |
| 1850 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 1851 | size = PyBytes_GET_SIZE(output); |
| 1852 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1853 | if (size != strlen(data)) { |
| 1854 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 1855 | Py_DECREF(output); |
| 1856 | return 0; |
| 1857 | } |
| 1858 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 1859 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
| 1862 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 1863 | int |
| 1864 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 1865 | { |
| 1866 | PyObject *output = NULL; |
| 1867 | Py_ssize_t size; |
| 1868 | void *data; |
| 1869 | if (arg == NULL) { |
| 1870 | Py_DECREF(*(PyObject**)addr); |
| 1871 | return 1; |
| 1872 | } |
| 1873 | if (PyUnicode_Check(arg)) { |
| 1874 | output = arg; |
| 1875 | Py_INCREF(output); |
| 1876 | } |
| 1877 | else { |
| 1878 | arg = PyBytes_FromObject(arg); |
| 1879 | if (!arg) |
| 1880 | return 0; |
| 1881 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 1882 | PyBytes_GET_SIZE(arg)); |
| 1883 | Py_DECREF(arg); |
| 1884 | if (!output) |
| 1885 | return 0; |
| 1886 | if (!PyUnicode_Check(output)) { |
| 1887 | Py_DECREF(output); |
| 1888 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 1889 | return 0; |
| 1890 | } |
| 1891 | } |
| 1892 | size = PyUnicode_GET_SIZE(output); |
| 1893 | data = PyUnicode_AS_UNICODE(output); |
| 1894 | if (size != Py_UNICODE_strlen(data)) { |
| 1895 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 1896 | Py_DECREF(output); |
| 1897 | return 0; |
| 1898 | } |
| 1899 | *(PyObject**)addr = output; |
| 1900 | return Py_CLEANUP_SUPPORTED; |
| 1901 | } |
| 1902 | |
| 1903 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1904 | char* |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1905 | _PyUnicode_AsStringAndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1906 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1907 | PyObject *bytes; |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 1908 | if (!PyUnicode_Check(unicode)) { |
| 1909 | PyErr_BadArgument(); |
| 1910 | return NULL; |
| 1911 | } |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1912 | bytes = _PyUnicode_AsDefaultEncodedString(unicode, NULL); |
| 1913 | if (bytes == NULL) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1914 | return NULL; |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1915 | if (psize != NULL) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1916 | *psize = PyBytes_GET_SIZE(bytes); |
| 1917 | return PyBytes_AS_STRING(bytes); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1918 | } |
| 1919 | |
| 1920 | char* |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1921 | _PyUnicode_AsString(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1922 | { |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1923 | return _PyUnicode_AsStringAndSize(unicode, NULL); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1924 | } |
| 1925 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1926 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 1927 | { |
| 1928 | if (!PyUnicode_Check(unicode)) { |
| 1929 | PyErr_BadArgument(); |
| 1930 | goto onError; |
| 1931 | } |
| 1932 | return PyUnicode_AS_UNICODE(unicode); |
| 1933 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1934 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1935 | return NULL; |
| 1936 | } |
| 1937 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1938 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1939 | { |
| 1940 | if (!PyUnicode_Check(unicode)) { |
| 1941 | PyErr_BadArgument(); |
| 1942 | goto onError; |
| 1943 | } |
| 1944 | return PyUnicode_GET_SIZE(unicode); |
| 1945 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1946 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1947 | return -1; |
| 1948 | } |
| 1949 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 1950 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1951 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 1952 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1953 | } |
| 1954 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 1955 | /* create or adjust a UnicodeDecodeError */ |
| 1956 | static void |
| 1957 | make_decode_exception(PyObject **exceptionObject, |
| 1958 | const char *encoding, |
| 1959 | const char *input, Py_ssize_t length, |
| 1960 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 1961 | const char *reason) |
| 1962 | { |
| 1963 | if (*exceptionObject == NULL) { |
| 1964 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 1965 | encoding, input, length, startpos, endpos, reason); |
| 1966 | } |
| 1967 | else { |
| 1968 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 1969 | goto onError; |
| 1970 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 1971 | goto onError; |
| 1972 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 1973 | goto onError; |
| 1974 | } |
| 1975 | return; |
| 1976 | |
| 1977 | onError: |
| 1978 | Py_DECREF(*exceptionObject); |
| 1979 | *exceptionObject = NULL; |
| 1980 | } |
| 1981 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1982 | /* error handling callback helper: |
| 1983 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 1984 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1985 | and adjust various state variables. |
| 1986 | return 0 on success, -1 on error |
| 1987 | */ |
| 1988 | |
| 1989 | static |
| 1990 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1991 | const char *encoding, const char *reason, |
| 1992 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 1993 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 1994 | PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1995 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1996 | 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] | 1997 | |
| 1998 | PyObject *restuple = NULL; |
| 1999 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2000 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2001 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2002 | Py_ssize_t requiredsize; |
| 2003 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2004 | Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2005 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2006 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2007 | int res = -1; |
| 2008 | |
| 2009 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2010 | *errorHandler = PyCodec_LookupError(errors); |
| 2011 | if (*errorHandler == NULL) |
| 2012 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2013 | } |
| 2014 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 2015 | make_decode_exception(exceptionObject, |
| 2016 | encoding, |
| 2017 | *input, *inend - *input, |
| 2018 | *startinpos, *endinpos, |
| 2019 | reason); |
| 2020 | if (*exceptionObject == NULL) |
| 2021 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2022 | |
| 2023 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 2024 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2025 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2026 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 2027 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2028 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2029 | } |
| 2030 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2031 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2032 | |
| 2033 | /* Copy back the bytes variables, which might have been modified by the |
| 2034 | callback */ |
| 2035 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 2036 | if (!inputobj) |
| 2037 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2038 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2039 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2040 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2041 | *input = PyBytes_AS_STRING(inputobj); |
| 2042 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2043 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 2044 | /* we can DECREF safely, as the exception has another reference, |
| 2045 | so the object won't go away. */ |
| 2046 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2047 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2048 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2049 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 2050 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2051 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 2052 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 2053 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2054 | |
| 2055 | /* need more space? (at least enough for what we |
| 2056 | have+the replacement+the rest of the string (starting |
| 2057 | at the new input position), so we won't have to check space |
| 2058 | when there are no errors in the rest of the string) */ |
| 2059 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 2060 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 2061 | requiredsize = *outpos + repsize + insize-newpos; |
| 2062 | if (requiredsize > outsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2063 | if (requiredsize<2*outsize) |
| 2064 | requiredsize = 2*outsize; |
| 2065 | if (_PyUnicode_Resize(output, requiredsize) < 0) |
| 2066 | goto onError; |
| 2067 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2068 | } |
| 2069 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2070 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2071 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 2072 | *outptr += repsize; |
| 2073 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2074 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2075 | /* we made it! */ |
| 2076 | res = 0; |
| 2077 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2078 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2079 | Py_XDECREF(restuple); |
| 2080 | return res; |
| 2081 | } |
| 2082 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2083 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 2084 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2085 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 2086 | |
| 2087 | /* Three simple macros defining base-64. */ |
| 2088 | |
| 2089 | /* Is c a base-64 character? */ |
| 2090 | |
| 2091 | #define IS_BASE64(c) \ |
| 2092 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 2093 | ((c) >= 'a' && (c) <= 'z') || \ |
| 2094 | ((c) >= '0' && (c) <= '9') || \ |
| 2095 | (c) == '+' || (c) == '/') |
| 2096 | |
| 2097 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 2098 | |
| 2099 | #define FROM_BASE64(c) \ |
| 2100 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 2101 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 2102 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 2103 | (c) == '+' ? 62 : 63) |
| 2104 | |
| 2105 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 2106 | |
| 2107 | #define TO_BASE64(n) \ |
| 2108 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 2109 | |
| 2110 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 2111 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 2112 | * byte not decoding to itself is the + which begins a base64 |
| 2113 | * string. */ |
| 2114 | |
| 2115 | #define DECODE_DIRECT(c) \ |
| 2116 | ((c) <= 127 && (c) != '+') |
| 2117 | |
| 2118 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 2119 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 2120 | * the above). See RFC2152. This array identifies these different |
| 2121 | * sets: |
| 2122 | * 0 : "Set D" |
| 2123 | * alphanumeric and '(),-./:? |
| 2124 | * 1 : "Set O" |
| 2125 | * !"#$%&*;<=>@[]^_`{|} |
| 2126 | * 2 : "whitespace" |
| 2127 | * ht nl cr sp |
| 2128 | * 3 : special (must be base64 encoded) |
| 2129 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 2130 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2131 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2132 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2133 | char utf7_category[128] = { |
| 2134 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 2135 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 2136 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 2137 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 2138 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 2139 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 2140 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 2141 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 2142 | /* @ A B C D E F G H I J K L M N O */ |
| 2143 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2144 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 2145 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 2146 | /* ` a b c d e f g h i j k l m n o */ |
| 2147 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2148 | /* p q r s t u v w x y z { | } ~ del */ |
| 2149 | 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] | 2150 | }; |
| 2151 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2152 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 2153 | * answer depends on whether we are encoding set O as itself, and also |
| 2154 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 2155 | * clear that the answers to these questions vary between |
| 2156 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 2157 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2158 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 2159 | ((c) < 128 && (c) > 0 && \ |
| 2160 | ((utf7_category[(c)] == 0) || \ |
| 2161 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 2162 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2163 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2164 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2165 | Py_ssize_t size, |
| 2166 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2167 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2168 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 2169 | } |
| 2170 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2171 | /* The decoder. The only state we preserve is our read position, |
| 2172 | * i.e. how many characters we have consumed. So if we end in the |
| 2173 | * middle of a shift sequence we have to back off the read position |
| 2174 | * and the output to the beginning of the sequence, otherwise we lose |
| 2175 | * all the shift state (seen bits, number of bits seen, high |
| 2176 | * surrogate). */ |
| 2177 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2178 | PyObject *PyUnicode_DecodeUTF7Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2179 | Py_ssize_t size, |
| 2180 | const char *errors, |
| 2181 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2182 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2183 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2184 | Py_ssize_t startinpos; |
| 2185 | Py_ssize_t endinpos; |
| 2186 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2187 | const char *e; |
| 2188 | PyUnicodeObject *unicode; |
| 2189 | Py_UNICODE *p; |
| 2190 | const char *errmsg = ""; |
| 2191 | int inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2192 | Py_UNICODE *shiftOutStart; |
| 2193 | unsigned int base64bits = 0; |
| 2194 | unsigned long base64buffer = 0; |
| 2195 | Py_UNICODE surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2196 | PyObject *errorHandler = NULL; |
| 2197 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2198 | |
| 2199 | unicode = _PyUnicode_New(size); |
| 2200 | if (!unicode) |
| 2201 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2202 | if (size == 0) { |
| 2203 | if (consumed) |
| 2204 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2205 | return (PyObject *)unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2206 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2207 | |
| 2208 | p = unicode->str; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2209 | shiftOutStart = p; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2210 | e = s + size; |
| 2211 | |
| 2212 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2213 | Py_UNICODE ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2214 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 2215 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2216 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2217 | if (inShift) { /* in a base-64 section */ |
| 2218 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 2219 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 2220 | base64bits += 6; |
| 2221 | s++; |
| 2222 | if (base64bits >= 16) { |
| 2223 | /* we have enough bits for a UTF-16 value */ |
| 2224 | Py_UNICODE outCh = (Py_UNICODE) |
| 2225 | (base64buffer >> (base64bits-16)); |
| 2226 | base64bits -= 16; |
| 2227 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 2228 | if (surrogate) { |
| 2229 | /* expecting a second surrogate */ |
| 2230 | if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 2231 | #ifdef Py_UNICODE_WIDE |
| 2232 | *p++ = (((surrogate & 0x3FF)<<10) |
| 2233 | | (outCh & 0x3FF)) + 0x10000; |
| 2234 | #else |
| 2235 | *p++ = surrogate; |
| 2236 | *p++ = outCh; |
| 2237 | #endif |
| 2238 | surrogate = 0; |
| 2239 | } |
| 2240 | else { |
| 2241 | surrogate = 0; |
| 2242 | errmsg = "second surrogate missing"; |
| 2243 | goto utf7Error; |
| 2244 | } |
| 2245 | } |
| 2246 | else if (outCh >= 0xD800 && outCh <= 0xDBFF) { |
| 2247 | /* first surrogate */ |
| 2248 | surrogate = outCh; |
| 2249 | } |
| 2250 | else if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 2251 | errmsg = "unexpected second surrogate"; |
| 2252 | goto utf7Error; |
| 2253 | } |
| 2254 | else { |
| 2255 | *p++ = outCh; |
| 2256 | } |
| 2257 | } |
| 2258 | } |
| 2259 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2260 | inShift = 0; |
| 2261 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2262 | if (surrogate) { |
| 2263 | errmsg = "second surrogate missing at end of shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2264 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2265 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2266 | if (base64bits > 0) { /* left-over bits */ |
| 2267 | if (base64bits >= 6) { |
| 2268 | /* We've seen at least one base-64 character */ |
| 2269 | errmsg = "partial character in shift sequence"; |
| 2270 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2271 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2272 | else { |
| 2273 | /* Some bits remain; they should be zero */ |
| 2274 | if (base64buffer != 0) { |
| 2275 | errmsg = "non-zero padding bits in shift sequence"; |
| 2276 | goto utf7Error; |
| 2277 | } |
| 2278 | } |
| 2279 | } |
| 2280 | if (ch != '-') { |
| 2281 | /* '-' is absorbed; other terminating |
| 2282 | characters are preserved */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2283 | *p++ = ch; |
| 2284 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2285 | } |
| 2286 | } |
| 2287 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2288 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2289 | s++; /* consume '+' */ |
| 2290 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2291 | s++; |
| 2292 | *p++ = '+'; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2293 | } |
| 2294 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2295 | inShift = 1; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2296 | shiftOutStart = p; |
| 2297 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2298 | } |
| 2299 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2300 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2301 | *p++ = ch; |
| 2302 | s++; |
| 2303 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2304 | else { |
| 2305 | startinpos = s-starts; |
| 2306 | s++; |
| 2307 | errmsg = "unexpected special character"; |
| 2308 | goto utf7Error; |
| 2309 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2310 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2311 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2312 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2313 | endinpos = s-starts; |
| 2314 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2315 | errors, &errorHandler, |
| 2316 | "utf7", errmsg, |
| 2317 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2318 | &unicode, &outpos, &p)) |
| 2319 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2320 | } |
| 2321 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2322 | /* end of string */ |
| 2323 | |
| 2324 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 2325 | /* if we're in an inconsistent state, that's an error */ |
| 2326 | if (surrogate || |
| 2327 | (base64bits >= 6) || |
| 2328 | (base64bits > 0 && base64buffer != 0)) { |
| 2329 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2330 | endinpos = size; |
| 2331 | if (unicode_decode_call_errorhandler( |
| 2332 | errors, &errorHandler, |
| 2333 | "utf7", "unterminated shift sequence", |
| 2334 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2335 | &unicode, &outpos, &p)) |
| 2336 | goto onError; |
| 2337 | if (s < e) |
| 2338 | goto restart; |
| 2339 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2340 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2341 | |
| 2342 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2343 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2344 | if (inShift) { |
| 2345 | p = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2346 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2347 | } |
| 2348 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2349 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2350 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2351 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2352 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2353 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2354 | goto onError; |
| 2355 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2356 | Py_XDECREF(errorHandler); |
| 2357 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2358 | return (PyObject *)unicode; |
| 2359 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2360 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2361 | Py_XDECREF(errorHandler); |
| 2362 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2363 | Py_DECREF(unicode); |
| 2364 | return NULL; |
| 2365 | } |
| 2366 | |
| 2367 | |
| 2368 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2369 | Py_ssize_t size, |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2370 | int base64SetO, |
| 2371 | int base64WhiteSpace, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2372 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2373 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2374 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2375 | /* It might be possible to tighten this worst case */ |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 2376 | Py_ssize_t allocated = 8 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2377 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2378 | Py_ssize_t i = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2379 | unsigned int base64bits = 0; |
| 2380 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2381 | char * out; |
| 2382 | char * start; |
| 2383 | |
| 2384 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2385 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2386 | |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 2387 | if (allocated / 8 != size) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2388 | return PyErr_NoMemory(); |
| 2389 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2390 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2391 | if (v == NULL) |
| 2392 | return NULL; |
| 2393 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2394 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2395 | for (;i < size; ++i) { |
| 2396 | Py_UNICODE ch = s[i]; |
| 2397 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2398 | if (inShift) { |
| 2399 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 2400 | /* shifting out */ |
| 2401 | if (base64bits) { /* output remaining bits */ |
| 2402 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 2403 | base64buffer = 0; |
| 2404 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2405 | } |
| 2406 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2407 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 2408 | so no '-' is required, except if the character is itself a '-' */ |
| 2409 | if (IS_BASE64(ch) || ch == '-') { |
| 2410 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2411 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2412 | *out++ = (char) ch; |
| 2413 | } |
| 2414 | else { |
| 2415 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2416 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2417 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2418 | else { /* not in a shift sequence */ |
| 2419 | if (ch == '+') { |
| 2420 | *out++ = '+'; |
| 2421 | *out++ = '-'; |
| 2422 | } |
| 2423 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 2424 | *out++ = (char) ch; |
| 2425 | } |
| 2426 | else { |
| 2427 | *out++ = '+'; |
| 2428 | inShift = 1; |
| 2429 | goto encode_char; |
| 2430 | } |
| 2431 | } |
| 2432 | continue; |
| 2433 | encode_char: |
| 2434 | #ifdef Py_UNICODE_WIDE |
| 2435 | if (ch >= 0x10000) { |
| 2436 | /* code first surrogate */ |
| 2437 | base64bits += 16; |
| 2438 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 2439 | while (base64bits >= 6) { |
| 2440 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 2441 | base64bits -= 6; |
| 2442 | } |
| 2443 | /* prepare second surrogate */ |
| 2444 | ch = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2445 | } |
| 2446 | #endif |
| 2447 | base64bits += 16; |
| 2448 | base64buffer = (base64buffer << 16) | ch; |
| 2449 | while (base64bits >= 6) { |
| 2450 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 2451 | base64bits -= 6; |
| 2452 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 2453 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2454 | if (base64bits) |
| 2455 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 2456 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2457 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2458 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 2459 | return NULL; |
| 2460 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2461 | } |
| 2462 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2463 | #undef IS_BASE64 |
| 2464 | #undef FROM_BASE64 |
| 2465 | #undef TO_BASE64 |
| 2466 | #undef DECODE_DIRECT |
| 2467 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2468 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2469 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 2470 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2471 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2472 | char utf8_code_length[256] = { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2473 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 2474 | illegal prefix. See RFC 3629 for details */ |
| 2475 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 2476 | 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] | 2477 | 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] | 2478 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2479 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2480 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2481 | 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] | 2482 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 2483 | 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] | 2484 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2485 | 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] | 2486 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 2487 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 2488 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 2489 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 2490 | 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] | 2491 | }; |
| 2492 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2493 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2494 | Py_ssize_t size, |
| 2495 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2496 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2497 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 2498 | } |
| 2499 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2500 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 2501 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 2502 | |
| 2503 | /* Mask to quickly check whether a C 'long' contains a |
| 2504 | non-ASCII, UTF8-encoded char. */ |
| 2505 | #if (SIZEOF_LONG == 8) |
| 2506 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 2507 | #elif (SIZEOF_LONG == 4) |
| 2508 | # define ASCII_CHAR_MASK 0x80808080L |
| 2509 | #else |
| 2510 | # error C 'long' size should be either 4 or 8! |
| 2511 | #endif |
| 2512 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2513 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2514 | Py_ssize_t size, |
| 2515 | const char *errors, |
| 2516 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2517 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2518 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2519 | int n; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2520 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2521 | Py_ssize_t startinpos; |
| 2522 | Py_ssize_t endinpos; |
| 2523 | Py_ssize_t outpos; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2524 | const char *e, *aligned_end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2525 | PyUnicodeObject *unicode; |
| 2526 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2527 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2528 | PyObject *errorHandler = NULL; |
| 2529 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2530 | |
| 2531 | /* Note: size will always be longer than the resulting Unicode |
| 2532 | character count */ |
| 2533 | unicode = _PyUnicode_New(size); |
| 2534 | if (!unicode) |
| 2535 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2536 | if (size == 0) { |
| 2537 | if (consumed) |
| 2538 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2539 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2540 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2541 | |
| 2542 | /* Unpack UTF-8 encoded data */ |
| 2543 | p = unicode->str; |
| 2544 | e = s + size; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2545 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2546 | |
| 2547 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2548 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2549 | |
| 2550 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2551 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 2552 | input will consist of an overwhelming majority of ASCII |
| 2553 | characters, we try to optimize for this case by checking |
| 2554 | as many characters as a C 'long' can contain. |
| 2555 | First, check if we can do an aligned read, as most CPUs have |
| 2556 | a penalty for unaligned reads. |
| 2557 | */ |
| 2558 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 2559 | /* Help register allocation */ |
| 2560 | register const char *_s = s; |
| 2561 | register Py_UNICODE *_p = p; |
| 2562 | while (_s < aligned_end) { |
| 2563 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 2564 | and do a fast unrolled copy if it only contains ASCII |
| 2565 | characters. */ |
| 2566 | unsigned long data = *(unsigned long *) _s; |
| 2567 | if (data & ASCII_CHAR_MASK) |
| 2568 | break; |
| 2569 | _p[0] = (unsigned char) _s[0]; |
| 2570 | _p[1] = (unsigned char) _s[1]; |
| 2571 | _p[2] = (unsigned char) _s[2]; |
| 2572 | _p[3] = (unsigned char) _s[3]; |
| 2573 | #if (SIZEOF_LONG == 8) |
| 2574 | _p[4] = (unsigned char) _s[4]; |
| 2575 | _p[5] = (unsigned char) _s[5]; |
| 2576 | _p[6] = (unsigned char) _s[6]; |
| 2577 | _p[7] = (unsigned char) _s[7]; |
| 2578 | #endif |
| 2579 | _s += SIZEOF_LONG; |
| 2580 | _p += SIZEOF_LONG; |
| 2581 | } |
| 2582 | s = _s; |
| 2583 | p = _p; |
| 2584 | if (s == e) |
| 2585 | break; |
| 2586 | ch = (unsigned char)*s; |
| 2587 | } |
| 2588 | } |
| 2589 | |
| 2590 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2591 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2592 | s++; |
| 2593 | continue; |
| 2594 | } |
| 2595 | |
| 2596 | n = utf8_code_length[ch]; |
| 2597 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2598 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2599 | if (consumed) |
| 2600 | break; |
| 2601 | else { |
| 2602 | errmsg = "unexpected end of data"; |
| 2603 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2604 | endinpos = startinpos+1; |
| 2605 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 2606 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2607 | goto utf8Error; |
| 2608 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2609 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2610 | |
| 2611 | switch (n) { |
| 2612 | |
| 2613 | case 0: |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2614 | errmsg = "invalid start byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2615 | startinpos = s-starts; |
| 2616 | endinpos = startinpos+1; |
| 2617 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2618 | |
| 2619 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2620 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2621 | startinpos = s-starts; |
| 2622 | endinpos = startinpos+1; |
| 2623 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2624 | |
| 2625 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2626 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2627 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2628 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2629 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2630 | goto utf8Error; |
| 2631 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2632 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2633 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 2634 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2635 | break; |
| 2636 | |
| 2637 | case 3: |
Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 2638 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 2639 | will result in surrogates in range d800-dfff. Surrogates are |
| 2640 | not valid UTF-8 so they are rejected. |
| 2641 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 2642 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2643 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2644 | (s[2] & 0xc0) != 0x80 || |
| 2645 | ((unsigned char)s[0] == 0xE0 && |
| 2646 | (unsigned char)s[1] < 0xA0) || |
| 2647 | ((unsigned char)s[0] == 0xED && |
| 2648 | (unsigned char)s[1] > 0x9F)) { |
| 2649 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2650 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2651 | endinpos = startinpos + 1; |
| 2652 | |
| 2653 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 2654 | continuation byte is s[2], so increment endinpos by 1, |
| 2655 | if not, s[1] is invalid and endinpos doesn't need to |
| 2656 | be incremented. */ |
| 2657 | if ((s[1] & 0xC0) == 0x80) |
| 2658 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2659 | goto utf8Error; |
| 2660 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2661 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2662 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
| 2663 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2664 | break; |
| 2665 | |
| 2666 | case 4: |
| 2667 | if ((s[1] & 0xc0) != 0x80 || |
| 2668 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2669 | (s[3] & 0xc0) != 0x80 || |
| 2670 | ((unsigned char)s[0] == 0xF0 && |
| 2671 | (unsigned char)s[1] < 0x90) || |
| 2672 | ((unsigned char)s[0] == 0xF4 && |
| 2673 | (unsigned char)s[1] > 0x8F)) { |
| 2674 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2675 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2676 | endinpos = startinpos + 1; |
| 2677 | if ((s[1] & 0xC0) == 0x80) { |
| 2678 | endinpos++; |
| 2679 | if ((s[2] & 0xC0) == 0x80) |
| 2680 | endinpos++; |
| 2681 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2682 | goto utf8Error; |
| 2683 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2684 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2685 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 2686 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 2687 | |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2688 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2689 | *p++ = (Py_UNICODE)ch; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2690 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2691 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2692 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2693 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 2694 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2695 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2696 | /* high surrogate = top 10 bits added to D800 */ |
| 2697 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2698 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2699 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2700 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2701 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2702 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2703 | } |
| 2704 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2705 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2706 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2707 | utf8Error: |
| 2708 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2709 | if (unicode_decode_call_errorhandler( |
| 2710 | errors, &errorHandler, |
| 2711 | "utf8", errmsg, |
| 2712 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2713 | &unicode, &outpos, &p)) |
| 2714 | goto onError; |
| 2715 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2716 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2717 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2718 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2719 | |
| 2720 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2721 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2722 | goto onError; |
| 2723 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2724 | Py_XDECREF(errorHandler); |
| 2725 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2726 | return (PyObject *)unicode; |
| 2727 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2728 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2729 | Py_XDECREF(errorHandler); |
| 2730 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2731 | Py_DECREF(unicode); |
| 2732 | return NULL; |
| 2733 | } |
| 2734 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2735 | #undef ASCII_CHAR_MASK |
| 2736 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 2737 | #ifdef __APPLE__ |
| 2738 | |
| 2739 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
| 2740 | used to decode the command line arguments on Mac OS X. */ |
| 2741 | |
| 2742 | wchar_t* |
| 2743 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 2744 | { |
| 2745 | int n; |
| 2746 | const char *e; |
| 2747 | wchar_t *unicode, *p; |
| 2748 | |
| 2749 | /* Note: size will always be longer than the resulting Unicode |
| 2750 | character count */ |
| 2751 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) { |
| 2752 | PyErr_NoMemory(); |
| 2753 | return NULL; |
| 2754 | } |
| 2755 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 2756 | if (!unicode) |
| 2757 | return NULL; |
| 2758 | |
| 2759 | /* Unpack UTF-8 encoded data */ |
| 2760 | p = unicode; |
| 2761 | e = s + size; |
| 2762 | while (s < e) { |
| 2763 | Py_UCS4 ch = (unsigned char)*s; |
| 2764 | |
| 2765 | if (ch < 0x80) { |
| 2766 | *p++ = (wchar_t)ch; |
| 2767 | s++; |
| 2768 | continue; |
| 2769 | } |
| 2770 | |
| 2771 | n = utf8_code_length[ch]; |
| 2772 | if (s + n > e) { |
| 2773 | goto surrogateescape; |
| 2774 | } |
| 2775 | |
| 2776 | switch (n) { |
| 2777 | case 0: |
| 2778 | case 1: |
| 2779 | goto surrogateescape; |
| 2780 | |
| 2781 | case 2: |
| 2782 | if ((s[1] & 0xc0) != 0x80) |
| 2783 | goto surrogateescape; |
| 2784 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
| 2785 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 2786 | *p++ = (wchar_t)ch; |
| 2787 | break; |
| 2788 | |
| 2789 | case 3: |
| 2790 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 2791 | will result in surrogates in range d800-dfff. Surrogates are |
| 2792 | not valid UTF-8 so they are rejected. |
| 2793 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 2794 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
| 2795 | if ((s[1] & 0xc0) != 0x80 || |
| 2796 | (s[2] & 0xc0) != 0x80 || |
| 2797 | ((unsigned char)s[0] == 0xE0 && |
| 2798 | (unsigned char)s[1] < 0xA0) || |
| 2799 | ((unsigned char)s[0] == 0xED && |
| 2800 | (unsigned char)s[1] > 0x9F)) { |
| 2801 | |
| 2802 | goto surrogateescape; |
| 2803 | } |
| 2804 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
| 2805 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
| 2806 | *p++ = (Py_UNICODE)ch; |
| 2807 | break; |
| 2808 | |
| 2809 | case 4: |
| 2810 | if ((s[1] & 0xc0) != 0x80 || |
| 2811 | (s[2] & 0xc0) != 0x80 || |
| 2812 | (s[3] & 0xc0) != 0x80 || |
| 2813 | ((unsigned char)s[0] == 0xF0 && |
| 2814 | (unsigned char)s[1] < 0x90) || |
| 2815 | ((unsigned char)s[0] == 0xF4 && |
| 2816 | (unsigned char)s[1] > 0x8F)) { |
| 2817 | goto surrogateescape; |
| 2818 | } |
| 2819 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 2820 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 2821 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 2822 | |
| 2823 | #if SIZEOF_WCHAR_T == 4 |
| 2824 | *p++ = (wchar_t)ch; |
| 2825 | #else |
| 2826 | /* compute and append the two surrogates: */ |
| 2827 | |
| 2828 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 2829 | ch -= 0x10000; |
| 2830 | |
| 2831 | /* high surrogate = top 10 bits added to D800 */ |
| 2832 | *p++ = (wchar_t)(0xD800 + (ch >> 10)); |
| 2833 | |
| 2834 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 2835 | *p++ = (wchar_t)(0xDC00 + (ch & 0x03FF)); |
| 2836 | #endif |
| 2837 | break; |
| 2838 | } |
| 2839 | s += n; |
| 2840 | continue; |
| 2841 | |
| 2842 | surrogateescape: |
| 2843 | *p++ = 0xDC00 + ch; |
| 2844 | s++; |
| 2845 | } |
| 2846 | *p = L'\0'; |
| 2847 | return unicode; |
| 2848 | } |
| 2849 | |
| 2850 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2851 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2852 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 2853 | and allocate exactly as much space needed at the end. Else allocate the |
| 2854 | maximum possible needed (4 result bytes per Unicode character), and return |
| 2855 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2856 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 2857 | PyObject * |
| 2858 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2859 | Py_ssize_t size, |
| 2860 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2861 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2862 | #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] | 2863 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2864 | Py_ssize_t i; /* index into s of next input byte */ |
| 2865 | PyObject *result; /* result string object */ |
| 2866 | char *p; /* next free byte in output buffer */ |
| 2867 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 2868 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2869 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2870 | PyObject *errorHandler = NULL; |
| 2871 | PyObject *exc = NULL; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2872 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2873 | assert(s != NULL); |
| 2874 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2875 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2876 | if (size <= MAX_SHORT_UNICHARS) { |
| 2877 | /* Write into the stack buffer; nallocated can't overflow. |
| 2878 | * At the end, we'll allocate exactly as much heap space as it |
| 2879 | * turns out we need. |
| 2880 | */ |
| 2881 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2882 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2883 | p = stackbuf; |
| 2884 | } |
| 2885 | else { |
| 2886 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 2887 | nallocated = size * 4; |
| 2888 | if (nallocated / 4 != size) /* overflow! */ |
| 2889 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2890 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2891 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2892 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2893 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2894 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2895 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2896 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2897 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2898 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2899 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2900 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2901 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2902 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2903 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2904 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 2905 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 2906 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2907 | } else if (0xD800 <= ch && ch <= 0xDFFF) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2908 | #ifndef Py_UNICODE_WIDE |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2909 | /* Special case: check for high and low surrogate */ |
| 2910 | if (ch <= 0xDBFF && i != size && 0xDC00 <= s[i] && s[i] <= 0xDFFF) { |
| 2911 | Py_UCS4 ch2 = s[i]; |
| 2912 | /* Combine the two surrogates to form a UCS4 value */ |
| 2913 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
| 2914 | i++; |
| 2915 | |
| 2916 | /* Encode UCS4 Unicode ordinals */ |
| 2917 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2918 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2919 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2920 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2921 | } else { |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 2922 | #endif |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2923 | Py_ssize_t newpos; |
| 2924 | PyObject *rep; |
| 2925 | Py_ssize_t repsize, k; |
| 2926 | rep = unicode_encode_call_errorhandler |
| 2927 | (errors, &errorHandler, "utf-8", "surrogates not allowed", |
| 2928 | s, size, &exc, i-1, i, &newpos); |
| 2929 | if (!rep) |
| 2930 | goto error; |
| 2931 | |
| 2932 | if (PyBytes_Check(rep)) |
| 2933 | repsize = PyBytes_GET_SIZE(rep); |
| 2934 | else |
| 2935 | repsize = PyUnicode_GET_SIZE(rep); |
| 2936 | |
| 2937 | if (repsize > 4) { |
| 2938 | Py_ssize_t offset; |
| 2939 | |
| 2940 | if (result == NULL) |
| 2941 | offset = p - stackbuf; |
| 2942 | else |
| 2943 | offset = p - PyBytes_AS_STRING(result); |
| 2944 | |
| 2945 | if (nallocated > PY_SSIZE_T_MAX - repsize + 4) { |
| 2946 | /* integer overflow */ |
| 2947 | PyErr_NoMemory(); |
| 2948 | goto error; |
| 2949 | } |
| 2950 | nallocated += repsize - 4; |
| 2951 | if (result != NULL) { |
| 2952 | if (_PyBytes_Resize(&result, nallocated) < 0) |
| 2953 | goto error; |
| 2954 | } else { |
| 2955 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
| 2956 | if (result == NULL) |
| 2957 | goto error; |
| 2958 | Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset); |
| 2959 | } |
| 2960 | p = PyBytes_AS_STRING(result) + offset; |
| 2961 | } |
| 2962 | |
| 2963 | if (PyBytes_Check(rep)) { |
| 2964 | char *prep = PyBytes_AS_STRING(rep); |
| 2965 | for(k = repsize; k > 0; k--) |
| 2966 | *p++ = *prep++; |
| 2967 | } else /* rep is unicode */ { |
| 2968 | Py_UNICODE *prep = PyUnicode_AS_UNICODE(rep); |
| 2969 | Py_UNICODE c; |
| 2970 | |
| 2971 | for(k=0; k<repsize; k++) { |
| 2972 | c = prep[k]; |
| 2973 | if (0x80 <= c) { |
| 2974 | raise_encode_exception(&exc, "utf-8", s, size, |
| 2975 | i-1, i, "surrogates not allowed"); |
| 2976 | goto error; |
| 2977 | } |
| 2978 | *p++ = (char)prep[k]; |
| 2979 | } |
| 2980 | } |
| 2981 | Py_DECREF(rep); |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 2982 | #ifndef Py_UNICODE_WIDE |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2983 | } |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 2984 | #endif |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2985 | } else if (ch < 0x10000) { |
| 2986 | *p++ = (char)(0xe0 | (ch >> 12)); |
| 2987 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2988 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2989 | } else /* ch >= 0x10000 */ { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2990 | /* Encode UCS4 Unicode ordinals */ |
| 2991 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2992 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 2993 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2994 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2995 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2996 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 2997 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2998 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2999 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3000 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 3001 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3002 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 3003 | } |
| 3004 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3005 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3006 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 3007 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3008 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 3009 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 3010 | Py_XDECREF(errorHandler); |
| 3011 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3012 | return result; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 3013 | error: |
| 3014 | Py_XDECREF(errorHandler); |
| 3015 | Py_XDECREF(exc); |
| 3016 | Py_XDECREF(result); |
| 3017 | return NULL; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 3018 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 3019 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3020 | } |
| 3021 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3022 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 3023 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3024 | if (!PyUnicode_Check(unicode)) { |
| 3025 | PyErr_BadArgument(); |
| 3026 | return NULL; |
| 3027 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 3028 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3029 | PyUnicode_GET_SIZE(unicode), |
| 3030 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3031 | } |
| 3032 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3033 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 3034 | |
| 3035 | PyObject * |
| 3036 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3037 | Py_ssize_t size, |
| 3038 | const char *errors, |
| 3039 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3040 | { |
| 3041 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 3042 | } |
| 3043 | |
| 3044 | PyObject * |
| 3045 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3046 | Py_ssize_t size, |
| 3047 | const char *errors, |
| 3048 | int *byteorder, |
| 3049 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3050 | { |
| 3051 | const char *starts = s; |
| 3052 | Py_ssize_t startinpos; |
| 3053 | Py_ssize_t endinpos; |
| 3054 | Py_ssize_t outpos; |
| 3055 | PyUnicodeObject *unicode; |
| 3056 | Py_UNICODE *p; |
| 3057 | #ifndef Py_UNICODE_WIDE |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 3058 | int pairs = 0; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 3059 | const unsigned char *qq; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3060 | #else |
| 3061 | const int pairs = 0; |
| 3062 | #endif |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 3063 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3064 | int bo = 0; /* assume native ordering by default */ |
| 3065 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3066 | /* Offsets from q for retrieving bytes in the right order. */ |
| 3067 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3068 | int iorder[] = {0, 1, 2, 3}; |
| 3069 | #else |
| 3070 | int iorder[] = {3, 2, 1, 0}; |
| 3071 | #endif |
| 3072 | PyObject *errorHandler = NULL; |
| 3073 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 3074 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3075 | q = (unsigned char *)s; |
| 3076 | e = q + size; |
| 3077 | |
| 3078 | if (byteorder) |
| 3079 | bo = *byteorder; |
| 3080 | |
| 3081 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 3082 | byte order setting accordingly. In native mode, the leading BOM |
| 3083 | mark is skipped, in all other modes, it is copied to the output |
| 3084 | stream as-is (giving a ZWNBSP character). */ |
| 3085 | if (bo == 0) { |
| 3086 | if (size >= 4) { |
| 3087 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3088 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3089 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3090 | if (bom == 0x0000FEFF) { |
| 3091 | q += 4; |
| 3092 | bo = -1; |
| 3093 | } |
| 3094 | else if (bom == 0xFFFE0000) { |
| 3095 | q += 4; |
| 3096 | bo = 1; |
| 3097 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3098 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3099 | if (bom == 0x0000FEFF) { |
| 3100 | q += 4; |
| 3101 | bo = 1; |
| 3102 | } |
| 3103 | else if (bom == 0xFFFE0000) { |
| 3104 | q += 4; |
| 3105 | bo = -1; |
| 3106 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3107 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3108 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3109 | } |
| 3110 | |
| 3111 | if (bo == -1) { |
| 3112 | /* force LE */ |
| 3113 | iorder[0] = 0; |
| 3114 | iorder[1] = 1; |
| 3115 | iorder[2] = 2; |
| 3116 | iorder[3] = 3; |
| 3117 | } |
| 3118 | else if (bo == 1) { |
| 3119 | /* force BE */ |
| 3120 | iorder[0] = 3; |
| 3121 | iorder[1] = 2; |
| 3122 | iorder[2] = 1; |
| 3123 | iorder[3] = 0; |
| 3124 | } |
| 3125 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 3126 | /* On narrow builds we split characters outside the BMP into two |
| 3127 | codepoints => count how much extra space we need. */ |
| 3128 | #ifndef Py_UNICODE_WIDE |
| 3129 | for (qq = q; qq < e; qq += 4) |
| 3130 | if (qq[iorder[2]] != 0 || qq[iorder[3]] != 0) |
| 3131 | pairs++; |
| 3132 | #endif |
| 3133 | |
| 3134 | /* This might be one to much, because of a BOM */ |
| 3135 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 3136 | if (!unicode) |
| 3137 | return NULL; |
| 3138 | if (size == 0) |
| 3139 | return (PyObject *)unicode; |
| 3140 | |
| 3141 | /* Unpack UTF-32 encoded data */ |
| 3142 | p = unicode->str; |
| 3143 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3144 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3145 | Py_UCS4 ch; |
| 3146 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 3147 | if (e-q<4) { |
| 3148 | if (consumed) |
| 3149 | break; |
| 3150 | errmsg = "truncated data"; |
| 3151 | startinpos = ((const char *)q)-starts; |
| 3152 | endinpos = ((const char *)e)-starts; |
| 3153 | goto utf32Error; |
| 3154 | /* The remaining input chars are ignored if the callback |
| 3155 | chooses to skip the input */ |
| 3156 | } |
| 3157 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 3158 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3159 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3160 | if (ch >= 0x110000) |
| 3161 | { |
| 3162 | errmsg = "codepoint not in range(0x110000)"; |
| 3163 | startinpos = ((const char *)q)-starts; |
| 3164 | endinpos = startinpos+4; |
| 3165 | goto utf32Error; |
| 3166 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3167 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3168 | if (ch >= 0x10000) |
| 3169 | { |
| 3170 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 3171 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 3172 | } |
| 3173 | else |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3174 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3175 | *p++ = ch; |
| 3176 | q += 4; |
| 3177 | continue; |
| 3178 | utf32Error: |
| 3179 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 3180 | if (unicode_decode_call_errorhandler( |
| 3181 | errors, &errorHandler, |
| 3182 | "utf32", errmsg, |
| 3183 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
| 3184 | &unicode, &outpos, &p)) |
| 3185 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3186 | } |
| 3187 | |
| 3188 | if (byteorder) |
| 3189 | *byteorder = bo; |
| 3190 | |
| 3191 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3192 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3193 | |
| 3194 | /* Adjust length */ |
| 3195 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
| 3196 | goto onError; |
| 3197 | |
| 3198 | Py_XDECREF(errorHandler); |
| 3199 | Py_XDECREF(exc); |
| 3200 | return (PyObject *)unicode; |
| 3201 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3202 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3203 | Py_DECREF(unicode); |
| 3204 | Py_XDECREF(errorHandler); |
| 3205 | Py_XDECREF(exc); |
| 3206 | return NULL; |
| 3207 | } |
| 3208 | |
| 3209 | PyObject * |
| 3210 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3211 | Py_ssize_t size, |
| 3212 | const char *errors, |
| 3213 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3214 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3215 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3216 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3217 | Py_ssize_t nsize, bytesize; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3218 | #ifndef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3219 | Py_ssize_t i, pairs; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3220 | #else |
| 3221 | const int pairs = 0; |
| 3222 | #endif |
| 3223 | /* Offsets from p for storing byte pairs in the right order. */ |
| 3224 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3225 | int iorder[] = {0, 1, 2, 3}; |
| 3226 | #else |
| 3227 | int iorder[] = {3, 2, 1, 0}; |
| 3228 | #endif |
| 3229 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3230 | #define STORECHAR(CH) \ |
| 3231 | do { \ |
| 3232 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 3233 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 3234 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 3235 | p[iorder[0]] = (CH) & 0xff; \ |
| 3236 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3237 | } while(0) |
| 3238 | |
| 3239 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 3240 | so we need less space. */ |
| 3241 | #ifndef Py_UNICODE_WIDE |
| 3242 | for (i = pairs = 0; i < size-1; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3243 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 3244 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 3245 | pairs++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3246 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3247 | nsize = (size - pairs + (byteorder == 0)); |
| 3248 | bytesize = nsize * 4; |
| 3249 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3250 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3251 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3252 | if (v == NULL) |
| 3253 | return NULL; |
| 3254 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3255 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3256 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3257 | STORECHAR(0xFEFF); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3258 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3259 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3260 | |
| 3261 | if (byteorder == -1) { |
| 3262 | /* force LE */ |
| 3263 | iorder[0] = 0; |
| 3264 | iorder[1] = 1; |
| 3265 | iorder[2] = 2; |
| 3266 | iorder[3] = 3; |
| 3267 | } |
| 3268 | else if (byteorder == 1) { |
| 3269 | /* force BE */ |
| 3270 | iorder[0] = 3; |
| 3271 | iorder[1] = 2; |
| 3272 | iorder[2] = 1; |
| 3273 | iorder[3] = 0; |
| 3274 | } |
| 3275 | |
| 3276 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3277 | Py_UCS4 ch = *s++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3278 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3279 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 3280 | Py_UCS4 ch2 = *s; |
| 3281 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 3282 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 3283 | s++; |
| 3284 | size--; |
| 3285 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3286 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3287 | #endif |
| 3288 | STORECHAR(ch); |
| 3289 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3290 | |
| 3291 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3292 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3293 | #undef STORECHAR |
| 3294 | } |
| 3295 | |
| 3296 | PyObject *PyUnicode_AsUTF32String(PyObject *unicode) |
| 3297 | { |
| 3298 | if (!PyUnicode_Check(unicode)) { |
| 3299 | PyErr_BadArgument(); |
| 3300 | return NULL; |
| 3301 | } |
| 3302 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3303 | PyUnicode_GET_SIZE(unicode), |
| 3304 | NULL, |
| 3305 | 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3306 | } |
| 3307 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3308 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 3309 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3310 | PyObject * |
| 3311 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3312 | Py_ssize_t size, |
| 3313 | const char *errors, |
| 3314 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3315 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3316 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 3317 | } |
| 3318 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3319 | /* Two masks for fast checking of whether a C 'long' may contain |
| 3320 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 3321 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 3322 | rare in most input. |
| 3323 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 3324 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3325 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3326 | #if (SIZEOF_LONG == 8) |
| 3327 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 3328 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 3329 | #elif (SIZEOF_LONG == 4) |
| 3330 | # define FAST_CHAR_MASK 0x80008000L |
| 3331 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 3332 | #else |
| 3333 | # error C 'long' size should be either 4 or 8! |
| 3334 | #endif |
| 3335 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3336 | PyObject * |
| 3337 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3338 | Py_ssize_t size, |
| 3339 | const char *errors, |
| 3340 | int *byteorder, |
| 3341 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3342 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3343 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3344 | Py_ssize_t startinpos; |
| 3345 | Py_ssize_t endinpos; |
| 3346 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3347 | PyUnicodeObject *unicode; |
| 3348 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3349 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3350 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3351 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 3352 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3353 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 3354 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3355 | int ihi = 1, ilo = 0; |
| 3356 | #else |
| 3357 | int ihi = 0, ilo = 1; |
| 3358 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3359 | PyObject *errorHandler = NULL; |
| 3360 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3361 | |
| 3362 | /* Note: size will always be longer than the resulting Unicode |
| 3363 | character count */ |
| 3364 | unicode = _PyUnicode_New(size); |
| 3365 | if (!unicode) |
| 3366 | return NULL; |
| 3367 | if (size == 0) |
| 3368 | return (PyObject *)unicode; |
| 3369 | |
| 3370 | /* Unpack UTF-16 encoded data */ |
| 3371 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3372 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3373 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3374 | |
| 3375 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3376 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3377 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3378 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 3379 | byte order setting accordingly. In native mode, the leading BOM |
| 3380 | mark is skipped, in all other modes, it is copied to the output |
| 3381 | stream as-is (giving a ZWNBSP character). */ |
| 3382 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3383 | if (size >= 2) { |
| 3384 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3385 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3386 | if (bom == 0xFEFF) { |
| 3387 | q += 2; |
| 3388 | bo = -1; |
| 3389 | } |
| 3390 | else if (bom == 0xFFFE) { |
| 3391 | q += 2; |
| 3392 | bo = 1; |
| 3393 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3394 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3395 | if (bom == 0xFEFF) { |
| 3396 | q += 2; |
| 3397 | bo = 1; |
| 3398 | } |
| 3399 | else if (bom == 0xFFFE) { |
| 3400 | q += 2; |
| 3401 | bo = -1; |
| 3402 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3403 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3404 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3405 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3406 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3407 | if (bo == -1) { |
| 3408 | /* force LE */ |
| 3409 | ihi = 1; |
| 3410 | ilo = 0; |
| 3411 | } |
| 3412 | else if (bo == 1) { |
| 3413 | /* force BE */ |
| 3414 | ihi = 0; |
| 3415 | ilo = 1; |
| 3416 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3417 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3418 | native_ordering = ilo < ihi; |
| 3419 | #else |
| 3420 | native_ordering = ilo > ihi; |
| 3421 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3422 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3423 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3424 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3425 | Py_UNICODE ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3426 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 3427 | reads are more expensive, better to defer to another iteration. */ |
| 3428 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 3429 | /* Fast path for runs of non-surrogate chars. */ |
| 3430 | register const unsigned char *_q = q; |
| 3431 | Py_UNICODE *_p = p; |
| 3432 | if (native_ordering) { |
| 3433 | /* Native ordering is simple: as long as the input cannot |
| 3434 | possibly contain a surrogate char, do an unrolled copy |
| 3435 | of several 16-bit code points to the target object. |
| 3436 | The non-surrogate check is done on several input bytes |
| 3437 | at a time (as many as a C 'long' can contain). */ |
| 3438 | while (_q < aligned_end) { |
| 3439 | unsigned long data = * (unsigned long *) _q; |
| 3440 | if (data & FAST_CHAR_MASK) |
| 3441 | break; |
| 3442 | _p[0] = ((unsigned short *) _q)[0]; |
| 3443 | _p[1] = ((unsigned short *) _q)[1]; |
| 3444 | #if (SIZEOF_LONG == 8) |
| 3445 | _p[2] = ((unsigned short *) _q)[2]; |
| 3446 | _p[3] = ((unsigned short *) _q)[3]; |
| 3447 | #endif |
| 3448 | _q += SIZEOF_LONG; |
| 3449 | _p += SIZEOF_LONG / 2; |
| 3450 | } |
| 3451 | } |
| 3452 | else { |
| 3453 | /* Byteswapped ordering is similar, but we must decompose |
| 3454 | the copy bytewise, and take care of zero'ing out the |
| 3455 | upper bytes if the target object is in 32-bit units |
| 3456 | (that is, in UCS-4 builds). */ |
| 3457 | while (_q < aligned_end) { |
| 3458 | unsigned long data = * (unsigned long *) _q; |
| 3459 | if (data & SWAPPED_FAST_CHAR_MASK) |
| 3460 | break; |
| 3461 | /* Zero upper bytes in UCS-4 builds */ |
| 3462 | #if (Py_UNICODE_SIZE > 2) |
| 3463 | _p[0] = 0; |
| 3464 | _p[1] = 0; |
| 3465 | #if (SIZEOF_LONG == 8) |
| 3466 | _p[2] = 0; |
| 3467 | _p[3] = 0; |
| 3468 | #endif |
| 3469 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 3470 | /* Issue #4916; UCS-4 builds on big endian machines must |
| 3471 | fill the two last bytes of each 4-byte unit. */ |
| 3472 | #if (!defined(BYTEORDER_IS_LITTLE_ENDIAN) && Py_UNICODE_SIZE > 2) |
| 3473 | # define OFF 2 |
| 3474 | #else |
| 3475 | # define OFF 0 |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3476 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 3477 | ((unsigned char *) _p)[OFF + 1] = _q[0]; |
| 3478 | ((unsigned char *) _p)[OFF + 0] = _q[1]; |
| 3479 | ((unsigned char *) _p)[OFF + 1 + Py_UNICODE_SIZE] = _q[2]; |
| 3480 | ((unsigned char *) _p)[OFF + 0 + Py_UNICODE_SIZE] = _q[3]; |
| 3481 | #if (SIZEOF_LONG == 8) |
| 3482 | ((unsigned char *) _p)[OFF + 1 + 2 * Py_UNICODE_SIZE] = _q[4]; |
| 3483 | ((unsigned char *) _p)[OFF + 0 + 2 * Py_UNICODE_SIZE] = _q[5]; |
| 3484 | ((unsigned char *) _p)[OFF + 1 + 3 * Py_UNICODE_SIZE] = _q[6]; |
| 3485 | ((unsigned char *) _p)[OFF + 0 + 3 * Py_UNICODE_SIZE] = _q[7]; |
| 3486 | #endif |
| 3487 | #undef OFF |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3488 | _q += SIZEOF_LONG; |
| 3489 | _p += SIZEOF_LONG / 2; |
| 3490 | } |
| 3491 | } |
| 3492 | p = _p; |
| 3493 | q = _q; |
| 3494 | if (q >= e) |
| 3495 | break; |
| 3496 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3497 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3498 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3499 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3500 | |
| 3501 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 3502 | *p++ = ch; |
| 3503 | continue; |
| 3504 | } |
| 3505 | |
| 3506 | /* UTF-16 code pair: */ |
| 3507 | if (q > e) { |
| 3508 | errmsg = "unexpected end of data"; |
| 3509 | startinpos = (((const char *)q) - 2) - starts; |
| 3510 | endinpos = ((const char *)e) + 1 - starts; |
| 3511 | goto utf16Error; |
| 3512 | } |
| 3513 | if (0xD800 <= ch && ch <= 0xDBFF) { |
| 3514 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 3515 | q += 2; |
| 3516 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 3517 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3518 | *p++ = ch; |
| 3519 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3520 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3521 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3522 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3523 | continue; |
| 3524 | } |
| 3525 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3526 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3527 | startinpos = (((const char *)q)-4)-starts; |
| 3528 | endinpos = startinpos+2; |
| 3529 | goto utf16Error; |
| 3530 | } |
| 3531 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3532 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3533 | errmsg = "illegal encoding"; |
| 3534 | startinpos = (((const char *)q)-2)-starts; |
| 3535 | endinpos = startinpos+2; |
| 3536 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3537 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3538 | utf16Error: |
| 3539 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 3540 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3541 | errors, |
| 3542 | &errorHandler, |
| 3543 | "utf16", errmsg, |
| 3544 | &starts, |
| 3545 | (const char **)&e, |
| 3546 | &startinpos, |
| 3547 | &endinpos, |
| 3548 | &exc, |
| 3549 | (const char **)&q, |
| 3550 | &unicode, |
| 3551 | &outpos, |
| 3552 | &p)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3553 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3554 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3555 | /* remaining byte at the end? (size should be even) */ |
| 3556 | if (e == q) { |
| 3557 | if (!consumed) { |
| 3558 | errmsg = "truncated data"; |
| 3559 | startinpos = ((const char *)q) - starts; |
| 3560 | endinpos = ((const char *)e) + 1 - starts; |
| 3561 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 3562 | if (unicode_decode_call_errorhandler( |
| 3563 | errors, |
| 3564 | &errorHandler, |
| 3565 | "utf16", errmsg, |
| 3566 | &starts, |
| 3567 | (const char **)&e, |
| 3568 | &startinpos, |
| 3569 | &endinpos, |
| 3570 | &exc, |
| 3571 | (const char **)&q, |
| 3572 | &unicode, |
| 3573 | &outpos, |
| 3574 | &p)) |
| 3575 | goto onError; |
| 3576 | /* The remaining input chars are ignored if the callback |
| 3577 | chooses to skip the input */ |
| 3578 | } |
| 3579 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3580 | |
| 3581 | if (byteorder) |
| 3582 | *byteorder = bo; |
| 3583 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3584 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3585 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3586 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3587 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 3588 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3589 | goto onError; |
| 3590 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3591 | Py_XDECREF(errorHandler); |
| 3592 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3593 | return (PyObject *)unicode; |
| 3594 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3595 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3596 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3597 | Py_XDECREF(errorHandler); |
| 3598 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3599 | return NULL; |
| 3600 | } |
| 3601 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3602 | #undef FAST_CHAR_MASK |
| 3603 | #undef SWAPPED_FAST_CHAR_MASK |
| 3604 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3605 | PyObject * |
| 3606 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3607 | Py_ssize_t size, |
| 3608 | const char *errors, |
| 3609 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3610 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3611 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3612 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3613 | Py_ssize_t nsize, bytesize; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3614 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3615 | Py_ssize_t i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3616 | #else |
| 3617 | const int pairs = 0; |
| 3618 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3619 | /* Offsets from p for storing byte pairs in the right order. */ |
| 3620 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3621 | int ihi = 1, ilo = 0; |
| 3622 | #else |
| 3623 | int ihi = 0, ilo = 1; |
| 3624 | #endif |
| 3625 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3626 | #define STORECHAR(CH) \ |
| 3627 | do { \ |
| 3628 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 3629 | p[ilo] = (CH) & 0xff; \ |
| 3630 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3631 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3632 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3633 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3634 | for (i = pairs = 0; i < size; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3635 | if (s[i] >= 0x10000) |
| 3636 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3637 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3638 | /* 2 * (size + pairs + (byteorder == 0)) */ |
| 3639 | if (size > PY_SSIZE_T_MAX || |
| 3640 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3641 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3642 | nsize = size + pairs + (byteorder == 0); |
| 3643 | bytesize = nsize * 2; |
| 3644 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3645 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3646 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3647 | if (v == NULL) |
| 3648 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3649 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3650 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3651 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3652 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 3653 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3654 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3655 | |
| 3656 | if (byteorder == -1) { |
| 3657 | /* force LE */ |
| 3658 | ihi = 1; |
| 3659 | ilo = 0; |
| 3660 | } |
| 3661 | else if (byteorder == 1) { |
| 3662 | /* force BE */ |
| 3663 | ihi = 0; |
| 3664 | ilo = 1; |
| 3665 | } |
| 3666 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3667 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3668 | Py_UNICODE ch = *s++; |
| 3669 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3670 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3671 | if (ch >= 0x10000) { |
| 3672 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 3673 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 3674 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3675 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3676 | STORECHAR(ch); |
| 3677 | if (ch2) |
| 3678 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3679 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3680 | |
| 3681 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3682 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3683 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3684 | } |
| 3685 | |
| 3686 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 3687 | { |
| 3688 | if (!PyUnicode_Check(unicode)) { |
| 3689 | PyErr_BadArgument(); |
| 3690 | return NULL; |
| 3691 | } |
| 3692 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3693 | PyUnicode_GET_SIZE(unicode), |
| 3694 | NULL, |
| 3695 | 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3696 | } |
| 3697 | |
| 3698 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 3699 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 3700 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 3701 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3702 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3703 | Py_ssize_t size, |
| 3704 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3705 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3706 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3707 | Py_ssize_t startinpos; |
| 3708 | Py_ssize_t endinpos; |
| 3709 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3710 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3711 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3712 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3713 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3714 | char* message; |
| 3715 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3716 | PyObject *errorHandler = NULL; |
| 3717 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3718 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3719 | /* Escaped strings will always be longer than the resulting |
| 3720 | 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] | 3721 | length after conversion to the true value. |
| 3722 | (but if the error callback returns a long replacement string |
| 3723 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3724 | v = _PyUnicode_New(size); |
| 3725 | if (v == NULL) |
| 3726 | goto onError; |
| 3727 | if (size == 0) |
| 3728 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3729 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3730 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3731 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3732 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3733 | while (s < end) { |
| 3734 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 3735 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3736 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3737 | |
| 3738 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3739 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3740 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3741 | continue; |
| 3742 | } |
| 3743 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3744 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3745 | /* \ - Escapes */ |
| 3746 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3747 | c = *s++; |
| 3748 | if (s > end) |
| 3749 | c = '\0'; /* Invalid after \ */ |
| 3750 | switch (c) { |
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 | /* \x escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3753 | case '\n': break; |
| 3754 | case '\\': *p++ = '\\'; break; |
| 3755 | case '\'': *p++ = '\''; break; |
| 3756 | case '\"': *p++ = '\"'; break; |
| 3757 | case 'b': *p++ = '\b'; break; |
| 3758 | case 'f': *p++ = '\014'; break; /* FF */ |
| 3759 | case 't': *p++ = '\t'; break; |
| 3760 | case 'n': *p++ = '\n'; break; |
| 3761 | case 'r': *p++ = '\r'; break; |
| 3762 | case 'v': *p++ = '\013'; break; /* VT */ |
| 3763 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 3764 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3765 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3766 | case '0': case '1': case '2': case '3': |
| 3767 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3768 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3769 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3770 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3771 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3772 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3773 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3774 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3775 | break; |
| 3776 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3777 | /* hex escapes */ |
| 3778 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3779 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3780 | digits = 2; |
| 3781 | message = "truncated \\xXX escape"; |
| 3782 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3783 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3784 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3785 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3786 | digits = 4; |
| 3787 | message = "truncated \\uXXXX escape"; |
| 3788 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3789 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3790 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3791 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3792 | digits = 8; |
| 3793 | message = "truncated \\UXXXXXXXX escape"; |
| 3794 | hexescape: |
| 3795 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3796 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3797 | if (s+digits>end) { |
| 3798 | endinpos = size; |
| 3799 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3800 | errors, &errorHandler, |
| 3801 | "unicodeescape", "end of string in escape sequence", |
| 3802 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3803 | &v, &outpos, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3804 | goto onError; |
| 3805 | goto nextByte; |
| 3806 | } |
| 3807 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3808 | c = (unsigned char) s[i]; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 3809 | if (!Py_ISXDIGIT(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3810 | endinpos = (s+i+1)-starts; |
| 3811 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3812 | errors, &errorHandler, |
| 3813 | "unicodeescape", message, |
| 3814 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3815 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3816 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3817 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3818 | } |
| 3819 | chr = (chr<<4) & ~0xF; |
| 3820 | if (c >= '0' && c <= '9') |
| 3821 | chr += c - '0'; |
| 3822 | else if (c >= 'a' && c <= 'f') |
| 3823 | chr += 10 + c - 'a'; |
| 3824 | else |
| 3825 | chr += 10 + c - 'A'; |
| 3826 | } |
| 3827 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 3828 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3829 | /* _decoding_error will have already written into the |
| 3830 | target buffer. */ |
| 3831 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3832 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3833 | /* when we get here, chr is a 32-bit unicode character */ |
| 3834 | if (chr <= 0xffff) |
| 3835 | /* UCS-2 character */ |
| 3836 | *p++ = (Py_UNICODE) chr; |
| 3837 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3838 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3839 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 3840 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3841 | *p++ = chr; |
| 3842 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3843 | chr -= 0x10000L; |
| 3844 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 3845 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3846 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3847 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3848 | endinpos = s-starts; |
| 3849 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3850 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3851 | errors, &errorHandler, |
| 3852 | "unicodeescape", "illegal Unicode character", |
| 3853 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3854 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3855 | goto onError; |
| 3856 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3857 | break; |
| 3858 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3859 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3860 | case 'N': |
| 3861 | message = "malformed \\N character escape"; |
| 3862 | if (ucnhash_CAPI == NULL) { |
| 3863 | /* load the unicode data module */ |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3864 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import(PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3865 | if (ucnhash_CAPI == NULL) |
| 3866 | goto ucnhashError; |
| 3867 | } |
| 3868 | if (*s == '{') { |
| 3869 | const char *start = s+1; |
| 3870 | /* look for the closing brace */ |
| 3871 | while (*s != '}' && s < end) |
| 3872 | s++; |
| 3873 | if (s > start && s < end && *s == '}') { |
| 3874 | /* found a name. look it up in the unicode database */ |
| 3875 | message = "unknown Unicode character name"; |
| 3876 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 3877 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3878 | goto store; |
| 3879 | } |
| 3880 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3881 | endinpos = s-starts; |
| 3882 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3883 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3884 | errors, &errorHandler, |
| 3885 | "unicodeescape", message, |
| 3886 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3887 | &v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3888 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3889 | break; |
| 3890 | |
| 3891 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3892 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3893 | message = "\\ at end of string"; |
| 3894 | s--; |
| 3895 | endinpos = s-starts; |
| 3896 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3897 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3898 | errors, &errorHandler, |
| 3899 | "unicodeescape", message, |
| 3900 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3901 | &v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3902 | goto onError; |
| 3903 | } |
| 3904 | else { |
| 3905 | *p++ = '\\'; |
| 3906 | *p++ = (unsigned char)s[-1]; |
| 3907 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3908 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3909 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3910 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3911 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3912 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3913 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3914 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 3915 | Py_XDECREF(errorHandler); |
| 3916 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3917 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3918 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3919 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 3920 | PyErr_SetString( |
| 3921 | PyExc_UnicodeError, |
| 3922 | "\\N escapes not supported (can't load unicodedata module)" |
| 3923 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3924 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3925 | Py_XDECREF(errorHandler); |
| 3926 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 3927 | return NULL; |
| 3928 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3929 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3930 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3931 | Py_XDECREF(errorHandler); |
| 3932 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3933 | return NULL; |
| 3934 | } |
| 3935 | |
| 3936 | /* Return a Unicode-Escape string version of the Unicode object. |
| 3937 | |
| 3938 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 3939 | appropriate. |
| 3940 | |
| 3941 | */ |
| 3942 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3943 | Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3944 | Py_ssize_t size, |
| 3945 | Py_UNICODE ch) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3946 | { |
| 3947 | /* like wcschr, but doesn't stop at NULL characters */ |
| 3948 | |
| 3949 | while (size-- > 0) { |
| 3950 | if (*s == ch) |
| 3951 | return s; |
| 3952 | s++; |
| 3953 | } |
| 3954 | |
| 3955 | return NULL; |
| 3956 | } |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 3957 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3958 | static const char *hexdigits = "0123456789abcdef"; |
| 3959 | |
| 3960 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3961 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3962 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3963 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3964 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3965 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3966 | #ifdef Py_UNICODE_WIDE |
| 3967 | const Py_ssize_t expandsize = 10; |
| 3968 | #else |
| 3969 | const Py_ssize_t expandsize = 6; |
| 3970 | #endif |
| 3971 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3972 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 3973 | better to choose a different scheme. Perhaps scan the |
| 3974 | first N-chars of the string and allocate based on that size. |
| 3975 | */ |
| 3976 | /* Initial allocation is based on the longest-possible unichr |
| 3977 | escape. |
| 3978 | |
| 3979 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 3980 | unichr, so in this case it's the longest unichr escape. In |
| 3981 | narrow (UTF-16) builds this is five chars per source unichr |
| 3982 | since there are two unichrs in the surrogate pair, so in narrow |
| 3983 | (UTF-16) builds it's not the longest unichr escape. |
| 3984 | |
| 3985 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 3986 | so in the narrow (UTF-16) build case it's the longest unichr |
| 3987 | escape. |
| 3988 | */ |
| 3989 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3990 | if (size == 0) |
| 3991 | return PyBytes_FromStringAndSize(NULL, 0); |
| 3992 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3993 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3994 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3995 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3996 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3997 | 2 |
| 3998 | + expandsize*size |
| 3999 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4000 | if (repr == NULL) |
| 4001 | return NULL; |
| 4002 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4003 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4004 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4005 | while (size-- > 0) { |
| 4006 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 4007 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 4008 | /* Escape backslashes */ |
| 4009 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4010 | *p++ = '\\'; |
| 4011 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 4012 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4013 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 4014 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 4015 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4016 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 4017 | else if (ch >= 0x10000) { |
| 4018 | *p++ = '\\'; |
| 4019 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 4020 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 4021 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 4022 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 4023 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 4024 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 4025 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 4026 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 4027 | *p++ = hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4028 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4029 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4030 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4031 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 4032 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 4033 | Py_UNICODE ch2; |
| 4034 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4035 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4036 | ch2 = *s++; |
| 4037 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 4038 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4039 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 4040 | *p++ = '\\'; |
| 4041 | *p++ = 'U'; |
| 4042 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 4043 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 4044 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 4045 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 4046 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 4047 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 4048 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 4049 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 4050 | continue; |
| 4051 | } |
| 4052 | /* Fall through: isolated surrogates are copied as-is */ |
| 4053 | s--; |
| 4054 | size++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4055 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4056 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 4057 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4058 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 4059 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4060 | *p++ = '\\'; |
| 4061 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 4062 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 4063 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 4064 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 4065 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4066 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 4067 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 4068 | /* Map special whitespace to '\t', \n', '\r' */ |
| 4069 | else if (ch == '\t') { |
| 4070 | *p++ = '\\'; |
| 4071 | *p++ = 't'; |
| 4072 | } |
| 4073 | else if (ch == '\n') { |
| 4074 | *p++ = '\\'; |
| 4075 | *p++ = 'n'; |
| 4076 | } |
| 4077 | else if (ch == '\r') { |
| 4078 | *p++ = '\\'; |
| 4079 | *p++ = 'r'; |
| 4080 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 4081 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 4082 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 4083 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4084 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 4085 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 4086 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 4087 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4088 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 4089 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4090 | /* Copy everything else as-is */ |
| 4091 | else |
| 4092 | *p++ = (char) ch; |
| 4093 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4094 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4095 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 4096 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 4097 | return NULL; |
| 4098 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4099 | } |
| 4100 | |
Alexandre Vassalotti | 2056bed | 2008-12-27 19:46:35 +0000 | [diff] [blame] | 4101 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4102 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 4103 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4104 | if (!PyUnicode_Check(unicode)) { |
| 4105 | PyErr_BadArgument(); |
| 4106 | return NULL; |
| 4107 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 4108 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 4109 | PyUnicode_GET_SIZE(unicode)); |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 4110 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4111 | } |
| 4112 | |
| 4113 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 4114 | |
| 4115 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4116 | Py_ssize_t size, |
| 4117 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4118 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4119 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4120 | Py_ssize_t startinpos; |
| 4121 | Py_ssize_t endinpos; |
| 4122 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4123 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4124 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4125 | const char *end; |
| 4126 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4127 | PyObject *errorHandler = NULL; |
| 4128 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4129 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4130 | /* Escaped strings will always be longer than the resulting |
| 4131 | 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] | 4132 | length after conversion to the true value. (But decoding error |
| 4133 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4134 | v = _PyUnicode_New(size); |
| 4135 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4136 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4137 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4138 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4139 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4140 | end = s + size; |
| 4141 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4142 | unsigned char c; |
| 4143 | Py_UCS4 x; |
| 4144 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4145 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4146 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4147 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 4148 | if (*s != '\\') { |
| 4149 | *p++ = (unsigned char)*s++; |
| 4150 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4151 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4152 | startinpos = s-starts; |
| 4153 | |
| 4154 | /* \u-escapes are only interpreted iff the number of leading |
| 4155 | backslashes if odd */ |
| 4156 | bs = s; |
| 4157 | for (;s < end;) { |
| 4158 | if (*s != '\\') |
| 4159 | break; |
| 4160 | *p++ = (unsigned char)*s++; |
| 4161 | } |
| 4162 | if (((s - bs) & 1) == 0 || |
| 4163 | s >= end || |
| 4164 | (*s != 'u' && *s != 'U')) { |
| 4165 | continue; |
| 4166 | } |
| 4167 | p--; |
| 4168 | count = *s=='u' ? 4 : 8; |
| 4169 | s++; |
| 4170 | |
| 4171 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
| 4172 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4173 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 4174 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 4175 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4176 | endinpos = s-starts; |
| 4177 | if (unicode_decode_call_errorhandler( |
| 4178 | errors, &errorHandler, |
| 4179 | "rawunicodeescape", "truncated \\uXXXX", |
| 4180 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 4181 | &v, &outpos, &p)) |
| 4182 | goto onError; |
| 4183 | goto nextByte; |
| 4184 | } |
| 4185 | x = (x<<4) & ~0xF; |
| 4186 | if (c >= '0' && c <= '9') |
| 4187 | x += c - '0'; |
| 4188 | else if (c >= 'a' && c <= 'f') |
| 4189 | x += 10 + c - 'a'; |
| 4190 | else |
| 4191 | x += 10 + c - 'A'; |
| 4192 | } |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4193 | if (x <= 0xffff) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4194 | /* UCS-2 character */ |
| 4195 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4196 | else if (x <= 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4197 | /* UCS-4 character. Either store directly, or as |
| 4198 | surrogate pair. */ |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4199 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4200 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4201 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4202 | x -= 0x10000L; |
| 4203 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 4204 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4205 | #endif |
| 4206 | } else { |
| 4207 | endinpos = s-starts; |
| 4208 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4209 | if (unicode_decode_call_errorhandler( |
| 4210 | errors, &errorHandler, |
| 4211 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4212 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 4213 | &v, &outpos, &p)) |
| 4214 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4215 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4216 | nextByte: |
| 4217 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4218 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4219 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4220 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4221 | Py_XDECREF(errorHandler); |
| 4222 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4223 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4224 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4225 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4226 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4227 | Py_XDECREF(errorHandler); |
| 4228 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4229 | return NULL; |
| 4230 | } |
| 4231 | |
| 4232 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4233 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4234 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4235 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4236 | char *p; |
| 4237 | char *q; |
| 4238 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4239 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4240 | const Py_ssize_t expandsize = 10; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4241 | #else |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4242 | const Py_ssize_t expandsize = 6; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4243 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4244 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4245 | if (size > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4246 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4247 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4248 | repr = PyBytes_FromStringAndSize(NULL, expandsize * size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4249 | if (repr == NULL) |
| 4250 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 4251 | if (size == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4252 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4253 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4254 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4255 | while (size-- > 0) { |
| 4256 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4257 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4258 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 4259 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4260 | *p++ = '\\'; |
| 4261 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 4262 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 4263 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 4264 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 4265 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 4266 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 4267 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 4268 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 4269 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4270 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4271 | else |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4272 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4273 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 4274 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 4275 | Py_UNICODE ch2; |
| 4276 | Py_UCS4 ucs; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4277 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4278 | ch2 = *s++; |
| 4279 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 4280 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4281 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 4282 | *p++ = '\\'; |
| 4283 | *p++ = 'U'; |
| 4284 | *p++ = hexdigits[(ucs >> 28) & 0xf]; |
| 4285 | *p++ = hexdigits[(ucs >> 24) & 0xf]; |
| 4286 | *p++ = hexdigits[(ucs >> 20) & 0xf]; |
| 4287 | *p++ = hexdigits[(ucs >> 16) & 0xf]; |
| 4288 | *p++ = hexdigits[(ucs >> 12) & 0xf]; |
| 4289 | *p++ = hexdigits[(ucs >> 8) & 0xf]; |
| 4290 | *p++ = hexdigits[(ucs >> 4) & 0xf]; |
| 4291 | *p++ = hexdigits[ucs & 0xf]; |
| 4292 | continue; |
| 4293 | } |
| 4294 | /* Fall through: isolated surrogates are copied as-is */ |
| 4295 | s--; |
| 4296 | size++; |
| 4297 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4298 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4299 | /* Map 16-bit characters to '\uxxxx' */ |
| 4300 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4301 | *p++ = '\\'; |
| 4302 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 4303 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 4304 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 4305 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 4306 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4307 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4308 | /* Copy everything else as-is */ |
| 4309 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4310 | *p++ = (char) ch; |
| 4311 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4312 | size = p - q; |
| 4313 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4314 | assert(size > 0); |
| 4315 | if (_PyBytes_Resize(&repr, size) < 0) |
| 4316 | return NULL; |
| 4317 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4318 | } |
| 4319 | |
| 4320 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 4321 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 4322 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4323 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 4324 | PyErr_BadArgument(); |
| 4325 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4326 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 4327 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 4328 | PyUnicode_GET_SIZE(unicode)); |
| 4329 | |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 4330 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4331 | } |
| 4332 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4333 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 4334 | |
| 4335 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4336 | Py_ssize_t size, |
| 4337 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4338 | { |
| 4339 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4340 | Py_ssize_t startinpos; |
| 4341 | Py_ssize_t endinpos; |
| 4342 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4343 | PyUnicodeObject *v; |
| 4344 | Py_UNICODE *p; |
| 4345 | const char *end; |
| 4346 | const char *reason; |
| 4347 | PyObject *errorHandler = NULL; |
| 4348 | PyObject *exc = NULL; |
| 4349 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 4350 | #ifdef Py_UNICODE_WIDE |
| 4351 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 4352 | #endif |
| 4353 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4354 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4355 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 4356 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4357 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4358 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4359 | return (PyObject *)v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4360 | p = PyUnicode_AS_UNICODE(v); |
| 4361 | end = s + size; |
| 4362 | |
| 4363 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4364 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4365 | /* We have to sanity check the raw data, otherwise doom looms for |
| 4366 | some malformed UCS-4 data. */ |
| 4367 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4368 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4369 | *p > unimax || *p < 0 || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4370 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4371 | end-s < Py_UNICODE_SIZE |
| 4372 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4373 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4374 | startinpos = s - starts; |
| 4375 | if (end-s < Py_UNICODE_SIZE) { |
| 4376 | endinpos = end-starts; |
| 4377 | reason = "truncated input"; |
| 4378 | } |
| 4379 | else { |
| 4380 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 4381 | reason = "illegal code point (> 0x10FFFF)"; |
| 4382 | } |
| 4383 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 4384 | if (unicode_decode_call_errorhandler( |
| 4385 | errors, &errorHandler, |
| 4386 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4387 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 4388 | &v, &outpos, &p)) { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4389 | goto onError; |
| 4390 | } |
| 4391 | } |
| 4392 | else { |
| 4393 | p++; |
| 4394 | s += Py_UNICODE_SIZE; |
| 4395 | } |
| 4396 | } |
| 4397 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4398 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4399 | goto onError; |
| 4400 | Py_XDECREF(errorHandler); |
| 4401 | Py_XDECREF(exc); |
| 4402 | return (PyObject *)v; |
| 4403 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4404 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4405 | Py_XDECREF(v); |
| 4406 | Py_XDECREF(errorHandler); |
| 4407 | Py_XDECREF(exc); |
| 4408 | return NULL; |
| 4409 | } |
| 4410 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4411 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 4412 | |
| 4413 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4414 | Py_ssize_t size, |
| 4415 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4416 | { |
| 4417 | PyUnicodeObject *v; |
| 4418 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4419 | const char *e, *unrolled_end; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4420 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4421 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4422 | if (size == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4423 | Py_UNICODE r = *(unsigned char*)s; |
| 4424 | return PyUnicode_FromUnicode(&r, 1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4425 | } |
| 4426 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4427 | v = _PyUnicode_New(size); |
| 4428 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4429 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4430 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4431 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4432 | p = PyUnicode_AS_UNICODE(v); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4433 | e = s + size; |
| 4434 | /* Unrolling the copy makes it much faster by reducing the looping |
| 4435 | overhead. This is similar to what many memcpy() implementations do. */ |
| 4436 | unrolled_end = e - 4; |
| 4437 | while (s < unrolled_end) { |
| 4438 | p[0] = (unsigned char) s[0]; |
| 4439 | p[1] = (unsigned char) s[1]; |
| 4440 | p[2] = (unsigned char) s[2]; |
| 4441 | p[3] = (unsigned char) s[3]; |
| 4442 | s += 4; |
| 4443 | p += 4; |
| 4444 | } |
| 4445 | while (s < e) |
| 4446 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4447 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4448 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4449 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4450 | Py_XDECREF(v); |
| 4451 | return NULL; |
| 4452 | } |
| 4453 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4454 | /* create or adjust a UnicodeEncodeError */ |
| 4455 | static void make_encode_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4456 | const char *encoding, |
| 4457 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4458 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4459 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4460 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4461 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4462 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 4463 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4464 | } |
| 4465 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4466 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 4467 | goto onError; |
| 4468 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 4469 | goto onError; |
| 4470 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 4471 | goto onError; |
| 4472 | return; |
| 4473 | onError: |
| 4474 | Py_DECREF(*exceptionObject); |
| 4475 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4476 | } |
| 4477 | } |
| 4478 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4479 | /* raises a UnicodeEncodeError */ |
| 4480 | static void raise_encode_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4481 | const char *encoding, |
| 4482 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4483 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4484 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4485 | { |
| 4486 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4487 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4488 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4489 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4490 | } |
| 4491 | |
| 4492 | /* error handling callback helper: |
| 4493 | build arguments, call the callback and check the arguments, |
| 4494 | put the result into newpos and return the replacement string, which |
| 4495 | has to be freed by the caller */ |
| 4496 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4497 | PyObject **errorHandler, |
| 4498 | const char *encoding, const char *reason, |
| 4499 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 4500 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4501 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4502 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4503 | 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] | 4504 | |
| 4505 | PyObject *restuple; |
| 4506 | PyObject *resunicode; |
| 4507 | |
| 4508 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4509 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4510 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4511 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4512 | } |
| 4513 | |
| 4514 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4515 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4516 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4517 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4518 | |
| 4519 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4520 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4521 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4522 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4523 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4524 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4525 | Py_DECREF(restuple); |
| 4526 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4527 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4528 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4529 | &resunicode, newpos)) { |
| 4530 | Py_DECREF(restuple); |
| 4531 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4532 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4533 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 4534 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 4535 | Py_DECREF(restuple); |
| 4536 | return NULL; |
| 4537 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4538 | if (*newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4539 | *newpos = size+*newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4540 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4541 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 4542 | Py_DECREF(restuple); |
| 4543 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4544 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4545 | Py_INCREF(resunicode); |
| 4546 | Py_DECREF(restuple); |
| 4547 | return resunicode; |
| 4548 | } |
| 4549 | |
| 4550 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4551 | Py_ssize_t size, |
| 4552 | const char *errors, |
| 4553 | int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4554 | { |
| 4555 | /* output object */ |
| 4556 | PyObject *res; |
| 4557 | /* pointers to the beginning and end+1 of input */ |
| 4558 | const Py_UNICODE *startp = p; |
| 4559 | const Py_UNICODE *endp = p + size; |
| 4560 | /* pointer to the beginning of the unencodable characters */ |
| 4561 | /* const Py_UNICODE *badp = NULL; */ |
| 4562 | /* pointer into the output */ |
| 4563 | char *str; |
| 4564 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4565 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4566 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 4567 | 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] | 4568 | PyObject *errorHandler = NULL; |
| 4569 | PyObject *exc = NULL; |
| 4570 | /* the following variable is used for caching string comparisons |
| 4571 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 4572 | int known_errorHandler = -1; |
| 4573 | |
| 4574 | /* allocate enough for a simple encoding without |
| 4575 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4576 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4577 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4578 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4579 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4580 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4581 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4582 | ressize = size; |
| 4583 | |
| 4584 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4585 | Py_UNICODE c = *p; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4586 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4587 | /* can we encode this? */ |
| 4588 | if (c<limit) { |
| 4589 | /* no overflow check, because we know that the space is enough */ |
| 4590 | *str++ = (char)c; |
| 4591 | ++p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4592 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4593 | else { |
| 4594 | Py_ssize_t unicodepos = p-startp; |
| 4595 | Py_ssize_t requiredsize; |
| 4596 | PyObject *repunicode; |
| 4597 | Py_ssize_t repsize; |
| 4598 | Py_ssize_t newpos; |
| 4599 | Py_ssize_t respos; |
| 4600 | Py_UNICODE *uni2; |
| 4601 | /* startpos for collecting unencodable chars */ |
| 4602 | const Py_UNICODE *collstart = p; |
| 4603 | const Py_UNICODE *collend = p; |
| 4604 | /* find all unecodable characters */ |
| 4605 | while ((collend < endp) && ((*collend)>=limit)) |
| 4606 | ++collend; |
| 4607 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 4608 | if (known_errorHandler==-1) { |
| 4609 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4610 | known_errorHandler = 1; |
| 4611 | else if (!strcmp(errors, "replace")) |
| 4612 | known_errorHandler = 2; |
| 4613 | else if (!strcmp(errors, "ignore")) |
| 4614 | known_errorHandler = 3; |
| 4615 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4616 | known_errorHandler = 4; |
| 4617 | else |
| 4618 | known_errorHandler = 0; |
| 4619 | } |
| 4620 | switch (known_errorHandler) { |
| 4621 | case 1: /* strict */ |
| 4622 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 4623 | goto onError; |
| 4624 | case 2: /* replace */ |
| 4625 | while (collstart++<collend) |
| 4626 | *str++ = '?'; /* fall through */ |
| 4627 | case 3: /* ignore */ |
| 4628 | p = collend; |
| 4629 | break; |
| 4630 | case 4: /* xmlcharrefreplace */ |
| 4631 | respos = str - PyBytes_AS_STRING(res); |
| 4632 | /* determine replacement size (temporarily (mis)uses p) */ |
| 4633 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 4634 | if (*p<10) |
| 4635 | repsize += 2+1+1; |
| 4636 | else if (*p<100) |
| 4637 | repsize += 2+2+1; |
| 4638 | else if (*p<1000) |
| 4639 | repsize += 2+3+1; |
| 4640 | else if (*p<10000) |
| 4641 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 4642 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4643 | else |
| 4644 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 4645 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4646 | else if (*p<100000) |
| 4647 | repsize += 2+5+1; |
| 4648 | else if (*p<1000000) |
| 4649 | repsize += 2+6+1; |
| 4650 | else |
| 4651 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4652 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4653 | } |
| 4654 | requiredsize = respos+repsize+(endp-collend); |
| 4655 | if (requiredsize > ressize) { |
| 4656 | if (requiredsize<2*ressize) |
| 4657 | requiredsize = 2*ressize; |
| 4658 | if (_PyBytes_Resize(&res, requiredsize)) |
| 4659 | goto onError; |
| 4660 | str = PyBytes_AS_STRING(res) + respos; |
| 4661 | ressize = requiredsize; |
| 4662 | } |
| 4663 | /* generate replacement (temporarily (mis)uses p) */ |
| 4664 | for (p = collstart; p < collend; ++p) { |
| 4665 | str += sprintf(str, "&#%d;", (int)*p); |
| 4666 | } |
| 4667 | p = collend; |
| 4668 | break; |
| 4669 | default: |
| 4670 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 4671 | encoding, reason, startp, size, &exc, |
| 4672 | collstart-startp, collend-startp, &newpos); |
| 4673 | if (repunicode == NULL) |
| 4674 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4675 | if (PyBytes_Check(repunicode)) { |
| 4676 | /* Directly copy bytes result to output. */ |
| 4677 | repsize = PyBytes_Size(repunicode); |
| 4678 | if (repsize > 1) { |
| 4679 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 4680 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4681 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 4682 | Py_DECREF(repunicode); |
| 4683 | goto onError; |
| 4684 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 4685 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4686 | ressize += repsize-1; |
| 4687 | } |
| 4688 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 4689 | str += repsize; |
| 4690 | p = startp + newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4691 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4692 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4693 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4694 | /* need more space? (at least enough for what we |
| 4695 | have+the replacement+the rest of the string, so |
| 4696 | we won't have to check space for encodable characters) */ |
| 4697 | respos = str - PyBytes_AS_STRING(res); |
| 4698 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4699 | requiredsize = respos+repsize+(endp-collend); |
| 4700 | if (requiredsize > ressize) { |
| 4701 | if (requiredsize<2*ressize) |
| 4702 | requiredsize = 2*ressize; |
| 4703 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 4704 | Py_DECREF(repunicode); |
| 4705 | goto onError; |
| 4706 | } |
| 4707 | str = PyBytes_AS_STRING(res) + respos; |
| 4708 | ressize = requiredsize; |
| 4709 | } |
| 4710 | /* check if there is anything unencodable in the replacement |
| 4711 | and copy it to the output */ |
| 4712 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 4713 | c = *uni2; |
| 4714 | if (c >= limit) { |
| 4715 | raise_encode_exception(&exc, encoding, startp, size, |
| 4716 | unicodepos, unicodepos+1, reason); |
| 4717 | Py_DECREF(repunicode); |
| 4718 | goto onError; |
| 4719 | } |
| 4720 | *str = (char)c; |
| 4721 | } |
| 4722 | p = startp + newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4723 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4724 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4725 | } |
| 4726 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4727 | /* Resize if we allocated to much */ |
| 4728 | size = str - PyBytes_AS_STRING(res); |
| 4729 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 4730 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4731 | if (_PyBytes_Resize(&res, size) < 0) |
| 4732 | goto onError; |
| 4733 | } |
| 4734 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4735 | Py_XDECREF(errorHandler); |
| 4736 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4737 | return res; |
| 4738 | |
| 4739 | onError: |
| 4740 | Py_XDECREF(res); |
| 4741 | Py_XDECREF(errorHandler); |
| 4742 | Py_XDECREF(exc); |
| 4743 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4744 | } |
| 4745 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4746 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4747 | Py_ssize_t size, |
| 4748 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4749 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4750 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4751 | } |
| 4752 | |
| 4753 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 4754 | { |
| 4755 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4756 | PyErr_BadArgument(); |
| 4757 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4758 | } |
| 4759 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4760 | PyUnicode_GET_SIZE(unicode), |
| 4761 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4762 | } |
| 4763 | |
| 4764 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 4765 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4766 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4767 | Py_ssize_t size, |
| 4768 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4769 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4770 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4771 | PyUnicodeObject *v; |
| 4772 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4773 | Py_ssize_t startinpos; |
| 4774 | Py_ssize_t endinpos; |
| 4775 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4776 | const char *e; |
| 4777 | PyObject *errorHandler = NULL; |
| 4778 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4779 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4780 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4781 | if (size == 1 && *(unsigned char*)s < 128) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4782 | Py_UNICODE r = *(unsigned char*)s; |
| 4783 | return PyUnicode_FromUnicode(&r, 1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4784 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4785 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4786 | v = _PyUnicode_New(size); |
| 4787 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4788 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4789 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4790 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4791 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4792 | e = s + size; |
| 4793 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4794 | register unsigned char c = (unsigned char)*s; |
| 4795 | if (c < 128) { |
| 4796 | *p++ = c; |
| 4797 | ++s; |
| 4798 | } |
| 4799 | else { |
| 4800 | startinpos = s-starts; |
| 4801 | endinpos = startinpos + 1; |
| 4802 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
| 4803 | if (unicode_decode_call_errorhandler( |
| 4804 | errors, &errorHandler, |
| 4805 | "ascii", "ordinal not in range(128)", |
| 4806 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 4807 | &v, &outpos, &p)) |
| 4808 | goto onError; |
| 4809 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4810 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 4811 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4812 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 4813 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4814 | Py_XDECREF(errorHandler); |
| 4815 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4816 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4817 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4818 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4819 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4820 | Py_XDECREF(errorHandler); |
| 4821 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4822 | return NULL; |
| 4823 | } |
| 4824 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4825 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4826 | Py_ssize_t size, |
| 4827 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4828 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4829 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4830 | } |
| 4831 | |
| 4832 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 4833 | { |
| 4834 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4835 | PyErr_BadArgument(); |
| 4836 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4837 | } |
| 4838 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4839 | PyUnicode_GET_SIZE(unicode), |
| 4840 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4841 | } |
| 4842 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4843 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4844 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4845 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4846 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 4847 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4848 | #define NEED_RETRY |
| 4849 | #endif |
| 4850 | |
| 4851 | /* XXX This code is limited to "true" double-byte encodings, as |
| 4852 | a) it assumes an incomplete character consists of a single byte, and |
| 4853 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4854 | encodings, see IsDBCSLeadByteEx documentation. */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4855 | |
| 4856 | static int is_dbcs_lead_byte(const char *s, int offset) |
| 4857 | { |
| 4858 | const char *curr = s + offset; |
| 4859 | |
| 4860 | if (IsDBCSLeadByte(*curr)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4861 | const char *prev = CharPrev(s, curr); |
| 4862 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4863 | } |
| 4864 | return 0; |
| 4865 | } |
| 4866 | |
| 4867 | /* |
| 4868 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 4869 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 4870 | */ |
| 4871 | static int decode_mbcs(PyUnicodeObject **v, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4872 | const char *s, /* MBCS string */ |
| 4873 | int size, /* sizeof MBCS string */ |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4874 | int final, |
| 4875 | const char *errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4876 | { |
| 4877 | Py_UNICODE *p; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4878 | Py_ssize_t n; |
| 4879 | DWORD usize; |
| 4880 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4881 | |
| 4882 | assert(size >= 0); |
| 4883 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4884 | /* check and handle 'errors' arg */ |
| 4885 | if (errors==NULL || strcmp(errors, "strict")==0) |
| 4886 | flags = MB_ERR_INVALID_CHARS; |
| 4887 | else if (strcmp(errors, "ignore")==0) |
| 4888 | flags = 0; |
| 4889 | else { |
| 4890 | PyErr_Format(PyExc_ValueError, |
| 4891 | "mbcs encoding does not support errors='%s'", |
| 4892 | errors); |
| 4893 | return -1; |
| 4894 | } |
| 4895 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4896 | /* Skip trailing lead-byte unless 'final' is set */ |
| 4897 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4898 | --size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4899 | |
| 4900 | /* First get the size of the result */ |
| 4901 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4902 | usize = MultiByteToWideChar(CP_ACP, flags, s, size, NULL, 0); |
| 4903 | if (usize==0) |
| 4904 | goto mbcs_decode_error; |
| 4905 | } else |
| 4906 | usize = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4907 | |
| 4908 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4909 | /* Create unicode object */ |
| 4910 | *v = _PyUnicode_New(usize); |
| 4911 | if (*v == NULL) |
| 4912 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4913 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4914 | } |
| 4915 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4916 | /* Extend unicode object */ |
| 4917 | n = PyUnicode_GET_SIZE(*v); |
| 4918 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 4919 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4920 | } |
| 4921 | |
| 4922 | /* Do the conversion */ |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4923 | if (usize > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4924 | p = PyUnicode_AS_UNICODE(*v) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4925 | if (0 == MultiByteToWideChar(CP_ACP, flags, s, size, p, usize)) { |
| 4926 | goto mbcs_decode_error; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4927 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4928 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4929 | return size; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4930 | |
| 4931 | mbcs_decode_error: |
| 4932 | /* If the last error was ERROR_NO_UNICODE_TRANSLATION, then |
| 4933 | we raise a UnicodeDecodeError - else it is a 'generic' |
| 4934 | windows error |
| 4935 | */ |
| 4936 | if (GetLastError()==ERROR_NO_UNICODE_TRANSLATION) { |
| 4937 | /* Ideally, we should get reason from FormatMessage - this |
| 4938 | is the Windows 2000 English version of the message |
| 4939 | */ |
| 4940 | PyObject *exc = NULL; |
| 4941 | const char *reason = "No mapping for the Unicode character exists " |
| 4942 | "in the target multi-byte code page."; |
| 4943 | make_decode_exception(&exc, "mbcs", s, size, 0, 0, reason); |
| 4944 | if (exc != NULL) { |
| 4945 | PyCodec_StrictErrors(exc); |
| 4946 | Py_DECREF(exc); |
| 4947 | } |
| 4948 | } else { |
| 4949 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4950 | } |
| 4951 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4952 | } |
| 4953 | |
| 4954 | PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4955 | Py_ssize_t size, |
| 4956 | const char *errors, |
| 4957 | Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4958 | { |
| 4959 | PyUnicodeObject *v = NULL; |
| 4960 | int done; |
| 4961 | |
| 4962 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4963 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4964 | |
| 4965 | #ifdef NEED_RETRY |
| 4966 | retry: |
| 4967 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4968 | done = decode_mbcs(&v, s, INT_MAX, 0, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4969 | else |
| 4970 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4971 | done = decode_mbcs(&v, s, (int)size, !consumed, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4972 | |
| 4973 | if (done < 0) { |
| 4974 | Py_XDECREF(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4975 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4976 | } |
| 4977 | |
| 4978 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4979 | *consumed += done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4980 | |
| 4981 | #ifdef NEED_RETRY |
| 4982 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4983 | s += done; |
| 4984 | size -= done; |
| 4985 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4986 | } |
| 4987 | #endif |
| 4988 | |
| 4989 | return (PyObject *)v; |
| 4990 | } |
| 4991 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4992 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4993 | Py_ssize_t size, |
| 4994 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4995 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4996 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 4997 | } |
| 4998 | |
| 4999 | /* |
| 5000 | * Convert unicode into string object (MBCS). |
| 5001 | * Returns 0 if succeed, -1 otherwise. |
| 5002 | */ |
| 5003 | static int encode_mbcs(PyObject **repr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5004 | const Py_UNICODE *p, /* unicode */ |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 5005 | int size, /* size of unicode */ |
| 5006 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5007 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 5008 | BOOL usedDefaultChar = FALSE; |
| 5009 | BOOL *pusedDefaultChar; |
| 5010 | int mbcssize; |
| 5011 | Py_ssize_t n; |
| 5012 | PyObject *exc = NULL; |
| 5013 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5014 | |
| 5015 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 5016 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 5017 | /* check and handle 'errors' arg */ |
| 5018 | if (errors==NULL || strcmp(errors, "strict")==0) { |
| 5019 | flags = WC_NO_BEST_FIT_CHARS; |
| 5020 | pusedDefaultChar = &usedDefaultChar; |
| 5021 | } else if (strcmp(errors, "replace")==0) { |
| 5022 | flags = 0; |
| 5023 | pusedDefaultChar = NULL; |
| 5024 | } else { |
| 5025 | PyErr_Format(PyExc_ValueError, |
| 5026 | "mbcs encoding does not support errors='%s'", |
| 5027 | errors); |
| 5028 | return -1; |
| 5029 | } |
| 5030 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 5031 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5032 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 5033 | mbcssize = WideCharToMultiByte(CP_ACP, flags, p, size, NULL, 0, |
| 5034 | NULL, pusedDefaultChar); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5035 | if (mbcssize == 0) { |
| 5036 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 5037 | return -1; |
| 5038 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 5039 | /* If we used a default char, then we failed! */ |
| 5040 | if (pusedDefaultChar && *pusedDefaultChar) |
| 5041 | goto mbcs_encode_error; |
| 5042 | } else { |
| 5043 | mbcssize = 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 5044 | } |
| 5045 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5046 | if (*repr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5047 | /* Create string object */ |
| 5048 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); |
| 5049 | if (*repr == NULL) |
| 5050 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 5051 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5052 | } |
| 5053 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5054 | /* Extend string object */ |
| 5055 | n = PyBytes_Size(*repr); |
| 5056 | if (_PyBytes_Resize(repr, n + mbcssize) < 0) |
| 5057 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5058 | } |
| 5059 | |
| 5060 | /* Do the conversion */ |
| 5061 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5062 | char *s = PyBytes_AS_STRING(*repr) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 5063 | if (0 == WideCharToMultiByte(CP_ACP, flags, p, size, s, mbcssize, |
| 5064 | NULL, pusedDefaultChar)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5065 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 5066 | return -1; |
| 5067 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 5068 | if (pusedDefaultChar && *pusedDefaultChar) |
| 5069 | goto mbcs_encode_error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5070 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5071 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 5072 | |
| 5073 | mbcs_encode_error: |
| 5074 | raise_encode_exception(&exc, "mbcs", p, size, 0, 0, "invalid character"); |
| 5075 | Py_XDECREF(exc); |
| 5076 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 5077 | } |
| 5078 | |
| 5079 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5080 | Py_ssize_t size, |
| 5081 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 5082 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5083 | PyObject *repr = NULL; |
| 5084 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 5085 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5086 | #ifdef NEED_RETRY |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5087 | retry: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5088 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 5089 | ret = encode_mbcs(&repr, p, INT_MAX, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5090 | else |
| 5091 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 5092 | ret = encode_mbcs(&repr, p, (int)size, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 5093 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5094 | if (ret < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5095 | Py_XDECREF(repr); |
| 5096 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 5097 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5098 | |
| 5099 | #ifdef NEED_RETRY |
| 5100 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5101 | p += INT_MAX; |
| 5102 | size -= INT_MAX; |
| 5103 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5104 | } |
| 5105 | #endif |
| 5106 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 5107 | return repr; |
| 5108 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 5109 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 5110 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 5111 | { |
| 5112 | if (!PyUnicode_Check(unicode)) { |
| 5113 | PyErr_BadArgument(); |
| 5114 | return NULL; |
| 5115 | } |
| 5116 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5117 | PyUnicode_GET_SIZE(unicode), |
| 5118 | NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 5119 | } |
| 5120 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5121 | #undef NEED_RETRY |
| 5122 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5123 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 5124 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5125 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 5126 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5127 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5128 | Py_ssize_t size, |
| 5129 | PyObject *mapping, |
| 5130 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5131 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5132 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5133 | Py_ssize_t startinpos; |
| 5134 | Py_ssize_t endinpos; |
| 5135 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5136 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5137 | PyUnicodeObject *v; |
| 5138 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5139 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5140 | PyObject *errorHandler = NULL; |
| 5141 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 5142 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5143 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5144 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5145 | /* Default to Latin-1 */ |
| 5146 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5147 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5148 | |
| 5149 | v = _PyUnicode_New(size); |
| 5150 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5151 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5152 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5153 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5154 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5155 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 5156 | if (PyUnicode_CheckExact(mapping)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5157 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 5158 | maplen = PyUnicode_GET_SIZE(mapping); |
| 5159 | while (s < e) { |
| 5160 | unsigned char ch = *s; |
| 5161 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5162 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5163 | if (ch < maplen) |
| 5164 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5165 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5166 | if (x == 0xfffe) { |
| 5167 | /* undefined mapping */ |
| 5168 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 5169 | startinpos = s-starts; |
| 5170 | endinpos = startinpos+1; |
| 5171 | if (unicode_decode_call_errorhandler( |
| 5172 | errors, &errorHandler, |
| 5173 | "charmap", "character maps to <undefined>", |
| 5174 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 5175 | &v, &outpos, &p)) { |
| 5176 | goto onError; |
| 5177 | } |
| 5178 | continue; |
| 5179 | } |
| 5180 | *p++ = x; |
| 5181 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5182 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 5183 | } |
| 5184 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5185 | while (s < e) { |
| 5186 | unsigned char ch = *s; |
| 5187 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 5188 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5189 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 5190 | w = PyLong_FromLong((long)ch); |
| 5191 | if (w == NULL) |
| 5192 | goto onError; |
| 5193 | x = PyObject_GetItem(mapping, w); |
| 5194 | Py_DECREF(w); |
| 5195 | if (x == NULL) { |
| 5196 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 5197 | /* No mapping found means: mapping is undefined. */ |
| 5198 | PyErr_Clear(); |
| 5199 | x = Py_None; |
| 5200 | Py_INCREF(x); |
| 5201 | } else |
| 5202 | goto onError; |
| 5203 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5204 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5205 | /* Apply mapping */ |
| 5206 | if (PyLong_Check(x)) { |
| 5207 | long value = PyLong_AS_LONG(x); |
| 5208 | if (value < 0 || value > 65535) { |
| 5209 | PyErr_SetString(PyExc_TypeError, |
| 5210 | "character mapping must be in range(65536)"); |
| 5211 | Py_DECREF(x); |
| 5212 | goto onError; |
| 5213 | } |
| 5214 | *p++ = (Py_UNICODE)value; |
| 5215 | } |
| 5216 | else if (x == Py_None) { |
| 5217 | /* undefined mapping */ |
| 5218 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 5219 | startinpos = s-starts; |
| 5220 | endinpos = startinpos+1; |
| 5221 | if (unicode_decode_call_errorhandler( |
| 5222 | errors, &errorHandler, |
| 5223 | "charmap", "character maps to <undefined>", |
| 5224 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 5225 | &v, &outpos, &p)) { |
| 5226 | Py_DECREF(x); |
| 5227 | goto onError; |
| 5228 | } |
| 5229 | Py_DECREF(x); |
| 5230 | continue; |
| 5231 | } |
| 5232 | else if (PyUnicode_Check(x)) { |
| 5233 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5234 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5235 | if (targetsize == 1) |
| 5236 | /* 1-1 mapping */ |
| 5237 | *p++ = *PyUnicode_AS_UNICODE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5238 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5239 | else if (targetsize > 1) { |
| 5240 | /* 1-n mapping */ |
| 5241 | if (targetsize > extrachars) { |
| 5242 | /* resize first */ |
| 5243 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 5244 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 5245 | (targetsize << 2); |
| 5246 | extrachars += needed; |
| 5247 | /* XXX overflow detection missing */ |
| 5248 | if (_PyUnicode_Resize(&v, |
| 5249 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 5250 | Py_DECREF(x); |
| 5251 | goto onError; |
| 5252 | } |
| 5253 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 5254 | } |
| 5255 | Py_UNICODE_COPY(p, |
| 5256 | PyUnicode_AS_UNICODE(x), |
| 5257 | targetsize); |
| 5258 | p += targetsize; |
| 5259 | extrachars -= targetsize; |
| 5260 | } |
| 5261 | /* 1-0 mapping: skip the character */ |
| 5262 | } |
| 5263 | else { |
| 5264 | /* wrong return value */ |
| 5265 | PyErr_SetString(PyExc_TypeError, |
| 5266 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5267 | Py_DECREF(x); |
| 5268 | goto onError; |
| 5269 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5270 | Py_DECREF(x); |
| 5271 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5272 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5273 | } |
| 5274 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5275 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 5276 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5277 | Py_XDECREF(errorHandler); |
| 5278 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5279 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5280 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5281 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5282 | Py_XDECREF(errorHandler); |
| 5283 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5284 | Py_XDECREF(v); |
| 5285 | return NULL; |
| 5286 | } |
| 5287 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5288 | /* Charmap encoding: the lookup table */ |
| 5289 | |
| 5290 | struct encoding_map{ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5291 | PyObject_HEAD |
| 5292 | unsigned char level1[32]; |
| 5293 | int count2, count3; |
| 5294 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5295 | }; |
| 5296 | |
| 5297 | static PyObject* |
| 5298 | encoding_map_size(PyObject *obj, PyObject* args) |
| 5299 | { |
| 5300 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5301 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5302 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5303 | } |
| 5304 | |
| 5305 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5306 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5307 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 5308 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5309 | }; |
| 5310 | |
| 5311 | static void |
| 5312 | encoding_map_dealloc(PyObject* o) |
| 5313 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5314 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5315 | } |
| 5316 | |
| 5317 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5318 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5319 | "EncodingMap", /*tp_name*/ |
| 5320 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 5321 | 0, /*tp_itemsize*/ |
| 5322 | /* methods */ |
| 5323 | encoding_map_dealloc, /*tp_dealloc*/ |
| 5324 | 0, /*tp_print*/ |
| 5325 | 0, /*tp_getattr*/ |
| 5326 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 5327 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5328 | 0, /*tp_repr*/ |
| 5329 | 0, /*tp_as_number*/ |
| 5330 | 0, /*tp_as_sequence*/ |
| 5331 | 0, /*tp_as_mapping*/ |
| 5332 | 0, /*tp_hash*/ |
| 5333 | 0, /*tp_call*/ |
| 5334 | 0, /*tp_str*/ |
| 5335 | 0, /*tp_getattro*/ |
| 5336 | 0, /*tp_setattro*/ |
| 5337 | 0, /*tp_as_buffer*/ |
| 5338 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 5339 | 0, /*tp_doc*/ |
| 5340 | 0, /*tp_traverse*/ |
| 5341 | 0, /*tp_clear*/ |
| 5342 | 0, /*tp_richcompare*/ |
| 5343 | 0, /*tp_weaklistoffset*/ |
| 5344 | 0, /*tp_iter*/ |
| 5345 | 0, /*tp_iternext*/ |
| 5346 | encoding_map_methods, /*tp_methods*/ |
| 5347 | 0, /*tp_members*/ |
| 5348 | 0, /*tp_getset*/ |
| 5349 | 0, /*tp_base*/ |
| 5350 | 0, /*tp_dict*/ |
| 5351 | 0, /*tp_descr_get*/ |
| 5352 | 0, /*tp_descr_set*/ |
| 5353 | 0, /*tp_dictoffset*/ |
| 5354 | 0, /*tp_init*/ |
| 5355 | 0, /*tp_alloc*/ |
| 5356 | 0, /*tp_new*/ |
| 5357 | 0, /*tp_free*/ |
| 5358 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5359 | }; |
| 5360 | |
| 5361 | PyObject* |
| 5362 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 5363 | { |
| 5364 | Py_UNICODE *decode; |
| 5365 | PyObject *result; |
| 5366 | struct encoding_map *mresult; |
| 5367 | int i; |
| 5368 | int need_dict = 0; |
| 5369 | unsigned char level1[32]; |
| 5370 | unsigned char level2[512]; |
| 5371 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 5372 | int count2 = 0, count3 = 0; |
| 5373 | |
| 5374 | if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { |
| 5375 | PyErr_BadArgument(); |
| 5376 | return NULL; |
| 5377 | } |
| 5378 | decode = PyUnicode_AS_UNICODE(string); |
| 5379 | memset(level1, 0xFF, sizeof level1); |
| 5380 | memset(level2, 0xFF, sizeof level2); |
| 5381 | |
| 5382 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 5383 | or if there are non-BMP characters, we need to use |
| 5384 | a mapping dictionary. */ |
| 5385 | if (decode[0] != 0) |
| 5386 | need_dict = 1; |
| 5387 | for (i = 1; i < 256; i++) { |
| 5388 | int l1, l2; |
| 5389 | if (decode[i] == 0 |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5390 | #ifdef Py_UNICODE_WIDE |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5391 | || decode[i] > 0xFFFF |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5392 | #endif |
| 5393 | ) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5394 | need_dict = 1; |
| 5395 | break; |
| 5396 | } |
| 5397 | if (decode[i] == 0xFFFE) |
| 5398 | /* unmapped character */ |
| 5399 | continue; |
| 5400 | l1 = decode[i] >> 11; |
| 5401 | l2 = decode[i] >> 7; |
| 5402 | if (level1[l1] == 0xFF) |
| 5403 | level1[l1] = count2++; |
| 5404 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5405 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5406 | } |
| 5407 | |
| 5408 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 5409 | need_dict = 1; |
| 5410 | |
| 5411 | if (need_dict) { |
| 5412 | PyObject *result = PyDict_New(); |
| 5413 | PyObject *key, *value; |
| 5414 | if (!result) |
| 5415 | return NULL; |
| 5416 | for (i = 0; i < 256; i++) { |
| 5417 | key = value = NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5418 | key = PyLong_FromLong(decode[i]); |
| 5419 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5420 | if (!key || !value) |
| 5421 | goto failed1; |
| 5422 | if (PyDict_SetItem(result, key, value) == -1) |
| 5423 | goto failed1; |
| 5424 | Py_DECREF(key); |
| 5425 | Py_DECREF(value); |
| 5426 | } |
| 5427 | return result; |
| 5428 | failed1: |
| 5429 | Py_XDECREF(key); |
| 5430 | Py_XDECREF(value); |
| 5431 | Py_DECREF(result); |
| 5432 | return NULL; |
| 5433 | } |
| 5434 | |
| 5435 | /* Create a three-level trie */ |
| 5436 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 5437 | 16*count2 + 128*count3 - 1); |
| 5438 | if (!result) |
| 5439 | return PyErr_NoMemory(); |
| 5440 | PyObject_Init(result, &EncodingMapType); |
| 5441 | mresult = (struct encoding_map*)result; |
| 5442 | mresult->count2 = count2; |
| 5443 | mresult->count3 = count3; |
| 5444 | mlevel1 = mresult->level1; |
| 5445 | mlevel2 = mresult->level23; |
| 5446 | mlevel3 = mresult->level23 + 16*count2; |
| 5447 | memcpy(mlevel1, level1, 32); |
| 5448 | memset(mlevel2, 0xFF, 16*count2); |
| 5449 | memset(mlevel3, 0, 128*count3); |
| 5450 | count3 = 0; |
| 5451 | for (i = 1; i < 256; i++) { |
| 5452 | int o1, o2, o3, i2, i3; |
| 5453 | if (decode[i] == 0xFFFE) |
| 5454 | /* unmapped character */ |
| 5455 | continue; |
| 5456 | o1 = decode[i]>>11; |
| 5457 | o2 = (decode[i]>>7) & 0xF; |
| 5458 | i2 = 16*mlevel1[o1] + o2; |
| 5459 | if (mlevel2[i2] == 0xFF) |
| 5460 | mlevel2[i2] = count3++; |
| 5461 | o3 = decode[i] & 0x7F; |
| 5462 | i3 = 128*mlevel2[i2] + o3; |
| 5463 | mlevel3[i3] = i; |
| 5464 | } |
| 5465 | return result; |
| 5466 | } |
| 5467 | |
| 5468 | static int |
| 5469 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 5470 | { |
| 5471 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 5472 | int l1 = c>>11; |
| 5473 | int l2 = (c>>7) & 0xF; |
| 5474 | int l3 = c & 0x7F; |
| 5475 | int i; |
| 5476 | |
| 5477 | #ifdef Py_UNICODE_WIDE |
| 5478 | if (c > 0xFFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5479 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5480 | } |
| 5481 | #endif |
| 5482 | if (c == 0) |
| 5483 | return 0; |
| 5484 | /* level 1*/ |
| 5485 | i = map->level1[l1]; |
| 5486 | if (i == 0xFF) { |
| 5487 | return -1; |
| 5488 | } |
| 5489 | /* level 2*/ |
| 5490 | i = map->level23[16*i+l2]; |
| 5491 | if (i == 0xFF) { |
| 5492 | return -1; |
| 5493 | } |
| 5494 | /* level 3 */ |
| 5495 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 5496 | if (i == 0) { |
| 5497 | return -1; |
| 5498 | } |
| 5499 | return i; |
| 5500 | } |
| 5501 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5502 | /* Lookup the character ch in the mapping. If the character |
| 5503 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 5504 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5505 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5506 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5507 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5508 | PyObject *x; |
| 5509 | |
| 5510 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5511 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5512 | x = PyObject_GetItem(mapping, w); |
| 5513 | Py_DECREF(w); |
| 5514 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5515 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 5516 | /* No mapping found means: mapping is undefined. */ |
| 5517 | PyErr_Clear(); |
| 5518 | x = Py_None; |
| 5519 | Py_INCREF(x); |
| 5520 | return x; |
| 5521 | } else |
| 5522 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5523 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 5524 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5525 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5526 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5527 | long value = PyLong_AS_LONG(x); |
| 5528 | if (value < 0 || value > 255) { |
| 5529 | PyErr_SetString(PyExc_TypeError, |
| 5530 | "character mapping must be in range(256)"); |
| 5531 | Py_DECREF(x); |
| 5532 | return NULL; |
| 5533 | } |
| 5534 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5535 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5536 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5537 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5538 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5539 | /* wrong return value */ |
| 5540 | PyErr_Format(PyExc_TypeError, |
| 5541 | "character mapping must return integer, bytes or None, not %.400s", |
| 5542 | x->ob_type->tp_name); |
| 5543 | Py_DECREF(x); |
| 5544 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5545 | } |
| 5546 | } |
| 5547 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5548 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5549 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5550 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5551 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 5552 | /* exponentially overallocate to minimize reallocations */ |
| 5553 | if (requiredsize < 2*outsize) |
| 5554 | requiredsize = 2*outsize; |
| 5555 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 5556 | return -1; |
| 5557 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5558 | } |
| 5559 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5560 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5561 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5562 | }charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5563 | /* 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] | 5564 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5565 | space is available. Return a new reference to the object that |
| 5566 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 5567 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 5568 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5569 | static |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5570 | charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5571 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5572 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5573 | PyObject *rep; |
| 5574 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5575 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5576 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5577 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5578 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5579 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5580 | if (res == -1) |
| 5581 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5582 | if (outsize<requiredsize) |
| 5583 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 5584 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5585 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5586 | outstart[(*outpos)++] = (char)res; |
| 5587 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5588 | } |
| 5589 | |
| 5590 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5591 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5592 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5593 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5594 | Py_DECREF(rep); |
| 5595 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5596 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5597 | if (PyLong_Check(rep)) { |
| 5598 | Py_ssize_t requiredsize = *outpos+1; |
| 5599 | if (outsize<requiredsize) |
| 5600 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 5601 | Py_DECREF(rep); |
| 5602 | return enc_EXCEPTION; |
| 5603 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5604 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5605 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5606 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5607 | else { |
| 5608 | const char *repchars = PyBytes_AS_STRING(rep); |
| 5609 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 5610 | Py_ssize_t requiredsize = *outpos+repsize; |
| 5611 | if (outsize<requiredsize) |
| 5612 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 5613 | Py_DECREF(rep); |
| 5614 | return enc_EXCEPTION; |
| 5615 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5616 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5617 | memcpy(outstart + *outpos, repchars, repsize); |
| 5618 | *outpos += repsize; |
| 5619 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5620 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5621 | Py_DECREF(rep); |
| 5622 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5623 | } |
| 5624 | |
| 5625 | /* handle an error in PyUnicode_EncodeCharmap |
| 5626 | Return 0 on success, -1 on error */ |
| 5627 | static |
| 5628 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5629 | 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] | 5630 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 5631 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5632 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5633 | { |
| 5634 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5635 | Py_ssize_t repsize; |
| 5636 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5637 | Py_UNICODE *uni2; |
| 5638 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5639 | Py_ssize_t collstartpos = *inpos; |
| 5640 | Py_ssize_t collendpos = *inpos+1; |
| 5641 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5642 | char *encoding = "charmap"; |
| 5643 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5644 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5645 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5646 | /* find all unencodable characters */ |
| 5647 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5648 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5649 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5650 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 5651 | if (res != -1) |
| 5652 | break; |
| 5653 | ++collendpos; |
| 5654 | continue; |
| 5655 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5656 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5657 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 5658 | if (rep==NULL) |
| 5659 | return -1; |
| 5660 | else if (rep!=Py_None) { |
| 5661 | Py_DECREF(rep); |
| 5662 | break; |
| 5663 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5664 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5665 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5666 | } |
| 5667 | /* cache callback name lookup |
| 5668 | * (if not done yet, i.e. it's the first error) */ |
| 5669 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5670 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5671 | *known_errorHandler = 1; |
| 5672 | else if (!strcmp(errors, "replace")) |
| 5673 | *known_errorHandler = 2; |
| 5674 | else if (!strcmp(errors, "ignore")) |
| 5675 | *known_errorHandler = 3; |
| 5676 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5677 | *known_errorHandler = 4; |
| 5678 | else |
| 5679 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5680 | } |
| 5681 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5682 | case 1: /* strict */ |
| 5683 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5684 | return -1; |
| 5685 | case 2: /* replace */ |
| 5686 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5687 | x = charmapencode_output('?', mapping, res, respos); |
| 5688 | if (x==enc_EXCEPTION) { |
| 5689 | return -1; |
| 5690 | } |
| 5691 | else if (x==enc_FAILED) { |
| 5692 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5693 | return -1; |
| 5694 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5695 | } |
| 5696 | /* fall through */ |
| 5697 | case 3: /* ignore */ |
| 5698 | *inpos = collendpos; |
| 5699 | break; |
| 5700 | case 4: /* xmlcharrefreplace */ |
| 5701 | /* generate replacement (temporarily (mis)uses p) */ |
| 5702 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5703 | char buffer[2+29+1+1]; |
| 5704 | char *cp; |
| 5705 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 5706 | for (cp = buffer; *cp; ++cp) { |
| 5707 | x = charmapencode_output(*cp, mapping, res, respos); |
| 5708 | if (x==enc_EXCEPTION) |
| 5709 | return -1; |
| 5710 | else if (x==enc_FAILED) { |
| 5711 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5712 | return -1; |
| 5713 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5714 | } |
| 5715 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5716 | *inpos = collendpos; |
| 5717 | break; |
| 5718 | default: |
| 5719 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5720 | encoding, reason, p, size, exceptionObject, |
| 5721 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5722 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5723 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5724 | if (PyBytes_Check(repunicode)) { |
| 5725 | /* Directly copy bytes result to output. */ |
| 5726 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 5727 | Py_ssize_t requiredsize; |
| 5728 | repsize = PyBytes_Size(repunicode); |
| 5729 | requiredsize = *respos + repsize; |
| 5730 | if (requiredsize > outsize) |
| 5731 | /* Make room for all additional bytes. */ |
| 5732 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 5733 | Py_DECREF(repunicode); |
| 5734 | return -1; |
| 5735 | } |
| 5736 | memcpy(PyBytes_AsString(*res) + *respos, |
| 5737 | PyBytes_AsString(repunicode), repsize); |
| 5738 | *respos += repsize; |
| 5739 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5740 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5741 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5742 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5743 | /* generate replacement */ |
| 5744 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5745 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5746 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 5747 | if (x==enc_EXCEPTION) { |
| 5748 | return -1; |
| 5749 | } |
| 5750 | else if (x==enc_FAILED) { |
| 5751 | Py_DECREF(repunicode); |
| 5752 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5753 | return -1; |
| 5754 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5755 | } |
| 5756 | *inpos = newpos; |
| 5757 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5758 | } |
| 5759 | return 0; |
| 5760 | } |
| 5761 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5762 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5763 | Py_ssize_t size, |
| 5764 | PyObject *mapping, |
| 5765 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5766 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5767 | /* output object */ |
| 5768 | PyObject *res = NULL; |
| 5769 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5770 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5771 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5772 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5773 | PyObject *errorHandler = NULL; |
| 5774 | PyObject *exc = NULL; |
| 5775 | /* the following variable is used for caching string comparisons |
| 5776 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 5777 | * 3=ignore, 4=xmlcharrefreplace */ |
| 5778 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5779 | |
| 5780 | /* Default to Latin-1 */ |
| 5781 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5782 | return PyUnicode_EncodeLatin1(p, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5783 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5784 | /* allocate enough for a simple encoding without |
| 5785 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5786 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5787 | if (res == NULL) |
| 5788 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 5789 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5790 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5791 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5792 | while (inpos<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5793 | /* try to encode it */ |
| 5794 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 5795 | if (x==enc_EXCEPTION) /* error */ |
| 5796 | goto onError; |
| 5797 | if (x==enc_FAILED) { /* unencodable character */ |
| 5798 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 5799 | &exc, |
| 5800 | &known_errorHandler, &errorHandler, errors, |
| 5801 | &res, &respos)) { |
| 5802 | goto onError; |
| 5803 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5804 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5805 | else |
| 5806 | /* done with this character => adjust input position */ |
| 5807 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5808 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5809 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5810 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5811 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5812 | if (_PyBytes_Resize(&res, respos) < 0) |
| 5813 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5814 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5815 | Py_XDECREF(exc); |
| 5816 | Py_XDECREF(errorHandler); |
| 5817 | return res; |
| 5818 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5819 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5820 | Py_XDECREF(res); |
| 5821 | Py_XDECREF(exc); |
| 5822 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5823 | return NULL; |
| 5824 | } |
| 5825 | |
| 5826 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5827 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5828 | { |
| 5829 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5830 | PyErr_BadArgument(); |
| 5831 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5832 | } |
| 5833 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5834 | PyUnicode_GET_SIZE(unicode), |
| 5835 | mapping, |
| 5836 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5837 | } |
| 5838 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5839 | /* create or adjust a UnicodeTranslateError */ |
| 5840 | static void make_translate_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5841 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5842 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5843 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5844 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5845 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5846 | *exceptionObject = PyUnicodeTranslateError_Create( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5847 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5848 | } |
| 5849 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5850 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 5851 | goto onError; |
| 5852 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 5853 | goto onError; |
| 5854 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 5855 | goto onError; |
| 5856 | return; |
| 5857 | onError: |
| 5858 | Py_DECREF(*exceptionObject); |
| 5859 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5860 | } |
| 5861 | } |
| 5862 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5863 | /* raises a UnicodeTranslateError */ |
| 5864 | static void raise_translate_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5865 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5866 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5867 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5868 | { |
| 5869 | make_translate_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5870 | unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5871 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5872 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5873 | } |
| 5874 | |
| 5875 | /* error handling callback helper: |
| 5876 | build arguments, call the callback and check the arguments, |
| 5877 | put the result into newpos and return the replacement string, which |
| 5878 | has to be freed by the caller */ |
| 5879 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5880 | PyObject **errorHandler, |
| 5881 | const char *reason, |
| 5882 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 5883 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5884 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5885 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 5886 | 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] | 5887 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5888 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5889 | PyObject *restuple; |
| 5890 | PyObject *resunicode; |
| 5891 | |
| 5892 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5893 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5894 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5895 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5896 | } |
| 5897 | |
| 5898 | make_translate_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5899 | unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5900 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5901 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5902 | |
| 5903 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5904 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5905 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5906 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5907 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 5908 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5909 | Py_DECREF(restuple); |
| 5910 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5911 | } |
| 5912 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5913 | &resunicode, &i_newpos)) { |
| 5914 | Py_DECREF(restuple); |
| 5915 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5916 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5917 | if (i_newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5918 | *newpos = size+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5919 | else |
| 5920 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5921 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5922 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 5923 | Py_DECREF(restuple); |
| 5924 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5925 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5926 | Py_INCREF(resunicode); |
| 5927 | Py_DECREF(restuple); |
| 5928 | return resunicode; |
| 5929 | } |
| 5930 | |
| 5931 | /* Lookup the character ch in the mapping and put the result in result, |
| 5932 | which must be decrefed by the caller. |
| 5933 | Return 0 on success, -1 on error */ |
| 5934 | static |
| 5935 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 5936 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5937 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5938 | PyObject *x; |
| 5939 | |
| 5940 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5941 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5942 | x = PyObject_GetItem(mapping, w); |
| 5943 | Py_DECREF(w); |
| 5944 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5945 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 5946 | /* No mapping found means: use 1:1 mapping. */ |
| 5947 | PyErr_Clear(); |
| 5948 | *result = NULL; |
| 5949 | return 0; |
| 5950 | } else |
| 5951 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5952 | } |
| 5953 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5954 | *result = x; |
| 5955 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5956 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5957 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5958 | long value = PyLong_AS_LONG(x); |
| 5959 | long max = PyUnicode_GetMax(); |
| 5960 | if (value < 0 || value > max) { |
| 5961 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 5962 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5963 | Py_DECREF(x); |
| 5964 | return -1; |
| 5965 | } |
| 5966 | *result = x; |
| 5967 | return 0; |
| 5968 | } |
| 5969 | else if (PyUnicode_Check(x)) { |
| 5970 | *result = x; |
| 5971 | return 0; |
| 5972 | } |
| 5973 | else { |
| 5974 | /* wrong return value */ |
| 5975 | PyErr_SetString(PyExc_TypeError, |
| 5976 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5977 | Py_DECREF(x); |
| 5978 | return -1; |
| 5979 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5980 | } |
| 5981 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5982 | if not reallocate and adjust various state variables. |
| 5983 | Return 0 on success, -1 on error */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5984 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5985 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5986 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5987 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5988 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5989 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5990 | /* remember old output position */ |
| 5991 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
| 5992 | /* exponentially overallocate to minimize reallocations */ |
| 5993 | if (requiredsize < 2 * oldsize) |
| 5994 | requiredsize = 2 * oldsize; |
| 5995 | if (PyUnicode_Resize(outobj, requiredsize) < 0) |
| 5996 | return -1; |
| 5997 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5998 | } |
| 5999 | return 0; |
| 6000 | } |
| 6001 | /* lookup the character, put the result in the output string and adjust |
| 6002 | various state variables. Return a new reference to the object that |
| 6003 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 6004 | undefined (in which case no character was written). |
| 6005 | The called must decref result. |
| 6006 | Return 0 on success, -1 on error. */ |
| 6007 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 6008 | int charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6009 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
| 6010 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6011 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 6012 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6013 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6014 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6015 | /* not found => default to 1:1 mapping */ |
| 6016 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6017 | } |
| 6018 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6019 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6020 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6021 | /* no overflow check, because we know that the space is enough */ |
| 6022 | *(*outp)++ = (Py_UNICODE)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6023 | } |
| 6024 | else if (PyUnicode_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6025 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
| 6026 | if (repsize==1) { |
| 6027 | /* no overflow check, because we know that the space is enough */ |
| 6028 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 6029 | } |
| 6030 | else if (repsize!=0) { |
| 6031 | /* more than one character */ |
| 6032 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
| 6033 | (insize - (curinp-startinp)) + |
| 6034 | repsize - 1; |
| 6035 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
| 6036 | return -1; |
| 6037 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 6038 | *outp += repsize; |
| 6039 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6040 | } |
| 6041 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6042 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6043 | return 0; |
| 6044 | } |
| 6045 | |
| 6046 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6047 | Py_ssize_t size, |
| 6048 | PyObject *mapping, |
| 6049 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6050 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6051 | /* output object */ |
| 6052 | PyObject *res = NULL; |
| 6053 | /* pointers to the beginning and end+1 of input */ |
| 6054 | const Py_UNICODE *startp = p; |
| 6055 | const Py_UNICODE *endp = p + size; |
| 6056 | /* pointer into the output */ |
| 6057 | Py_UNICODE *str; |
| 6058 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6059 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6060 | char *reason = "character maps to <undefined>"; |
| 6061 | PyObject *errorHandler = NULL; |
| 6062 | PyObject *exc = NULL; |
| 6063 | /* the following variable is used for caching string comparisons |
| 6064 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 6065 | * 3=ignore, 4=xmlcharrefreplace */ |
| 6066 | int known_errorHandler = -1; |
| 6067 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6068 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6069 | PyErr_BadArgument(); |
| 6070 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6071 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6072 | |
| 6073 | /* allocate enough for a simple 1:1 translation without |
| 6074 | replacements, if we need more, we'll resize */ |
| 6075 | res = PyUnicode_FromUnicode(NULL, size); |
| 6076 | if (res == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6077 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6078 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6079 | return res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6080 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6081 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6082 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6083 | /* try to encode it */ |
| 6084 | PyObject *x = NULL; |
| 6085 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
| 6086 | Py_XDECREF(x); |
| 6087 | goto onError; |
| 6088 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6089 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6090 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 6091 | ++p; |
| 6092 | else { /* untranslatable character */ |
| 6093 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 6094 | Py_ssize_t repsize; |
| 6095 | Py_ssize_t newpos; |
| 6096 | Py_UNICODE *uni2; |
| 6097 | /* startpos for collecting untranslatable chars */ |
| 6098 | const Py_UNICODE *collstart = p; |
| 6099 | const Py_UNICODE *collend = p+1; |
| 6100 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6101 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6102 | /* find all untranslatable characters */ |
| 6103 | while (collend < endp) { |
| 6104 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
| 6105 | goto onError; |
| 6106 | Py_XDECREF(x); |
| 6107 | if (x!=Py_None) |
| 6108 | break; |
| 6109 | ++collend; |
| 6110 | } |
| 6111 | /* cache callback name lookup |
| 6112 | * (if not done yet, i.e. it's the first error) */ |
| 6113 | if (known_errorHandler==-1) { |
| 6114 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6115 | known_errorHandler = 1; |
| 6116 | else if (!strcmp(errors, "replace")) |
| 6117 | known_errorHandler = 2; |
| 6118 | else if (!strcmp(errors, "ignore")) |
| 6119 | known_errorHandler = 3; |
| 6120 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6121 | known_errorHandler = 4; |
| 6122 | else |
| 6123 | known_errorHandler = 0; |
| 6124 | } |
| 6125 | switch (known_errorHandler) { |
| 6126 | case 1: /* strict */ |
| 6127 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6128 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6129 | case 2: /* replace */ |
| 6130 | /* No need to check for space, this is a 1:1 replacement */ |
| 6131 | for (coll = collstart; coll<collend; ++coll) |
| 6132 | *str++ = '?'; |
| 6133 | /* fall through */ |
| 6134 | case 3: /* ignore */ |
| 6135 | p = collend; |
| 6136 | break; |
| 6137 | case 4: /* xmlcharrefreplace */ |
| 6138 | /* generate replacement (temporarily (mis)uses p) */ |
| 6139 | for (p = collstart; p < collend; ++p) { |
| 6140 | char buffer[2+29+1+1]; |
| 6141 | char *cp; |
| 6142 | sprintf(buffer, "&#%d;", (int)*p); |
| 6143 | if (charmaptranslate_makespace(&res, &str, |
| 6144 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 6145 | goto onError; |
| 6146 | for (cp = buffer; *cp; ++cp) |
| 6147 | *str++ = *cp; |
| 6148 | } |
| 6149 | p = collend; |
| 6150 | break; |
| 6151 | default: |
| 6152 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 6153 | reason, startp, size, &exc, |
| 6154 | collstart-startp, collend-startp, &newpos); |
| 6155 | if (repunicode == NULL) |
| 6156 | goto onError; |
| 6157 | /* generate replacement */ |
| 6158 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 6159 | if (charmaptranslate_makespace(&res, &str, |
| 6160 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 6161 | Py_DECREF(repunicode); |
| 6162 | goto onError; |
| 6163 | } |
| 6164 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 6165 | *str++ = *uni2; |
| 6166 | p = startp + newpos; |
| 6167 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6168 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6169 | } |
| 6170 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6171 | /* Resize if we allocated to much */ |
| 6172 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 6173 | if (respos<PyUnicode_GET_SIZE(res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6174 | if (PyUnicode_Resize(&res, respos) < 0) |
| 6175 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6176 | } |
| 6177 | Py_XDECREF(exc); |
| 6178 | Py_XDECREF(errorHandler); |
| 6179 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6180 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6181 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6182 | Py_XDECREF(res); |
| 6183 | Py_XDECREF(exc); |
| 6184 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6185 | return NULL; |
| 6186 | } |
| 6187 | |
| 6188 | PyObject *PyUnicode_Translate(PyObject *str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6189 | PyObject *mapping, |
| 6190 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6191 | { |
| 6192 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6193 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6194 | str = PyUnicode_FromObject(str); |
| 6195 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6196 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6197 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6198 | PyUnicode_GET_SIZE(str), |
| 6199 | mapping, |
| 6200 | errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6201 | Py_DECREF(str); |
| 6202 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6203 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6204 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6205 | Py_XDECREF(str); |
| 6206 | return NULL; |
| 6207 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6208 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6209 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 6210 | |
| 6211 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6212 | Py_ssize_t length, |
| 6213 | char *output, |
| 6214 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6215 | { |
| 6216 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6217 | PyObject *errorHandler = NULL; |
| 6218 | PyObject *exc = NULL; |
| 6219 | const char *encoding = "decimal"; |
| 6220 | const char *reason = "invalid decimal Unicode string"; |
| 6221 | /* the following variable is used for caching string comparisons |
| 6222 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6223 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6224 | |
| 6225 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6226 | PyErr_BadArgument(); |
| 6227 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6228 | } |
| 6229 | |
| 6230 | p = s; |
| 6231 | end = s + length; |
| 6232 | while (p < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6233 | register Py_UNICODE ch = *p; |
| 6234 | int decimal; |
| 6235 | PyObject *repunicode; |
| 6236 | Py_ssize_t repsize; |
| 6237 | Py_ssize_t newpos; |
| 6238 | Py_UNICODE *uni2; |
| 6239 | Py_UNICODE *collstart; |
| 6240 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6241 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6242 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6243 | *output++ = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6244 | ++p; |
| 6245 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6246 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6247 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 6248 | if (decimal >= 0) { |
| 6249 | *output++ = '0' + decimal; |
| 6250 | ++p; |
| 6251 | continue; |
| 6252 | } |
| 6253 | if (0 < ch && ch < 256) { |
| 6254 | *output++ = (char)ch; |
| 6255 | ++p; |
| 6256 | continue; |
| 6257 | } |
| 6258 | /* All other characters are considered unencodable */ |
| 6259 | collstart = p; |
| 6260 | collend = p+1; |
| 6261 | while (collend < end) { |
| 6262 | if ((0 < *collend && *collend < 256) || |
| 6263 | !Py_UNICODE_ISSPACE(*collend) || |
| 6264 | Py_UNICODE_TODECIMAL(*collend)) |
| 6265 | break; |
| 6266 | } |
| 6267 | /* cache callback name lookup |
| 6268 | * (if not done yet, i.e. it's the first error) */ |
| 6269 | if (known_errorHandler==-1) { |
| 6270 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6271 | known_errorHandler = 1; |
| 6272 | else if (!strcmp(errors, "replace")) |
| 6273 | known_errorHandler = 2; |
| 6274 | else if (!strcmp(errors, "ignore")) |
| 6275 | known_errorHandler = 3; |
| 6276 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6277 | known_errorHandler = 4; |
| 6278 | else |
| 6279 | known_errorHandler = 0; |
| 6280 | } |
| 6281 | switch (known_errorHandler) { |
| 6282 | case 1: /* strict */ |
| 6283 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 6284 | goto onError; |
| 6285 | case 2: /* replace */ |
| 6286 | for (p = collstart; p < collend; ++p) |
| 6287 | *output++ = '?'; |
| 6288 | /* fall through */ |
| 6289 | case 3: /* ignore */ |
| 6290 | p = collend; |
| 6291 | break; |
| 6292 | case 4: /* xmlcharrefreplace */ |
| 6293 | /* generate replacement (temporarily (mis)uses p) */ |
| 6294 | for (p = collstart; p < collend; ++p) |
| 6295 | output += sprintf(output, "&#%d;", (int)*p); |
| 6296 | p = collend; |
| 6297 | break; |
| 6298 | default: |
| 6299 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 6300 | encoding, reason, s, length, &exc, |
| 6301 | collstart-s, collend-s, &newpos); |
| 6302 | if (repunicode == NULL) |
| 6303 | goto onError; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6304 | if (!PyUnicode_Check(repunicode)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6305 | /* Byte results not supported, since they have no decimal property. */ |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6306 | PyErr_SetString(PyExc_TypeError, "error handler should return unicode"); |
| 6307 | Py_DECREF(repunicode); |
| 6308 | goto onError; |
| 6309 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6310 | /* generate replacement */ |
| 6311 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 6312 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 6313 | Py_UNICODE ch = *uni2; |
| 6314 | if (Py_UNICODE_ISSPACE(ch)) |
| 6315 | *output++ = ' '; |
| 6316 | else { |
| 6317 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 6318 | if (decimal >= 0) |
| 6319 | *output++ = '0' + decimal; |
| 6320 | else if (0 < ch && ch < 256) |
| 6321 | *output++ = (char)ch; |
| 6322 | else { |
| 6323 | Py_DECREF(repunicode); |
| 6324 | raise_encode_exception(&exc, encoding, |
| 6325 | s, length, collstart-s, collend-s, reason); |
| 6326 | goto onError; |
| 6327 | } |
| 6328 | } |
| 6329 | } |
| 6330 | p = s + newpos; |
| 6331 | Py_DECREF(repunicode); |
| 6332 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6333 | } |
| 6334 | /* 0-terminate the output string */ |
| 6335 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6336 | Py_XDECREF(exc); |
| 6337 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6338 | return 0; |
| 6339 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6340 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6341 | Py_XDECREF(exc); |
| 6342 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6343 | return -1; |
| 6344 | } |
| 6345 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6346 | /* --- Helpers ------------------------------------------------------------ */ |
| 6347 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 6348 | #include "stringlib/unicodedefs.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6349 | #include "stringlib/fastsearch.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6350 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6351 | #include "stringlib/count.h" |
| 6352 | #include "stringlib/find.h" |
| 6353 | #include "stringlib/partition.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6354 | #include "stringlib/split.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6355 | |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 6356 | #define _Py_InsertThousandsGrouping _PyUnicode_InsertThousandsGrouping |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame] | 6357 | #define _Py_InsertThousandsGroupingLocale _PyUnicode_InsertThousandsGroupingLocale |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 6358 | #include "stringlib/localeutil.h" |
| 6359 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6360 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6361 | #define ADJUST_INDICES(start, end, len) \ |
| 6362 | if (end > len) \ |
| 6363 | end = len; \ |
| 6364 | else if (end < 0) { \ |
| 6365 | end += len; \ |
| 6366 | if (end < 0) \ |
| 6367 | end = 0; \ |
| 6368 | } \ |
| 6369 | if (start < 0) { \ |
| 6370 | start += len; \ |
| 6371 | if (start < 0) \ |
| 6372 | start = 0; \ |
| 6373 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6374 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6375 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6376 | PyObject *substr, |
| 6377 | Py_ssize_t start, |
| 6378 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6379 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6380 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6381 | PyUnicodeObject* str_obj; |
| 6382 | PyUnicodeObject* sub_obj; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6383 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6384 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
| 6385 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6386 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6387 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
| 6388 | if (!sub_obj) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6389 | Py_DECREF(str_obj); |
| 6390 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6391 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6392 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6393 | ADJUST_INDICES(start, end, str_obj->length); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6394 | result = stringlib_count( |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6395 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length, |
| 6396 | PY_SSIZE_T_MAX |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6397 | ); |
| 6398 | |
| 6399 | Py_DECREF(sub_obj); |
| 6400 | Py_DECREF(str_obj); |
| 6401 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6402 | return result; |
| 6403 | } |
| 6404 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6405 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6406 | PyObject *sub, |
| 6407 | Py_ssize_t start, |
| 6408 | Py_ssize_t end, |
| 6409 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6410 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6411 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6412 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6413 | str = PyUnicode_FromObject(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6414 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6415 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6416 | sub = PyUnicode_FromObject(sub); |
| 6417 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6418 | Py_DECREF(str); |
| 6419 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6420 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6421 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6422 | if (direction > 0) |
| 6423 | result = stringlib_find_slice( |
| 6424 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 6425 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 6426 | start, end |
| 6427 | ); |
| 6428 | else |
| 6429 | result = stringlib_rfind_slice( |
| 6430 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 6431 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 6432 | start, end |
| 6433 | ); |
| 6434 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6435 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6436 | Py_DECREF(sub); |
| 6437 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6438 | return result; |
| 6439 | } |
| 6440 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6441 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6442 | int tailmatch(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6443 | PyUnicodeObject *substring, |
| 6444 | Py_ssize_t start, |
| 6445 | Py_ssize_t end, |
| 6446 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6447 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6448 | if (substring->length == 0) |
| 6449 | return 1; |
| 6450 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6451 | ADJUST_INDICES(start, end, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6452 | end -= substring->length; |
| 6453 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6454 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6455 | |
| 6456 | if (direction > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6457 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 6458 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6459 | } else { |
| 6460 | if (Py_UNICODE_MATCH(self, start, substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6461 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6462 | } |
| 6463 | |
| 6464 | return 0; |
| 6465 | } |
| 6466 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6467 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6468 | PyObject *substr, |
| 6469 | Py_ssize_t start, |
| 6470 | Py_ssize_t end, |
| 6471 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6472 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6473 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6474 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6475 | str = PyUnicode_FromObject(str); |
| 6476 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6477 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6478 | substr = PyUnicode_FromObject(substr); |
| 6479 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6480 | Py_DECREF(str); |
| 6481 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6482 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6483 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6484 | result = tailmatch((PyUnicodeObject *)str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6485 | (PyUnicodeObject *)substr, |
| 6486 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6487 | Py_DECREF(str); |
| 6488 | Py_DECREF(substr); |
| 6489 | return result; |
| 6490 | } |
| 6491 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6492 | /* Apply fixfct filter to the Unicode object self and return a |
| 6493 | reference to the modified object */ |
| 6494 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6495 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6496 | PyObject *fixup(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6497 | int (*fixfct)(PyUnicodeObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6498 | { |
| 6499 | |
| 6500 | PyUnicodeObject *u; |
| 6501 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6502 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6503 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6504 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6505 | |
| 6506 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6507 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6508 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6509 | /* fixfct should return TRUE if it modified the buffer. If |
| 6510 | FALSE, return a reference to the original buffer instead |
| 6511 | (to save space, not time) */ |
| 6512 | Py_INCREF(self); |
| 6513 | Py_DECREF(u); |
| 6514 | return (PyObject*) self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6515 | } |
| 6516 | return (PyObject*) u; |
| 6517 | } |
| 6518 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6519 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6520 | int fixupper(PyUnicodeObject *self) |
| 6521 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6522 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6523 | Py_UNICODE *s = self->str; |
| 6524 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6525 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6526 | while (len-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6527 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6528 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6529 | ch = Py_UNICODE_TOUPPER(*s); |
| 6530 | if (ch != *s) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6531 | status = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6532 | *s = ch; |
| 6533 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6534 | s++; |
| 6535 | } |
| 6536 | |
| 6537 | return status; |
| 6538 | } |
| 6539 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6540 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6541 | int fixlower(PyUnicodeObject *self) |
| 6542 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6543 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6544 | Py_UNICODE *s = self->str; |
| 6545 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6546 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6547 | while (len-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6548 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6549 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6550 | ch = Py_UNICODE_TOLOWER(*s); |
| 6551 | if (ch != *s) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6552 | status = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6553 | *s = ch; |
| 6554 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6555 | s++; |
| 6556 | } |
| 6557 | |
| 6558 | return status; |
| 6559 | } |
| 6560 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6561 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6562 | int fixswapcase(PyUnicodeObject *self) |
| 6563 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6564 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6565 | Py_UNICODE *s = self->str; |
| 6566 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6567 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6568 | while (len-- > 0) { |
| 6569 | if (Py_UNICODE_ISUPPER(*s)) { |
| 6570 | *s = Py_UNICODE_TOLOWER(*s); |
| 6571 | status = 1; |
| 6572 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 6573 | *s = Py_UNICODE_TOUPPER(*s); |
| 6574 | status = 1; |
| 6575 | } |
| 6576 | s++; |
| 6577 | } |
| 6578 | |
| 6579 | return status; |
| 6580 | } |
| 6581 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6582 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6583 | int fixcapitalize(PyUnicodeObject *self) |
| 6584 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6585 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6586 | Py_UNICODE *s = self->str; |
| 6587 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6588 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6589 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6590 | return 0; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6591 | if (Py_UNICODE_ISLOWER(*s)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6592 | *s = Py_UNICODE_TOUPPER(*s); |
| 6593 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6594 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6595 | s++; |
| 6596 | while (--len > 0) { |
| 6597 | if (Py_UNICODE_ISUPPER(*s)) { |
| 6598 | *s = Py_UNICODE_TOLOWER(*s); |
| 6599 | status = 1; |
| 6600 | } |
| 6601 | s++; |
| 6602 | } |
| 6603 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6604 | } |
| 6605 | |
| 6606 | static |
| 6607 | int fixtitle(PyUnicodeObject *self) |
| 6608 | { |
| 6609 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6610 | register Py_UNICODE *e; |
| 6611 | int previous_is_cased; |
| 6612 | |
| 6613 | /* Shortcut for single character strings */ |
| 6614 | if (PyUnicode_GET_SIZE(self) == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6615 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 6616 | if (*p != ch) { |
| 6617 | *p = ch; |
| 6618 | return 1; |
| 6619 | } |
| 6620 | else |
| 6621 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6622 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6623 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6624 | e = p + PyUnicode_GET_SIZE(self); |
| 6625 | previous_is_cased = 0; |
| 6626 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6627 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6628 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6629 | if (previous_is_cased) |
| 6630 | *p = Py_UNICODE_TOLOWER(ch); |
| 6631 | else |
| 6632 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6633 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6634 | if (Py_UNICODE_ISLOWER(ch) || |
| 6635 | Py_UNICODE_ISUPPER(ch) || |
| 6636 | Py_UNICODE_ISTITLE(ch)) |
| 6637 | previous_is_cased = 1; |
| 6638 | else |
| 6639 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6640 | } |
| 6641 | return 1; |
| 6642 | } |
| 6643 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6644 | PyObject * |
| 6645 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6646 | { |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 6647 | const Py_UNICODE blank = ' '; |
| 6648 | const Py_UNICODE *sep = ␣ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6649 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6650 | PyUnicodeObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6651 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 6652 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6653 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 6654 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6655 | PyObject *item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6656 | Py_ssize_t sz, i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6657 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6658 | fseq = PySequence_Fast(seq, ""); |
| 6659 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6660 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6661 | } |
| 6662 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6663 | /* NOTE: the following code can't call back into Python code, |
| 6664 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 6665 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6666 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6667 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 6668 | /* If empty sequence, return u"". */ |
| 6669 | if (seqlen == 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6670 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 6671 | goto Done; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6672 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6673 | items = PySequence_Fast_ITEMS(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6674 | /* If singleton sequence with an exact Unicode, return that. */ |
| 6675 | if (seqlen == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6676 | item = items[0]; |
| 6677 | if (PyUnicode_CheckExact(item)) { |
| 6678 | Py_INCREF(item); |
| 6679 | res = (PyUnicodeObject *)item; |
| 6680 | goto Done; |
| 6681 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6682 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6683 | else { |
| 6684 | /* Set up sep and seplen */ |
| 6685 | if (separator == NULL) { |
| 6686 | sep = ␣ |
| 6687 | seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6688 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6689 | else { |
| 6690 | if (!PyUnicode_Check(separator)) { |
| 6691 | PyErr_Format(PyExc_TypeError, |
| 6692 | "separator: expected str instance," |
| 6693 | " %.80s found", |
| 6694 | Py_TYPE(separator)->tp_name); |
| 6695 | goto onError; |
| 6696 | } |
| 6697 | sep = PyUnicode_AS_UNICODE(separator); |
| 6698 | seplen = PyUnicode_GET_SIZE(separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6699 | } |
| 6700 | } |
| 6701 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6702 | /* There are at least two things to join, or else we have a subclass |
| 6703 | * of str in the sequence. |
| 6704 | * Do a pre-pass to figure out the total amount of space we'll |
| 6705 | * need (sz), and see whether all argument are strings. |
| 6706 | */ |
| 6707 | sz = 0; |
| 6708 | for (i = 0; i < seqlen; i++) { |
| 6709 | const Py_ssize_t old_sz = sz; |
| 6710 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6711 | if (!PyUnicode_Check(item)) { |
| 6712 | PyErr_Format(PyExc_TypeError, |
| 6713 | "sequence item %zd: expected str instance," |
| 6714 | " %.80s found", |
| 6715 | i, Py_TYPE(item)->tp_name); |
| 6716 | goto onError; |
| 6717 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6718 | sz += PyUnicode_GET_SIZE(item); |
| 6719 | if (i != 0) |
| 6720 | sz += seplen; |
| 6721 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 6722 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6723 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6724 | goto onError; |
| 6725 | } |
| 6726 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6727 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6728 | res = _PyUnicode_New(sz); |
| 6729 | if (res == NULL) |
| 6730 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 6731 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6732 | /* Catenate everything. */ |
| 6733 | res_p = PyUnicode_AS_UNICODE(res); |
| 6734 | for (i = 0; i < seqlen; ++i) { |
| 6735 | Py_ssize_t itemlen; |
| 6736 | item = items[i]; |
| 6737 | itemlen = PyUnicode_GET_SIZE(item); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6738 | /* Copy item, and maybe the separator. */ |
| 6739 | if (i) { |
| 6740 | Py_UNICODE_COPY(res_p, sep, seplen); |
| 6741 | res_p += seplen; |
| 6742 | } |
| 6743 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
| 6744 | res_p += itemlen; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6745 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6746 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6747 | Done: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6748 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6749 | return (PyObject *)res; |
| 6750 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6751 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6752 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6753 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6754 | return NULL; |
| 6755 | } |
| 6756 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6757 | static |
| 6758 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6759 | Py_ssize_t left, |
| 6760 | Py_ssize_t right, |
| 6761 | Py_UNICODE fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6762 | { |
| 6763 | PyUnicodeObject *u; |
| 6764 | |
| 6765 | if (left < 0) |
| 6766 | left = 0; |
| 6767 | if (right < 0) |
| 6768 | right = 0; |
| 6769 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6770 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6771 | Py_INCREF(self); |
| 6772 | return self; |
| 6773 | } |
| 6774 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6775 | if (left > PY_SSIZE_T_MAX - self->length || |
| 6776 | right > PY_SSIZE_T_MAX - (left + self->length)) { |
| 6777 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 6778 | return NULL; |
| 6779 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6780 | u = _PyUnicode_New(left + self->length + right); |
| 6781 | if (u) { |
| 6782 | if (left) |
| 6783 | Py_UNICODE_FILL(u->str, fill, left); |
| 6784 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 6785 | if (right) |
| 6786 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 6787 | } |
| 6788 | |
| 6789 | return u; |
| 6790 | } |
| 6791 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6792 | PyObject *PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6793 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6794 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6795 | |
| 6796 | string = PyUnicode_FromObject(string); |
| 6797 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6798 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6799 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6800 | list = stringlib_splitlines( |
| 6801 | (PyObject*) string, PyUnicode_AS_UNICODE(string), |
| 6802 | PyUnicode_GET_SIZE(string), keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6803 | |
| 6804 | Py_DECREF(string); |
| 6805 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6806 | } |
| 6807 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6808 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6809 | PyObject *split(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6810 | PyUnicodeObject *substring, |
| 6811 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6812 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6813 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6814 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6815 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6816 | if (substring == NULL) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6817 | return stringlib_split_whitespace( |
| 6818 | (PyObject*) self, self->str, self->length, maxcount |
| 6819 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6820 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6821 | return stringlib_split( |
| 6822 | (PyObject*) self, self->str, self->length, |
| 6823 | substring->str, substring->length, |
| 6824 | maxcount |
| 6825 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6826 | } |
| 6827 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6828 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6829 | PyObject *rsplit(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6830 | PyUnicodeObject *substring, |
| 6831 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6832 | { |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6833 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6834 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6835 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6836 | if (substring == NULL) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6837 | return stringlib_rsplit_whitespace( |
| 6838 | (PyObject*) self, self->str, self->length, maxcount |
| 6839 | ); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6840 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6841 | return stringlib_rsplit( |
| 6842 | (PyObject*) self, self->str, self->length, |
| 6843 | substring->str, substring->length, |
| 6844 | maxcount |
| 6845 | ); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6846 | } |
| 6847 | |
| 6848 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6849 | PyObject *replace(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6850 | PyUnicodeObject *str1, |
| 6851 | PyUnicodeObject *str2, |
| 6852 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6853 | { |
| 6854 | PyUnicodeObject *u; |
| 6855 | |
| 6856 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6857 | maxcount = PY_SSIZE_T_MAX; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6858 | else if (maxcount == 0 || self->length == 0) |
| 6859 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6860 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6861 | if (str1->length == str2->length) { |
Antoine Pitrou | cbfdee3 | 2010-01-13 08:58:08 +0000 | [diff] [blame] | 6862 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6863 | /* same length */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6864 | if (str1->length == 0) |
| 6865 | goto nothing; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6866 | if (str1->length == 1) { |
| 6867 | /* replace characters */ |
| 6868 | Py_UNICODE u1, u2; |
| 6869 | if (!findchar(self->str, self->length, str1->str[0])) |
| 6870 | goto nothing; |
| 6871 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6872 | if (!u) |
| 6873 | return NULL; |
| 6874 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6875 | u1 = str1->str[0]; |
| 6876 | u2 = str2->str[0]; |
| 6877 | for (i = 0; i < u->length; i++) |
| 6878 | if (u->str[i] == u1) { |
| 6879 | if (--maxcount < 0) |
| 6880 | break; |
| 6881 | u->str[i] = u2; |
| 6882 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6883 | } else { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6884 | i = stringlib_find( |
| 6885 | self->str, self->length, str1->str, str1->length, 0 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6886 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6887 | if (i < 0) |
| 6888 | goto nothing; |
| 6889 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6890 | if (!u) |
| 6891 | return NULL; |
| 6892 | Py_UNICODE_COPY(u->str, self->str, self->length); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6893 | |
| 6894 | /* change everything in-place, starting with this one */ |
| 6895 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 6896 | i += str1->length; |
| 6897 | |
| 6898 | while ( --maxcount > 0) { |
| 6899 | i = stringlib_find(self->str+i, self->length-i, |
| 6900 | str1->str, str1->length, |
| 6901 | i); |
| 6902 | if (i == -1) |
| 6903 | break; |
| 6904 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 6905 | i += str1->length; |
| 6906 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6907 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6908 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6909 | |
| 6910 | Py_ssize_t n, i, j, e; |
| 6911 | Py_ssize_t product, new_size, delta; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6912 | Py_UNICODE *p; |
| 6913 | |
| 6914 | /* replace strings */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6915 | n = stringlib_count(self->str, self->length, str1->str, str1->length, |
| 6916 | maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6917 | if (n == 0) |
| 6918 | goto nothing; |
| 6919 | /* new_size = self->length + n * (str2->length - str1->length)); */ |
| 6920 | delta = (str2->length - str1->length); |
| 6921 | if (delta == 0) { |
| 6922 | new_size = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6923 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6924 | product = n * (str2->length - str1->length); |
| 6925 | if ((product / (str2->length - str1->length)) != n) { |
| 6926 | PyErr_SetString(PyExc_OverflowError, |
| 6927 | "replace string is too long"); |
| 6928 | return NULL; |
| 6929 | } |
| 6930 | new_size = self->length + product; |
| 6931 | if (new_size < 0) { |
| 6932 | PyErr_SetString(PyExc_OverflowError, |
| 6933 | "replace string is too long"); |
| 6934 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6935 | } |
| 6936 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6937 | u = _PyUnicode_New(new_size); |
| 6938 | if (!u) |
| 6939 | return NULL; |
| 6940 | i = 0; |
| 6941 | p = u->str; |
| 6942 | e = self->length - str1->length; |
| 6943 | if (str1->length > 0) { |
| 6944 | while (n-- > 0) { |
| 6945 | /* look for next match */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6946 | j = stringlib_find(self->str+i, self->length-i, |
| 6947 | str1->str, str1->length, |
| 6948 | i); |
| 6949 | if (j == -1) |
| 6950 | break; |
| 6951 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6952 | /* copy unchanged part [i:j] */ |
| 6953 | Py_UNICODE_COPY(p, self->str+i, j-i); |
| 6954 | p += j - i; |
| 6955 | } |
| 6956 | /* copy substitution string */ |
| 6957 | if (str2->length > 0) { |
| 6958 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6959 | p += str2->length; |
| 6960 | } |
| 6961 | i = j + str1->length; |
| 6962 | } |
| 6963 | if (i < self->length) |
| 6964 | /* copy tail [i:] */ |
| 6965 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6966 | } else { |
| 6967 | /* interleave */ |
| 6968 | while (n > 0) { |
| 6969 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6970 | p += str2->length; |
| 6971 | if (--n <= 0) |
| 6972 | break; |
| 6973 | *p++ = self->str[i++]; |
| 6974 | } |
| 6975 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6976 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6977 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6978 | return (PyObject *) u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6979 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6980 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6981 | /* nothing to replace; return original string (when possible) */ |
| 6982 | if (PyUnicode_CheckExact(self)) { |
| 6983 | Py_INCREF(self); |
| 6984 | return (PyObject *) self; |
| 6985 | } |
| 6986 | return PyUnicode_FromUnicode(self->str, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6987 | } |
| 6988 | |
| 6989 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 6990 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6991 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6992 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6993 | \n\ |
| 6994 | 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] | 6995 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6996 | |
| 6997 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6998 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6999 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7000 | return fixup(self, fixtitle); |
| 7001 | } |
| 7002 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7003 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7004 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7005 | \n\ |
| 7006 | 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] | 7007 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7008 | |
| 7009 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7010 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7011 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7012 | return fixup(self, fixcapitalize); |
| 7013 | } |
| 7014 | |
| 7015 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7016 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7017 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7018 | \n\ |
| 7019 | 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] | 7020 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7021 | |
| 7022 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7023 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7024 | { |
| 7025 | PyObject *list; |
| 7026 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7027 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7028 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7029 | /* Split into words */ |
| 7030 | list = split(self, NULL, -1); |
| 7031 | if (!list) |
| 7032 | return NULL; |
| 7033 | |
| 7034 | /* Capitalize each word */ |
| 7035 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 7036 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7037 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7038 | if (item == NULL) |
| 7039 | goto onError; |
| 7040 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 7041 | PyList_SET_ITEM(list, i, item); |
| 7042 | } |
| 7043 | |
| 7044 | /* Join the words to form a new string */ |
| 7045 | item = PyUnicode_Join(NULL, list); |
| 7046 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7047 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7048 | Py_DECREF(list); |
| 7049 | return (PyObject *)item; |
| 7050 | } |
| 7051 | #endif |
| 7052 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7053 | /* Argument converter. Coerces to a single unicode character */ |
| 7054 | |
| 7055 | static int |
| 7056 | convert_uc(PyObject *obj, void *addr) |
| 7057 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7058 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 7059 | PyObject *uniobj; |
| 7060 | Py_UNICODE *unistr; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7061 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7062 | uniobj = PyUnicode_FromObject(obj); |
| 7063 | if (uniobj == NULL) { |
| 7064 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7065 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7066 | return 0; |
| 7067 | } |
| 7068 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 7069 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7070 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7071 | Py_DECREF(uniobj); |
| 7072 | return 0; |
| 7073 | } |
| 7074 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 7075 | *fillcharloc = unistr[0]; |
| 7076 | Py_DECREF(uniobj); |
| 7077 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7078 | } |
| 7079 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7080 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7081 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7082 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7083 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7084 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7085 | |
| 7086 | static PyObject * |
| 7087 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 7088 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7089 | Py_ssize_t marg, left; |
| 7090 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7091 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7092 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 7093 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7094 | return NULL; |
| 7095 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7096 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7097 | Py_INCREF(self); |
| 7098 | return (PyObject*) self; |
| 7099 | } |
| 7100 | |
| 7101 | marg = width - self->length; |
| 7102 | left = marg / 2 + (marg & width & 1); |
| 7103 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7104 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7105 | } |
| 7106 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 7107 | #if 0 |
| 7108 | |
| 7109 | /* This code should go into some future Unicode collation support |
| 7110 | module. The basic comparison should compare ordinals on a naive |
Georg Brandl | c6c3178 | 2009-06-08 13:41:29 +0000 | [diff] [blame] | 7111 | basis (this is what Java does and thus Jython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 7112 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 7113 | /* speedy UTF-16 code point order comparison */ |
| 7114 | /* gleaned from: */ |
| 7115 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 7116 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 7117 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 7118 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 7119 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7120 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 7121 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 7122 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 7123 | }; |
| 7124 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7125 | static int |
| 7126 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 7127 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7128 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 7129 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7130 | Py_UNICODE *s1 = str1->str; |
| 7131 | Py_UNICODE *s2 = str2->str; |
| 7132 | |
| 7133 | len1 = str1->length; |
| 7134 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7135 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7136 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7137 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 7138 | |
| 7139 | c1 = *s1++; |
| 7140 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 7141 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7142 | if (c1 > (1<<11) * 26) |
| 7143 | c1 += utf16Fixup[c1>>11]; |
| 7144 | if (c2 > (1<<11) * 26) |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 7145 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 7146 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 7147 | |
| 7148 | if (c1 != c2) |
| 7149 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7150 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 7151 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7152 | } |
| 7153 | |
| 7154 | return (len1 < len2) ? -1 : (len1 != len2); |
| 7155 | } |
| 7156 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 7157 | #else |
| 7158 | |
| 7159 | static int |
| 7160 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 7161 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7162 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 7163 | |
| 7164 | Py_UNICODE *s1 = str1->str; |
| 7165 | Py_UNICODE *s2 = str2->str; |
| 7166 | |
| 7167 | len1 = str1->length; |
| 7168 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7169 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 7170 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7171 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 7172 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 7173 | c1 = *s1++; |
| 7174 | c2 = *s2++; |
| 7175 | |
| 7176 | if (c1 != c2) |
| 7177 | return (c1 < c2) ? -1 : 1; |
| 7178 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 7179 | len1--; len2--; |
| 7180 | } |
| 7181 | |
| 7182 | return (len1 < len2) ? -1 : (len1 != len2); |
| 7183 | } |
| 7184 | |
| 7185 | #endif |
| 7186 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7187 | int PyUnicode_Compare(PyObject *left, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7188 | PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7189 | { |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 7190 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) |
| 7191 | return unicode_compare((PyUnicodeObject *)left, |
| 7192 | (PyUnicodeObject *)right); |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 7193 | PyErr_Format(PyExc_TypeError, |
| 7194 | "Can't compare %.100s and %.100s", |
| 7195 | left->ob_type->tp_name, |
| 7196 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7197 | return -1; |
| 7198 | } |
| 7199 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 7200 | int |
| 7201 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 7202 | { |
| 7203 | int i; |
| 7204 | Py_UNICODE *id; |
| 7205 | assert(PyUnicode_Check(uni)); |
| 7206 | id = PyUnicode_AS_UNICODE(uni); |
| 7207 | /* Compare Unicode string and source character set string */ |
| 7208 | for (i = 0; id[i] && str[i]; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7209 | if (id[i] != str[i]) |
| 7210 | return ((int)id[i] < (int)str[i]) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 7211 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 7212 | to C strings identical up to that point. */ |
Benjamin Peterson | a23831f | 2010-04-25 21:54:00 +0000 | [diff] [blame] | 7213 | if (PyUnicode_GET_SIZE(uni) != i || id[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7214 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 7215 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7216 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 7217 | return 0; |
| 7218 | } |
| 7219 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 7220 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7221 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7222 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 7223 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 7224 | PyObject *PyUnicode_RichCompare(PyObject *left, |
| 7225 | PyObject *right, |
| 7226 | int op) |
| 7227 | { |
| 7228 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7229 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 7230 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 7231 | PyObject *v; |
| 7232 | if (((PyUnicodeObject *) left)->length != |
| 7233 | ((PyUnicodeObject *) right)->length) { |
| 7234 | if (op == Py_EQ) { |
| 7235 | Py_INCREF(Py_False); |
| 7236 | return Py_False; |
| 7237 | } |
| 7238 | if (op == Py_NE) { |
| 7239 | Py_INCREF(Py_True); |
| 7240 | return Py_True; |
| 7241 | } |
| 7242 | } |
| 7243 | if (left == right) |
| 7244 | result = 0; |
| 7245 | else |
| 7246 | result = unicode_compare((PyUnicodeObject *)left, |
| 7247 | (PyUnicodeObject *)right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7248 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 7249 | /* Convert the return value to a Boolean */ |
| 7250 | switch (op) { |
| 7251 | case Py_EQ: |
| 7252 | v = TEST_COND(result == 0); |
| 7253 | break; |
| 7254 | case Py_NE: |
| 7255 | v = TEST_COND(result != 0); |
| 7256 | break; |
| 7257 | case Py_LE: |
| 7258 | v = TEST_COND(result <= 0); |
| 7259 | break; |
| 7260 | case Py_GE: |
| 7261 | v = TEST_COND(result >= 0); |
| 7262 | break; |
| 7263 | case Py_LT: |
| 7264 | v = TEST_COND(result == -1); |
| 7265 | break; |
| 7266 | case Py_GT: |
| 7267 | v = TEST_COND(result == 1); |
| 7268 | break; |
| 7269 | default: |
| 7270 | PyErr_BadArgument(); |
| 7271 | return NULL; |
| 7272 | } |
| 7273 | Py_INCREF(v); |
| 7274 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 7275 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7276 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 7277 | Py_INCREF(Py_NotImplemented); |
| 7278 | return Py_NotImplemented; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 7279 | } |
| 7280 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7281 | int PyUnicode_Contains(PyObject *container, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7282 | PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7283 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7284 | PyObject *str, *sub; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7285 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7286 | |
| 7287 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7288 | sub = PyUnicode_FromObject(element); |
| 7289 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7290 | PyErr_Format(PyExc_TypeError, |
| 7291 | "'in <string>' requires string as left operand, not %s", |
| 7292 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7293 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7294 | } |
| 7295 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7296 | str = PyUnicode_FromObject(container); |
| 7297 | if (!str) { |
| 7298 | Py_DECREF(sub); |
| 7299 | return -1; |
| 7300 | } |
| 7301 | |
| 7302 | result = stringlib_contains_obj(str, sub); |
| 7303 | |
| 7304 | Py_DECREF(str); |
| 7305 | Py_DECREF(sub); |
| 7306 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7307 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7308 | } |
| 7309 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7310 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 7311 | |
| 7312 | PyObject *PyUnicode_Concat(PyObject *left, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7313 | PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7314 | { |
| 7315 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 7316 | |
| 7317 | /* Coerce the two arguments */ |
| 7318 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 7319 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7320 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7321 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 7322 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7323 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7324 | |
| 7325 | /* Shortcuts */ |
| 7326 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7327 | Py_DECREF(v); |
| 7328 | return (PyObject *)u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7329 | } |
| 7330 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7331 | Py_DECREF(u); |
| 7332 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7333 | } |
| 7334 | |
| 7335 | /* Concat the two Unicode strings */ |
| 7336 | w = _PyUnicode_New(u->length + v->length); |
| 7337 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7338 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7339 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 7340 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 7341 | |
| 7342 | Py_DECREF(u); |
| 7343 | Py_DECREF(v); |
| 7344 | return (PyObject *)w; |
| 7345 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7346 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7347 | Py_XDECREF(u); |
| 7348 | Py_XDECREF(v); |
| 7349 | return NULL; |
| 7350 | } |
| 7351 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7352 | void |
| 7353 | PyUnicode_Append(PyObject **pleft, PyObject *right) |
| 7354 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7355 | PyObject *new; |
| 7356 | if (*pleft == NULL) |
| 7357 | return; |
| 7358 | if (right == NULL || !PyUnicode_Check(*pleft)) { |
| 7359 | Py_DECREF(*pleft); |
| 7360 | *pleft = NULL; |
| 7361 | return; |
| 7362 | } |
| 7363 | new = PyUnicode_Concat(*pleft, right); |
| 7364 | Py_DECREF(*pleft); |
| 7365 | *pleft = new; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7366 | } |
| 7367 | |
| 7368 | void |
| 7369 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 7370 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7371 | PyUnicode_Append(pleft, right); |
| 7372 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7373 | } |
| 7374 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7375 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7376 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7377 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7378 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7379 | 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] | 7380 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7381 | |
| 7382 | static PyObject * |
| 7383 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 7384 | { |
| 7385 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7386 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7387 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7388 | PyObject *result; |
| 7389 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 7390 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7391 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7392 | return NULL; |
| 7393 | |
| 7394 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7395 | (PyObject *)substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7396 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7397 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7398 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 7399 | ADJUST_INDICES(start, end, self->length); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7400 | result = PyLong_FromSsize_t( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7401 | stringlib_count(self->str + start, end - start, |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 7402 | substring->str, substring->length, |
| 7403 | PY_SSIZE_T_MAX) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7404 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7405 | |
| 7406 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7407 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7408 | return result; |
| 7409 | } |
| 7410 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7411 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 7412 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7413 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 7414 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 7415 | is 'utf-8'. errors may be given to set a different error\n\ |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7416 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7417 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 7418 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 7419 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7420 | |
| 7421 | static PyObject * |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 7422 | unicode_encode(PyUnicodeObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7423 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 7424 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7425 | char *encoding = NULL; |
| 7426 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7427 | PyObject *v; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 7428 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 7429 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 7430 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7431 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 7432 | v = PyUnicode_AsEncodedString((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 7433 | if (v == NULL) |
| 7434 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7435 | if (!PyBytes_Check(v)) { |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7436 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 7437 | "encoder did not return a bytes object " |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7438 | "(type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7439 | Py_TYPE(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7440 | Py_DECREF(v); |
| 7441 | return NULL; |
| 7442 | } |
| 7443 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 7444 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7445 | onError: |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 7446 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7447 | } |
| 7448 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7449 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7450 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7451 | \n\ |
| 7452 | 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] | 7453 | 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] | 7454 | |
| 7455 | static PyObject* |
| 7456 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 7457 | { |
| 7458 | Py_UNICODE *e; |
| 7459 | Py_UNICODE *p; |
| 7460 | Py_UNICODE *q; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7461 | Py_UNICODE *qe; |
| 7462 | Py_ssize_t i, j, incr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7463 | PyUnicodeObject *u; |
| 7464 | int tabsize = 8; |
| 7465 | |
| 7466 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7467 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7468 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 7469 | /* First pass: determine size of output string */ |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7470 | i = 0; /* chars up to and including most recent \n or \r */ |
| 7471 | j = 0; /* chars since most recent \n or \r (use in tab calculations) */ |
| 7472 | e = self->str + self->length; /* end of input */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7473 | for (p = self->str; p < e; p++) |
| 7474 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7475 | if (tabsize > 0) { |
| 7476 | incr = tabsize - (j % tabsize); /* cannot overflow */ |
| 7477 | if (j > PY_SSIZE_T_MAX - incr) |
| 7478 | goto overflow1; |
| 7479 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7480 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7481 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7482 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7483 | if (j > PY_SSIZE_T_MAX - 1) |
| 7484 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7485 | j++; |
| 7486 | if (*p == '\n' || *p == '\r') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7487 | if (i > PY_SSIZE_T_MAX - j) |
| 7488 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7489 | i += j; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7490 | j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7491 | } |
| 7492 | } |
| 7493 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7494 | if (i > PY_SSIZE_T_MAX - j) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7495 | goto overflow1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 7496 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7497 | /* Second pass: create output string and fill it */ |
| 7498 | u = _PyUnicode_New(i + j); |
| 7499 | if (!u) |
| 7500 | return NULL; |
| 7501 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7502 | j = 0; /* same as in first pass */ |
| 7503 | q = u->str; /* next output char */ |
| 7504 | qe = u->str + u->length; /* end of output */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7505 | |
| 7506 | for (p = self->str; p < e; p++) |
| 7507 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7508 | if (tabsize > 0) { |
| 7509 | i = tabsize - (j % tabsize); |
| 7510 | j += i; |
| 7511 | while (i--) { |
| 7512 | if (q >= qe) |
| 7513 | goto overflow2; |
| 7514 | *q++ = ' '; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7515 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7516 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7517 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7518 | else { |
| 7519 | if (q >= qe) |
| 7520 | goto overflow2; |
| 7521 | *q++ = *p; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7522 | j++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7523 | if (*p == '\n' || *p == '\r') |
| 7524 | j = 0; |
| 7525 | } |
| 7526 | |
| 7527 | return (PyObject*) u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7528 | |
| 7529 | overflow2: |
| 7530 | Py_DECREF(u); |
| 7531 | overflow1: |
| 7532 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 7533 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7534 | } |
| 7535 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7536 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7537 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7538 | \n\ |
| 7539 | 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] | 7540 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7541 | arguments start and end are interpreted as in slice notation.\n\ |
| 7542 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7543 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7544 | |
| 7545 | static PyObject * |
| 7546 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 7547 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7548 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7549 | Py_ssize_t start; |
| 7550 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7551 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7552 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7553 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7554 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7555 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7556 | result = stringlib_find_slice( |
| 7557 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7558 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7559 | start, end |
| 7560 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7561 | |
| 7562 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7563 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7564 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7565 | } |
| 7566 | |
| 7567 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7568 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7569 | { |
| 7570 | if (index < 0 || index >= self->length) { |
| 7571 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 7572 | return NULL; |
| 7573 | } |
| 7574 | |
| 7575 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 7576 | } |
| 7577 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7578 | /* Believe it or not, this produces the same value for ASCII strings |
| 7579 | as string_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 7580 | static Py_hash_t |
Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 7581 | unicode_hash(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7582 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7583 | Py_ssize_t len; |
| 7584 | Py_UNICODE *p; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 7585 | Py_hash_t x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7586 | |
| 7587 | if (self->hash != -1) |
| 7588 | return self->hash; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7589 | len = Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7590 | p = self->str; |
| 7591 | x = *p << 7; |
| 7592 | while (--len >= 0) |
| 7593 | x = (1000003*x) ^ *p++; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7594 | x ^= Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7595 | if (x == -1) |
| 7596 | x = -2; |
| 7597 | self->hash = x; |
| 7598 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7599 | } |
| 7600 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7601 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7602 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7603 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7604 | 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] | 7605 | |
| 7606 | static PyObject * |
| 7607 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 7608 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7609 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7610 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7611 | Py_ssize_t start; |
| 7612 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7613 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7614 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7615 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7616 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7617 | result = stringlib_find_slice( |
| 7618 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7619 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7620 | start, end |
| 7621 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7622 | |
| 7623 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7624 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7625 | if (result < 0) { |
| 7626 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 7627 | return NULL; |
| 7628 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7629 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7630 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7631 | } |
| 7632 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7633 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7634 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7635 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7636 | 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] | 7637 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7638 | |
| 7639 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7640 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7641 | { |
| 7642 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7643 | register const Py_UNICODE *e; |
| 7644 | int cased; |
| 7645 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7646 | /* Shortcut for single character strings */ |
| 7647 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7648 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7649 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7650 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7651 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7652 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7653 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7654 | e = p + PyUnicode_GET_SIZE(self); |
| 7655 | cased = 0; |
| 7656 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7657 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7658 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7659 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 7660 | return PyBool_FromLong(0); |
| 7661 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 7662 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7663 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7664 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7665 | } |
| 7666 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7667 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7668 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7669 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7670 | 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] | 7671 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7672 | |
| 7673 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7674 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7675 | { |
| 7676 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7677 | register const Py_UNICODE *e; |
| 7678 | int cased; |
| 7679 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7680 | /* Shortcut for single character strings */ |
| 7681 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7682 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7683 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7684 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7685 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7686 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7687 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7688 | e = p + PyUnicode_GET_SIZE(self); |
| 7689 | cased = 0; |
| 7690 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7691 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7692 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7693 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 7694 | return PyBool_FromLong(0); |
| 7695 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 7696 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7697 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7698 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7699 | } |
| 7700 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7701 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7702 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7703 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7704 | Return True if S is a titlecased string and there is at least one\n\ |
| 7705 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 7706 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 7707 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7708 | |
| 7709 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7710 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7711 | { |
| 7712 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7713 | register const Py_UNICODE *e; |
| 7714 | int cased, previous_is_cased; |
| 7715 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7716 | /* Shortcut for single character strings */ |
| 7717 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7718 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 7719 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7720 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7721 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7722 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7723 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7724 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7725 | e = p + PyUnicode_GET_SIZE(self); |
| 7726 | cased = 0; |
| 7727 | previous_is_cased = 0; |
| 7728 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7729 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7730 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7731 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 7732 | if (previous_is_cased) |
| 7733 | return PyBool_FromLong(0); |
| 7734 | previous_is_cased = 1; |
| 7735 | cased = 1; |
| 7736 | } |
| 7737 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 7738 | if (!previous_is_cased) |
| 7739 | return PyBool_FromLong(0); |
| 7740 | previous_is_cased = 1; |
| 7741 | cased = 1; |
| 7742 | } |
| 7743 | else |
| 7744 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7745 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7746 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7747 | } |
| 7748 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7749 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7750 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7751 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7752 | Return True if all characters in S are whitespace\n\ |
| 7753 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7754 | |
| 7755 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7756 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7757 | { |
| 7758 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7759 | register const Py_UNICODE *e; |
| 7760 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7761 | /* Shortcut for single character strings */ |
| 7762 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7763 | Py_UNICODE_ISSPACE(*p)) |
| 7764 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7765 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7766 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7767 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7768 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7769 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7770 | e = p + PyUnicode_GET_SIZE(self); |
| 7771 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7772 | if (!Py_UNICODE_ISSPACE(*p)) |
| 7773 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7774 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7775 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7776 | } |
| 7777 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7778 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7779 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7780 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7781 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7782 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7783 | |
| 7784 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7785 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7786 | { |
| 7787 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7788 | register const Py_UNICODE *e; |
| 7789 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7790 | /* Shortcut for single character strings */ |
| 7791 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7792 | Py_UNICODE_ISALPHA(*p)) |
| 7793 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7794 | |
| 7795 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7796 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7797 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7798 | |
| 7799 | e = p + PyUnicode_GET_SIZE(self); |
| 7800 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7801 | if (!Py_UNICODE_ISALPHA(*p)) |
| 7802 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7803 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7804 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7805 | } |
| 7806 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7807 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7808 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7809 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7810 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7811 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7812 | |
| 7813 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7814 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7815 | { |
| 7816 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7817 | register const Py_UNICODE *e; |
| 7818 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7819 | /* Shortcut for single character strings */ |
| 7820 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7821 | Py_UNICODE_ISALNUM(*p)) |
| 7822 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7823 | |
| 7824 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7825 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7826 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7827 | |
| 7828 | e = p + PyUnicode_GET_SIZE(self); |
| 7829 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7830 | if (!Py_UNICODE_ISALNUM(*p)) |
| 7831 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7832 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7833 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7834 | } |
| 7835 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7836 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7837 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7838 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7839 | 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] | 7840 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7841 | |
| 7842 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7843 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7844 | { |
| 7845 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7846 | register const Py_UNICODE *e; |
| 7847 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7848 | /* Shortcut for single character strings */ |
| 7849 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7850 | Py_UNICODE_ISDECIMAL(*p)) |
| 7851 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7852 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7853 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7854 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7855 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7856 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7857 | e = p + PyUnicode_GET_SIZE(self); |
| 7858 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7859 | if (!Py_UNICODE_ISDECIMAL(*p)) |
| 7860 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7861 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7862 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7863 | } |
| 7864 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7865 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7866 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7867 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7868 | Return True if all characters in S are digits\n\ |
| 7869 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7870 | |
| 7871 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7872 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7873 | { |
| 7874 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7875 | register const Py_UNICODE *e; |
| 7876 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7877 | /* Shortcut for single character strings */ |
| 7878 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7879 | Py_UNICODE_ISDIGIT(*p)) |
| 7880 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7881 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7882 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7883 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7884 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7885 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7886 | e = p + PyUnicode_GET_SIZE(self); |
| 7887 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7888 | if (!Py_UNICODE_ISDIGIT(*p)) |
| 7889 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7890 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7891 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7892 | } |
| 7893 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7894 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7895 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7896 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7897 | 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] | 7898 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7899 | |
| 7900 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7901 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7902 | { |
| 7903 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7904 | register const Py_UNICODE *e; |
| 7905 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7906 | /* Shortcut for single character strings */ |
| 7907 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7908 | Py_UNICODE_ISNUMERIC(*p)) |
| 7909 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7910 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7911 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7912 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7913 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7914 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7915 | e = p + PyUnicode_GET_SIZE(self); |
| 7916 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7917 | if (!Py_UNICODE_ISNUMERIC(*p)) |
| 7918 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7919 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7920 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7921 | } |
| 7922 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7923 | int |
| 7924 | PyUnicode_IsIdentifier(PyObject *self) |
| 7925 | { |
| 7926 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE((PyUnicodeObject*)self); |
| 7927 | register const Py_UNICODE *e; |
| 7928 | |
| 7929 | /* Special case for empty strings */ |
| 7930 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7931 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7932 | |
| 7933 | /* PEP 3131 says that the first character must be in |
| 7934 | XID_Start and subsequent characters in XID_Continue, |
| 7935 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7936 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7937 | letters, digits, underscore). However, given the current |
| 7938 | definition of XID_Start and XID_Continue, it is sufficient |
| 7939 | to check just for these, except that _ must be allowed |
| 7940 | as starting an identifier. */ |
| 7941 | if (!_PyUnicode_IsXidStart(*p) && *p != 0x5F /* LOW LINE */) |
| 7942 | return 0; |
| 7943 | |
| 7944 | e = p + PyUnicode_GET_SIZE(self); |
| 7945 | for (p++; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7946 | if (!_PyUnicode_IsXidContinue(*p)) |
| 7947 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7948 | } |
| 7949 | return 1; |
| 7950 | } |
| 7951 | |
| 7952 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7953 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7954 | \n\ |
| 7955 | Return True if S is a valid identifier according\n\ |
| 7956 | to the language definition."); |
| 7957 | |
| 7958 | static PyObject* |
| 7959 | unicode_isidentifier(PyObject *self) |
| 7960 | { |
| 7961 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 7962 | } |
| 7963 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7964 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7965 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7966 | \n\ |
| 7967 | Return True if all characters in S are considered\n\ |
| 7968 | printable in repr() or S is empty, False otherwise."); |
| 7969 | |
| 7970 | static PyObject* |
| 7971 | unicode_isprintable(PyObject *self) |
| 7972 | { |
| 7973 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7974 | register const Py_UNICODE *e; |
| 7975 | |
| 7976 | /* Shortcut for single character strings */ |
| 7977 | if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISPRINTABLE(*p)) { |
| 7978 | Py_RETURN_TRUE; |
| 7979 | } |
| 7980 | |
| 7981 | e = p + PyUnicode_GET_SIZE(self); |
| 7982 | for (; p < e; p++) { |
| 7983 | if (!Py_UNICODE_ISPRINTABLE(*p)) { |
| 7984 | Py_RETURN_FALSE; |
| 7985 | } |
| 7986 | } |
| 7987 | Py_RETURN_TRUE; |
| 7988 | } |
| 7989 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7990 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 7991 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7992 | \n\ |
| 7993 | 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] | 7994 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7995 | |
| 7996 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7997 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7998 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7999 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8000 | } |
| 8001 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8002 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8003 | unicode_length(PyUnicodeObject *self) |
| 8004 | { |
| 8005 | return self->length; |
| 8006 | } |
| 8007 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8008 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8009 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8010 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 8011 | 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] | 8012 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8013 | |
| 8014 | static PyObject * |
| 8015 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 8016 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8017 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 8018 | Py_UNICODE fillchar = ' '; |
| 8019 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8020 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8021 | return NULL; |
| 8022 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 8023 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8024 | Py_INCREF(self); |
| 8025 | return (PyObject*) self; |
| 8026 | } |
| 8027 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 8028 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8029 | } |
| 8030 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8031 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8032 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8033 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8034 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8035 | |
| 8036 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8037 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8038 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8039 | return fixup(self, fixlower); |
| 8040 | } |
| 8041 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8042 | #define LEFTSTRIP 0 |
| 8043 | #define RIGHTSTRIP 1 |
| 8044 | #define BOTHSTRIP 2 |
| 8045 | |
| 8046 | /* Arrays indexed by above */ |
| 8047 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 8048 | |
| 8049 | #define STRIPNAME(i) (stripformat[i]+3) |
| 8050 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8051 | /* externally visible for str.strip(unicode) */ |
| 8052 | PyObject * |
| 8053 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 8054 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8055 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 8056 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
| 8057 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
| 8058 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 8059 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8060 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8061 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8062 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8063 | i = 0; |
| 8064 | if (striptype != RIGHTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8065 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 8066 | i++; |
| 8067 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8068 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8069 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8070 | j = len; |
| 8071 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8072 | do { |
| 8073 | j--; |
| 8074 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 8075 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8076 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8077 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8078 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8079 | Py_INCREF(self); |
| 8080 | return (PyObject*)self; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8081 | } |
| 8082 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8083 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8084 | } |
| 8085 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8086 | |
| 8087 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8088 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8089 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8090 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 8091 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8092 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8093 | i = 0; |
| 8094 | if (striptype != RIGHTSTRIP) { |
| 8095 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 8096 | i++; |
| 8097 | } |
| 8098 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8099 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8100 | j = len; |
| 8101 | if (striptype != LEFTSTRIP) { |
| 8102 | do { |
| 8103 | j--; |
| 8104 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 8105 | j++; |
| 8106 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8107 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8108 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 8109 | Py_INCREF(self); |
| 8110 | return (PyObject*)self; |
| 8111 | } |
| 8112 | else |
| 8113 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8114 | } |
| 8115 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8116 | |
| 8117 | static PyObject * |
| 8118 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 8119 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8120 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8121 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8122 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 8123 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8124 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8125 | if (sep != NULL && sep != Py_None) { |
| 8126 | if (PyUnicode_Check(sep)) |
| 8127 | return _PyUnicode_XStrip(self, striptype, sep); |
| 8128 | else { |
| 8129 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8130 | "%s arg must be None or str", |
| 8131 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8132 | return NULL; |
| 8133 | } |
| 8134 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8135 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8136 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8137 | } |
| 8138 | |
| 8139 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8140 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8141 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8142 | \n\ |
| 8143 | Return a copy of the string S with leading and trailing\n\ |
| 8144 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8145 | 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] | 8146 | |
| 8147 | static PyObject * |
| 8148 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 8149 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8150 | if (PyTuple_GET_SIZE(args) == 0) |
| 8151 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 8152 | else |
| 8153 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8154 | } |
| 8155 | |
| 8156 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8157 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8158 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8159 | \n\ |
| 8160 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8161 | 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] | 8162 | |
| 8163 | static PyObject * |
| 8164 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 8165 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8166 | if (PyTuple_GET_SIZE(args) == 0) |
| 8167 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 8168 | else |
| 8169 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8170 | } |
| 8171 | |
| 8172 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8173 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8174 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8175 | \n\ |
| 8176 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8177 | 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] | 8178 | |
| 8179 | static PyObject * |
| 8180 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 8181 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8182 | if (PyTuple_GET_SIZE(args) == 0) |
| 8183 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 8184 | else |
| 8185 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8186 | } |
| 8187 | |
| 8188 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8189 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8190 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8191 | { |
| 8192 | PyUnicodeObject *u; |
| 8193 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8194 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 8195 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8196 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 8197 | if (len < 1) { |
| 8198 | Py_INCREF(unicode_empty); |
| 8199 | return (PyObject *)unicode_empty; |
| 8200 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8201 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 8202 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8203 | /* no repeat, return original string */ |
| 8204 | Py_INCREF(str); |
| 8205 | return (PyObject*) str; |
| 8206 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 8207 | |
| 8208 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 8209 | * needed doesn't overflow size_t |
| 8210 | */ |
| 8211 | nchars = len * str->length; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 8212 | if (nchars / len != str->length) { |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 8213 | PyErr_SetString(PyExc_OverflowError, |
| 8214 | "repeated string is too long"); |
| 8215 | return NULL; |
| 8216 | } |
| 8217 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 8218 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 8219 | PyErr_SetString(PyExc_OverflowError, |
| 8220 | "repeated string is too long"); |
| 8221 | return NULL; |
| 8222 | } |
| 8223 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8224 | if (!u) |
| 8225 | return NULL; |
| 8226 | |
| 8227 | p = u->str; |
| 8228 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 8229 | if (str->length == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8230 | Py_UNICODE_FILL(p, str->str[0], len); |
| 8231 | } else { |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 8232 | Py_ssize_t done = str->length; /* number of characters copied this far */ |
| 8233 | Py_UNICODE_COPY(p, str->str, str->length); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8234 | while (done < nchars) { |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 8235 | Py_ssize_t n = (done <= nchars-done) ? done : nchars-done; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8236 | Py_UNICODE_COPY(p+done, p, n); |
| 8237 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8238 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8239 | } |
| 8240 | |
| 8241 | return (PyObject*) u; |
| 8242 | } |
| 8243 | |
| 8244 | PyObject *PyUnicode_Replace(PyObject *obj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8245 | PyObject *subobj, |
| 8246 | PyObject *replobj, |
| 8247 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8248 | { |
| 8249 | PyObject *self; |
| 8250 | PyObject *str1; |
| 8251 | PyObject *str2; |
| 8252 | PyObject *result; |
| 8253 | |
| 8254 | self = PyUnicode_FromObject(obj); |
| 8255 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8256 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8257 | str1 = PyUnicode_FromObject(subobj); |
| 8258 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8259 | Py_DECREF(self); |
| 8260 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8261 | } |
| 8262 | str2 = PyUnicode_FromObject(replobj); |
| 8263 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8264 | Py_DECREF(self); |
| 8265 | Py_DECREF(str1); |
| 8266 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8267 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8268 | result = replace((PyUnicodeObject *)self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8269 | (PyUnicodeObject *)str1, |
| 8270 | (PyUnicodeObject *)str2, |
| 8271 | maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8272 | Py_DECREF(self); |
| 8273 | Py_DECREF(str1); |
| 8274 | Py_DECREF(str2); |
| 8275 | return result; |
| 8276 | } |
| 8277 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8278 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 8279 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8280 | \n\ |
| 8281 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 8282 | old replaced by new. If the optional argument count is\n\ |
| 8283 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8284 | |
| 8285 | static PyObject* |
| 8286 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 8287 | { |
| 8288 | PyUnicodeObject *str1; |
| 8289 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8290 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8291 | PyObject *result; |
| 8292 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8293 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8294 | return NULL; |
| 8295 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 8296 | if (str1 == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8297 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8298 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 8299 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8300 | Py_DECREF(str1); |
| 8301 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 8302 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8303 | |
| 8304 | result = replace(self, str1, str2, maxcount); |
| 8305 | |
| 8306 | Py_DECREF(str1); |
| 8307 | Py_DECREF(str2); |
| 8308 | return result; |
| 8309 | } |
| 8310 | |
| 8311 | static |
| 8312 | PyObject *unicode_repr(PyObject *unicode) |
| 8313 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8314 | PyObject *repr; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8315 | Py_UNICODE *p; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8316 | Py_UNICODE *s = PyUnicode_AS_UNICODE(unicode); |
| 8317 | Py_ssize_t size = PyUnicode_GET_SIZE(unicode); |
| 8318 | |
| 8319 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 8320 | better to choose a different scheme. Perhaps scan the |
| 8321 | first N-chars of the string and allocate based on that size. |
| 8322 | */ |
| 8323 | /* Initial allocation is based on the longest-possible unichr |
| 8324 | escape. |
| 8325 | |
| 8326 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 8327 | unichr, so in this case it's the longest unichr escape. In |
| 8328 | narrow (UTF-16) builds this is five chars per source unichr |
| 8329 | since there are two unichrs in the surrogate pair, so in narrow |
| 8330 | (UTF-16) builds it's not the longest unichr escape. |
| 8331 | |
| 8332 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 8333 | so in the narrow (UTF-16) build case it's the longest unichr |
| 8334 | escape. |
| 8335 | */ |
| 8336 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8337 | repr = PyUnicode_FromUnicode(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8338 | 2 /* quotes */ |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8339 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8340 | + 10*size |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8341 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8342 | + 6*size |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8343 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8344 | + 1); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8345 | if (repr == NULL) |
| 8346 | return NULL; |
| 8347 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8348 | p = PyUnicode_AS_UNICODE(repr); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8349 | |
| 8350 | /* Add quote */ |
| 8351 | *p++ = (findchar(s, size, '\'') && |
| 8352 | !findchar(s, size, '"')) ? '"' : '\''; |
| 8353 | while (size-- > 0) { |
| 8354 | Py_UNICODE ch = *s++; |
| 8355 | |
| 8356 | /* Escape quotes and backslashes */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8357 | if ((ch == PyUnicode_AS_UNICODE(repr)[0]) || (ch == '\\')) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8358 | *p++ = '\\'; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8359 | *p++ = ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8360 | continue; |
| 8361 | } |
| 8362 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8363 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8364 | if (ch == '\t') { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8365 | *p++ = '\\'; |
| 8366 | *p++ = 't'; |
| 8367 | } |
| 8368 | else if (ch == '\n') { |
| 8369 | *p++ = '\\'; |
| 8370 | *p++ = 'n'; |
| 8371 | } |
| 8372 | else if (ch == '\r') { |
| 8373 | *p++ = '\\'; |
| 8374 | *p++ = 'r'; |
| 8375 | } |
| 8376 | |
| 8377 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8378 | else if (ch < ' ' || ch == 0x7F) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8379 | *p++ = '\\'; |
| 8380 | *p++ = 'x'; |
| 8381 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 8382 | *p++ = hexdigits[ch & 0x000F]; |
| 8383 | } |
| 8384 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8385 | /* Copy ASCII characters as-is */ |
| 8386 | else if (ch < 0x7F) { |
| 8387 | *p++ = ch; |
| 8388 | } |
| 8389 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8390 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8391 | else { |
| 8392 | Py_UCS4 ucs = ch; |
| 8393 | |
| 8394 | #ifndef Py_UNICODE_WIDE |
| 8395 | Py_UNICODE ch2 = 0; |
| 8396 | /* Get code point from surrogate pair */ |
| 8397 | if (size > 0) { |
| 8398 | ch2 = *s; |
| 8399 | if (ch >= 0xD800 && ch < 0xDC00 && ch2 >= 0xDC00 |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8400 | && ch2 <= 0xDFFF) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8401 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8402 | + 0x00010000; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8403 | s++; |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8404 | size--; |
| 8405 | } |
| 8406 | } |
| 8407 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8408 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8409 | (categories Z* and C* except ASCII space) |
| 8410 | */ |
| 8411 | if (!Py_UNICODE_ISPRINTABLE(ucs)) { |
| 8412 | /* Map 8-bit characters to '\xhh' */ |
| 8413 | if (ucs <= 0xff) { |
| 8414 | *p++ = '\\'; |
| 8415 | *p++ = 'x'; |
| 8416 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 8417 | *p++ = hexdigits[ch & 0x000F]; |
| 8418 | } |
| 8419 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 8420 | else if (ucs >= 0x10000) { |
| 8421 | *p++ = '\\'; |
| 8422 | *p++ = 'U'; |
| 8423 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 8424 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 8425 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 8426 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 8427 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 8428 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 8429 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 8430 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 8431 | } |
| 8432 | /* Map 16-bit characters to '\uxxxx' */ |
| 8433 | else { |
| 8434 | *p++ = '\\'; |
| 8435 | *p++ = 'u'; |
| 8436 | *p++ = hexdigits[(ucs >> 12) & 0x000F]; |
| 8437 | *p++ = hexdigits[(ucs >> 8) & 0x000F]; |
| 8438 | *p++ = hexdigits[(ucs >> 4) & 0x000F]; |
| 8439 | *p++ = hexdigits[ucs & 0x000F]; |
| 8440 | } |
| 8441 | } |
| 8442 | /* Copy characters as-is */ |
| 8443 | else { |
| 8444 | *p++ = ch; |
| 8445 | #ifndef Py_UNICODE_WIDE |
| 8446 | if (ucs >= 0x10000) |
| 8447 | *p++ = ch2; |
| 8448 | #endif |
| 8449 | } |
| 8450 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8451 | } |
| 8452 | /* Add quote */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8453 | *p++ = PyUnicode_AS_UNICODE(repr)[0]; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8454 | |
| 8455 | *p = '\0'; |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 8456 | PyUnicode_Resize(&repr, p - PyUnicode_AS_UNICODE(repr)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8457 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8458 | } |
| 8459 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8460 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8461 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8462 | \n\ |
| 8463 | 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] | 8464 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8465 | arguments start and end are interpreted as in slice notation.\n\ |
| 8466 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8467 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8468 | |
| 8469 | static PyObject * |
| 8470 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 8471 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8472 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8473 | Py_ssize_t start; |
| 8474 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8475 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8476 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8477 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8478 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8479 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8480 | result = stringlib_rfind_slice( |
| 8481 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 8482 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 8483 | start, end |
| 8484 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8485 | |
| 8486 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8487 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8488 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8489 | } |
| 8490 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8491 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8492 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8493 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8494 | 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] | 8495 | |
| 8496 | static PyObject * |
| 8497 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 8498 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8499 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8500 | Py_ssize_t start; |
| 8501 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8502 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8503 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8504 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8505 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8506 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8507 | result = stringlib_rfind_slice( |
| 8508 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 8509 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 8510 | start, end |
| 8511 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8512 | |
| 8513 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8514 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8515 | if (result < 0) { |
| 8516 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 8517 | return NULL; |
| 8518 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8519 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8520 | } |
| 8521 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8522 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8523 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8524 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 8525 | 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] | 8526 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8527 | |
| 8528 | static PyObject * |
| 8529 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 8530 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8531 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 8532 | Py_UNICODE fillchar = ' '; |
| 8533 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8534 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8535 | return NULL; |
| 8536 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 8537 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8538 | Py_INCREF(self); |
| 8539 | return (PyObject*) self; |
| 8540 | } |
| 8541 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 8542 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8543 | } |
| 8544 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8545 | PyObject *PyUnicode_Split(PyObject *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8546 | PyObject *sep, |
| 8547 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8548 | { |
| 8549 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8550 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8551 | s = PyUnicode_FromObject(s); |
| 8552 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8553 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8554 | if (sep != NULL) { |
| 8555 | sep = PyUnicode_FromObject(sep); |
| 8556 | if (sep == NULL) { |
| 8557 | Py_DECREF(s); |
| 8558 | return NULL; |
| 8559 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8560 | } |
| 8561 | |
| 8562 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 8563 | |
| 8564 | Py_DECREF(s); |
| 8565 | Py_XDECREF(sep); |
| 8566 | return result; |
| 8567 | } |
| 8568 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8569 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8570 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8571 | \n\ |
| 8572 | Return a list of the words in S, using sep as the\n\ |
| 8573 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 8574 | 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] | 8575 | whitespace string is a separator and empty strings are\n\ |
| 8576 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8577 | |
| 8578 | static PyObject* |
| 8579 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 8580 | { |
| 8581 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8582 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8583 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8584 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8585 | return NULL; |
| 8586 | |
| 8587 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8588 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8589 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8590 | return split(self, (PyUnicodeObject *)substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8591 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8592 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8593 | } |
| 8594 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8595 | PyObject * |
| 8596 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 8597 | { |
| 8598 | PyObject* str_obj; |
| 8599 | PyObject* sep_obj; |
| 8600 | PyObject* out; |
| 8601 | |
| 8602 | str_obj = PyUnicode_FromObject(str_in); |
| 8603 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8604 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8605 | sep_obj = PyUnicode_FromObject(sep_in); |
| 8606 | if (!sep_obj) { |
| 8607 | Py_DECREF(str_obj); |
| 8608 | return NULL; |
| 8609 | } |
| 8610 | |
| 8611 | out = stringlib_partition( |
| 8612 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 8613 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 8614 | ); |
| 8615 | |
| 8616 | Py_DECREF(sep_obj); |
| 8617 | Py_DECREF(str_obj); |
| 8618 | |
| 8619 | return out; |
| 8620 | } |
| 8621 | |
| 8622 | |
| 8623 | PyObject * |
| 8624 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 8625 | { |
| 8626 | PyObject* str_obj; |
| 8627 | PyObject* sep_obj; |
| 8628 | PyObject* out; |
| 8629 | |
| 8630 | str_obj = PyUnicode_FromObject(str_in); |
| 8631 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8632 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8633 | sep_obj = PyUnicode_FromObject(sep_in); |
| 8634 | if (!sep_obj) { |
| 8635 | Py_DECREF(str_obj); |
| 8636 | return NULL; |
| 8637 | } |
| 8638 | |
| 8639 | out = stringlib_rpartition( |
| 8640 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 8641 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 8642 | ); |
| 8643 | |
| 8644 | Py_DECREF(sep_obj); |
| 8645 | Py_DECREF(str_obj); |
| 8646 | |
| 8647 | return out; |
| 8648 | } |
| 8649 | |
| 8650 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8651 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8652 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8653 | 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] | 8654 | 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] | 8655 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8656 | |
| 8657 | static PyObject* |
| 8658 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
| 8659 | { |
| 8660 | return PyUnicode_Partition((PyObject *)self, separator); |
| 8661 | } |
| 8662 | |
| 8663 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 8664 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8665 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8666 | 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] | 8667 | 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] | 8668 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8669 | |
| 8670 | static PyObject* |
| 8671 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 8672 | { |
| 8673 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 8674 | } |
| 8675 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8676 | PyObject *PyUnicode_RSplit(PyObject *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8677 | PyObject *sep, |
| 8678 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8679 | { |
| 8680 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8681 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8682 | s = PyUnicode_FromObject(s); |
| 8683 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8684 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8685 | if (sep != NULL) { |
| 8686 | sep = PyUnicode_FromObject(sep); |
| 8687 | if (sep == NULL) { |
| 8688 | Py_DECREF(s); |
| 8689 | return NULL; |
| 8690 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8691 | } |
| 8692 | |
| 8693 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 8694 | |
| 8695 | Py_DECREF(s); |
| 8696 | Py_XDECREF(sep); |
| 8697 | return result; |
| 8698 | } |
| 8699 | |
| 8700 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8701 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8702 | \n\ |
| 8703 | Return a list of the words in S, using sep as the\n\ |
| 8704 | delimiter string, starting at the end of the string and\n\ |
| 8705 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 8706 | splits are done. If sep is not specified, any whitespace string\n\ |
| 8707 | is a separator."); |
| 8708 | |
| 8709 | static PyObject* |
| 8710 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 8711 | { |
| 8712 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8713 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8714 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8715 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8716 | return NULL; |
| 8717 | |
| 8718 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8719 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8720 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8721 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8722 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8723 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8724 | } |
| 8725 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8726 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8727 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8728 | \n\ |
| 8729 | 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] | 8730 | 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] | 8731 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8732 | |
| 8733 | static PyObject* |
| 8734 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 8735 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8736 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8737 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8738 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8739 | return NULL; |
| 8740 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8741 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8742 | } |
| 8743 | |
| 8744 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 8745 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8746 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 8747 | if (PyUnicode_CheckExact(self)) { |
| 8748 | Py_INCREF(self); |
| 8749 | return self; |
| 8750 | } else |
| 8751 | /* Subtype -- return genuine unicode string with the same value. */ |
| 8752 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(self), |
| 8753 | PyUnicode_GET_SIZE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8754 | } |
| 8755 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8756 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8757 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8758 | \n\ |
| 8759 | 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] | 8760 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8761 | |
| 8762 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8763 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8764 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8765 | return fixup(self, fixswapcase); |
| 8766 | } |
| 8767 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8768 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8769 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8770 | \n\ |
| 8771 | Return a translation table usable for str.translate().\n\ |
| 8772 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 8773 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8774 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8775 | If there are two arguments, they must be strings of equal length, and\n\ |
| 8776 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 8777 | character at the same position in y. If there is a third argument, it\n\ |
| 8778 | must be a string, whose characters will be mapped to None in the result."); |
| 8779 | |
| 8780 | static PyObject* |
| 8781 | unicode_maketrans(PyUnicodeObject *null, PyObject *args) |
| 8782 | { |
| 8783 | PyObject *x, *y = NULL, *z = NULL; |
| 8784 | PyObject *new = NULL, *key, *value; |
| 8785 | Py_ssize_t i = 0; |
| 8786 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8787 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8788 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 8789 | return NULL; |
| 8790 | new = PyDict_New(); |
| 8791 | if (!new) |
| 8792 | return NULL; |
| 8793 | if (y != NULL) { |
| 8794 | /* x must be a string too, of equal length */ |
| 8795 | Py_ssize_t ylen = PyUnicode_GET_SIZE(y); |
| 8796 | if (!PyUnicode_Check(x)) { |
| 8797 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 8798 | "be a string if there is a second argument"); |
| 8799 | goto err; |
| 8800 | } |
| 8801 | if (PyUnicode_GET_SIZE(x) != ylen) { |
| 8802 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 8803 | "arguments must have equal length"); |
| 8804 | goto err; |
| 8805 | } |
| 8806 | /* create entries for translating chars in x to those in y */ |
| 8807 | for (i = 0; i < PyUnicode_GET_SIZE(x); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8808 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(x)[i]); |
| 8809 | value = PyLong_FromLong(PyUnicode_AS_UNICODE(y)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8810 | if (!key || !value) |
| 8811 | goto err; |
| 8812 | res = PyDict_SetItem(new, key, value); |
| 8813 | Py_DECREF(key); |
| 8814 | Py_DECREF(value); |
| 8815 | if (res < 0) |
| 8816 | goto err; |
| 8817 | } |
| 8818 | /* create entries for deleting chars in z */ |
| 8819 | if (z != NULL) { |
| 8820 | for (i = 0; i < PyUnicode_GET_SIZE(z); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8821 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(z)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8822 | if (!key) |
| 8823 | goto err; |
| 8824 | res = PyDict_SetItem(new, key, Py_None); |
| 8825 | Py_DECREF(key); |
| 8826 | if (res < 0) |
| 8827 | goto err; |
| 8828 | } |
| 8829 | } |
| 8830 | } else { |
| 8831 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 8832 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8833 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 8834 | "to maketrans it must be a dict"); |
| 8835 | goto err; |
| 8836 | } |
| 8837 | /* copy entries into the new dict, converting string keys to int keys */ |
| 8838 | while (PyDict_Next(x, &i, &key, &value)) { |
| 8839 | if (PyUnicode_Check(key)) { |
| 8840 | /* convert string keys to integer keys */ |
| 8841 | PyObject *newkey; |
| 8842 | if (PyUnicode_GET_SIZE(key) != 1) { |
| 8843 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 8844 | "table must be of length 1"); |
| 8845 | goto err; |
| 8846 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8847 | newkey = PyLong_FromLong(PyUnicode_AS_UNICODE(key)[0]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8848 | if (!newkey) |
| 8849 | goto err; |
| 8850 | res = PyDict_SetItem(new, newkey, value); |
| 8851 | Py_DECREF(newkey); |
| 8852 | if (res < 0) |
| 8853 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8854 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8855 | /* just keep integer keys */ |
| 8856 | if (PyDict_SetItem(new, key, value) < 0) |
| 8857 | goto err; |
| 8858 | } else { |
| 8859 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 8860 | "be strings or integers"); |
| 8861 | goto err; |
| 8862 | } |
| 8863 | } |
| 8864 | } |
| 8865 | return new; |
| 8866 | err: |
| 8867 | Py_DECREF(new); |
| 8868 | return NULL; |
| 8869 | } |
| 8870 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8871 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8872 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8873 | \n\ |
| 8874 | Return a copy of the string S, where all characters have been mapped\n\ |
| 8875 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8876 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 8877 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 8878 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8879 | |
| 8880 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8881 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8882 | { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8883 | return PyUnicode_TranslateCharmap(self->str, self->length, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8884 | } |
| 8885 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8886 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8887 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8888 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8889 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8890 | |
| 8891 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8892 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8893 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8894 | return fixup(self, fixupper); |
| 8895 | } |
| 8896 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8897 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8898 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8899 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 8900 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 8901 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8902 | |
| 8903 | static PyObject * |
| 8904 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 8905 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8906 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8907 | PyUnicodeObject *u; |
| 8908 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8909 | Py_ssize_t width; |
| 8910 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8911 | return NULL; |
| 8912 | |
| 8913 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 8914 | if (PyUnicode_CheckExact(self)) { |
| 8915 | Py_INCREF(self); |
| 8916 | return (PyObject*) self; |
| 8917 | } |
| 8918 | else |
| 8919 | return PyUnicode_FromUnicode( |
| 8920 | PyUnicode_AS_UNICODE(self), |
| 8921 | PyUnicode_GET_SIZE(self) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8922 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8923 | } |
| 8924 | |
| 8925 | fill = width - self->length; |
| 8926 | |
| 8927 | u = pad(self, fill, 0, '0'); |
| 8928 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8929 | if (u == NULL) |
| 8930 | return NULL; |
| 8931 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8932 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 8933 | /* move sign to beginning of string */ |
| 8934 | u->str[0] = u->str[fill]; |
| 8935 | u->str[fill] = '0'; |
| 8936 | } |
| 8937 | |
| 8938 | return (PyObject*) u; |
| 8939 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8940 | |
| 8941 | #if 0 |
| 8942 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8943 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8944 | { |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 8945 | return PyLong_FromLong(numfree); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8946 | } |
| 8947 | #endif |
| 8948 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8949 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8950 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8951 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 8952 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 8953 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8954 | With optional end, stop comparing S at that position.\n\ |
| 8955 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8956 | |
| 8957 | static PyObject * |
| 8958 | unicode_startswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8959 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8960 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8961 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8962 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8963 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8964 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8965 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8966 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8967 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8968 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 8969 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8970 | if (PyTuple_Check(subobj)) { |
| 8971 | Py_ssize_t i; |
| 8972 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 8973 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8974 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8975 | if (substring == NULL) |
| 8976 | return NULL; |
| 8977 | result = tailmatch(self, substring, start, end, -1); |
| 8978 | Py_DECREF(substring); |
| 8979 | if (result) { |
| 8980 | Py_RETURN_TRUE; |
| 8981 | } |
| 8982 | } |
| 8983 | /* nothing matched */ |
| 8984 | Py_RETURN_FALSE; |
| 8985 | } |
| 8986 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8987 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8988 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8989 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8990 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8991 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8992 | } |
| 8993 | |
| 8994 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8995 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8996 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8997 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 8998 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 8999 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9000 | With optional end, stop comparing S at that position.\n\ |
| 9001 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9002 | |
| 9003 | static PyObject * |
| 9004 | unicode_endswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9005 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9006 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9007 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9008 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9009 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9010 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9011 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9012 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9013 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9014 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 9015 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9016 | if (PyTuple_Check(subobj)) { |
| 9017 | Py_ssize_t i; |
| 9018 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 9019 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9020 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9021 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9022 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9023 | result = tailmatch(self, substring, start, end, +1); |
| 9024 | Py_DECREF(substring); |
| 9025 | if (result) { |
| 9026 | Py_RETURN_TRUE; |
| 9027 | } |
| 9028 | } |
| 9029 | Py_RETURN_FALSE; |
| 9030 | } |
| 9031 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9032 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9033 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9034 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9035 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9036 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9037 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9038 | } |
| 9039 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 9040 | #include "stringlib/string_format.h" |
| 9041 | |
| 9042 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9043 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 9044 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 9045 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 9046 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 9047 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 9048 | PyDoc_STRVAR(format_map__doc__, |
| 9049 | "S.format_map(mapping) -> str\n\ |
| 9050 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 9051 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 9052 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 9053 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 9054 | static PyObject * |
| 9055 | unicode__format__(PyObject* self, PyObject* args) |
| 9056 | { |
| 9057 | PyObject *format_spec; |
| 9058 | |
| 9059 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 9060 | return NULL; |
| 9061 | |
| 9062 | return _PyUnicode_FormatAdvanced(self, |
| 9063 | PyUnicode_AS_UNICODE(format_spec), |
| 9064 | PyUnicode_GET_SIZE(format_spec)); |
| 9065 | } |
| 9066 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 9067 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9068 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 9069 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 9070 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 9071 | |
| 9072 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 9073 | unicode__sizeof__(PyUnicodeObject *v) |
| 9074 | { |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 9075 | return PyLong_FromSsize_t(sizeof(PyUnicodeObject) + |
| 9076 | sizeof(Py_UNICODE) * (v->length + 1)); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 9077 | } |
| 9078 | |
| 9079 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9080 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 9081 | |
| 9082 | static PyObject * |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 9083 | unicode_getnewargs(PyUnicodeObject *v) |
| 9084 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9085 | return Py_BuildValue("(u#)", v->str, v->length); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 9086 | } |
| 9087 | |
| 9088 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9089 | static PyMethodDef unicode_methods[] = { |
| 9090 | |
| 9091 | /* Order is according to common usage: often used methods should |
| 9092 | appear first, since lookup is done sequentially. */ |
| 9093 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 9094 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9095 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 9096 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9097 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9098 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 9099 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 9100 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 9101 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 9102 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 9103 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 9104 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9105 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9106 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 9107 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 9108 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 9109 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9110 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 9111 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 9112 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 9113 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9114 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9115 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 9116 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9117 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 9118 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 9119 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 9120 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 9121 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 9122 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 9123 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 9124 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 9125 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 9126 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 9127 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 9128 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 9129 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 9130 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 9131 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 9132 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9133 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 9134 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 9135 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 9136 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 9137 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 9138 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 9139 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 9140 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9141 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9142 | #endif |
| 9143 | |
| 9144 | #if 0 |
| 9145 | /* This one is just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9146 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9147 | #endif |
| 9148 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9149 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9150 | {NULL, NULL} |
| 9151 | }; |
| 9152 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 9153 | static PyObject * |
| 9154 | unicode_mod(PyObject *v, PyObject *w) |
| 9155 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9156 | if (!PyUnicode_Check(v)) { |
| 9157 | Py_INCREF(Py_NotImplemented); |
| 9158 | return Py_NotImplemented; |
| 9159 | } |
| 9160 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 9161 | } |
| 9162 | |
| 9163 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9164 | 0, /*nb_add*/ |
| 9165 | 0, /*nb_subtract*/ |
| 9166 | 0, /*nb_multiply*/ |
| 9167 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 9168 | }; |
| 9169 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9170 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9171 | (lenfunc) unicode_length, /* sq_length */ |
| 9172 | PyUnicode_Concat, /* sq_concat */ |
| 9173 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 9174 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 9175 | 0, /* sq_slice */ |
| 9176 | 0, /* sq_ass_item */ |
| 9177 | 0, /* sq_ass_slice */ |
| 9178 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9179 | }; |
| 9180 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9181 | static PyObject* |
| 9182 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 9183 | { |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9184 | if (PyIndex_Check(item)) { |
| 9185 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9186 | if (i == -1 && PyErr_Occurred()) |
| 9187 | return NULL; |
| 9188 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 9189 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9190 | return unicode_getitem(self, i); |
| 9191 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9192 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9193 | Py_UNICODE* source_buf; |
| 9194 | Py_UNICODE* result_buf; |
| 9195 | PyObject* result; |
| 9196 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 9197 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9198 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9199 | return NULL; |
| 9200 | } |
| 9201 | |
| 9202 | if (slicelength <= 0) { |
| 9203 | return PyUnicode_FromUnicode(NULL, 0); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 9204 | } else if (start == 0 && step == 1 && slicelength == self->length && |
| 9205 | PyUnicode_CheckExact(self)) { |
| 9206 | Py_INCREF(self); |
| 9207 | return (PyObject *)self; |
| 9208 | } else if (step == 1) { |
| 9209 | return PyUnicode_FromUnicode(self->str + start, slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9210 | } else { |
| 9211 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 9212 | result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* |
| 9213 | sizeof(Py_UNICODE)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9214 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9215 | if (result_buf == NULL) |
| 9216 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9217 | |
| 9218 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 9219 | result_buf[i] = source_buf[cur]; |
| 9220 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9221 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9222 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 9223 | PyObject_FREE(result_buf); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9224 | return result; |
| 9225 | } |
| 9226 | } else { |
| 9227 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 9228 | return NULL; |
| 9229 | } |
| 9230 | } |
| 9231 | |
| 9232 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9233 | (lenfunc)unicode_length, /* mp_length */ |
| 9234 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 9235 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9236 | }; |
| 9237 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9238 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9239 | /* Helpers for PyUnicode_Format() */ |
| 9240 | |
| 9241 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9242 | 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] | 9243 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9244 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9245 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9246 | (*p_argidx)++; |
| 9247 | if (arglen < 0) |
| 9248 | return args; |
| 9249 | else |
| 9250 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9251 | } |
| 9252 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9253 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9254 | return NULL; |
| 9255 | } |
| 9256 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9257 | /* 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] | 9258 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9259 | static PyObject * |
| 9260 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9261 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9262 | char *p; |
| 9263 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9264 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9265 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9266 | x = PyFloat_AsDouble(v); |
| 9267 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9268 | return NULL; |
| 9269 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9270 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9271 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 9272 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 9273 | p = PyOS_double_to_string(x, type, prec, |
| 9274 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9275 | if (p == NULL) |
| 9276 | return NULL; |
| 9277 | result = PyUnicode_FromStringAndSize(p, strlen(p)); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 9278 | PyMem_Free(p); |
| 9279 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9280 | } |
| 9281 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9282 | static PyObject* |
| 9283 | formatlong(PyObject *val, int flags, int prec, int type) |
| 9284 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9285 | char *buf; |
| 9286 | int len; |
| 9287 | PyObject *str; /* temporary string object. */ |
| 9288 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9289 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9290 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 9291 | if (!str) |
| 9292 | return NULL; |
| 9293 | result = PyUnicode_FromStringAndSize(buf, len); |
| 9294 | Py_DECREF(str); |
| 9295 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9296 | } |
| 9297 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9298 | static int |
| 9299 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9300 | size_t buflen, |
| 9301 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9302 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 9303 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 9304 | if (PyUnicode_Check(v)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9305 | if (PyUnicode_GET_SIZE(v) == 1) { |
| 9306 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
| 9307 | buf[1] = '\0'; |
| 9308 | return 1; |
| 9309 | } |
| 9310 | #ifndef Py_UNICODE_WIDE |
| 9311 | if (PyUnicode_GET_SIZE(v) == 2) { |
| 9312 | /* Decode a valid surrogate pair */ |
| 9313 | int c0 = PyUnicode_AS_UNICODE(v)[0]; |
| 9314 | int c1 = PyUnicode_AS_UNICODE(v)[1]; |
| 9315 | if (0xD800 <= c0 && c0 <= 0xDBFF && |
| 9316 | 0xDC00 <= c1 && c1 <= 0xDFFF) { |
| 9317 | buf[0] = c0; |
| 9318 | buf[1] = c1; |
| 9319 | buf[2] = '\0'; |
| 9320 | return 2; |
| 9321 | } |
| 9322 | } |
| 9323 | #endif |
| 9324 | goto onError; |
| 9325 | } |
| 9326 | else { |
| 9327 | /* Integer input truncated to a character */ |
| 9328 | long x; |
| 9329 | x = PyLong_AsLong(v); |
| 9330 | if (x == -1 && PyErr_Occurred()) |
| 9331 | goto onError; |
| 9332 | |
| 9333 | if (x < 0 || x > 0x10ffff) { |
| 9334 | PyErr_SetString(PyExc_OverflowError, |
| 9335 | "%c arg not in range(0x110000)"); |
| 9336 | return -1; |
| 9337 | } |
| 9338 | |
| 9339 | #ifndef Py_UNICODE_WIDE |
| 9340 | if (x > 0xffff) { |
| 9341 | x -= 0x10000; |
| 9342 | buf[0] = (Py_UNICODE)(0xD800 | (x >> 10)); |
| 9343 | buf[1] = (Py_UNICODE)(0xDC00 | (x & 0x3FF)); |
| 9344 | return 2; |
| 9345 | } |
| 9346 | #endif |
| 9347 | buf[0] = (Py_UNICODE) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9348 | buf[1] = '\0'; |
| 9349 | return 1; |
| 9350 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 9351 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9352 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 9353 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9354 | "%c requires int or char"); |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 9355 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9356 | } |
| 9357 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9358 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9359 | 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] | 9360 | */ |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9361 | #define FORMATBUFLEN (size_t)10 |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9362 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9363 | PyObject *PyUnicode_Format(PyObject *format, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9364 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9365 | { |
| 9366 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9367 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9368 | int args_owned = 0; |
| 9369 | PyUnicodeObject *result = NULL; |
| 9370 | PyObject *dict = NULL; |
| 9371 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9372 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9373 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9374 | PyErr_BadInternalCall(); |
| 9375 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9376 | } |
| 9377 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 9378 | if (uformat == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9379 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9380 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 9381 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 9382 | |
| 9383 | reslen = rescnt = fmtcnt + 100; |
| 9384 | result = _PyUnicode_New(reslen); |
| 9385 | if (result == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9386 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9387 | res = PyUnicode_AS_UNICODE(result); |
| 9388 | |
| 9389 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9390 | arglen = PyTuple_Size(args); |
| 9391 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9392 | } |
| 9393 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9394 | arglen = -1; |
| 9395 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9396 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 9397 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 9398 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9399 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9400 | |
| 9401 | while (--fmtcnt >= 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9402 | if (*fmt != '%') { |
| 9403 | if (--rescnt < 0) { |
| 9404 | rescnt = fmtcnt + 100; |
| 9405 | reslen += rescnt; |
| 9406 | if (_PyUnicode_Resize(&result, reslen) < 0) |
| 9407 | goto onError; |
| 9408 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 9409 | --rescnt; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9410 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9411 | *res++ = *fmt++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9412 | } |
| 9413 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9414 | /* Got a format specifier */ |
| 9415 | int flags = 0; |
| 9416 | Py_ssize_t width = -1; |
| 9417 | int prec = -1; |
| 9418 | Py_UNICODE c = '\0'; |
| 9419 | Py_UNICODE fill; |
| 9420 | int isnumok; |
| 9421 | PyObject *v = NULL; |
| 9422 | PyObject *temp = NULL; |
| 9423 | Py_UNICODE *pbuf; |
| 9424 | Py_UNICODE sign; |
| 9425 | Py_ssize_t len; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9426 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For formatchar() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9427 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9428 | fmt++; |
| 9429 | if (*fmt == '(') { |
| 9430 | Py_UNICODE *keystart; |
| 9431 | Py_ssize_t keylen; |
| 9432 | PyObject *key; |
| 9433 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 9434 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9435 | if (dict == NULL) { |
| 9436 | PyErr_SetString(PyExc_TypeError, |
| 9437 | "format requires a mapping"); |
| 9438 | goto onError; |
| 9439 | } |
| 9440 | ++fmt; |
| 9441 | --fmtcnt; |
| 9442 | keystart = fmt; |
| 9443 | /* Skip over balanced parentheses */ |
| 9444 | while (pcount > 0 && --fmtcnt >= 0) { |
| 9445 | if (*fmt == ')') |
| 9446 | --pcount; |
| 9447 | else if (*fmt == '(') |
| 9448 | ++pcount; |
| 9449 | fmt++; |
| 9450 | } |
| 9451 | keylen = fmt - keystart - 1; |
| 9452 | if (fmtcnt < 0 || pcount > 0) { |
| 9453 | PyErr_SetString(PyExc_ValueError, |
| 9454 | "incomplete format key"); |
| 9455 | goto onError; |
| 9456 | } |
| 9457 | #if 0 |
| 9458 | /* keys are converted to strings using UTF-8 and |
| 9459 | then looked up since Python uses strings to hold |
| 9460 | variables names etc. in its namespaces and we |
| 9461 | wouldn't want to break common idioms. */ |
| 9462 | key = PyUnicode_EncodeUTF8(keystart, |
| 9463 | keylen, |
| 9464 | NULL); |
| 9465 | #else |
| 9466 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 9467 | #endif |
| 9468 | if (key == NULL) |
| 9469 | goto onError; |
| 9470 | if (args_owned) { |
| 9471 | Py_DECREF(args); |
| 9472 | args_owned = 0; |
| 9473 | } |
| 9474 | args = PyObject_GetItem(dict, key); |
| 9475 | Py_DECREF(key); |
| 9476 | if (args == NULL) { |
| 9477 | goto onError; |
| 9478 | } |
| 9479 | args_owned = 1; |
| 9480 | arglen = -1; |
| 9481 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9482 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9483 | while (--fmtcnt >= 0) { |
| 9484 | switch (c = *fmt++) { |
| 9485 | case '-': flags |= F_LJUST; continue; |
| 9486 | case '+': flags |= F_SIGN; continue; |
| 9487 | case ' ': flags |= F_BLANK; continue; |
| 9488 | case '#': flags |= F_ALT; continue; |
| 9489 | case '0': flags |= F_ZERO; continue; |
| 9490 | } |
| 9491 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9492 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9493 | if (c == '*') { |
| 9494 | v = getnextarg(args, arglen, &argidx); |
| 9495 | if (v == NULL) |
| 9496 | goto onError; |
| 9497 | if (!PyLong_Check(v)) { |
| 9498 | PyErr_SetString(PyExc_TypeError, |
| 9499 | "* wants int"); |
| 9500 | goto onError; |
| 9501 | } |
| 9502 | width = PyLong_AsLong(v); |
| 9503 | if (width == -1 && PyErr_Occurred()) |
| 9504 | goto onError; |
| 9505 | if (width < 0) { |
| 9506 | flags |= F_LJUST; |
| 9507 | width = -width; |
| 9508 | } |
| 9509 | if (--fmtcnt >= 0) |
| 9510 | c = *fmt++; |
| 9511 | } |
| 9512 | else if (c >= '0' && c <= '9') { |
| 9513 | width = c - '0'; |
| 9514 | while (--fmtcnt >= 0) { |
| 9515 | c = *fmt++; |
| 9516 | if (c < '0' || c > '9') |
| 9517 | break; |
| 9518 | if ((width*10) / 10 != width) { |
| 9519 | PyErr_SetString(PyExc_ValueError, |
| 9520 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9521 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9522 | } |
| 9523 | width = width*10 + (c - '0'); |
| 9524 | } |
| 9525 | } |
| 9526 | if (c == '.') { |
| 9527 | prec = 0; |
| 9528 | if (--fmtcnt >= 0) |
| 9529 | c = *fmt++; |
| 9530 | if (c == '*') { |
| 9531 | v = getnextarg(args, arglen, &argidx); |
| 9532 | if (v == NULL) |
| 9533 | goto onError; |
| 9534 | if (!PyLong_Check(v)) { |
| 9535 | PyErr_SetString(PyExc_TypeError, |
| 9536 | "* wants int"); |
| 9537 | goto onError; |
| 9538 | } |
| 9539 | prec = PyLong_AsLong(v); |
| 9540 | if (prec == -1 && PyErr_Occurred()) |
| 9541 | goto onError; |
| 9542 | if (prec < 0) |
| 9543 | prec = 0; |
| 9544 | if (--fmtcnt >= 0) |
| 9545 | c = *fmt++; |
| 9546 | } |
| 9547 | else if (c >= '0' && c <= '9') { |
| 9548 | prec = c - '0'; |
| 9549 | while (--fmtcnt >= 0) { |
Stefan Krah | 99212f6 | 2010-07-19 17:58:26 +0000 | [diff] [blame] | 9550 | c = *fmt++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9551 | if (c < '0' || c > '9') |
| 9552 | break; |
| 9553 | if ((prec*10) / 10 != prec) { |
| 9554 | PyErr_SetString(PyExc_ValueError, |
| 9555 | "prec too big"); |
| 9556 | goto onError; |
| 9557 | } |
| 9558 | prec = prec*10 + (c - '0'); |
| 9559 | } |
| 9560 | } |
| 9561 | } /* prec */ |
| 9562 | if (fmtcnt >= 0) { |
| 9563 | if (c == 'h' || c == 'l' || c == 'L') { |
| 9564 | if (--fmtcnt >= 0) |
| 9565 | c = *fmt++; |
| 9566 | } |
| 9567 | } |
| 9568 | if (fmtcnt < 0) { |
| 9569 | PyErr_SetString(PyExc_ValueError, |
| 9570 | "incomplete format"); |
| 9571 | goto onError; |
| 9572 | } |
| 9573 | if (c != '%') { |
| 9574 | v = getnextarg(args, arglen, &argidx); |
| 9575 | if (v == NULL) |
| 9576 | goto onError; |
| 9577 | } |
| 9578 | sign = 0; |
| 9579 | fill = ' '; |
| 9580 | switch (c) { |
| 9581 | |
| 9582 | case '%': |
| 9583 | pbuf = formatbuf; |
| 9584 | /* presume that buffer length is at least 1 */ |
| 9585 | pbuf[0] = '%'; |
| 9586 | len = 1; |
| 9587 | break; |
| 9588 | |
| 9589 | case 's': |
| 9590 | case 'r': |
| 9591 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 9592 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9593 | temp = v; |
| 9594 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9595 | } |
| 9596 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9597 | if (c == 's') |
| 9598 | temp = PyObject_Str(v); |
| 9599 | else if (c == 'r') |
| 9600 | temp = PyObject_Repr(v); |
| 9601 | else |
| 9602 | temp = PyObject_ASCII(v); |
| 9603 | if (temp == NULL) |
| 9604 | goto onError; |
| 9605 | if (PyUnicode_Check(temp)) |
| 9606 | /* nothing to do */; |
| 9607 | else { |
| 9608 | Py_DECREF(temp); |
| 9609 | PyErr_SetString(PyExc_TypeError, |
| 9610 | "%s argument has non-string str()"); |
| 9611 | goto onError; |
| 9612 | } |
| 9613 | } |
| 9614 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9615 | len = PyUnicode_GET_SIZE(temp); |
| 9616 | if (prec >= 0 && len > prec) |
| 9617 | len = prec; |
| 9618 | break; |
| 9619 | |
| 9620 | case 'i': |
| 9621 | case 'd': |
| 9622 | case 'u': |
| 9623 | case 'o': |
| 9624 | case 'x': |
| 9625 | case 'X': |
| 9626 | if (c == 'i') |
| 9627 | c = 'd'; |
| 9628 | isnumok = 0; |
| 9629 | if (PyNumber_Check(v)) { |
| 9630 | PyObject *iobj=NULL; |
| 9631 | |
| 9632 | if (PyLong_Check(v)) { |
| 9633 | iobj = v; |
| 9634 | Py_INCREF(iobj); |
| 9635 | } |
| 9636 | else { |
| 9637 | iobj = PyNumber_Long(v); |
| 9638 | } |
| 9639 | if (iobj!=NULL) { |
| 9640 | if (PyLong_Check(iobj)) { |
| 9641 | isnumok = 1; |
| 9642 | temp = formatlong(iobj, flags, prec, c); |
| 9643 | Py_DECREF(iobj); |
| 9644 | if (!temp) |
| 9645 | goto onError; |
| 9646 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9647 | len = PyUnicode_GET_SIZE(temp); |
| 9648 | sign = 1; |
| 9649 | } |
| 9650 | else { |
| 9651 | Py_DECREF(iobj); |
| 9652 | } |
| 9653 | } |
| 9654 | } |
| 9655 | if (!isnumok) { |
| 9656 | PyErr_Format(PyExc_TypeError, |
| 9657 | "%%%c format: a number is required, " |
| 9658 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 9659 | goto onError; |
| 9660 | } |
| 9661 | if (flags & F_ZERO) |
| 9662 | fill = '0'; |
| 9663 | break; |
| 9664 | |
| 9665 | case 'e': |
| 9666 | case 'E': |
| 9667 | case 'f': |
| 9668 | case 'F': |
| 9669 | case 'g': |
| 9670 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9671 | temp = formatfloat(v, flags, prec, c); |
| 9672 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9673 | goto onError; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9674 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9675 | len = PyUnicode_GET_SIZE(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9676 | sign = 1; |
| 9677 | if (flags & F_ZERO) |
| 9678 | fill = '0'; |
| 9679 | break; |
| 9680 | |
| 9681 | case 'c': |
| 9682 | pbuf = formatbuf; |
| 9683 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
| 9684 | if (len < 0) |
| 9685 | goto onError; |
| 9686 | break; |
| 9687 | |
| 9688 | default: |
| 9689 | PyErr_Format(PyExc_ValueError, |
| 9690 | "unsupported format character '%c' (0x%x) " |
| 9691 | "at index %zd", |
| 9692 | (31<=c && c<=126) ? (char)c : '?', |
| 9693 | (int)c, |
| 9694 | (Py_ssize_t)(fmt - 1 - |
| 9695 | PyUnicode_AS_UNICODE(uformat))); |
| 9696 | goto onError; |
| 9697 | } |
| 9698 | if (sign) { |
| 9699 | if (*pbuf == '-' || *pbuf == '+') { |
| 9700 | sign = *pbuf++; |
| 9701 | len--; |
| 9702 | } |
| 9703 | else if (flags & F_SIGN) |
| 9704 | sign = '+'; |
| 9705 | else if (flags & F_BLANK) |
| 9706 | sign = ' '; |
| 9707 | else |
| 9708 | sign = 0; |
| 9709 | } |
| 9710 | if (width < len) |
| 9711 | width = len; |
| 9712 | if (rescnt - (sign != 0) < width) { |
| 9713 | reslen -= rescnt; |
| 9714 | rescnt = width + fmtcnt + 100; |
| 9715 | reslen += rescnt; |
| 9716 | if (reslen < 0) { |
| 9717 | Py_XDECREF(temp); |
| 9718 | PyErr_NoMemory(); |
| 9719 | goto onError; |
| 9720 | } |
| 9721 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 9722 | Py_XDECREF(temp); |
| 9723 | goto onError; |
| 9724 | } |
| 9725 | res = PyUnicode_AS_UNICODE(result) |
| 9726 | + reslen - rescnt; |
| 9727 | } |
| 9728 | if (sign) { |
| 9729 | if (fill != ' ') |
| 9730 | *res++ = sign; |
| 9731 | rescnt--; |
| 9732 | if (width > len) |
| 9733 | width--; |
| 9734 | } |
| 9735 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
| 9736 | assert(pbuf[0] == '0'); |
| 9737 | assert(pbuf[1] == c); |
| 9738 | if (fill != ' ') { |
| 9739 | *res++ = *pbuf++; |
| 9740 | *res++ = *pbuf++; |
| 9741 | } |
| 9742 | rescnt -= 2; |
| 9743 | width -= 2; |
| 9744 | if (width < 0) |
| 9745 | width = 0; |
| 9746 | len -= 2; |
| 9747 | } |
| 9748 | if (width > len && !(flags & F_LJUST)) { |
| 9749 | do { |
| 9750 | --rescnt; |
| 9751 | *res++ = fill; |
| 9752 | } while (--width > len); |
| 9753 | } |
| 9754 | if (fill == ' ') { |
| 9755 | if (sign) |
| 9756 | *res++ = sign; |
| 9757 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
| 9758 | assert(pbuf[0] == '0'); |
| 9759 | assert(pbuf[1] == c); |
| 9760 | *res++ = *pbuf++; |
| 9761 | *res++ = *pbuf++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9762 | } |
| 9763 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9764 | Py_UNICODE_COPY(res, pbuf, len); |
| 9765 | res += len; |
| 9766 | rescnt -= len; |
| 9767 | while (--width >= len) { |
| 9768 | --rescnt; |
| 9769 | *res++ = ' '; |
| 9770 | } |
| 9771 | if (dict && (argidx < arglen) && c != '%') { |
| 9772 | PyErr_SetString(PyExc_TypeError, |
| 9773 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9774 | Py_XDECREF(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9775 | goto onError; |
| 9776 | } |
| 9777 | Py_XDECREF(temp); |
| 9778 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9779 | } /* until end */ |
| 9780 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9781 | PyErr_SetString(PyExc_TypeError, |
| 9782 | "not all arguments converted during string formatting"); |
| 9783 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9784 | } |
| 9785 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9786 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9787 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9788 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9789 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9790 | } |
| 9791 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9792 | return (PyObject *)result; |
| 9793 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9794 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9795 | Py_XDECREF(result); |
| 9796 | Py_DECREF(uformat); |
| 9797 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9798 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9799 | } |
| 9800 | return NULL; |
| 9801 | } |
| 9802 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 9803 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9804 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 9805 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9806 | static PyObject * |
| 9807 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9808 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9809 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9810 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 9811 | char *encoding = NULL; |
| 9812 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9813 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9814 | if (type != &PyUnicode_Type) |
| 9815 | return unicode_subtype_new(type, args, kwds); |
| 9816 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9817 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9818 | return NULL; |
| 9819 | if (x == NULL) |
| 9820 | return (PyObject *)_PyUnicode_New(0); |
| 9821 | if (encoding == NULL && errors == NULL) |
| 9822 | return PyObject_Str(x); |
| 9823 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9824 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9825 | } |
| 9826 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9827 | static PyObject * |
| 9828 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9829 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9830 | PyUnicodeObject *tmp, *pnew; |
| 9831 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9832 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9833 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 9834 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 9835 | if (tmp == NULL) |
| 9836 | return NULL; |
| 9837 | assert(PyUnicode_Check(tmp)); |
| 9838 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
| 9839 | if (pnew == NULL) { |
| 9840 | Py_DECREF(tmp); |
| 9841 | return NULL; |
| 9842 | } |
| 9843 | pnew->str = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1)); |
| 9844 | if (pnew->str == NULL) { |
| 9845 | _Py_ForgetReference((PyObject *)pnew); |
| 9846 | PyObject_Del(pnew); |
| 9847 | Py_DECREF(tmp); |
| 9848 | return PyErr_NoMemory(); |
| 9849 | } |
| 9850 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 9851 | pnew->length = n; |
| 9852 | pnew->hash = tmp->hash; |
| 9853 | Py_DECREF(tmp); |
| 9854 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9855 | } |
| 9856 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9857 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9858 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9859 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 9860 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 9861 | encoding defaults to the current default string encoding.\n\ |
| 9862 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9863 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9864 | static PyObject *unicode_iter(PyObject *seq); |
| 9865 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9866 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 9867 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9868 | "str", /* tp_name */ |
| 9869 | sizeof(PyUnicodeObject), /* tp_size */ |
| 9870 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9871 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9872 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 9873 | 0, /* tp_print */ |
| 9874 | 0, /* tp_getattr */ |
| 9875 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 9876 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9877 | unicode_repr, /* tp_repr */ |
| 9878 | &unicode_as_number, /* tp_as_number */ |
| 9879 | &unicode_as_sequence, /* tp_as_sequence */ |
| 9880 | &unicode_as_mapping, /* tp_as_mapping */ |
| 9881 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 9882 | 0, /* tp_call*/ |
| 9883 | (reprfunc) unicode_str, /* tp_str */ |
| 9884 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9885 | 0, /* tp_setattro */ |
| 9886 | 0, /* tp_as_buffer */ |
| 9887 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9888 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9889 | unicode_doc, /* tp_doc */ |
| 9890 | 0, /* tp_traverse */ |
| 9891 | 0, /* tp_clear */ |
| 9892 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 9893 | 0, /* tp_weaklistoffset */ |
| 9894 | unicode_iter, /* tp_iter */ |
| 9895 | 0, /* tp_iternext */ |
| 9896 | unicode_methods, /* tp_methods */ |
| 9897 | 0, /* tp_members */ |
| 9898 | 0, /* tp_getset */ |
| 9899 | &PyBaseObject_Type, /* tp_base */ |
| 9900 | 0, /* tp_dict */ |
| 9901 | 0, /* tp_descr_get */ |
| 9902 | 0, /* tp_descr_set */ |
| 9903 | 0, /* tp_dictoffset */ |
| 9904 | 0, /* tp_init */ |
| 9905 | 0, /* tp_alloc */ |
| 9906 | unicode_new, /* tp_new */ |
| 9907 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9908 | }; |
| 9909 | |
| 9910 | /* Initialize the Unicode implementation */ |
| 9911 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9912 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9913 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9914 | int i; |
| 9915 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9916 | /* XXX - move this array to unicodectype.c ? */ |
| 9917 | Py_UNICODE linebreak[] = { |
| 9918 | 0x000A, /* LINE FEED */ |
| 9919 | 0x000D, /* CARRIAGE RETURN */ |
| 9920 | 0x001C, /* FILE SEPARATOR */ |
| 9921 | 0x001D, /* GROUP SEPARATOR */ |
| 9922 | 0x001E, /* RECORD SEPARATOR */ |
| 9923 | 0x0085, /* NEXT LINE */ |
| 9924 | 0x2028, /* LINE SEPARATOR */ |
| 9925 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 9926 | }; |
| 9927 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 9928 | /* Init the implementation */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 9929 | free_list = NULL; |
| 9930 | numfree = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9931 | unicode_empty = _PyUnicode_New(0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9932 | if (!unicode_empty) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9933 | return; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9934 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9935 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9936 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 9937 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9938 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9939 | |
| 9940 | /* initialize the linebreak bloom filter */ |
| 9941 | bloom_linebreak = make_bloom_mask( |
| 9942 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 9943 | ); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9944 | |
| 9945 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9946 | } |
| 9947 | |
| 9948 | /* Finalize the Unicode implementation */ |
| 9949 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9950 | int |
| 9951 | PyUnicode_ClearFreeList(void) |
| 9952 | { |
| 9953 | int freelist_size = numfree; |
| 9954 | PyUnicodeObject *u; |
| 9955 | |
| 9956 | for (u = free_list; u != NULL;) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9957 | PyUnicodeObject *v = u; |
| 9958 | u = *(PyUnicodeObject **)u; |
| 9959 | if (v->str) |
| 9960 | PyObject_DEL(v->str); |
| 9961 | Py_XDECREF(v->defenc); |
| 9962 | PyObject_Del(v); |
| 9963 | numfree--; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9964 | } |
| 9965 | free_list = NULL; |
| 9966 | assert(numfree == 0); |
| 9967 | return freelist_size; |
| 9968 | } |
| 9969 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9970 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9971 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9972 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9973 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9974 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 9975 | Py_XDECREF(unicode_empty); |
| 9976 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 9977 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9978 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9979 | if (unicode_latin1[i]) { |
| 9980 | Py_DECREF(unicode_latin1[i]); |
| 9981 | unicode_latin1[i] = NULL; |
| 9982 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9983 | } |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9984 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9985 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9986 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9987 | void |
| 9988 | PyUnicode_InternInPlace(PyObject **p) |
| 9989 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9990 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 9991 | PyObject *t; |
| 9992 | if (s == NULL || !PyUnicode_Check(s)) |
| 9993 | Py_FatalError( |
| 9994 | "PyUnicode_InternInPlace: unicode strings only please!"); |
| 9995 | /* If it's a subclass, we don't really know what putting |
| 9996 | it in the interned dict might do. */ |
| 9997 | if (!PyUnicode_CheckExact(s)) |
| 9998 | return; |
| 9999 | if (PyUnicode_CHECK_INTERNED(s)) |
| 10000 | return; |
| 10001 | if (interned == NULL) { |
| 10002 | interned = PyDict_New(); |
| 10003 | if (interned == NULL) { |
| 10004 | PyErr_Clear(); /* Don't leave an exception */ |
| 10005 | return; |
| 10006 | } |
| 10007 | } |
| 10008 | /* It might be that the GetItem call fails even |
| 10009 | though the key is present in the dictionary, |
| 10010 | namely when this happens during a stack overflow. */ |
| 10011 | Py_ALLOW_RECURSION |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10012 | t = PyDict_GetItem(interned, (PyObject *)s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10013 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10014 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10015 | if (t) { |
| 10016 | Py_INCREF(t); |
| 10017 | Py_DECREF(*p); |
| 10018 | *p = t; |
| 10019 | return; |
| 10020 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 10021 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10022 | PyThreadState_GET()->recursion_critical = 1; |
| 10023 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 10024 | PyErr_Clear(); |
| 10025 | PyThreadState_GET()->recursion_critical = 0; |
| 10026 | return; |
| 10027 | } |
| 10028 | PyThreadState_GET()->recursion_critical = 0; |
| 10029 | /* The two references in interned are not counted by refcnt. |
| 10030 | The deallocator will take care of this */ |
| 10031 | Py_REFCNT(s) -= 2; |
| 10032 | PyUnicode_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 10033 | } |
| 10034 | |
| 10035 | void |
| 10036 | PyUnicode_InternImmortal(PyObject **p) |
| 10037 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10038 | PyUnicode_InternInPlace(p); |
| 10039 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
| 10040 | PyUnicode_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL; |
| 10041 | Py_INCREF(*p); |
| 10042 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 10043 | } |
| 10044 | |
| 10045 | PyObject * |
| 10046 | PyUnicode_InternFromString(const char *cp) |
| 10047 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10048 | PyObject *s = PyUnicode_FromString(cp); |
| 10049 | if (s == NULL) |
| 10050 | return NULL; |
| 10051 | PyUnicode_InternInPlace(&s); |
| 10052 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 10053 | } |
| 10054 | |
| 10055 | void _Py_ReleaseInternedUnicodeStrings(void) |
| 10056 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10057 | PyObject *keys; |
| 10058 | PyUnicodeObject *s; |
| 10059 | Py_ssize_t i, n; |
| 10060 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 10061 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10062 | if (interned == NULL || !PyDict_Check(interned)) |
| 10063 | return; |
| 10064 | keys = PyDict_Keys(interned); |
| 10065 | if (keys == NULL || !PyList_Check(keys)) { |
| 10066 | PyErr_Clear(); |
| 10067 | return; |
| 10068 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 10069 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10070 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 10071 | detector, interned unicode strings are not forcibly deallocated; |
| 10072 | rather, we give them their stolen references back, and then clear |
| 10073 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 10074 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10075 | n = PyList_GET_SIZE(keys); |
| 10076 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10077 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10078 | for (i = 0; i < n; i++) { |
| 10079 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
| 10080 | switch (s->state) { |
| 10081 | case SSTATE_NOT_INTERNED: |
| 10082 | /* XXX Shouldn't happen */ |
| 10083 | break; |
| 10084 | case SSTATE_INTERNED_IMMORTAL: |
| 10085 | Py_REFCNT(s) += 1; |
| 10086 | immortal_size += s->length; |
| 10087 | break; |
| 10088 | case SSTATE_INTERNED_MORTAL: |
| 10089 | Py_REFCNT(s) += 2; |
| 10090 | mortal_size += s->length; |
| 10091 | break; |
| 10092 | default: |
| 10093 | Py_FatalError("Inconsistent interned string state."); |
| 10094 | } |
| 10095 | s->state = SSTATE_NOT_INTERNED; |
| 10096 | } |
| 10097 | fprintf(stderr, "total size of all interned strings: " |
| 10098 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 10099 | "mortal/immortal\n", mortal_size, immortal_size); |
| 10100 | Py_DECREF(keys); |
| 10101 | PyDict_Clear(interned); |
| 10102 | Py_DECREF(interned); |
| 10103 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 10104 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10105 | |
| 10106 | |
| 10107 | /********************* Unicode Iterator **************************/ |
| 10108 | |
| 10109 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10110 | PyObject_HEAD |
| 10111 | Py_ssize_t it_index; |
| 10112 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10113 | } unicodeiterobject; |
| 10114 | |
| 10115 | static void |
| 10116 | unicodeiter_dealloc(unicodeiterobject *it) |
| 10117 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10118 | _PyObject_GC_UNTRACK(it); |
| 10119 | Py_XDECREF(it->it_seq); |
| 10120 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10121 | } |
| 10122 | |
| 10123 | static int |
| 10124 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 10125 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10126 | Py_VISIT(it->it_seq); |
| 10127 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10128 | } |
| 10129 | |
| 10130 | static PyObject * |
| 10131 | unicodeiter_next(unicodeiterobject *it) |
| 10132 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10133 | PyUnicodeObject *seq; |
| 10134 | PyObject *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10135 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10136 | assert(it != NULL); |
| 10137 | seq = it->it_seq; |
| 10138 | if (seq == NULL) |
| 10139 | return NULL; |
| 10140 | assert(PyUnicode_Check(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10141 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10142 | if (it->it_index < PyUnicode_GET_SIZE(seq)) { |
| 10143 | item = PyUnicode_FromUnicode( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10144 | PyUnicode_AS_UNICODE(seq)+it->it_index, 1); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10145 | if (item != NULL) |
| 10146 | ++it->it_index; |
| 10147 | return item; |
| 10148 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10149 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10150 | Py_DECREF(seq); |
| 10151 | it->it_seq = NULL; |
| 10152 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10153 | } |
| 10154 | |
| 10155 | static PyObject * |
| 10156 | unicodeiter_len(unicodeiterobject *it) |
| 10157 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10158 | Py_ssize_t len = 0; |
| 10159 | if (it->it_seq) |
| 10160 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
| 10161 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10162 | } |
| 10163 | |
| 10164 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 10165 | |
| 10166 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10167 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10168 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10169 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10170 | }; |
| 10171 | |
| 10172 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10173 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 10174 | "str_iterator", /* tp_name */ |
| 10175 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 10176 | 0, /* tp_itemsize */ |
| 10177 | /* methods */ |
| 10178 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 10179 | 0, /* tp_print */ |
| 10180 | 0, /* tp_getattr */ |
| 10181 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 10182 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10183 | 0, /* tp_repr */ |
| 10184 | 0, /* tp_as_number */ |
| 10185 | 0, /* tp_as_sequence */ |
| 10186 | 0, /* tp_as_mapping */ |
| 10187 | 0, /* tp_hash */ |
| 10188 | 0, /* tp_call */ |
| 10189 | 0, /* tp_str */ |
| 10190 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 10191 | 0, /* tp_setattro */ |
| 10192 | 0, /* tp_as_buffer */ |
| 10193 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 10194 | 0, /* tp_doc */ |
| 10195 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 10196 | 0, /* tp_clear */ |
| 10197 | 0, /* tp_richcompare */ |
| 10198 | 0, /* tp_weaklistoffset */ |
| 10199 | PyObject_SelfIter, /* tp_iter */ |
| 10200 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 10201 | unicodeiter_methods, /* tp_methods */ |
| 10202 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10203 | }; |
| 10204 | |
| 10205 | static PyObject * |
| 10206 | unicode_iter(PyObject *seq) |
| 10207 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10208 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10209 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10210 | if (!PyUnicode_Check(seq)) { |
| 10211 | PyErr_BadInternalCall(); |
| 10212 | return NULL; |
| 10213 | } |
| 10214 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 10215 | if (it == NULL) |
| 10216 | return NULL; |
| 10217 | it->it_index = 0; |
| 10218 | Py_INCREF(seq); |
| 10219 | it->it_seq = (PyUnicodeObject *)seq; |
| 10220 | _PyObject_GC_TRACK(it); |
| 10221 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10222 | } |
| 10223 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10224 | size_t |
| 10225 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 10226 | { |
| 10227 | int res = 0; |
| 10228 | while(*u++) |
| 10229 | res++; |
| 10230 | return res; |
| 10231 | } |
| 10232 | |
| 10233 | Py_UNICODE* |
| 10234 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 10235 | { |
| 10236 | Py_UNICODE *u = s1; |
| 10237 | while ((*u++ = *s2++)); |
| 10238 | return s1; |
| 10239 | } |
| 10240 | |
| 10241 | Py_UNICODE* |
| 10242 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 10243 | { |
| 10244 | Py_UNICODE *u = s1; |
| 10245 | while ((*u++ = *s2++)) |
| 10246 | if (n-- == 0) |
| 10247 | break; |
| 10248 | return s1; |
| 10249 | } |
| 10250 | |
Victor Stinner | c4eb765 | 2010-09-01 23:43:50 +0000 | [diff] [blame] | 10251 | Py_UNICODE* |
| 10252 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 10253 | { |
| 10254 | Py_UNICODE *u1 = s1; |
| 10255 | u1 += Py_UNICODE_strlen(u1); |
| 10256 | Py_UNICODE_strcpy(u1, s2); |
| 10257 | return s1; |
| 10258 | } |
| 10259 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10260 | int |
| 10261 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 10262 | { |
| 10263 | while (*s1 && *s2 && *s1 == *s2) |
| 10264 | s1++, s2++; |
| 10265 | if (*s1 && *s2) |
| 10266 | return (*s1 < *s2) ? -1 : +1; |
| 10267 | if (*s1) |
| 10268 | return 1; |
| 10269 | if (*s2) |
| 10270 | return -1; |
| 10271 | return 0; |
| 10272 | } |
| 10273 | |
Victor Stinner | ef8d95c | 2010-08-16 22:03:11 +0000 | [diff] [blame] | 10274 | int |
| 10275 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 10276 | { |
| 10277 | register Py_UNICODE u1, u2; |
| 10278 | for (; n != 0; n--) { |
| 10279 | u1 = *s1; |
| 10280 | u2 = *s2; |
| 10281 | if (u1 != u2) |
| 10282 | return (u1 < u2) ? -1 : +1; |
| 10283 | if (u1 == '\0') |
| 10284 | return 0; |
| 10285 | s1++; |
| 10286 | s2++; |
| 10287 | } |
| 10288 | return 0; |
| 10289 | } |
| 10290 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10291 | Py_UNICODE* |
| 10292 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 10293 | { |
| 10294 | const Py_UNICODE *p; |
| 10295 | for (p = s; *p; p++) |
| 10296 | if (*p == c) |
| 10297 | return (Py_UNICODE*)p; |
| 10298 | return NULL; |
| 10299 | } |
| 10300 | |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 10301 | Py_UNICODE* |
| 10302 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 10303 | { |
| 10304 | const Py_UNICODE *p; |
| 10305 | p = s + Py_UNICODE_strlen(s); |
| 10306 | while (p != s) { |
| 10307 | p--; |
| 10308 | if (*p == c) |
| 10309 | return (Py_UNICODE*)p; |
| 10310 | } |
| 10311 | return NULL; |
| 10312 | } |
| 10313 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 10314 | Py_UNICODE* |
Victor Stinner | 4640860 | 2010-09-03 16:18:00 +0000 | [diff] [blame] | 10315 | PyUnicode_AsUnicodeCopy(PyObject *object) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 10316 | { |
| 10317 | PyUnicodeObject *unicode = (PyUnicodeObject *)object; |
| 10318 | Py_UNICODE *copy; |
| 10319 | Py_ssize_t size; |
| 10320 | |
| 10321 | /* Ensure we won't overflow the size. */ |
| 10322 | if (PyUnicode_GET_SIZE(unicode) > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 10323 | PyErr_NoMemory(); |
| 10324 | return NULL; |
| 10325 | } |
| 10326 | size = PyUnicode_GET_SIZE(unicode) + 1; /* copy the nul character */ |
| 10327 | size *= sizeof(Py_UNICODE); |
| 10328 | copy = PyMem_Malloc(size); |
| 10329 | if (copy == NULL) { |
| 10330 | PyErr_NoMemory(); |
| 10331 | return NULL; |
| 10332 | } |
| 10333 | memcpy(copy, PyUnicode_AS_UNICODE(unicode), size); |
| 10334 | return copy; |
| 10335 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10336 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 10337 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 10338 | to the string.Formatter class implemented in Python. */ |
| 10339 | |
| 10340 | static PyMethodDef _string_methods[] = { |
| 10341 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 10342 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 10343 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 10344 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 10345 | {NULL, NULL} |
| 10346 | }; |
| 10347 | |
| 10348 | static struct PyModuleDef _string_module = { |
| 10349 | PyModuleDef_HEAD_INIT, |
| 10350 | "_string", |
| 10351 | PyDoc_STR("string helper module"), |
| 10352 | 0, |
| 10353 | _string_methods, |
| 10354 | NULL, |
| 10355 | NULL, |
| 10356 | NULL, |
| 10357 | NULL |
| 10358 | }; |
| 10359 | |
| 10360 | PyMODINIT_FUNC |
| 10361 | PyInit__string(void) |
| 10362 | { |
| 10363 | return PyModule_Create(&_string_module); |
| 10364 | } |
| 10365 | |
| 10366 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10367 | #ifdef __cplusplus |
| 10368 | } |
| 10369 | #endif |