Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1 | /* |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2 | |
| 3 | Unicode implementation based on original code by Fredrik Lundh, |
Fred Drake | 785d14f | 2000-05-09 19:54:43 +0000 | [diff] [blame] | 4 | modified by Marc-Andre Lemburg <mal@lemburg.com> according to the |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5 | Unicode Integration Proposal (see file Misc/unicode.txt). |
| 6 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7 | Major speed upgrades to the method implementations at the Reykjavik |
| 8 | NeedForSpeed sprint, by Fredrik Lundh and Andrew Dalke. |
| 9 | |
Guido van Rossum | 16b1ad9 | 2000-08-03 16:24:25 +0000 | [diff] [blame] | 10 | Copyright (c) Corporation for National Research Initiatives. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 12 | -------------------------------------------------------------------- |
| 13 | The original string type implementation is: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 15 | Copyright (c) 1999 by Secret Labs AB |
| 16 | Copyright (c) 1999 by Fredrik Lundh |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 17 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 18 | By obtaining, using, and/or copying this software and/or its |
| 19 | associated documentation, you agree that you have read, understood, |
| 20 | and will comply with the following terms and conditions: |
| 21 | |
| 22 | Permission to use, copy, modify, and distribute this software and its |
| 23 | associated documentation for any purpose and without fee is hereby |
| 24 | granted, provided that the above copyright notice appears in all |
| 25 | copies, and that both that copyright notice and this permission notice |
| 26 | appear in supporting documentation, and that the name of Secret Labs |
| 27 | AB or the author not be used in advertising or publicity pertaining to |
| 28 | distribution of the software without specific, written prior |
| 29 | permission. |
| 30 | |
| 31 | SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 32 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 33 | FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR |
| 34 | ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 35 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 36 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 37 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 38 | -------------------------------------------------------------------- |
| 39 | |
| 40 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 41 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 42 | #define PY_SSIZE_T_CLEAN |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 43 | #include "Python.h" |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 44 | #include "bytes_methods.h" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 45 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 46 | #include "unicodeobject.h" |
Marc-André Lemburg | d49e5b4 | 2000-06-30 14:58:20 +0000 | [diff] [blame] | 47 | #include "ucnhash.h" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 48 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 49 | #ifdef MS_WINDOWS |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 50 | #include <windows.h> |
| 51 | #endif |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 52 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 53 | /* Limit for the Unicode object free list */ |
| 54 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 55 | #define PyUnicode_MAXFREELIST 1024 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 56 | |
| 57 | /* Limit for the Unicode object free list stay alive optimization. |
| 58 | |
| 59 | The implementation will keep allocated Unicode memory intact for |
| 60 | all objects on the free list having a size less than this |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 61 | limit. This reduces malloc() overhead for small Unicode objects. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 62 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 63 | At worst this will result in PyUnicode_MAXFREELIST * |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 64 | (sizeof(PyUnicodeObject) + KEEPALIVE_SIZE_LIMIT + |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 65 | malloc()-overhead) bytes of unused garbage. |
| 66 | |
| 67 | Setting the limit to 0 effectively turns the feature off. |
| 68 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 69 | Note: This is an experimental feature ! If you get core dumps when |
| 70 | using Unicode objects, turn this feature off. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 71 | |
| 72 | */ |
| 73 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 74 | #define KEEPALIVE_SIZE_LIMIT 9 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 75 | |
| 76 | /* Endianness switches; defaults to little endian */ |
| 77 | |
| 78 | #ifdef WORDS_BIGENDIAN |
| 79 | # define BYTEORDER_IS_BIG_ENDIAN |
| 80 | #else |
| 81 | # define BYTEORDER_IS_LITTLE_ENDIAN |
| 82 | #endif |
| 83 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 84 | /* --- Globals ------------------------------------------------------------ |
| 85 | |
| 86 | The globals are initialized by the _PyUnicode_Init() API and should |
| 87 | not be used before calling that API. |
| 88 | |
| 89 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 90 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 91 | |
| 92 | #ifdef __cplusplus |
| 93 | extern "C" { |
| 94 | #endif |
| 95 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 96 | /* This dictionary holds all interned unicode strings. Note that references |
| 97 | to strings in this dictionary are *not* counted in the string's ob_refcnt. |
| 98 | When the interned string reaches a refcnt of 0 the string deallocation |
| 99 | function will delete the reference from this dictionary. |
| 100 | |
| 101 | Another way to look at this is that to say that the actual reference |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 102 | count of a string is: s->ob_refcnt + (s->state ? 2 : 0) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 103 | */ |
| 104 | static PyObject *interned; |
| 105 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 106 | /* Free list for Unicode objects */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 107 | static PyUnicodeObject *free_list; |
| 108 | static int numfree; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 109 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 110 | /* The empty Unicode object is shared to improve performance. */ |
| 111 | static PyUnicodeObject *unicode_empty; |
| 112 | |
| 113 | /* Single character Unicode strings in the Latin-1 range are being |
| 114 | shared as well. */ |
| 115 | static PyUnicodeObject *unicode_latin1[256]; |
| 116 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 117 | /* Fast detection of the most frequent whitespace characters */ |
| 118 | const unsigned char _Py_ascii_whitespace[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 119 | 0, 0, 0, 0, 0, 0, 0, 0, |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 120 | /* case 0x0009: * CHARACTER TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 121 | /* case 0x000A: * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 122 | /* case 0x000B: * LINE TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 123 | /* case 0x000C: * FORM FEED */ |
| 124 | /* case 0x000D: * CARRIAGE RETURN */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 125 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 126 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 127 | /* case 0x001C: * FILE SEPARATOR */ |
| 128 | /* case 0x001D: * GROUP SEPARATOR */ |
| 129 | /* case 0x001E: * RECORD SEPARATOR */ |
| 130 | /* case 0x001F: * UNIT SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 131 | 0, 0, 0, 0, 1, 1, 1, 1, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 132 | /* case 0x0020: * SPACE */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 133 | 1, 0, 0, 0, 0, 0, 0, 0, |
| 134 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 135 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 136 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 137 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 138 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 139 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 140 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 141 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 142 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 143 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 144 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 145 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 146 | }; |
| 147 | |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 148 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
| 149 | PyObject **errorHandler,const char *encoding, const char *reason, |
| 150 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 151 | Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos); |
| 152 | |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 153 | static void raise_encode_exception(PyObject **exceptionObject, |
| 154 | const char *encoding, |
| 155 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 156 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 157 | const char *reason); |
| 158 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 159 | /* Same for linebreaks */ |
| 160 | static unsigned char ascii_linebreak[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 161 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 162 | /* 0x000A, * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 163 | /* 0x000B, * LINE TABULATION */ |
| 164 | /* 0x000C, * FORM FEED */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 165 | /* 0x000D, * CARRIAGE RETURN */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 166 | 0, 0, 1, 1, 1, 1, 0, 0, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 167 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 168 | /* 0x001C, * FILE SEPARATOR */ |
| 169 | /* 0x001D, * GROUP SEPARATOR */ |
| 170 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 171 | 0, 0, 0, 0, 1, 1, 1, 0, |
| 172 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 173 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 174 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 175 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 176 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 177 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 178 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 179 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 180 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 181 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 182 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 183 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 184 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 188 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 189 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 190 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 191 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 192 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 193 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 194 | /* This is actually an illegal character, so it should |
| 195 | not be passed to unichr. */ |
| 196 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 197 | #endif |
| 198 | } |
| 199 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 200 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 201 | |
| 202 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 203 | to keep things simple, we use a single bitmask, using the least 5 |
| 204 | bits from each unicode characters as the bit index. */ |
| 205 | |
| 206 | /* the linebreak mask is set up by Unicode_Init below */ |
| 207 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 208 | #if LONG_BIT >= 128 |
| 209 | #define BLOOM_WIDTH 128 |
| 210 | #elif LONG_BIT >= 64 |
| 211 | #define BLOOM_WIDTH 64 |
| 212 | #elif LONG_BIT >= 32 |
| 213 | #define BLOOM_WIDTH 32 |
| 214 | #else |
| 215 | #error "LONG_BIT is smaller than 32" |
| 216 | #endif |
| 217 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 218 | #define BLOOM_MASK unsigned long |
| 219 | |
| 220 | static BLOOM_MASK bloom_linebreak; |
| 221 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 222 | #define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
| 223 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 224 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 225 | #define BLOOM_LINEBREAK(ch) \ |
| 226 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 227 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 228 | |
| 229 | Py_LOCAL_INLINE(BLOOM_MASK) make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len) |
| 230 | { |
| 231 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 232 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 233 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 234 | Py_ssize_t i; |
| 235 | |
| 236 | mask = 0; |
| 237 | for (i = 0; i < len; i++) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 238 | BLOOM_ADD(mask, ptr[i]); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 239 | |
| 240 | return mask; |
| 241 | } |
| 242 | |
| 243 | Py_LOCAL_INLINE(int) unicode_member(Py_UNICODE chr, Py_UNICODE* set, Py_ssize_t setlen) |
| 244 | { |
| 245 | Py_ssize_t i; |
| 246 | |
| 247 | for (i = 0; i < setlen; i++) |
| 248 | if (set[i] == chr) |
| 249 | return 1; |
| 250 | |
| 251 | return 0; |
| 252 | } |
| 253 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 254 | #define BLOOM_MEMBER(mask, chr, set, setlen) \ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 255 | BLOOM(mask, chr) && unicode_member(chr, set, setlen) |
| 256 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 257 | /* --- Unicode Object ----------------------------------------------------- */ |
| 258 | |
| 259 | static |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 260 | int unicode_resize(register PyUnicodeObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 261 | Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 262 | { |
| 263 | void *oldstr; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 264 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 265 | /* Shortcut if there's nothing much to do. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 266 | if (unicode->length == length) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 267 | goto reset; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 268 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 269 | /* Resizing shared object (unicode_empty or single character |
| 270 | objects) in-place is not allowed. Use PyUnicode_Resize() |
| 271 | instead ! */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 272 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 273 | if (unicode == unicode_empty || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 274 | (unicode->length == 1 && |
| 275 | unicode->str[0] < 256U && |
| 276 | unicode_latin1[unicode->str[0]] == unicode)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 277 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 278 | "can't resize shared str objects"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 279 | return -1; |
| 280 | } |
| 281 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 282 | /* We allocate one more byte to make sure the string is Ux0000 terminated. |
| 283 | The overallocation is also used by fastsearch, which assumes that it's |
| 284 | safe to look at str[length] (without making any assumptions about what |
| 285 | it contains). */ |
| 286 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 287 | oldstr = unicode->str; |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 288 | unicode->str = PyObject_REALLOC(unicode->str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 289 | sizeof(Py_UNICODE) * (length + 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 290 | if (!unicode->str) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 291 | unicode->str = (Py_UNICODE *)oldstr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 292 | PyErr_NoMemory(); |
| 293 | return -1; |
| 294 | } |
| 295 | unicode->str[length] = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 296 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 297 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 298 | reset: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 299 | /* Reset the object caches */ |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 300 | if (unicode->defenc) { |
Georg Brandl | 8ee604b | 2010-07-29 14:23:06 +0000 | [diff] [blame] | 301 | Py_CLEAR(unicode->defenc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 302 | } |
| 303 | unicode->hash = -1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 304 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | /* We allocate one more byte to make sure the string is |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 309 | Ux0000 terminated; some code (e.g. new_identifier) |
| 310 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 311 | |
| 312 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 313 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 314 | |
| 315 | */ |
| 316 | |
| 317 | static |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 318 | PyUnicodeObject *_PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 319 | { |
| 320 | register PyUnicodeObject *unicode; |
| 321 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 322 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 323 | if (length == 0 && unicode_empty != NULL) { |
| 324 | Py_INCREF(unicode_empty); |
| 325 | return unicode_empty; |
| 326 | } |
| 327 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 328 | /* Ensure we won't overflow the size. */ |
| 329 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 330 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 331 | } |
| 332 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 333 | /* Unicode freelist & memory allocation */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 334 | if (free_list) { |
| 335 | unicode = free_list; |
| 336 | free_list = *(PyUnicodeObject **)unicode; |
| 337 | numfree--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 338 | if (unicode->str) { |
| 339 | /* Keep-Alive optimization: we only upsize the buffer, |
| 340 | never downsize it. */ |
| 341 | if ((unicode->length < length) && |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 342 | unicode_resize(unicode, length) < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 343 | PyObject_DEL(unicode->str); |
| 344 | unicode->str = NULL; |
| 345 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 346 | } |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 347 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 348 | size_t new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 349 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 350 | } |
| 351 | PyObject_INIT(unicode, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 352 | } |
| 353 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 354 | size_t new_size; |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 355 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 356 | if (unicode == NULL) |
| 357 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 358 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 359 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 362 | if (!unicode->str) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 363 | PyErr_NoMemory(); |
| 364 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 365 | } |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 366 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 367 | * the caller fails before initializing str -- unicode_resize() |
| 368 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 369 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 370 | * We don't want unicode_resize to read uninitialized memory in |
| 371 | * that case. |
| 372 | */ |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 373 | unicode->str[0] = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 374 | unicode->str[length] = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 375 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 376 | unicode->hash = -1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 377 | unicode->state = 0; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 378 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 379 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 380 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 381 | onError: |
Amaury Forgeot d'Arc | 7888d08 | 2008-08-01 01:06:32 +0000 | [diff] [blame] | 382 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
| 383 | _Py_DEC_REFTOTAL; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 384 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 385 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 386 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | static |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 390 | void unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 391 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 392 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 393 | case SSTATE_NOT_INTERNED: |
| 394 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 395 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 396 | case SSTATE_INTERNED_MORTAL: |
| 397 | /* revive dead object temporarily for DelItem */ |
| 398 | Py_REFCNT(unicode) = 3; |
| 399 | if (PyDict_DelItem(interned, (PyObject *)unicode) != 0) |
| 400 | Py_FatalError( |
| 401 | "deletion of interned string failed"); |
| 402 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 403 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 404 | case SSTATE_INTERNED_IMMORTAL: |
| 405 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 406 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 407 | default: |
| 408 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 411 | if (PyUnicode_CheckExact(unicode) && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 412 | numfree < PyUnicode_MAXFREELIST) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 413 | /* Keep-Alive optimization */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 414 | if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { |
| 415 | PyObject_DEL(unicode->str); |
| 416 | unicode->str = NULL; |
| 417 | unicode->length = 0; |
| 418 | } |
| 419 | if (unicode->defenc) { |
Georg Brandl | 8ee604b | 2010-07-29 14:23:06 +0000 | [diff] [blame] | 420 | Py_CLEAR(unicode->defenc); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 421 | } |
| 422 | /* Add to free list */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 423 | *(PyUnicodeObject **)unicode = free_list; |
| 424 | free_list = unicode; |
| 425 | numfree++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 426 | } |
| 427 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 428 | PyObject_DEL(unicode->str); |
| 429 | Py_XDECREF(unicode->defenc); |
| 430 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 434 | static |
| 435 | int _PyUnicode_Resize(PyUnicodeObject **unicode, Py_ssize_t length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 436 | { |
| 437 | register PyUnicodeObject *v; |
| 438 | |
| 439 | /* Argument checks */ |
| 440 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 441 | PyErr_BadInternalCall(); |
| 442 | return -1; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 443 | } |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 444 | v = *unicode; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 445 | if (v == NULL || !PyUnicode_Check(v) || Py_REFCNT(v) != 1 || length < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 446 | PyErr_BadInternalCall(); |
| 447 | return -1; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | /* Resizing unicode_empty and single character objects is not |
| 451 | possible since these are being shared. We simply return a fresh |
| 452 | copy with the same Unicode content. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 453 | if (v->length != length && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 454 | (v == unicode_empty || v->length == 1)) { |
| 455 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 456 | if (w == NULL) |
| 457 | return -1; |
| 458 | Py_UNICODE_COPY(w->str, v->str, |
| 459 | length < v->length ? length : v->length); |
| 460 | Py_DECREF(*unicode); |
| 461 | *unicode = w; |
| 462 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 466 | objects, since we can modify them in-place. */ |
| 467 | return unicode_resize(v, length); |
| 468 | } |
| 469 | |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 470 | int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) |
| 471 | { |
| 472 | return _PyUnicode_Resize((PyUnicodeObject **)unicode, length); |
| 473 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 474 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 475 | PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 476 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 477 | { |
| 478 | PyUnicodeObject *unicode; |
| 479 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 480 | /* If the Unicode data is known at construction time, we can apply |
| 481 | some optimizations which share commonly used objects. */ |
| 482 | if (u != NULL) { |
| 483 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 484 | /* Optimization for empty strings */ |
| 485 | if (size == 0 && unicode_empty != NULL) { |
| 486 | Py_INCREF(unicode_empty); |
| 487 | return (PyObject *)unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 488 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 489 | |
| 490 | /* Single character Unicode objects in the Latin-1 range are |
| 491 | shared when using this constructor */ |
| 492 | if (size == 1 && *u < 256) { |
| 493 | unicode = unicode_latin1[*u]; |
| 494 | if (!unicode) { |
| 495 | unicode = _PyUnicode_New(1); |
| 496 | if (!unicode) |
| 497 | return NULL; |
| 498 | unicode->str[0] = *u; |
| 499 | unicode_latin1[*u] = unicode; |
| 500 | } |
| 501 | Py_INCREF(unicode); |
| 502 | return (PyObject *)unicode; |
| 503 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 504 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 505 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 506 | unicode = _PyUnicode_New(size); |
| 507 | if (!unicode) |
| 508 | return NULL; |
| 509 | |
| 510 | /* Copy the Unicode data into the new object */ |
| 511 | if (u != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 512 | Py_UNICODE_COPY(unicode->str, u, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 513 | |
| 514 | return (PyObject *)unicode; |
| 515 | } |
| 516 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 517 | PyObject *PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 518 | { |
| 519 | PyUnicodeObject *unicode; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 520 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 521 | if (size < 0) { |
| 522 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 523 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 524 | return NULL; |
| 525 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 526 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 527 | /* If the Unicode data is known at construction time, we can apply |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 528 | some optimizations which share commonly used objects. |
| 529 | Also, this means the input must be UTF-8, so fall back to the |
| 530 | UTF-8 decoder at the end. */ |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 531 | if (u != NULL) { |
| 532 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 533 | /* Optimization for empty strings */ |
| 534 | if (size == 0 && unicode_empty != NULL) { |
| 535 | Py_INCREF(unicode_empty); |
| 536 | return (PyObject *)unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 537 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 538 | |
| 539 | /* Single characters are shared when using this constructor. |
| 540 | Restrict to ASCII, since the input must be UTF-8. */ |
| 541 | if (size == 1 && Py_CHARMASK(*u) < 128) { |
| 542 | unicode = unicode_latin1[Py_CHARMASK(*u)]; |
| 543 | if (!unicode) { |
| 544 | unicode = _PyUnicode_New(1); |
| 545 | if (!unicode) |
| 546 | return NULL; |
| 547 | unicode->str[0] = Py_CHARMASK(*u); |
| 548 | unicode_latin1[Py_CHARMASK(*u)] = unicode; |
| 549 | } |
| 550 | Py_INCREF(unicode); |
| 551 | return (PyObject *)unicode; |
| 552 | } |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 553 | |
| 554 | return PyUnicode_DecodeUTF8(u, size, NULL); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Walter Dörwald | 5550731 | 2007-05-18 13:12:10 +0000 | [diff] [blame] | 557 | unicode = _PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 558 | if (!unicode) |
| 559 | return NULL; |
| 560 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 561 | return (PyObject *)unicode; |
| 562 | } |
| 563 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 564 | PyObject *PyUnicode_FromString(const char *u) |
| 565 | { |
| 566 | size_t size = strlen(u); |
| 567 | if (size > PY_SSIZE_T_MAX) { |
| 568 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 569 | return NULL; |
| 570 | } |
| 571 | |
| 572 | return PyUnicode_FromStringAndSize(u, size); |
| 573 | } |
| 574 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 575 | #ifdef HAVE_WCHAR_H |
| 576 | |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 577 | #if (Py_UNICODE_SIZE == 2) && defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
| 578 | # define CONVERT_WCHAR_TO_SURROGATES |
| 579 | #endif |
| 580 | |
| 581 | #ifdef CONVERT_WCHAR_TO_SURROGATES |
| 582 | |
| 583 | /* Here sizeof(wchar_t) is 4 but Py_UNICODE_SIZE == 2, so we need |
| 584 | to convert from UTF32 to UTF16. */ |
| 585 | |
| 586 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
| 587 | Py_ssize_t size) |
| 588 | { |
| 589 | PyUnicodeObject *unicode; |
| 590 | register Py_ssize_t i; |
| 591 | Py_ssize_t alloc; |
| 592 | const wchar_t *orig_w; |
| 593 | |
| 594 | if (w == NULL) { |
| 595 | if (size == 0) |
| 596 | return PyUnicode_FromStringAndSize(NULL, 0); |
| 597 | PyErr_BadInternalCall(); |
| 598 | return NULL; |
| 599 | } |
| 600 | |
| 601 | if (size == -1) { |
| 602 | size = wcslen(w); |
| 603 | } |
| 604 | |
| 605 | alloc = size; |
| 606 | orig_w = w; |
| 607 | for (i = size; i > 0; i--) { |
| 608 | if (*w > 0xFFFF) |
| 609 | alloc++; |
| 610 | w++; |
| 611 | } |
| 612 | w = orig_w; |
| 613 | unicode = _PyUnicode_New(alloc); |
| 614 | if (!unicode) |
| 615 | return NULL; |
| 616 | |
| 617 | /* Copy the wchar_t data into the new object */ |
| 618 | { |
| 619 | register Py_UNICODE *u; |
| 620 | u = PyUnicode_AS_UNICODE(unicode); |
| 621 | for (i = size; i > 0; i--) { |
| 622 | if (*w > 0xFFFF) { |
| 623 | wchar_t ordinal = *w++; |
| 624 | ordinal -= 0x10000; |
| 625 | *u++ = 0xD800 | (ordinal >> 10); |
| 626 | *u++ = 0xDC00 | (ordinal & 0x3FF); |
| 627 | } |
| 628 | else |
| 629 | *u++ = *w++; |
| 630 | } |
| 631 | } |
| 632 | return (PyObject *)unicode; |
| 633 | } |
| 634 | |
| 635 | #else |
| 636 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 637 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 638 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 639 | { |
| 640 | PyUnicodeObject *unicode; |
| 641 | |
| 642 | if (w == NULL) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 643 | if (size == 0) |
| 644 | return PyUnicode_FromStringAndSize(NULL, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 645 | PyErr_BadInternalCall(); |
| 646 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 647 | } |
| 648 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 649 | if (size == -1) { |
| 650 | size = wcslen(w); |
| 651 | } |
| 652 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 653 | unicode = _PyUnicode_New(size); |
| 654 | if (!unicode) |
| 655 | return NULL; |
| 656 | |
| 657 | /* Copy the wchar_t data into the new object */ |
Daniel Stutzbach | 8515eae | 2010-08-24 21:57:33 +0000 | [diff] [blame] | 658 | #if Py_UNICODE_SIZE == SIZEOF_WCHAR_T |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 659 | memcpy(unicode->str, w, size * sizeof(wchar_t)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 660 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 661 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 662 | register Py_UNICODE *u; |
| 663 | register Py_ssize_t i; |
| 664 | u = PyUnicode_AS_UNICODE(unicode); |
| 665 | for (i = size; i > 0; i--) |
| 666 | *u++ = *w++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 667 | } |
| 668 | #endif |
| 669 | |
| 670 | return (PyObject *)unicode; |
| 671 | } |
| 672 | |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 673 | #endif /* CONVERT_WCHAR_TO_SURROGATES */ |
| 674 | |
| 675 | #undef CONVERT_WCHAR_TO_SURROGATES |
| 676 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 677 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 678 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 679 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 680 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 681 | *fmt++ = '%'; |
| 682 | if (width) { |
| 683 | if (zeropad) |
| 684 | *fmt++ = '0'; |
| 685 | fmt += sprintf(fmt, "%d", width); |
| 686 | } |
| 687 | if (precision) |
| 688 | fmt += sprintf(fmt, ".%d", precision); |
| 689 | if (longflag) |
| 690 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 691 | else if (longlongflag) { |
| 692 | /* longlongflag should only ever be nonzero on machines with |
| 693 | HAVE_LONG_LONG defined */ |
| 694 | #ifdef HAVE_LONG_LONG |
| 695 | char *f = PY_FORMAT_LONG_LONG; |
| 696 | while (*f) |
| 697 | *fmt++ = *f++; |
| 698 | #else |
| 699 | /* we shouldn't ever get here */ |
| 700 | assert(0); |
| 701 | *fmt++ = 'l'; |
| 702 | #endif |
| 703 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 704 | else if (size_tflag) { |
| 705 | char *f = PY_FORMAT_SIZE_T; |
| 706 | while (*f) |
| 707 | *fmt++ = *f++; |
| 708 | } |
| 709 | *fmt++ = c; |
| 710 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 713 | #define appendstring(string) {for (copy = string;*copy;) *s++ = *copy++;} |
| 714 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 715 | /* size of fixed-size buffer for formatting single arguments */ |
| 716 | #define ITEM_BUFFER_LEN 21 |
| 717 | /* maximum number of characters required for output of %ld. 21 characters |
| 718 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 719 | #define MAX_LONG_CHARS 21 |
| 720 | /* maximum number of characters required for output of %lld. |
| 721 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 722 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 723 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 724 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 725 | PyObject * |
| 726 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 727 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 728 | va_list count; |
| 729 | Py_ssize_t callcount = 0; |
| 730 | PyObject **callresults = NULL; |
| 731 | PyObject **callresult = NULL; |
| 732 | Py_ssize_t n = 0; |
| 733 | int width = 0; |
| 734 | int precision = 0; |
| 735 | int zeropad; |
| 736 | const char* f; |
| 737 | Py_UNICODE *s; |
| 738 | PyObject *string; |
| 739 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 740 | char buffer[ITEM_BUFFER_LEN+1]; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 741 | /* use abuffer instead of buffer, if we need more space |
| 742 | * (which can happen if there's a format specifier with width). */ |
| 743 | char *abuffer = NULL; |
| 744 | char *realbuffer; |
| 745 | Py_ssize_t abuffersize = 0; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 746 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 747 | const char *copy; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 748 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 749 | Py_VA_COPY(count, vargs); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 750 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 751 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 752 | * PyUnicode_DecodeUTF8() for these objects once during step 3 and put the |
| 753 | * result in an array) */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 754 | for (f = format; *f; f++) { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 755 | if (*f == '%') { |
| 756 | if (*(f+1)=='%') |
| 757 | continue; |
| 758 | if (*(f+1)=='S' || *(f+1)=='R' || *(f+1)=='A') |
| 759 | ++callcount; |
| 760 | while (ISDIGIT((unsigned)*f)) |
| 761 | width = (width*10) + *f++ - '0'; |
| 762 | while (*++f && *f != '%' && !ISALPHA((unsigned)*f)) |
| 763 | ; |
| 764 | if (*f == 's') |
| 765 | ++callcount; |
| 766 | } |
Benjamin Peterson | 9be0b2e | 2010-09-12 03:40:54 +0000 | [diff] [blame] | 767 | else if (128 <= (unsigned char)*f) { |
| 768 | PyErr_Format(PyExc_ValueError, |
| 769 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
Victor Stinner | 4c7db31 | 2010-09-12 07:51:18 +0000 | [diff] [blame] | 770 | "string, got a non-ASCII byte: 0x%02x", |
Benjamin Peterson | 9be0b2e | 2010-09-12 03:40:54 +0000 | [diff] [blame] | 771 | (unsigned char)*f); |
Benjamin Peterson | d4ac96a | 2010-09-12 16:40:53 +0000 | [diff] [blame] | 772 | return NULL; |
Benjamin Peterson | 9be0b2e | 2010-09-12 03:40:54 +0000 | [diff] [blame] | 773 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 774 | } |
| 775 | /* step 2: allocate memory for the results of |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 776 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 777 | if (callcount) { |
| 778 | callresults = PyObject_Malloc(sizeof(PyObject *)*callcount); |
| 779 | if (!callresults) { |
| 780 | PyErr_NoMemory(); |
| 781 | return NULL; |
| 782 | } |
| 783 | callresult = callresults; |
| 784 | } |
| 785 | /* step 3: figure out how large a buffer we need */ |
| 786 | for (f = format; *f; f++) { |
| 787 | if (*f == '%') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 788 | #ifdef HAVE_LONG_LONG |
| 789 | int longlongflag = 0; |
| 790 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 791 | const char* p = f; |
| 792 | width = 0; |
| 793 | while (ISDIGIT((unsigned)*f)) |
| 794 | width = (width*10) + *f++ - '0'; |
| 795 | while (*++f && *f != '%' && !ISALPHA((unsigned)*f)) |
| 796 | ; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 797 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 798 | /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
| 799 | * they don't affect the amount of space we reserve. |
| 800 | */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 801 | if (*f == 'l') { |
| 802 | if (f[1] == 'd' || f[1] == 'u') { |
| 803 | ++f; |
| 804 | } |
| 805 | #ifdef HAVE_LONG_LONG |
| 806 | else if (f[1] == 'l' && |
| 807 | (f[2] == 'd' || f[2] == 'u')) { |
| 808 | longlongflag = 1; |
| 809 | f += 2; |
| 810 | } |
| 811 | #endif |
| 812 | } |
| 813 | else if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 814 | ++f; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 815 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 816 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 817 | switch (*f) { |
| 818 | case 'c': |
| 819 | (void)va_arg(count, int); |
| 820 | /* fall through... */ |
| 821 | case '%': |
| 822 | n++; |
| 823 | break; |
| 824 | case 'd': case 'u': case 'i': case 'x': |
| 825 | (void) va_arg(count, int); |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 826 | #ifdef HAVE_LONG_LONG |
| 827 | if (longlongflag) { |
| 828 | if (width < MAX_LONG_LONG_CHARS) |
| 829 | width = MAX_LONG_LONG_CHARS; |
| 830 | } |
| 831 | else |
| 832 | #endif |
| 833 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 834 | including sign. Decimal takes the most space. This |
| 835 | isn't enough for octal. If a width is specified we |
| 836 | need more (which we allocate later). */ |
| 837 | if (width < MAX_LONG_CHARS) |
| 838 | width = MAX_LONG_CHARS; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 839 | n += width; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 840 | /* XXX should allow for large precision here too. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 841 | if (abuffersize < width) |
| 842 | abuffersize = width; |
| 843 | break; |
| 844 | case 's': |
| 845 | { |
| 846 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 847 | const char *s = va_arg(count, const char*); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 848 | PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); |
| 849 | if (!str) |
| 850 | goto fail; |
| 851 | n += PyUnicode_GET_SIZE(str); |
| 852 | /* Remember the str and switch to the next slot */ |
| 853 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 854 | break; |
| 855 | } |
| 856 | case 'U': |
| 857 | { |
| 858 | PyObject *obj = va_arg(count, PyObject *); |
| 859 | assert(obj && PyUnicode_Check(obj)); |
| 860 | n += PyUnicode_GET_SIZE(obj); |
| 861 | break; |
| 862 | } |
| 863 | case 'V': |
| 864 | { |
| 865 | PyObject *obj = va_arg(count, PyObject *); |
| 866 | const char *str = va_arg(count, const char *); |
| 867 | assert(obj || str); |
| 868 | assert(!obj || PyUnicode_Check(obj)); |
| 869 | if (obj) |
| 870 | n += PyUnicode_GET_SIZE(obj); |
| 871 | else |
| 872 | n += strlen(str); |
| 873 | break; |
| 874 | } |
| 875 | case 'S': |
| 876 | { |
| 877 | PyObject *obj = va_arg(count, PyObject *); |
| 878 | PyObject *str; |
| 879 | assert(obj); |
| 880 | str = PyObject_Str(obj); |
| 881 | if (!str) |
| 882 | goto fail; |
| 883 | n += PyUnicode_GET_SIZE(str); |
| 884 | /* Remember the str and switch to the next slot */ |
| 885 | *callresult++ = str; |
| 886 | break; |
| 887 | } |
| 888 | case 'R': |
| 889 | { |
| 890 | PyObject *obj = va_arg(count, PyObject *); |
| 891 | PyObject *repr; |
| 892 | assert(obj); |
| 893 | repr = PyObject_Repr(obj); |
| 894 | if (!repr) |
| 895 | goto fail; |
| 896 | n += PyUnicode_GET_SIZE(repr); |
| 897 | /* Remember the repr and switch to the next slot */ |
| 898 | *callresult++ = repr; |
| 899 | break; |
| 900 | } |
| 901 | case 'A': |
| 902 | { |
| 903 | PyObject *obj = va_arg(count, PyObject *); |
| 904 | PyObject *ascii; |
| 905 | assert(obj); |
| 906 | ascii = PyObject_ASCII(obj); |
| 907 | if (!ascii) |
| 908 | goto fail; |
| 909 | n += PyUnicode_GET_SIZE(ascii); |
| 910 | /* Remember the repr and switch to the next slot */ |
| 911 | *callresult++ = ascii; |
| 912 | break; |
| 913 | } |
| 914 | case 'p': |
| 915 | (void) va_arg(count, int); |
| 916 | /* maximum 64-bit pointer representation: |
| 917 | * 0xffffffffffffffff |
| 918 | * so 19 characters is enough. |
| 919 | * XXX I count 18 -- what's the extra for? |
| 920 | */ |
| 921 | n += 19; |
| 922 | break; |
| 923 | default: |
| 924 | /* if we stumble upon an unknown |
| 925 | formatting code, copy the rest of |
| 926 | the format string to the output |
| 927 | string. (we cannot just skip the |
| 928 | code, since there's no way to know |
| 929 | what's in the argument list) */ |
| 930 | n += strlen(p); |
| 931 | goto expand; |
| 932 | } |
| 933 | } else |
| 934 | n++; |
| 935 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 936 | expand: |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 937 | if (abuffersize > ITEM_BUFFER_LEN) { |
| 938 | /* add 1 for sprintf's trailing null byte */ |
| 939 | abuffer = PyObject_Malloc(abuffersize + 1); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 940 | if (!abuffer) { |
| 941 | PyErr_NoMemory(); |
| 942 | goto fail; |
| 943 | } |
| 944 | realbuffer = abuffer; |
| 945 | } |
| 946 | else |
| 947 | realbuffer = buffer; |
| 948 | /* step 4: fill the buffer */ |
| 949 | /* Since we've analyzed how much space we need for the worst case, |
| 950 | we don't have to resize the string. |
| 951 | There can be no errors beyond this point. */ |
| 952 | string = PyUnicode_FromUnicode(NULL, n); |
| 953 | if (!string) |
| 954 | goto fail; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 955 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 956 | s = PyUnicode_AS_UNICODE(string); |
| 957 | callresult = callresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 958 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 959 | for (f = format; *f; f++) { |
| 960 | if (*f == '%') { |
| 961 | const char* p = f++; |
| 962 | int longflag = 0; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 963 | int longlongflag = 0; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 964 | int size_tflag = 0; |
| 965 | zeropad = (*f == '0'); |
| 966 | /* parse the width.precision part */ |
| 967 | width = 0; |
| 968 | while (ISDIGIT((unsigned)*f)) |
| 969 | width = (width*10) + *f++ - '0'; |
| 970 | precision = 0; |
| 971 | if (*f == '.') { |
| 972 | f++; |
| 973 | while (ISDIGIT((unsigned)*f)) |
| 974 | precision = (precision*10) + *f++ - '0'; |
| 975 | } |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 976 | /* Handle %ld, %lu, %lld and %llu. */ |
| 977 | if (*f == 'l') { |
| 978 | if (f[1] == 'd' || f[1] == 'u') { |
| 979 | longflag = 1; |
| 980 | ++f; |
| 981 | } |
| 982 | #ifdef HAVE_LONG_LONG |
| 983 | else if (f[1] == 'l' && |
| 984 | (f[2] == 'd' || f[2] == 'u')) { |
| 985 | longlongflag = 1; |
| 986 | f += 2; |
| 987 | } |
| 988 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 989 | } |
| 990 | /* handle the size_t flag. */ |
| 991 | if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
| 992 | size_tflag = 1; |
| 993 | ++f; |
| 994 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 995 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 996 | switch (*f) { |
| 997 | case 'c': |
| 998 | *s++ = va_arg(vargs, int); |
| 999 | break; |
| 1000 | case 'd': |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1001 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 1002 | width, precision, 'd'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1003 | if (longflag) |
| 1004 | sprintf(realbuffer, fmt, va_arg(vargs, long)); |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1005 | #ifdef HAVE_LONG_LONG |
| 1006 | else if (longlongflag) |
| 1007 | sprintf(realbuffer, fmt, va_arg(vargs, PY_LONG_LONG)); |
| 1008 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1009 | else if (size_tflag) |
| 1010 | sprintf(realbuffer, fmt, va_arg(vargs, Py_ssize_t)); |
| 1011 | else |
| 1012 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 1013 | appendstring(realbuffer); |
| 1014 | break; |
| 1015 | case 'u': |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1016 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 1017 | width, precision, 'u'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1018 | if (longflag) |
| 1019 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned long)); |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1020 | #ifdef HAVE_LONG_LONG |
| 1021 | else if (longlongflag) |
| 1022 | sprintf(realbuffer, fmt, va_arg(vargs, |
| 1023 | unsigned PY_LONG_LONG)); |
| 1024 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1025 | else if (size_tflag) |
| 1026 | sprintf(realbuffer, fmt, va_arg(vargs, size_t)); |
| 1027 | else |
| 1028 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned int)); |
| 1029 | appendstring(realbuffer); |
| 1030 | break; |
| 1031 | case 'i': |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1032 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'i'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1033 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 1034 | appendstring(realbuffer); |
| 1035 | break; |
| 1036 | case 'x': |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1037 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1038 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 1039 | appendstring(realbuffer); |
| 1040 | break; |
| 1041 | case 's': |
| 1042 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1043 | /* unused, since we already have the result */ |
| 1044 | (void) va_arg(vargs, char *); |
| 1045 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(*callresult), |
| 1046 | PyUnicode_GET_SIZE(*callresult)); |
| 1047 | s += PyUnicode_GET_SIZE(*callresult); |
| 1048 | /* We're done with the unicode()/repr() => forget it */ |
| 1049 | Py_DECREF(*callresult); |
| 1050 | /* switch to next unicode()/repr() result */ |
| 1051 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1052 | break; |
| 1053 | } |
| 1054 | case 'U': |
| 1055 | { |
| 1056 | PyObject *obj = va_arg(vargs, PyObject *); |
| 1057 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 1058 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 1059 | s += size; |
| 1060 | break; |
| 1061 | } |
| 1062 | case 'V': |
| 1063 | { |
| 1064 | PyObject *obj = va_arg(vargs, PyObject *); |
| 1065 | const char *str = va_arg(vargs, const char *); |
| 1066 | if (obj) { |
| 1067 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 1068 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 1069 | s += size; |
| 1070 | } else { |
| 1071 | appendstring(str); |
| 1072 | } |
| 1073 | break; |
| 1074 | } |
| 1075 | case 'S': |
| 1076 | case 'R': |
| 1077 | { |
| 1078 | Py_UNICODE *ucopy; |
| 1079 | Py_ssize_t usize; |
| 1080 | Py_ssize_t upos; |
| 1081 | /* unused, since we already have the result */ |
| 1082 | (void) va_arg(vargs, PyObject *); |
| 1083 | ucopy = PyUnicode_AS_UNICODE(*callresult); |
| 1084 | usize = PyUnicode_GET_SIZE(*callresult); |
| 1085 | for (upos = 0; upos<usize;) |
| 1086 | *s++ = ucopy[upos++]; |
| 1087 | /* We're done with the unicode()/repr() => forget it */ |
| 1088 | Py_DECREF(*callresult); |
| 1089 | /* switch to next unicode()/repr() result */ |
| 1090 | ++callresult; |
| 1091 | break; |
| 1092 | } |
| 1093 | case 'p': |
| 1094 | sprintf(buffer, "%p", va_arg(vargs, void*)); |
| 1095 | /* %p is ill-defined: ensure leading 0x. */ |
| 1096 | if (buffer[1] == 'X') |
| 1097 | buffer[1] = 'x'; |
| 1098 | else if (buffer[1] != 'x') { |
| 1099 | memmove(buffer+2, buffer, strlen(buffer)+1); |
| 1100 | buffer[0] = '0'; |
| 1101 | buffer[1] = 'x'; |
| 1102 | } |
| 1103 | appendstring(buffer); |
| 1104 | break; |
| 1105 | case '%': |
| 1106 | *s++ = '%'; |
| 1107 | break; |
| 1108 | default: |
| 1109 | appendstring(p); |
| 1110 | goto end; |
| 1111 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 1112 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 1113 | else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1114 | *s++ = *f; |
| 1115 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1116 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1117 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1118 | if (callresults) |
| 1119 | PyObject_Free(callresults); |
| 1120 | if (abuffer) |
| 1121 | PyObject_Free(abuffer); |
| 1122 | PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string)); |
| 1123 | return string; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1124 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1125 | if (callresults) { |
| 1126 | PyObject **callresult2 = callresults; |
| 1127 | while (callresult2 < callresult) { |
| 1128 | Py_DECREF(*callresult2); |
| 1129 | ++callresult2; |
| 1130 | } |
| 1131 | PyObject_Free(callresults); |
| 1132 | } |
| 1133 | if (abuffer) |
| 1134 | PyObject_Free(abuffer); |
| 1135 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | #undef appendstring |
| 1139 | |
| 1140 | PyObject * |
| 1141 | PyUnicode_FromFormat(const char *format, ...) |
| 1142 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1143 | PyObject* ret; |
| 1144 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1145 | |
| 1146 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1147 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1148 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1149 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1150 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1151 | ret = PyUnicode_FromFormatV(format, vargs); |
| 1152 | va_end(vargs); |
| 1153 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1156 | static void |
| 1157 | unicode_aswidechar(PyUnicodeObject *unicode, |
| 1158 | wchar_t *w, |
| 1159 | Py_ssize_t size) |
| 1160 | { |
| 1161 | #if Py_UNICODE_SIZE == SIZEOF_WCHAR_T |
| 1162 | memcpy(w, unicode->str, size * sizeof(wchar_t)); |
| 1163 | #else |
| 1164 | register Py_UNICODE *u; |
| 1165 | register Py_ssize_t i; |
| 1166 | u = PyUnicode_AS_UNICODE(unicode); |
| 1167 | for (i = size; i > 0; i--) |
| 1168 | *w++ = *u++; |
| 1169 | #endif |
| 1170 | } |
| 1171 | |
| 1172 | Py_ssize_t |
| 1173 | PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
| 1174 | wchar_t *w, |
| 1175 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1176 | { |
| 1177 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1178 | PyErr_BadInternalCall(); |
| 1179 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1180 | } |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1181 | |
| 1182 | /* If possible, try to copy the 0-termination as well */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1183 | if (size > PyUnicode_GET_SIZE(unicode)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1184 | size = PyUnicode_GET_SIZE(unicode) + 1; |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1185 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1186 | unicode_aswidechar(unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1187 | |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1188 | if (size > PyUnicode_GET_SIZE(unicode)) |
| 1189 | return PyUnicode_GET_SIZE(unicode); |
| 1190 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1191 | return size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1192 | } |
| 1193 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1194 | wchar_t* |
| 1195 | PyUnicode_AsWideCharString(PyUnicodeObject *unicode, |
| 1196 | Py_ssize_t *size) |
| 1197 | { |
| 1198 | wchar_t* buffer; |
| 1199 | Py_ssize_t buflen; |
| 1200 | |
| 1201 | if (unicode == NULL) { |
| 1202 | PyErr_BadInternalCall(); |
| 1203 | return NULL; |
| 1204 | } |
| 1205 | |
| 1206 | if ((PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) < PyUnicode_GET_SIZE(unicode)) { |
| 1207 | PyErr_NoMemory(); |
| 1208 | return NULL; |
| 1209 | } |
| 1210 | |
| 1211 | buflen = PyUnicode_GET_SIZE(unicode) + 1; /* copy L'\0' */ |
| 1212 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 1213 | if (buffer == NULL) { |
| 1214 | PyErr_NoMemory(); |
| 1215 | return NULL; |
| 1216 | } |
| 1217 | unicode_aswidechar(unicode, buffer, buflen); |
| 1218 | return buffer; |
| 1219 | } |
| 1220 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1221 | #endif |
| 1222 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1223 | PyObject *PyUnicode_FromOrdinal(int ordinal) |
| 1224 | { |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1225 | Py_UNICODE s[2]; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1226 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1227 | if (ordinal < 0 || ordinal > 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1228 | PyErr_SetString(PyExc_ValueError, |
| 1229 | "chr() arg not in range(0x110000)"); |
| 1230 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1231 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1232 | |
| 1233 | #ifndef Py_UNICODE_WIDE |
| 1234 | if (ordinal > 0xffff) { |
| 1235 | ordinal -= 0x10000; |
| 1236 | s[0] = 0xD800 | (ordinal >> 10); |
| 1237 | s[1] = 0xDC00 | (ordinal & 0x3FF); |
| 1238 | return PyUnicode_FromUnicode(s, 2); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1239 | } |
| 1240 | #endif |
| 1241 | |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 1242 | s[0] = (Py_UNICODE)ordinal; |
| 1243 | return PyUnicode_FromUnicode(s, 1); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1246 | PyObject *PyUnicode_FromObject(register PyObject *obj) |
| 1247 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1248 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1249 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1250 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1251 | Py_INCREF(obj); |
| 1252 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1253 | } |
| 1254 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1255 | /* For a Unicode subtype that's not a Unicode object, |
| 1256 | return a true Unicode object with the same data. */ |
| 1257 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 1258 | PyUnicode_GET_SIZE(obj)); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1259 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1260 | PyErr_Format(PyExc_TypeError, |
| 1261 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1262 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1263 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1267 | const char *encoding, |
| 1268 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1269 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 1270 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1271 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1272 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1273 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1274 | PyErr_BadInternalCall(); |
| 1275 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1276 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1277 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 1278 | /* Decoding bytes objects is the most common case and should be fast */ |
| 1279 | if (PyBytes_Check(obj)) { |
| 1280 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 1281 | Py_INCREF(unicode_empty); |
| 1282 | v = (PyObject *) unicode_empty; |
| 1283 | } |
| 1284 | else { |
| 1285 | v = PyUnicode_Decode( |
| 1286 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 1287 | encoding, errors); |
| 1288 | } |
| 1289 | return v; |
| 1290 | } |
| 1291 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1292 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1293 | PyErr_SetString(PyExc_TypeError, |
| 1294 | "decoding str is not supported"); |
| 1295 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1296 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1297 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 1298 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 1299 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 1300 | PyErr_Format(PyExc_TypeError, |
| 1301 | "coercing to str: need bytes, bytearray " |
| 1302 | "or buffer-like object, %.80s found", |
| 1303 | Py_TYPE(obj)->tp_name); |
| 1304 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1305 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1306 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 1307 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1308 | Py_INCREF(unicode_empty); |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 1309 | v = (PyObject *) unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1310 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1311 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 1312 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 1313 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 1314 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1315 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 1318 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1319 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 1320 | 1 on success. */ |
| 1321 | static int |
| 1322 | normalize_encoding(const char *encoding, |
| 1323 | char *lower, |
| 1324 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1325 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1326 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 1327 | char *l; |
| 1328 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1329 | |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1330 | e = encoding; |
| 1331 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 1332 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1333 | while (*e) { |
| 1334 | if (l == l_end) |
| 1335 | return 0; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1336 | if (ISUPPER(*e)) { |
| 1337 | *l++ = TOLOWER(*e++); |
| 1338 | } |
| 1339 | else if (*e == '_') { |
| 1340 | *l++ = '-'; |
| 1341 | e++; |
| 1342 | } |
| 1343 | else { |
| 1344 | *l++ = *e++; |
| 1345 | } |
| 1346 | } |
| 1347 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1348 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | PyObject *PyUnicode_Decode(const char *s, |
| 1352 | Py_ssize_t size, |
| 1353 | const char *encoding, |
| 1354 | const char *errors) |
| 1355 | { |
| 1356 | PyObject *buffer = NULL, *unicode; |
| 1357 | Py_buffer info; |
| 1358 | char lower[11]; /* Enough for any encoding shortcut */ |
| 1359 | |
| 1360 | if (encoding == NULL) |
| 1361 | encoding = PyUnicode_GetDefaultEncoding(); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1362 | |
| 1363 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1364 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
| 1365 | if (strcmp(lower, "utf-8") == 0) |
| 1366 | return PyUnicode_DecodeUTF8(s, size, errors); |
| 1367 | else if ((strcmp(lower, "latin-1") == 0) || |
| 1368 | (strcmp(lower, "iso-8859-1") == 0)) |
| 1369 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1370 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1371 | else if (strcmp(lower, "mbcs") == 0) |
| 1372 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1373 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1374 | else if (strcmp(lower, "ascii") == 0) |
| 1375 | return PyUnicode_DecodeASCII(s, size, errors); |
| 1376 | else if (strcmp(lower, "utf-16") == 0) |
| 1377 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 1378 | else if (strcmp(lower, "utf-32") == 0) |
| 1379 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 1380 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1381 | |
| 1382 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1383 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 1384 | 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] | 1385 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 1386 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1387 | if (buffer == NULL) |
| 1388 | goto onError; |
| 1389 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 1390 | if (unicode == NULL) |
| 1391 | goto onError; |
| 1392 | if (!PyUnicode_Check(unicode)) { |
| 1393 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1394 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1395 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1396 | Py_DECREF(unicode); |
| 1397 | goto onError; |
| 1398 | } |
| 1399 | Py_DECREF(buffer); |
| 1400 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1401 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1402 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1403 | Py_XDECREF(buffer); |
| 1404 | return NULL; |
| 1405 | } |
| 1406 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1407 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 1408 | const char *encoding, |
| 1409 | const char *errors) |
| 1410 | { |
| 1411 | PyObject *v; |
| 1412 | |
| 1413 | if (!PyUnicode_Check(unicode)) { |
| 1414 | PyErr_BadArgument(); |
| 1415 | goto onError; |
| 1416 | } |
| 1417 | |
| 1418 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1419 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1420 | |
| 1421 | /* Decode via the codec registry */ |
| 1422 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1423 | if (v == NULL) |
| 1424 | goto onError; |
| 1425 | return v; |
| 1426 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1427 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1428 | return NULL; |
| 1429 | } |
| 1430 | |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1431 | PyObject *PyUnicode_AsDecodedUnicode(PyObject *unicode, |
| 1432 | const char *encoding, |
| 1433 | const char *errors) |
| 1434 | { |
| 1435 | PyObject *v; |
| 1436 | |
| 1437 | if (!PyUnicode_Check(unicode)) { |
| 1438 | PyErr_BadArgument(); |
| 1439 | goto onError; |
| 1440 | } |
| 1441 | |
| 1442 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1443 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1444 | |
| 1445 | /* Decode via the codec registry */ |
| 1446 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1447 | if (v == NULL) |
| 1448 | goto onError; |
| 1449 | if (!PyUnicode_Check(v)) { |
| 1450 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1451 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1452 | Py_TYPE(v)->tp_name); |
| 1453 | Py_DECREF(v); |
| 1454 | goto onError; |
| 1455 | } |
| 1456 | return v; |
| 1457 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1458 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1459 | return NULL; |
| 1460 | } |
| 1461 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1462 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1463 | Py_ssize_t size, |
| 1464 | const char *encoding, |
| 1465 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1466 | { |
| 1467 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1468 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1469 | unicode = PyUnicode_FromUnicode(s, size); |
| 1470 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1471 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1472 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 1473 | Py_DECREF(unicode); |
| 1474 | return v; |
| 1475 | } |
| 1476 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1477 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 1478 | const char *encoding, |
| 1479 | const char *errors) |
| 1480 | { |
| 1481 | PyObject *v; |
| 1482 | |
| 1483 | if (!PyUnicode_Check(unicode)) { |
| 1484 | PyErr_BadArgument(); |
| 1485 | goto onError; |
| 1486 | } |
| 1487 | |
| 1488 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1489 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1490 | |
| 1491 | /* Encode via the codec registry */ |
| 1492 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1493 | if (v == NULL) |
| 1494 | goto onError; |
| 1495 | return v; |
| 1496 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1497 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1498 | return NULL; |
| 1499 | } |
| 1500 | |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 1501 | PyObject *PyUnicode_EncodeFSDefault(PyObject *unicode) |
| 1502 | { |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 1503 | if (Py_FileSystemDefaultEncoding) { |
| 1504 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 1505 | if (strcmp(Py_FileSystemDefaultEncoding, "mbcs") == 0) |
| 1506 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 1507 | PyUnicode_GET_SIZE(unicode), |
| 1508 | NULL); |
| 1509 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 1510 | return PyUnicode_AsEncodedString(unicode, |
| 1511 | Py_FileSystemDefaultEncoding, |
| 1512 | "surrogateescape"); |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 1513 | } else |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 1514 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Victor Stinner | 3119ed7 | 2010-08-18 22:26:50 +0000 | [diff] [blame] | 1515 | PyUnicode_GET_SIZE(unicode), |
| 1516 | "surrogateescape"); |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1519 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 1520 | const char *encoding, |
| 1521 | const char *errors) |
| 1522 | { |
| 1523 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 1524 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1525 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1526 | if (!PyUnicode_Check(unicode)) { |
| 1527 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1528 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1529 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1530 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1531 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1532 | encoding = PyUnicode_GetDefaultEncoding(); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1533 | |
| 1534 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1535 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
| 1536 | if (strcmp(lower, "utf-8") == 0) |
| 1537 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 1538 | PyUnicode_GET_SIZE(unicode), |
| 1539 | errors); |
| 1540 | else if ((strcmp(lower, "latin-1") == 0) || |
| 1541 | (strcmp(lower, "iso-8859-1") == 0)) |
| 1542 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 1543 | PyUnicode_GET_SIZE(unicode), |
| 1544 | errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1545 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1546 | else if (strcmp(lower, "mbcs") == 0) |
| 1547 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 1548 | PyUnicode_GET_SIZE(unicode), |
| 1549 | errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1550 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1551 | else if (strcmp(lower, "ascii") == 0) |
| 1552 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 1553 | PyUnicode_GET_SIZE(unicode), |
| 1554 | errors); |
| 1555 | } |
Victor Stinner | 59e62db | 2010-05-15 13:14:32 +0000 | [diff] [blame] | 1556 | /* During bootstrap, we may need to find the encodings |
| 1557 | package, to load the file system encoding, and require the |
| 1558 | file system encoding in order to load the encodings |
| 1559 | package. |
Christian Heimes | 6a27efa | 2008-10-30 21:48:26 +0000 | [diff] [blame] | 1560 | |
Victor Stinner | 59e62db | 2010-05-15 13:14:32 +0000 | [diff] [blame] | 1561 | Break out of this dependency by assuming that the path to |
| 1562 | the encodings module is ASCII-only. XXX could try wcstombs |
| 1563 | instead, if the file system encoding is the locale's |
| 1564 | encoding. */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1565 | if (Py_FileSystemDefaultEncoding && |
Victor Stinner | 59e62db | 2010-05-15 13:14:32 +0000 | [diff] [blame] | 1566 | strcmp(encoding, Py_FileSystemDefaultEncoding) == 0 && |
| 1567 | !PyThreadState_GET()->interp->codecs_initialized) |
| 1568 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 1569 | PyUnicode_GET_SIZE(unicode), |
| 1570 | errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1571 | |
| 1572 | /* Encode via the codec registry */ |
| 1573 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1574 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1575 | return NULL; |
| 1576 | |
| 1577 | /* The normal path */ |
| 1578 | if (PyBytes_Check(v)) |
| 1579 | return v; |
| 1580 | |
| 1581 | /* 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] | 1582 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 1583 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1584 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 1585 | |
| 1586 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 1587 | "encoder %s returned bytearray instead of bytes", |
| 1588 | encoding); |
| 1589 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1590 | Py_DECREF(v); |
| 1591 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1592 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1593 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1594 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 1595 | Py_DECREF(v); |
| 1596 | return b; |
| 1597 | } |
| 1598 | |
| 1599 | PyErr_Format(PyExc_TypeError, |
| 1600 | "encoder did not return a bytes object (type=%.400s)", |
| 1601 | Py_TYPE(v)->tp_name); |
| 1602 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1603 | return NULL; |
| 1604 | } |
| 1605 | |
| 1606 | PyObject *PyUnicode_AsEncodedUnicode(PyObject *unicode, |
| 1607 | const char *encoding, |
| 1608 | const char *errors) |
| 1609 | { |
| 1610 | PyObject *v; |
| 1611 | |
| 1612 | if (!PyUnicode_Check(unicode)) { |
| 1613 | PyErr_BadArgument(); |
| 1614 | goto onError; |
| 1615 | } |
| 1616 | |
| 1617 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1618 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1619 | |
| 1620 | /* Encode via the codec registry */ |
| 1621 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1622 | if (v == NULL) |
| 1623 | goto onError; |
| 1624 | if (!PyUnicode_Check(v)) { |
| 1625 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1626 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1627 | Py_TYPE(v)->tp_name); |
| 1628 | Py_DECREF(v); |
| 1629 | goto onError; |
| 1630 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1631 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1632 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1633 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1634 | return NULL; |
| 1635 | } |
| 1636 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1637 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1638 | const char *errors) |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1639 | { |
| 1640 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1641 | if (v) |
| 1642 | return v; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1643 | if (errors != NULL) |
| 1644 | Py_FatalError("non-NULL encoding in _PyUnicode_AsDefaultEncodedString"); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1645 | v = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Guido van Rossum | 0661009 | 2007-08-16 21:02:22 +0000 | [diff] [blame] | 1646 | PyUnicode_GET_SIZE(unicode), |
| 1647 | NULL); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1648 | if (!v) |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1649 | return NULL; |
Guido van Rossum | e7a0d39 | 2007-07-12 07:53:00 +0000 | [diff] [blame] | 1650 | ((PyUnicodeObject *)unicode)->defenc = v; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1651 | return v; |
| 1652 | } |
| 1653 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1654 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1655 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1656 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1657 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 1658 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1659 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1660 | PyObject* |
| 1661 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 1662 | { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1663 | /* During the early bootstrapping process, Py_FileSystemDefaultEncoding |
| 1664 | can be undefined. If it is case, decode using UTF-8. The following assumes |
| 1665 | that Py_FileSystemDefaultEncoding is set to a built-in encoding during the |
| 1666 | bootstrapping process where the codecs aren't ready yet. |
| 1667 | */ |
| 1668 | if (Py_FileSystemDefaultEncoding) { |
| 1669 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1670 | if (strcmp(Py_FileSystemDefaultEncoding, "mbcs") == 0) { |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 1671 | return PyUnicode_DecodeMBCS(s, size, NULL); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1672 | } |
| 1673 | #elif defined(__APPLE__) |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1674 | if (strcmp(Py_FileSystemDefaultEncoding, "utf-8") == 0) { |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 1675 | return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1676 | } |
| 1677 | #endif |
| 1678 | return PyUnicode_Decode(s, size, |
| 1679 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 1680 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1681 | } |
| 1682 | else { |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 1683 | return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1684 | } |
| 1685 | } |
| 1686 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1687 | |
| 1688 | int |
| 1689 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 1690 | { |
| 1691 | PyObject *output = NULL; |
| 1692 | Py_ssize_t size; |
| 1693 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 1694 | if (arg == NULL) { |
| 1695 | Py_DECREF(*(PyObject**)addr); |
| 1696 | return 1; |
| 1697 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 1698 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1699 | output = arg; |
| 1700 | Py_INCREF(output); |
| 1701 | } |
| 1702 | else { |
| 1703 | arg = PyUnicode_FromObject(arg); |
| 1704 | if (!arg) |
| 1705 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 1706 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1707 | Py_DECREF(arg); |
| 1708 | if (!output) |
| 1709 | return 0; |
| 1710 | if (!PyBytes_Check(output)) { |
| 1711 | Py_DECREF(output); |
| 1712 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 1713 | return 0; |
| 1714 | } |
| 1715 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 1716 | size = PyBytes_GET_SIZE(output); |
| 1717 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1718 | if (size != strlen(data)) { |
| 1719 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 1720 | Py_DECREF(output); |
| 1721 | return 0; |
| 1722 | } |
| 1723 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 1724 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1725 | } |
| 1726 | |
| 1727 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 1728 | int |
| 1729 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 1730 | { |
| 1731 | PyObject *output = NULL; |
| 1732 | Py_ssize_t size; |
| 1733 | void *data; |
| 1734 | if (arg == NULL) { |
| 1735 | Py_DECREF(*(PyObject**)addr); |
| 1736 | return 1; |
| 1737 | } |
| 1738 | if (PyUnicode_Check(arg)) { |
| 1739 | output = arg; |
| 1740 | Py_INCREF(output); |
| 1741 | } |
| 1742 | else { |
| 1743 | arg = PyBytes_FromObject(arg); |
| 1744 | if (!arg) |
| 1745 | return 0; |
| 1746 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 1747 | PyBytes_GET_SIZE(arg)); |
| 1748 | Py_DECREF(arg); |
| 1749 | if (!output) |
| 1750 | return 0; |
| 1751 | if (!PyUnicode_Check(output)) { |
| 1752 | Py_DECREF(output); |
| 1753 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 1754 | return 0; |
| 1755 | } |
| 1756 | } |
| 1757 | size = PyUnicode_GET_SIZE(output); |
| 1758 | data = PyUnicode_AS_UNICODE(output); |
| 1759 | if (size != Py_UNICODE_strlen(data)) { |
| 1760 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 1761 | Py_DECREF(output); |
| 1762 | return 0; |
| 1763 | } |
| 1764 | *(PyObject**)addr = output; |
| 1765 | return Py_CLEANUP_SUPPORTED; |
| 1766 | } |
| 1767 | |
| 1768 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1769 | char* |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1770 | _PyUnicode_AsStringAndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1771 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1772 | PyObject *bytes; |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 1773 | if (!PyUnicode_Check(unicode)) { |
| 1774 | PyErr_BadArgument(); |
| 1775 | return NULL; |
| 1776 | } |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1777 | bytes = _PyUnicode_AsDefaultEncodedString(unicode, NULL); |
| 1778 | if (bytes == NULL) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1779 | return NULL; |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1780 | if (psize != NULL) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1781 | *psize = PyBytes_GET_SIZE(bytes); |
| 1782 | return PyBytes_AS_STRING(bytes); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
| 1785 | char* |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1786 | _PyUnicode_AsString(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1787 | { |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1788 | return _PyUnicode_AsStringAndSize(unicode, NULL); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1789 | } |
| 1790 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1791 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 1792 | { |
| 1793 | if (!PyUnicode_Check(unicode)) { |
| 1794 | PyErr_BadArgument(); |
| 1795 | goto onError; |
| 1796 | } |
| 1797 | return PyUnicode_AS_UNICODE(unicode); |
| 1798 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1799 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1800 | return NULL; |
| 1801 | } |
| 1802 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1803 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1804 | { |
| 1805 | if (!PyUnicode_Check(unicode)) { |
| 1806 | PyErr_BadArgument(); |
| 1807 | goto onError; |
| 1808 | } |
| 1809 | return PyUnicode_GET_SIZE(unicode); |
| 1810 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1811 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1812 | return -1; |
| 1813 | } |
| 1814 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 1815 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1816 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 1817 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1818 | } |
| 1819 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 1820 | /* create or adjust a UnicodeDecodeError */ |
| 1821 | static void |
| 1822 | make_decode_exception(PyObject **exceptionObject, |
| 1823 | const char *encoding, |
| 1824 | const char *input, Py_ssize_t length, |
| 1825 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 1826 | const char *reason) |
| 1827 | { |
| 1828 | if (*exceptionObject == NULL) { |
| 1829 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 1830 | encoding, input, length, startpos, endpos, reason); |
| 1831 | } |
| 1832 | else { |
| 1833 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 1834 | goto onError; |
| 1835 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 1836 | goto onError; |
| 1837 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 1838 | goto onError; |
| 1839 | } |
| 1840 | return; |
| 1841 | |
| 1842 | onError: |
| 1843 | Py_DECREF(*exceptionObject); |
| 1844 | *exceptionObject = NULL; |
| 1845 | } |
| 1846 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1847 | /* error handling callback helper: |
| 1848 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 1849 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1850 | and adjust various state variables. |
| 1851 | return 0 on success, -1 on error |
| 1852 | */ |
| 1853 | |
| 1854 | static |
| 1855 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1856 | const char *encoding, const char *reason, |
| 1857 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 1858 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 1859 | PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1860 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1861 | 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] | 1862 | |
| 1863 | PyObject *restuple = NULL; |
| 1864 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1865 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1866 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1867 | Py_ssize_t requiredsize; |
| 1868 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1869 | Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1870 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1871 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1872 | int res = -1; |
| 1873 | |
| 1874 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1875 | *errorHandler = PyCodec_LookupError(errors); |
| 1876 | if (*errorHandler == NULL) |
| 1877 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1878 | } |
| 1879 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 1880 | make_decode_exception(exceptionObject, |
| 1881 | encoding, |
| 1882 | *input, *inend - *input, |
| 1883 | *startinpos, *endinpos, |
| 1884 | reason); |
| 1885 | if (*exceptionObject == NULL) |
| 1886 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1887 | |
| 1888 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 1889 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1890 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1891 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 1892 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1893 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1894 | } |
| 1895 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1896 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1897 | |
| 1898 | /* Copy back the bytes variables, which might have been modified by the |
| 1899 | callback */ |
| 1900 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 1901 | if (!inputobj) |
| 1902 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1903 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1904 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1905 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1906 | *input = PyBytes_AS_STRING(inputobj); |
| 1907 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1908 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 1909 | /* we can DECREF safely, as the exception has another reference, |
| 1910 | so the object won't go away. */ |
| 1911 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1912 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1913 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1914 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1915 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1916 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 1917 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1918 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1919 | |
| 1920 | /* need more space? (at least enough for what we |
| 1921 | have+the replacement+the rest of the string (starting |
| 1922 | at the new input position), so we won't have to check space |
| 1923 | when there are no errors in the rest of the string) */ |
| 1924 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 1925 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 1926 | requiredsize = *outpos + repsize + insize-newpos; |
| 1927 | if (requiredsize > outsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1928 | if (requiredsize<2*outsize) |
| 1929 | requiredsize = 2*outsize; |
| 1930 | if (_PyUnicode_Resize(output, requiredsize) < 0) |
| 1931 | goto onError; |
| 1932 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1933 | } |
| 1934 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1935 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1936 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 1937 | *outptr += repsize; |
| 1938 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1939 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1940 | /* we made it! */ |
| 1941 | res = 0; |
| 1942 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1943 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1944 | Py_XDECREF(restuple); |
| 1945 | return res; |
| 1946 | } |
| 1947 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1948 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 1949 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1950 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 1951 | |
| 1952 | /* Three simple macros defining base-64. */ |
| 1953 | |
| 1954 | /* Is c a base-64 character? */ |
| 1955 | |
| 1956 | #define IS_BASE64(c) \ |
| 1957 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 1958 | ((c) >= 'a' && (c) <= 'z') || \ |
| 1959 | ((c) >= '0' && (c) <= '9') || \ |
| 1960 | (c) == '+' || (c) == '/') |
| 1961 | |
| 1962 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 1963 | |
| 1964 | #define FROM_BASE64(c) \ |
| 1965 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 1966 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 1967 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 1968 | (c) == '+' ? 62 : 63) |
| 1969 | |
| 1970 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 1971 | |
| 1972 | #define TO_BASE64(n) \ |
| 1973 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 1974 | |
| 1975 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 1976 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 1977 | * byte not decoding to itself is the + which begins a base64 |
| 1978 | * string. */ |
| 1979 | |
| 1980 | #define DECODE_DIRECT(c) \ |
| 1981 | ((c) <= 127 && (c) != '+') |
| 1982 | |
| 1983 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 1984 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 1985 | * the above). See RFC2152. This array identifies these different |
| 1986 | * sets: |
| 1987 | * 0 : "Set D" |
| 1988 | * alphanumeric and '(),-./:? |
| 1989 | * 1 : "Set O" |
| 1990 | * !"#$%&*;<=>@[]^_`{|} |
| 1991 | * 2 : "whitespace" |
| 1992 | * ht nl cr sp |
| 1993 | * 3 : special (must be base64 encoded) |
| 1994 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 1995 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1996 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1997 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1998 | char utf7_category[128] = { |
| 1999 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 2000 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 2001 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 2002 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 2003 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 2004 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 2005 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 2006 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 2007 | /* @ A B C D E F G H I J K L M N O */ |
| 2008 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2009 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 2010 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 2011 | /* ` a b c d e f g h i j k l m n o */ |
| 2012 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2013 | /* p q r s t u v w x y z { | } ~ del */ |
| 2014 | 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] | 2015 | }; |
| 2016 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2017 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 2018 | * answer depends on whether we are encoding set O as itself, and also |
| 2019 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 2020 | * clear that the answers to these questions vary between |
| 2021 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 2022 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2023 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 2024 | ((c) < 128 && (c) > 0 && \ |
| 2025 | ((utf7_category[(c)] == 0) || \ |
| 2026 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 2027 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2028 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2029 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2030 | Py_ssize_t size, |
| 2031 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2032 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2033 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 2034 | } |
| 2035 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2036 | /* The decoder. The only state we preserve is our read position, |
| 2037 | * i.e. how many characters we have consumed. So if we end in the |
| 2038 | * middle of a shift sequence we have to back off the read position |
| 2039 | * and the output to the beginning of the sequence, otherwise we lose |
| 2040 | * all the shift state (seen bits, number of bits seen, high |
| 2041 | * surrogate). */ |
| 2042 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2043 | PyObject *PyUnicode_DecodeUTF7Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2044 | Py_ssize_t size, |
| 2045 | const char *errors, |
| 2046 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2047 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2048 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2049 | Py_ssize_t startinpos; |
| 2050 | Py_ssize_t endinpos; |
| 2051 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2052 | const char *e; |
| 2053 | PyUnicodeObject *unicode; |
| 2054 | Py_UNICODE *p; |
| 2055 | const char *errmsg = ""; |
| 2056 | int inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2057 | Py_UNICODE *shiftOutStart; |
| 2058 | unsigned int base64bits = 0; |
| 2059 | unsigned long base64buffer = 0; |
| 2060 | Py_UNICODE surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2061 | PyObject *errorHandler = NULL; |
| 2062 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2063 | |
| 2064 | unicode = _PyUnicode_New(size); |
| 2065 | if (!unicode) |
| 2066 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2067 | if (size == 0) { |
| 2068 | if (consumed) |
| 2069 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2070 | return (PyObject *)unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2071 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2072 | |
| 2073 | p = unicode->str; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2074 | shiftOutStart = p; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2075 | e = s + size; |
| 2076 | |
| 2077 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2078 | Py_UNICODE ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2079 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 2080 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2081 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2082 | if (inShift) { /* in a base-64 section */ |
| 2083 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 2084 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 2085 | base64bits += 6; |
| 2086 | s++; |
| 2087 | if (base64bits >= 16) { |
| 2088 | /* we have enough bits for a UTF-16 value */ |
| 2089 | Py_UNICODE outCh = (Py_UNICODE) |
| 2090 | (base64buffer >> (base64bits-16)); |
| 2091 | base64bits -= 16; |
| 2092 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 2093 | if (surrogate) { |
| 2094 | /* expecting a second surrogate */ |
| 2095 | if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 2096 | #ifdef Py_UNICODE_WIDE |
| 2097 | *p++ = (((surrogate & 0x3FF)<<10) |
| 2098 | | (outCh & 0x3FF)) + 0x10000; |
| 2099 | #else |
| 2100 | *p++ = surrogate; |
| 2101 | *p++ = outCh; |
| 2102 | #endif |
| 2103 | surrogate = 0; |
| 2104 | } |
| 2105 | else { |
| 2106 | surrogate = 0; |
| 2107 | errmsg = "second surrogate missing"; |
| 2108 | goto utf7Error; |
| 2109 | } |
| 2110 | } |
| 2111 | else if (outCh >= 0xD800 && outCh <= 0xDBFF) { |
| 2112 | /* first surrogate */ |
| 2113 | surrogate = outCh; |
| 2114 | } |
| 2115 | else if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 2116 | errmsg = "unexpected second surrogate"; |
| 2117 | goto utf7Error; |
| 2118 | } |
| 2119 | else { |
| 2120 | *p++ = outCh; |
| 2121 | } |
| 2122 | } |
| 2123 | } |
| 2124 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2125 | inShift = 0; |
| 2126 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2127 | if (surrogate) { |
| 2128 | errmsg = "second surrogate missing at end of shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2129 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2130 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2131 | if (base64bits > 0) { /* left-over bits */ |
| 2132 | if (base64bits >= 6) { |
| 2133 | /* We've seen at least one base-64 character */ |
| 2134 | errmsg = "partial character in shift sequence"; |
| 2135 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2136 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2137 | else { |
| 2138 | /* Some bits remain; they should be zero */ |
| 2139 | if (base64buffer != 0) { |
| 2140 | errmsg = "non-zero padding bits in shift sequence"; |
| 2141 | goto utf7Error; |
| 2142 | } |
| 2143 | } |
| 2144 | } |
| 2145 | if (ch != '-') { |
| 2146 | /* '-' is absorbed; other terminating |
| 2147 | characters are preserved */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2148 | *p++ = ch; |
| 2149 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2150 | } |
| 2151 | } |
| 2152 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2153 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2154 | s++; /* consume '+' */ |
| 2155 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2156 | s++; |
| 2157 | *p++ = '+'; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2158 | } |
| 2159 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2160 | inShift = 1; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2161 | shiftOutStart = p; |
| 2162 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2163 | } |
| 2164 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2165 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2166 | *p++ = ch; |
| 2167 | s++; |
| 2168 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2169 | else { |
| 2170 | startinpos = s-starts; |
| 2171 | s++; |
| 2172 | errmsg = "unexpected special character"; |
| 2173 | goto utf7Error; |
| 2174 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2175 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2176 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2177 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2178 | endinpos = s-starts; |
| 2179 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2180 | errors, &errorHandler, |
| 2181 | "utf7", errmsg, |
| 2182 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2183 | &unicode, &outpos, &p)) |
| 2184 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2185 | } |
| 2186 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2187 | /* end of string */ |
| 2188 | |
| 2189 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 2190 | /* if we're in an inconsistent state, that's an error */ |
| 2191 | if (surrogate || |
| 2192 | (base64bits >= 6) || |
| 2193 | (base64bits > 0 && base64buffer != 0)) { |
| 2194 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2195 | endinpos = size; |
| 2196 | if (unicode_decode_call_errorhandler( |
| 2197 | errors, &errorHandler, |
| 2198 | "utf7", "unterminated shift sequence", |
| 2199 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2200 | &unicode, &outpos, &p)) |
| 2201 | goto onError; |
| 2202 | if (s < e) |
| 2203 | goto restart; |
| 2204 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2205 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2206 | |
| 2207 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2208 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2209 | if (inShift) { |
| 2210 | p = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2211 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2212 | } |
| 2213 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2214 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2215 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2216 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2217 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2218 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2219 | goto onError; |
| 2220 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2221 | Py_XDECREF(errorHandler); |
| 2222 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2223 | return (PyObject *)unicode; |
| 2224 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2225 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2226 | Py_XDECREF(errorHandler); |
| 2227 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2228 | Py_DECREF(unicode); |
| 2229 | return NULL; |
| 2230 | } |
| 2231 | |
| 2232 | |
| 2233 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2234 | Py_ssize_t size, |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2235 | int base64SetO, |
| 2236 | int base64WhiteSpace, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2237 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2238 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2239 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2240 | /* It might be possible to tighten this worst case */ |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 2241 | Py_ssize_t allocated = 8 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2242 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2243 | Py_ssize_t i = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2244 | unsigned int base64bits = 0; |
| 2245 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2246 | char * out; |
| 2247 | char * start; |
| 2248 | |
| 2249 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2250 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2251 | |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 2252 | if (allocated / 8 != size) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2253 | return PyErr_NoMemory(); |
| 2254 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2255 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2256 | if (v == NULL) |
| 2257 | return NULL; |
| 2258 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2259 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2260 | for (;i < size; ++i) { |
| 2261 | Py_UNICODE ch = s[i]; |
| 2262 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2263 | if (inShift) { |
| 2264 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 2265 | /* shifting out */ |
| 2266 | if (base64bits) { /* output remaining bits */ |
| 2267 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 2268 | base64buffer = 0; |
| 2269 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2270 | } |
| 2271 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2272 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 2273 | so no '-' is required, except if the character is itself a '-' */ |
| 2274 | if (IS_BASE64(ch) || ch == '-') { |
| 2275 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2276 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2277 | *out++ = (char) ch; |
| 2278 | } |
| 2279 | else { |
| 2280 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2281 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2282 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2283 | else { /* not in a shift sequence */ |
| 2284 | if (ch == '+') { |
| 2285 | *out++ = '+'; |
| 2286 | *out++ = '-'; |
| 2287 | } |
| 2288 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 2289 | *out++ = (char) ch; |
| 2290 | } |
| 2291 | else { |
| 2292 | *out++ = '+'; |
| 2293 | inShift = 1; |
| 2294 | goto encode_char; |
| 2295 | } |
| 2296 | } |
| 2297 | continue; |
| 2298 | encode_char: |
| 2299 | #ifdef Py_UNICODE_WIDE |
| 2300 | if (ch >= 0x10000) { |
| 2301 | /* code first surrogate */ |
| 2302 | base64bits += 16; |
| 2303 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 2304 | while (base64bits >= 6) { |
| 2305 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 2306 | base64bits -= 6; |
| 2307 | } |
| 2308 | /* prepare second surrogate */ |
| 2309 | ch = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2310 | } |
| 2311 | #endif |
| 2312 | base64bits += 16; |
| 2313 | base64buffer = (base64buffer << 16) | ch; |
| 2314 | while (base64bits >= 6) { |
| 2315 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 2316 | base64bits -= 6; |
| 2317 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 2318 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2319 | if (base64bits) |
| 2320 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 2321 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2322 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2323 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 2324 | return NULL; |
| 2325 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2326 | } |
| 2327 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2328 | #undef IS_BASE64 |
| 2329 | #undef FROM_BASE64 |
| 2330 | #undef TO_BASE64 |
| 2331 | #undef DECODE_DIRECT |
| 2332 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2333 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2334 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 2335 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2336 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2337 | char utf8_code_length[256] = { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2338 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 2339 | illegal prefix. See RFC 3629 for details */ |
| 2340 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 2341 | 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] | 2342 | 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] | 2343 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2344 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2345 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2346 | 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] | 2347 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 2348 | 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] | 2349 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2350 | 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] | 2351 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 2352 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 2353 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 2354 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 2355 | 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] | 2356 | }; |
| 2357 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2358 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2359 | Py_ssize_t size, |
| 2360 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2361 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2362 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 2363 | } |
| 2364 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2365 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 2366 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 2367 | |
| 2368 | /* Mask to quickly check whether a C 'long' contains a |
| 2369 | non-ASCII, UTF8-encoded char. */ |
| 2370 | #if (SIZEOF_LONG == 8) |
| 2371 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 2372 | #elif (SIZEOF_LONG == 4) |
| 2373 | # define ASCII_CHAR_MASK 0x80808080L |
| 2374 | #else |
| 2375 | # error C 'long' size should be either 4 or 8! |
| 2376 | #endif |
| 2377 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2378 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2379 | Py_ssize_t size, |
| 2380 | const char *errors, |
| 2381 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2382 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2383 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2384 | int n; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2385 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2386 | Py_ssize_t startinpos; |
| 2387 | Py_ssize_t endinpos; |
| 2388 | Py_ssize_t outpos; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2389 | const char *e, *aligned_end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2390 | PyUnicodeObject *unicode; |
| 2391 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2392 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2393 | PyObject *errorHandler = NULL; |
| 2394 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2395 | |
| 2396 | /* Note: size will always be longer than the resulting Unicode |
| 2397 | character count */ |
| 2398 | unicode = _PyUnicode_New(size); |
| 2399 | if (!unicode) |
| 2400 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2401 | if (size == 0) { |
| 2402 | if (consumed) |
| 2403 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2404 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2405 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2406 | |
| 2407 | /* Unpack UTF-8 encoded data */ |
| 2408 | p = unicode->str; |
| 2409 | e = s + size; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2410 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2411 | |
| 2412 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2413 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2414 | |
| 2415 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2416 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 2417 | input will consist of an overwhelming majority of ASCII |
| 2418 | characters, we try to optimize for this case by checking |
| 2419 | as many characters as a C 'long' can contain. |
| 2420 | First, check if we can do an aligned read, as most CPUs have |
| 2421 | a penalty for unaligned reads. |
| 2422 | */ |
| 2423 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 2424 | /* Help register allocation */ |
| 2425 | register const char *_s = s; |
| 2426 | register Py_UNICODE *_p = p; |
| 2427 | while (_s < aligned_end) { |
| 2428 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 2429 | and do a fast unrolled copy if it only contains ASCII |
| 2430 | characters. */ |
| 2431 | unsigned long data = *(unsigned long *) _s; |
| 2432 | if (data & ASCII_CHAR_MASK) |
| 2433 | break; |
| 2434 | _p[0] = (unsigned char) _s[0]; |
| 2435 | _p[1] = (unsigned char) _s[1]; |
| 2436 | _p[2] = (unsigned char) _s[2]; |
| 2437 | _p[3] = (unsigned char) _s[3]; |
| 2438 | #if (SIZEOF_LONG == 8) |
| 2439 | _p[4] = (unsigned char) _s[4]; |
| 2440 | _p[5] = (unsigned char) _s[5]; |
| 2441 | _p[6] = (unsigned char) _s[6]; |
| 2442 | _p[7] = (unsigned char) _s[7]; |
| 2443 | #endif |
| 2444 | _s += SIZEOF_LONG; |
| 2445 | _p += SIZEOF_LONG; |
| 2446 | } |
| 2447 | s = _s; |
| 2448 | p = _p; |
| 2449 | if (s == e) |
| 2450 | break; |
| 2451 | ch = (unsigned char)*s; |
| 2452 | } |
| 2453 | } |
| 2454 | |
| 2455 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2456 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2457 | s++; |
| 2458 | continue; |
| 2459 | } |
| 2460 | |
| 2461 | n = utf8_code_length[ch]; |
| 2462 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2463 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2464 | if (consumed) |
| 2465 | break; |
| 2466 | else { |
| 2467 | errmsg = "unexpected end of data"; |
| 2468 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2469 | endinpos = startinpos+1; |
| 2470 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 2471 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2472 | goto utf8Error; |
| 2473 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2474 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2475 | |
| 2476 | switch (n) { |
| 2477 | |
| 2478 | case 0: |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2479 | errmsg = "invalid start byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2480 | startinpos = s-starts; |
| 2481 | endinpos = startinpos+1; |
| 2482 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2483 | |
| 2484 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2485 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2486 | startinpos = s-starts; |
| 2487 | endinpos = startinpos+1; |
| 2488 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2489 | |
| 2490 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2491 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2492 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2493 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2494 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2495 | goto utf8Error; |
| 2496 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2497 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2498 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 2499 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2500 | break; |
| 2501 | |
| 2502 | case 3: |
Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 2503 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 2504 | will result in surrogates in range d800-dfff. Surrogates are |
| 2505 | not valid UTF-8 so they are rejected. |
| 2506 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 2507 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2508 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2509 | (s[2] & 0xc0) != 0x80 || |
| 2510 | ((unsigned char)s[0] == 0xE0 && |
| 2511 | (unsigned char)s[1] < 0xA0) || |
| 2512 | ((unsigned char)s[0] == 0xED && |
| 2513 | (unsigned char)s[1] > 0x9F)) { |
| 2514 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2515 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2516 | endinpos = startinpos + 1; |
| 2517 | |
| 2518 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 2519 | continuation byte is s[2], so increment endinpos by 1, |
| 2520 | if not, s[1] is invalid and endinpos doesn't need to |
| 2521 | be incremented. */ |
| 2522 | if ((s[1] & 0xC0) == 0x80) |
| 2523 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2524 | goto utf8Error; |
| 2525 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2526 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2527 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
| 2528 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2529 | break; |
| 2530 | |
| 2531 | case 4: |
| 2532 | if ((s[1] & 0xc0) != 0x80 || |
| 2533 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2534 | (s[3] & 0xc0) != 0x80 || |
| 2535 | ((unsigned char)s[0] == 0xF0 && |
| 2536 | (unsigned char)s[1] < 0x90) || |
| 2537 | ((unsigned char)s[0] == 0xF4 && |
| 2538 | (unsigned char)s[1] > 0x8F)) { |
| 2539 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2540 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2541 | endinpos = startinpos + 1; |
| 2542 | if ((s[1] & 0xC0) == 0x80) { |
| 2543 | endinpos++; |
| 2544 | if ((s[2] & 0xC0) == 0x80) |
| 2545 | endinpos++; |
| 2546 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2547 | goto utf8Error; |
| 2548 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2549 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2550 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 2551 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 2552 | |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2553 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2554 | *p++ = (Py_UNICODE)ch; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2555 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2556 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2557 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2558 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 2559 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2560 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2561 | /* high surrogate = top 10 bits added to D800 */ |
| 2562 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2563 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2564 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2565 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2566 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2567 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2568 | } |
| 2569 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2570 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2571 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2572 | utf8Error: |
| 2573 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2574 | if (unicode_decode_call_errorhandler( |
| 2575 | errors, &errorHandler, |
| 2576 | "utf8", errmsg, |
| 2577 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2578 | &unicode, &outpos, &p)) |
| 2579 | goto onError; |
| 2580 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2581 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2582 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2583 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2584 | |
| 2585 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2586 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2587 | goto onError; |
| 2588 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2589 | Py_XDECREF(errorHandler); |
| 2590 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2591 | return (PyObject *)unicode; |
| 2592 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2593 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2594 | Py_XDECREF(errorHandler); |
| 2595 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2596 | Py_DECREF(unicode); |
| 2597 | return NULL; |
| 2598 | } |
| 2599 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2600 | #undef ASCII_CHAR_MASK |
| 2601 | |
| 2602 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2603 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 2604 | and allocate exactly as much space needed at the end. Else allocate the |
| 2605 | maximum possible needed (4 result bytes per Unicode character), and return |
| 2606 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2607 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 2608 | PyObject * |
| 2609 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2610 | Py_ssize_t size, |
| 2611 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2612 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2613 | #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] | 2614 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2615 | Py_ssize_t i; /* index into s of next input byte */ |
| 2616 | PyObject *result; /* result string object */ |
| 2617 | char *p; /* next free byte in output buffer */ |
| 2618 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 2619 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2620 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2621 | PyObject *errorHandler = NULL; |
| 2622 | PyObject *exc = NULL; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2623 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2624 | assert(s != NULL); |
| 2625 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2626 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2627 | if (size <= MAX_SHORT_UNICHARS) { |
| 2628 | /* Write into the stack buffer; nallocated can't overflow. |
| 2629 | * At the end, we'll allocate exactly as much heap space as it |
| 2630 | * turns out we need. |
| 2631 | */ |
| 2632 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2633 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2634 | p = stackbuf; |
| 2635 | } |
| 2636 | else { |
| 2637 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 2638 | nallocated = size * 4; |
| 2639 | if (nallocated / 4 != size) /* overflow! */ |
| 2640 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2641 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2642 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2643 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2644 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2645 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2646 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2647 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2648 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2649 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2650 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2651 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2652 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2653 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2654 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2655 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 2656 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 2657 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2658 | } else if (0xD800 <= ch && ch <= 0xDFFF) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2659 | #ifndef Py_UNICODE_WIDE |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2660 | /* Special case: check for high and low surrogate */ |
| 2661 | if (ch <= 0xDBFF && i != size && 0xDC00 <= s[i] && s[i] <= 0xDFFF) { |
| 2662 | Py_UCS4 ch2 = s[i]; |
| 2663 | /* Combine the two surrogates to form a UCS4 value */ |
| 2664 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
| 2665 | i++; |
| 2666 | |
| 2667 | /* Encode UCS4 Unicode ordinals */ |
| 2668 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2669 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2670 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2671 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2672 | } else { |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 2673 | #endif |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2674 | Py_ssize_t newpos; |
| 2675 | PyObject *rep; |
| 2676 | Py_ssize_t repsize, k; |
| 2677 | rep = unicode_encode_call_errorhandler |
| 2678 | (errors, &errorHandler, "utf-8", "surrogates not allowed", |
| 2679 | s, size, &exc, i-1, i, &newpos); |
| 2680 | if (!rep) |
| 2681 | goto error; |
| 2682 | |
| 2683 | if (PyBytes_Check(rep)) |
| 2684 | repsize = PyBytes_GET_SIZE(rep); |
| 2685 | else |
| 2686 | repsize = PyUnicode_GET_SIZE(rep); |
| 2687 | |
| 2688 | if (repsize > 4) { |
| 2689 | Py_ssize_t offset; |
| 2690 | |
| 2691 | if (result == NULL) |
| 2692 | offset = p - stackbuf; |
| 2693 | else |
| 2694 | offset = p - PyBytes_AS_STRING(result); |
| 2695 | |
| 2696 | if (nallocated > PY_SSIZE_T_MAX - repsize + 4) { |
| 2697 | /* integer overflow */ |
| 2698 | PyErr_NoMemory(); |
| 2699 | goto error; |
| 2700 | } |
| 2701 | nallocated += repsize - 4; |
| 2702 | if (result != NULL) { |
| 2703 | if (_PyBytes_Resize(&result, nallocated) < 0) |
| 2704 | goto error; |
| 2705 | } else { |
| 2706 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
| 2707 | if (result == NULL) |
| 2708 | goto error; |
| 2709 | Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset); |
| 2710 | } |
| 2711 | p = PyBytes_AS_STRING(result) + offset; |
| 2712 | } |
| 2713 | |
| 2714 | if (PyBytes_Check(rep)) { |
| 2715 | char *prep = PyBytes_AS_STRING(rep); |
| 2716 | for(k = repsize; k > 0; k--) |
| 2717 | *p++ = *prep++; |
| 2718 | } else /* rep is unicode */ { |
| 2719 | Py_UNICODE *prep = PyUnicode_AS_UNICODE(rep); |
| 2720 | Py_UNICODE c; |
| 2721 | |
| 2722 | for(k=0; k<repsize; k++) { |
| 2723 | c = prep[k]; |
| 2724 | if (0x80 <= c) { |
| 2725 | raise_encode_exception(&exc, "utf-8", s, size, |
| 2726 | i-1, i, "surrogates not allowed"); |
| 2727 | goto error; |
| 2728 | } |
| 2729 | *p++ = (char)prep[k]; |
| 2730 | } |
| 2731 | } |
| 2732 | Py_DECREF(rep); |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 2733 | #ifndef Py_UNICODE_WIDE |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2734 | } |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 2735 | #endif |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2736 | } else if (ch < 0x10000) { |
| 2737 | *p++ = (char)(0xe0 | (ch >> 12)); |
| 2738 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2739 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2740 | } else /* ch >= 0x10000 */ { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2741 | /* Encode UCS4 Unicode ordinals */ |
| 2742 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2743 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 2744 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2745 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2746 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2747 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 2748 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2749 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2750 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2751 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2752 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2753 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2754 | } |
| 2755 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 2756 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2757 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2758 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2759 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2760 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2761 | Py_XDECREF(errorHandler); |
| 2762 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2763 | return result; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2764 | error: |
| 2765 | Py_XDECREF(errorHandler); |
| 2766 | Py_XDECREF(exc); |
| 2767 | Py_XDECREF(result); |
| 2768 | return NULL; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2769 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2770 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2771 | } |
| 2772 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2773 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 2774 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2775 | if (!PyUnicode_Check(unicode)) { |
| 2776 | PyErr_BadArgument(); |
| 2777 | return NULL; |
| 2778 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 2779 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2780 | PyUnicode_GET_SIZE(unicode), |
| 2781 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2782 | } |
| 2783 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2784 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 2785 | |
| 2786 | PyObject * |
| 2787 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2788 | Py_ssize_t size, |
| 2789 | const char *errors, |
| 2790 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2791 | { |
| 2792 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 2793 | } |
| 2794 | |
| 2795 | PyObject * |
| 2796 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2797 | Py_ssize_t size, |
| 2798 | const char *errors, |
| 2799 | int *byteorder, |
| 2800 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2801 | { |
| 2802 | const char *starts = s; |
| 2803 | Py_ssize_t startinpos; |
| 2804 | Py_ssize_t endinpos; |
| 2805 | Py_ssize_t outpos; |
| 2806 | PyUnicodeObject *unicode; |
| 2807 | Py_UNICODE *p; |
| 2808 | #ifndef Py_UNICODE_WIDE |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 2809 | int pairs = 0; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 2810 | const unsigned char *qq; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2811 | #else |
| 2812 | const int pairs = 0; |
| 2813 | #endif |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 2814 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2815 | int bo = 0; /* assume native ordering by default */ |
| 2816 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2817 | /* Offsets from q for retrieving bytes in the right order. */ |
| 2818 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2819 | int iorder[] = {0, 1, 2, 3}; |
| 2820 | #else |
| 2821 | int iorder[] = {3, 2, 1, 0}; |
| 2822 | #endif |
| 2823 | PyObject *errorHandler = NULL; |
| 2824 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 2825 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2826 | q = (unsigned char *)s; |
| 2827 | e = q + size; |
| 2828 | |
| 2829 | if (byteorder) |
| 2830 | bo = *byteorder; |
| 2831 | |
| 2832 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2833 | byte order setting accordingly. In native mode, the leading BOM |
| 2834 | mark is skipped, in all other modes, it is copied to the output |
| 2835 | stream as-is (giving a ZWNBSP character). */ |
| 2836 | if (bo == 0) { |
| 2837 | if (size >= 4) { |
| 2838 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2839 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2840 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2841 | if (bom == 0x0000FEFF) { |
| 2842 | q += 4; |
| 2843 | bo = -1; |
| 2844 | } |
| 2845 | else if (bom == 0xFFFE0000) { |
| 2846 | q += 4; |
| 2847 | bo = 1; |
| 2848 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2849 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2850 | if (bom == 0x0000FEFF) { |
| 2851 | q += 4; |
| 2852 | bo = 1; |
| 2853 | } |
| 2854 | else if (bom == 0xFFFE0000) { |
| 2855 | q += 4; |
| 2856 | bo = -1; |
| 2857 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2858 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2859 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2860 | } |
| 2861 | |
| 2862 | if (bo == -1) { |
| 2863 | /* force LE */ |
| 2864 | iorder[0] = 0; |
| 2865 | iorder[1] = 1; |
| 2866 | iorder[2] = 2; |
| 2867 | iorder[3] = 3; |
| 2868 | } |
| 2869 | else if (bo == 1) { |
| 2870 | /* force BE */ |
| 2871 | iorder[0] = 3; |
| 2872 | iorder[1] = 2; |
| 2873 | iorder[2] = 1; |
| 2874 | iorder[3] = 0; |
| 2875 | } |
| 2876 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 2877 | /* On narrow builds we split characters outside the BMP into two |
| 2878 | codepoints => count how much extra space we need. */ |
| 2879 | #ifndef Py_UNICODE_WIDE |
| 2880 | for (qq = q; qq < e; qq += 4) |
| 2881 | if (qq[iorder[2]] != 0 || qq[iorder[3]] != 0) |
| 2882 | pairs++; |
| 2883 | #endif |
| 2884 | |
| 2885 | /* This might be one to much, because of a BOM */ |
| 2886 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 2887 | if (!unicode) |
| 2888 | return NULL; |
| 2889 | if (size == 0) |
| 2890 | return (PyObject *)unicode; |
| 2891 | |
| 2892 | /* Unpack UTF-32 encoded data */ |
| 2893 | p = unicode->str; |
| 2894 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2895 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2896 | Py_UCS4 ch; |
| 2897 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 2898 | if (e-q<4) { |
| 2899 | if (consumed) |
| 2900 | break; |
| 2901 | errmsg = "truncated data"; |
| 2902 | startinpos = ((const char *)q)-starts; |
| 2903 | endinpos = ((const char *)e)-starts; |
| 2904 | goto utf32Error; |
| 2905 | /* The remaining input chars are ignored if the callback |
| 2906 | chooses to skip the input */ |
| 2907 | } |
| 2908 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 2909 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2910 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2911 | if (ch >= 0x110000) |
| 2912 | { |
| 2913 | errmsg = "codepoint not in range(0x110000)"; |
| 2914 | startinpos = ((const char *)q)-starts; |
| 2915 | endinpos = startinpos+4; |
| 2916 | goto utf32Error; |
| 2917 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2918 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2919 | if (ch >= 0x10000) |
| 2920 | { |
| 2921 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 2922 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2923 | } |
| 2924 | else |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2925 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2926 | *p++ = ch; |
| 2927 | q += 4; |
| 2928 | continue; |
| 2929 | utf32Error: |
| 2930 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2931 | if (unicode_decode_call_errorhandler( |
| 2932 | errors, &errorHandler, |
| 2933 | "utf32", errmsg, |
| 2934 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
| 2935 | &unicode, &outpos, &p)) |
| 2936 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2937 | } |
| 2938 | |
| 2939 | if (byteorder) |
| 2940 | *byteorder = bo; |
| 2941 | |
| 2942 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2943 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2944 | |
| 2945 | /* Adjust length */ |
| 2946 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
| 2947 | goto onError; |
| 2948 | |
| 2949 | Py_XDECREF(errorHandler); |
| 2950 | Py_XDECREF(exc); |
| 2951 | return (PyObject *)unicode; |
| 2952 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2953 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2954 | Py_DECREF(unicode); |
| 2955 | Py_XDECREF(errorHandler); |
| 2956 | Py_XDECREF(exc); |
| 2957 | return NULL; |
| 2958 | } |
| 2959 | |
| 2960 | PyObject * |
| 2961 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2962 | Py_ssize_t size, |
| 2963 | const char *errors, |
| 2964 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2965 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2966 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2967 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2968 | Py_ssize_t nsize, bytesize; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2969 | #ifndef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2970 | Py_ssize_t i, pairs; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2971 | #else |
| 2972 | const int pairs = 0; |
| 2973 | #endif |
| 2974 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2975 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2976 | int iorder[] = {0, 1, 2, 3}; |
| 2977 | #else |
| 2978 | int iorder[] = {3, 2, 1, 0}; |
| 2979 | #endif |
| 2980 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2981 | #define STORECHAR(CH) \ |
| 2982 | do { \ |
| 2983 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 2984 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 2985 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 2986 | p[iorder[0]] = (CH) & 0xff; \ |
| 2987 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2988 | } while(0) |
| 2989 | |
| 2990 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 2991 | so we need less space. */ |
| 2992 | #ifndef Py_UNICODE_WIDE |
| 2993 | for (i = pairs = 0; i < size-1; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2994 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 2995 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 2996 | pairs++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2997 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2998 | nsize = (size - pairs + (byteorder == 0)); |
| 2999 | bytesize = nsize * 4; |
| 3000 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3001 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3002 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3003 | if (v == NULL) |
| 3004 | return NULL; |
| 3005 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3006 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3007 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3008 | STORECHAR(0xFEFF); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3009 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3010 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3011 | |
| 3012 | if (byteorder == -1) { |
| 3013 | /* force LE */ |
| 3014 | iorder[0] = 0; |
| 3015 | iorder[1] = 1; |
| 3016 | iorder[2] = 2; |
| 3017 | iorder[3] = 3; |
| 3018 | } |
| 3019 | else if (byteorder == 1) { |
| 3020 | /* force BE */ |
| 3021 | iorder[0] = 3; |
| 3022 | iorder[1] = 2; |
| 3023 | iorder[2] = 1; |
| 3024 | iorder[3] = 0; |
| 3025 | } |
| 3026 | |
| 3027 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3028 | Py_UCS4 ch = *s++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3029 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3030 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 3031 | Py_UCS4 ch2 = *s; |
| 3032 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 3033 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 3034 | s++; |
| 3035 | size--; |
| 3036 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3037 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3038 | #endif |
| 3039 | STORECHAR(ch); |
| 3040 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3041 | |
| 3042 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3043 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3044 | #undef STORECHAR |
| 3045 | } |
| 3046 | |
| 3047 | PyObject *PyUnicode_AsUTF32String(PyObject *unicode) |
| 3048 | { |
| 3049 | if (!PyUnicode_Check(unicode)) { |
| 3050 | PyErr_BadArgument(); |
| 3051 | return NULL; |
| 3052 | } |
| 3053 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3054 | PyUnicode_GET_SIZE(unicode), |
| 3055 | NULL, |
| 3056 | 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3057 | } |
| 3058 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3059 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 3060 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3061 | PyObject * |
| 3062 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3063 | Py_ssize_t size, |
| 3064 | const char *errors, |
| 3065 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3066 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3067 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 3068 | } |
| 3069 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3070 | /* Two masks for fast checking of whether a C 'long' may contain |
| 3071 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 3072 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 3073 | rare in most input. |
| 3074 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 3075 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3076 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3077 | #if (SIZEOF_LONG == 8) |
| 3078 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 3079 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 3080 | #elif (SIZEOF_LONG == 4) |
| 3081 | # define FAST_CHAR_MASK 0x80008000L |
| 3082 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 3083 | #else |
| 3084 | # error C 'long' size should be either 4 or 8! |
| 3085 | #endif |
| 3086 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3087 | PyObject * |
| 3088 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3089 | Py_ssize_t size, |
| 3090 | const char *errors, |
| 3091 | int *byteorder, |
| 3092 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3093 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3094 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3095 | Py_ssize_t startinpos; |
| 3096 | Py_ssize_t endinpos; |
| 3097 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3098 | PyUnicodeObject *unicode; |
| 3099 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3100 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3101 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3102 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 3103 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3104 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 3105 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3106 | int ihi = 1, ilo = 0; |
| 3107 | #else |
| 3108 | int ihi = 0, ilo = 1; |
| 3109 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3110 | PyObject *errorHandler = NULL; |
| 3111 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3112 | |
| 3113 | /* Note: size will always be longer than the resulting Unicode |
| 3114 | character count */ |
| 3115 | unicode = _PyUnicode_New(size); |
| 3116 | if (!unicode) |
| 3117 | return NULL; |
| 3118 | if (size == 0) |
| 3119 | return (PyObject *)unicode; |
| 3120 | |
| 3121 | /* Unpack UTF-16 encoded data */ |
| 3122 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3123 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3124 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3125 | |
| 3126 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3127 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3128 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3129 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 3130 | byte order setting accordingly. In native mode, the leading BOM |
| 3131 | mark is skipped, in all other modes, it is copied to the output |
| 3132 | stream as-is (giving a ZWNBSP character). */ |
| 3133 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3134 | if (size >= 2) { |
| 3135 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3136 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3137 | if (bom == 0xFEFF) { |
| 3138 | q += 2; |
| 3139 | bo = -1; |
| 3140 | } |
| 3141 | else if (bom == 0xFFFE) { |
| 3142 | q += 2; |
| 3143 | bo = 1; |
| 3144 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3145 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3146 | if (bom == 0xFEFF) { |
| 3147 | q += 2; |
| 3148 | bo = 1; |
| 3149 | } |
| 3150 | else if (bom == 0xFFFE) { |
| 3151 | q += 2; |
| 3152 | bo = -1; |
| 3153 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3154 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3155 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3156 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3157 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3158 | if (bo == -1) { |
| 3159 | /* force LE */ |
| 3160 | ihi = 1; |
| 3161 | ilo = 0; |
| 3162 | } |
| 3163 | else if (bo == 1) { |
| 3164 | /* force BE */ |
| 3165 | ihi = 0; |
| 3166 | ilo = 1; |
| 3167 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3168 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3169 | native_ordering = ilo < ihi; |
| 3170 | #else |
| 3171 | native_ordering = ilo > ihi; |
| 3172 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3173 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3174 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3175 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3176 | Py_UNICODE ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3177 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 3178 | reads are more expensive, better to defer to another iteration. */ |
| 3179 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 3180 | /* Fast path for runs of non-surrogate chars. */ |
| 3181 | register const unsigned char *_q = q; |
| 3182 | Py_UNICODE *_p = p; |
| 3183 | if (native_ordering) { |
| 3184 | /* Native ordering is simple: as long as the input cannot |
| 3185 | possibly contain a surrogate char, do an unrolled copy |
| 3186 | of several 16-bit code points to the target object. |
| 3187 | The non-surrogate check is done on several input bytes |
| 3188 | at a time (as many as a C 'long' can contain). */ |
| 3189 | while (_q < aligned_end) { |
| 3190 | unsigned long data = * (unsigned long *) _q; |
| 3191 | if (data & FAST_CHAR_MASK) |
| 3192 | break; |
| 3193 | _p[0] = ((unsigned short *) _q)[0]; |
| 3194 | _p[1] = ((unsigned short *) _q)[1]; |
| 3195 | #if (SIZEOF_LONG == 8) |
| 3196 | _p[2] = ((unsigned short *) _q)[2]; |
| 3197 | _p[3] = ((unsigned short *) _q)[3]; |
| 3198 | #endif |
| 3199 | _q += SIZEOF_LONG; |
| 3200 | _p += SIZEOF_LONG / 2; |
| 3201 | } |
| 3202 | } |
| 3203 | else { |
| 3204 | /* Byteswapped ordering is similar, but we must decompose |
| 3205 | the copy bytewise, and take care of zero'ing out the |
| 3206 | upper bytes if the target object is in 32-bit units |
| 3207 | (that is, in UCS-4 builds). */ |
| 3208 | while (_q < aligned_end) { |
| 3209 | unsigned long data = * (unsigned long *) _q; |
| 3210 | if (data & SWAPPED_FAST_CHAR_MASK) |
| 3211 | break; |
| 3212 | /* Zero upper bytes in UCS-4 builds */ |
| 3213 | #if (Py_UNICODE_SIZE > 2) |
| 3214 | _p[0] = 0; |
| 3215 | _p[1] = 0; |
| 3216 | #if (SIZEOF_LONG == 8) |
| 3217 | _p[2] = 0; |
| 3218 | _p[3] = 0; |
| 3219 | #endif |
| 3220 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 3221 | /* Issue #4916; UCS-4 builds on big endian machines must |
| 3222 | fill the two last bytes of each 4-byte unit. */ |
| 3223 | #if (!defined(BYTEORDER_IS_LITTLE_ENDIAN) && Py_UNICODE_SIZE > 2) |
| 3224 | # define OFF 2 |
| 3225 | #else |
| 3226 | # define OFF 0 |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3227 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 3228 | ((unsigned char *) _p)[OFF + 1] = _q[0]; |
| 3229 | ((unsigned char *) _p)[OFF + 0] = _q[1]; |
| 3230 | ((unsigned char *) _p)[OFF + 1 + Py_UNICODE_SIZE] = _q[2]; |
| 3231 | ((unsigned char *) _p)[OFF + 0 + Py_UNICODE_SIZE] = _q[3]; |
| 3232 | #if (SIZEOF_LONG == 8) |
| 3233 | ((unsigned char *) _p)[OFF + 1 + 2 * Py_UNICODE_SIZE] = _q[4]; |
| 3234 | ((unsigned char *) _p)[OFF + 0 + 2 * Py_UNICODE_SIZE] = _q[5]; |
| 3235 | ((unsigned char *) _p)[OFF + 1 + 3 * Py_UNICODE_SIZE] = _q[6]; |
| 3236 | ((unsigned char *) _p)[OFF + 0 + 3 * Py_UNICODE_SIZE] = _q[7]; |
| 3237 | #endif |
| 3238 | #undef OFF |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3239 | _q += SIZEOF_LONG; |
| 3240 | _p += SIZEOF_LONG / 2; |
| 3241 | } |
| 3242 | } |
| 3243 | p = _p; |
| 3244 | q = _q; |
| 3245 | if (q >= e) |
| 3246 | break; |
| 3247 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3248 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3249 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3250 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3251 | |
| 3252 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 3253 | *p++ = ch; |
| 3254 | continue; |
| 3255 | } |
| 3256 | |
| 3257 | /* UTF-16 code pair: */ |
| 3258 | if (q > e) { |
| 3259 | errmsg = "unexpected end of data"; |
| 3260 | startinpos = (((const char *)q) - 2) - starts; |
| 3261 | endinpos = ((const char *)e) + 1 - starts; |
| 3262 | goto utf16Error; |
| 3263 | } |
| 3264 | if (0xD800 <= ch && ch <= 0xDBFF) { |
| 3265 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 3266 | q += 2; |
| 3267 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 3268 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3269 | *p++ = ch; |
| 3270 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3271 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3272 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3273 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3274 | continue; |
| 3275 | } |
| 3276 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3277 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3278 | startinpos = (((const char *)q)-4)-starts; |
| 3279 | endinpos = startinpos+2; |
| 3280 | goto utf16Error; |
| 3281 | } |
| 3282 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3283 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3284 | errmsg = "illegal encoding"; |
| 3285 | startinpos = (((const char *)q)-2)-starts; |
| 3286 | endinpos = startinpos+2; |
| 3287 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3288 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3289 | utf16Error: |
| 3290 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 3291 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3292 | errors, |
| 3293 | &errorHandler, |
| 3294 | "utf16", errmsg, |
| 3295 | &starts, |
| 3296 | (const char **)&e, |
| 3297 | &startinpos, |
| 3298 | &endinpos, |
| 3299 | &exc, |
| 3300 | (const char **)&q, |
| 3301 | &unicode, |
| 3302 | &outpos, |
| 3303 | &p)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3304 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3305 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3306 | /* remaining byte at the end? (size should be even) */ |
| 3307 | if (e == q) { |
| 3308 | if (!consumed) { |
| 3309 | errmsg = "truncated data"; |
| 3310 | startinpos = ((const char *)q) - starts; |
| 3311 | endinpos = ((const char *)e) + 1 - starts; |
| 3312 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 3313 | if (unicode_decode_call_errorhandler( |
| 3314 | errors, |
| 3315 | &errorHandler, |
| 3316 | "utf16", errmsg, |
| 3317 | &starts, |
| 3318 | (const char **)&e, |
| 3319 | &startinpos, |
| 3320 | &endinpos, |
| 3321 | &exc, |
| 3322 | (const char **)&q, |
| 3323 | &unicode, |
| 3324 | &outpos, |
| 3325 | &p)) |
| 3326 | goto onError; |
| 3327 | /* The remaining input chars are ignored if the callback |
| 3328 | chooses to skip the input */ |
| 3329 | } |
| 3330 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3331 | |
| 3332 | if (byteorder) |
| 3333 | *byteorder = bo; |
| 3334 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3335 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3336 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3337 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3338 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 3339 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3340 | goto onError; |
| 3341 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3342 | Py_XDECREF(errorHandler); |
| 3343 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3344 | return (PyObject *)unicode; |
| 3345 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3346 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3347 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3348 | Py_XDECREF(errorHandler); |
| 3349 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3350 | return NULL; |
| 3351 | } |
| 3352 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3353 | #undef FAST_CHAR_MASK |
| 3354 | #undef SWAPPED_FAST_CHAR_MASK |
| 3355 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3356 | PyObject * |
| 3357 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3358 | Py_ssize_t size, |
| 3359 | const char *errors, |
| 3360 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3361 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3362 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3363 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3364 | Py_ssize_t nsize, bytesize; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3365 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3366 | Py_ssize_t i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3367 | #else |
| 3368 | const int pairs = 0; |
| 3369 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3370 | /* Offsets from p for storing byte pairs in the right order. */ |
| 3371 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3372 | int ihi = 1, ilo = 0; |
| 3373 | #else |
| 3374 | int ihi = 0, ilo = 1; |
| 3375 | #endif |
| 3376 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3377 | #define STORECHAR(CH) \ |
| 3378 | do { \ |
| 3379 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 3380 | p[ilo] = (CH) & 0xff; \ |
| 3381 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3382 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3383 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3384 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3385 | for (i = pairs = 0; i < size; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3386 | if (s[i] >= 0x10000) |
| 3387 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3388 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3389 | /* 2 * (size + pairs + (byteorder == 0)) */ |
| 3390 | if (size > PY_SSIZE_T_MAX || |
| 3391 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3392 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3393 | nsize = size + pairs + (byteorder == 0); |
| 3394 | bytesize = nsize * 2; |
| 3395 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3396 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3397 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3398 | if (v == NULL) |
| 3399 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3400 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3401 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3402 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3403 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 3404 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3405 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3406 | |
| 3407 | if (byteorder == -1) { |
| 3408 | /* force LE */ |
| 3409 | ihi = 1; |
| 3410 | ilo = 0; |
| 3411 | } |
| 3412 | else if (byteorder == 1) { |
| 3413 | /* force BE */ |
| 3414 | ihi = 0; |
| 3415 | ilo = 1; |
| 3416 | } |
| 3417 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3418 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3419 | Py_UNICODE ch = *s++; |
| 3420 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3421 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3422 | if (ch >= 0x10000) { |
| 3423 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 3424 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 3425 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3426 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3427 | STORECHAR(ch); |
| 3428 | if (ch2) |
| 3429 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3430 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3431 | |
| 3432 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3433 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3434 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3435 | } |
| 3436 | |
| 3437 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 3438 | { |
| 3439 | if (!PyUnicode_Check(unicode)) { |
| 3440 | PyErr_BadArgument(); |
| 3441 | return NULL; |
| 3442 | } |
| 3443 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3444 | PyUnicode_GET_SIZE(unicode), |
| 3445 | NULL, |
| 3446 | 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3447 | } |
| 3448 | |
| 3449 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 3450 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 3451 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 3452 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3453 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3454 | Py_ssize_t size, |
| 3455 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3456 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3457 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3458 | Py_ssize_t startinpos; |
| 3459 | Py_ssize_t endinpos; |
| 3460 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3461 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3462 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3463 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3464 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3465 | char* message; |
| 3466 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3467 | PyObject *errorHandler = NULL; |
| 3468 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3469 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3470 | /* Escaped strings will always be longer than the resulting |
| 3471 | 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] | 3472 | length after conversion to the true value. |
| 3473 | (but if the error callback returns a long replacement string |
| 3474 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3475 | v = _PyUnicode_New(size); |
| 3476 | if (v == NULL) |
| 3477 | goto onError; |
| 3478 | if (size == 0) |
| 3479 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3480 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3481 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3482 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3483 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3484 | while (s < end) { |
| 3485 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 3486 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3487 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3488 | |
| 3489 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3490 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3491 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3492 | continue; |
| 3493 | } |
| 3494 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3495 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3496 | /* \ - Escapes */ |
| 3497 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3498 | c = *s++; |
| 3499 | if (s > end) |
| 3500 | c = '\0'; /* Invalid after \ */ |
| 3501 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3502 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3503 | /* \x escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3504 | case '\n': break; |
| 3505 | case '\\': *p++ = '\\'; break; |
| 3506 | case '\'': *p++ = '\''; break; |
| 3507 | case '\"': *p++ = '\"'; break; |
| 3508 | case 'b': *p++ = '\b'; break; |
| 3509 | case 'f': *p++ = '\014'; break; /* FF */ |
| 3510 | case 't': *p++ = '\t'; break; |
| 3511 | case 'n': *p++ = '\n'; break; |
| 3512 | case 'r': *p++ = '\r'; break; |
| 3513 | case 'v': *p++ = '\013'; break; /* VT */ |
| 3514 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 3515 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3516 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3517 | case '0': case '1': case '2': case '3': |
| 3518 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3519 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3520 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3521 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3522 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3523 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3524 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3525 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3526 | break; |
| 3527 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3528 | /* hex escapes */ |
| 3529 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3530 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3531 | digits = 2; |
| 3532 | message = "truncated \\xXX escape"; |
| 3533 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3534 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3535 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3536 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3537 | digits = 4; |
| 3538 | message = "truncated \\uXXXX escape"; |
| 3539 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3540 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3541 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3542 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3543 | digits = 8; |
| 3544 | message = "truncated \\UXXXXXXXX escape"; |
| 3545 | hexescape: |
| 3546 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3547 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3548 | if (s+digits>end) { |
| 3549 | endinpos = size; |
| 3550 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3551 | errors, &errorHandler, |
| 3552 | "unicodeescape", "end of string in escape sequence", |
| 3553 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3554 | &v, &outpos, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3555 | goto onError; |
| 3556 | goto nextByte; |
| 3557 | } |
| 3558 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3559 | c = (unsigned char) s[i]; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3560 | if (!ISXDIGIT(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3561 | endinpos = (s+i+1)-starts; |
| 3562 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3563 | errors, &errorHandler, |
| 3564 | "unicodeescape", message, |
| 3565 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3566 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3567 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3568 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3569 | } |
| 3570 | chr = (chr<<4) & ~0xF; |
| 3571 | if (c >= '0' && c <= '9') |
| 3572 | chr += c - '0'; |
| 3573 | else if (c >= 'a' && c <= 'f') |
| 3574 | chr += 10 + c - 'a'; |
| 3575 | else |
| 3576 | chr += 10 + c - 'A'; |
| 3577 | } |
| 3578 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 3579 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3580 | /* _decoding_error will have already written into the |
| 3581 | target buffer. */ |
| 3582 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3583 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3584 | /* when we get here, chr is a 32-bit unicode character */ |
| 3585 | if (chr <= 0xffff) |
| 3586 | /* UCS-2 character */ |
| 3587 | *p++ = (Py_UNICODE) chr; |
| 3588 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3589 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3590 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 3591 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3592 | *p++ = chr; |
| 3593 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3594 | chr -= 0x10000L; |
| 3595 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 3596 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3597 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3598 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3599 | endinpos = s-starts; |
| 3600 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3601 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3602 | errors, &errorHandler, |
| 3603 | "unicodeescape", "illegal Unicode character", |
| 3604 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3605 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3606 | goto onError; |
| 3607 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3608 | break; |
| 3609 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3610 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3611 | case 'N': |
| 3612 | message = "malformed \\N character escape"; |
| 3613 | if (ucnhash_CAPI == NULL) { |
| 3614 | /* load the unicode data module */ |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3615 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import(PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3616 | if (ucnhash_CAPI == NULL) |
| 3617 | goto ucnhashError; |
| 3618 | } |
| 3619 | if (*s == '{') { |
| 3620 | const char *start = s+1; |
| 3621 | /* look for the closing brace */ |
| 3622 | while (*s != '}' && s < end) |
| 3623 | s++; |
| 3624 | if (s > start && s < end && *s == '}') { |
| 3625 | /* found a name. look it up in the unicode database */ |
| 3626 | message = "unknown Unicode character name"; |
| 3627 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 3628 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3629 | goto store; |
| 3630 | } |
| 3631 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3632 | endinpos = s-starts; |
| 3633 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3634 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3635 | errors, &errorHandler, |
| 3636 | "unicodeescape", message, |
| 3637 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3638 | &v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3639 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3640 | break; |
| 3641 | |
| 3642 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3643 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3644 | message = "\\ at end of string"; |
| 3645 | s--; |
| 3646 | endinpos = s-starts; |
| 3647 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3648 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3649 | errors, &errorHandler, |
| 3650 | "unicodeescape", message, |
| 3651 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3652 | &v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3653 | goto onError; |
| 3654 | } |
| 3655 | else { |
| 3656 | *p++ = '\\'; |
| 3657 | *p++ = (unsigned char)s[-1]; |
| 3658 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3659 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3660 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3661 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3662 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3663 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3664 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3665 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 3666 | Py_XDECREF(errorHandler); |
| 3667 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3668 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3669 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3670 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 3671 | PyErr_SetString( |
| 3672 | PyExc_UnicodeError, |
| 3673 | "\\N escapes not supported (can't load unicodedata module)" |
| 3674 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3675 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3676 | Py_XDECREF(errorHandler); |
| 3677 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 3678 | return NULL; |
| 3679 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3680 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3681 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3682 | Py_XDECREF(errorHandler); |
| 3683 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3684 | return NULL; |
| 3685 | } |
| 3686 | |
| 3687 | /* Return a Unicode-Escape string version of the Unicode object. |
| 3688 | |
| 3689 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 3690 | appropriate. |
| 3691 | |
| 3692 | */ |
| 3693 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3694 | Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3695 | Py_ssize_t size, |
| 3696 | Py_UNICODE ch) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3697 | { |
| 3698 | /* like wcschr, but doesn't stop at NULL characters */ |
| 3699 | |
| 3700 | while (size-- > 0) { |
| 3701 | if (*s == ch) |
| 3702 | return s; |
| 3703 | s++; |
| 3704 | } |
| 3705 | |
| 3706 | return NULL; |
| 3707 | } |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 3708 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3709 | static const char *hexdigits = "0123456789abcdef"; |
| 3710 | |
| 3711 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3712 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3713 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3714 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3715 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3716 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3717 | #ifdef Py_UNICODE_WIDE |
| 3718 | const Py_ssize_t expandsize = 10; |
| 3719 | #else |
| 3720 | const Py_ssize_t expandsize = 6; |
| 3721 | #endif |
| 3722 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3723 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 3724 | better to choose a different scheme. Perhaps scan the |
| 3725 | first N-chars of the string and allocate based on that size. |
| 3726 | */ |
| 3727 | /* Initial allocation is based on the longest-possible unichr |
| 3728 | escape. |
| 3729 | |
| 3730 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 3731 | unichr, so in this case it's the longest unichr escape. In |
| 3732 | narrow (UTF-16) builds this is five chars per source unichr |
| 3733 | since there are two unichrs in the surrogate pair, so in narrow |
| 3734 | (UTF-16) builds it's not the longest unichr escape. |
| 3735 | |
| 3736 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 3737 | so in the narrow (UTF-16) build case it's the longest unichr |
| 3738 | escape. |
| 3739 | */ |
| 3740 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3741 | if (size == 0) |
| 3742 | return PyBytes_FromStringAndSize(NULL, 0); |
| 3743 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3744 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3745 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3746 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3747 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3748 | 2 |
| 3749 | + expandsize*size |
| 3750 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3751 | if (repr == NULL) |
| 3752 | return NULL; |
| 3753 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3754 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3755 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3756 | while (size-- > 0) { |
| 3757 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3758 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3759 | /* Escape backslashes */ |
| 3760 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3761 | *p++ = '\\'; |
| 3762 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3763 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3764 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3765 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 3766 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3767 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 3768 | else if (ch >= 0x10000) { |
| 3769 | *p++ = '\\'; |
| 3770 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3771 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 3772 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 3773 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 3774 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 3775 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 3776 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 3777 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 3778 | *p++ = hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3779 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3780 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3781 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3782 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 3783 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 3784 | Py_UNICODE ch2; |
| 3785 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3786 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3787 | ch2 = *s++; |
| 3788 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 3789 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3790 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 3791 | *p++ = '\\'; |
| 3792 | *p++ = 'U'; |
| 3793 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 3794 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 3795 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 3796 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 3797 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 3798 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 3799 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 3800 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 3801 | continue; |
| 3802 | } |
| 3803 | /* Fall through: isolated surrogates are copied as-is */ |
| 3804 | s--; |
| 3805 | size++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3806 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3807 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3808 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3809 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3810 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3811 | *p++ = '\\'; |
| 3812 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3813 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 3814 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 3815 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 3816 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3817 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3818 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3819 | /* Map special whitespace to '\t', \n', '\r' */ |
| 3820 | else if (ch == '\t') { |
| 3821 | *p++ = '\\'; |
| 3822 | *p++ = 't'; |
| 3823 | } |
| 3824 | else if (ch == '\n') { |
| 3825 | *p++ = '\\'; |
| 3826 | *p++ = 'n'; |
| 3827 | } |
| 3828 | else if (ch == '\r') { |
| 3829 | *p++ = '\\'; |
| 3830 | *p++ = 'r'; |
| 3831 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3832 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3833 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 3834 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3835 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3836 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3837 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 3838 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3839 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3840 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3841 | /* Copy everything else as-is */ |
| 3842 | else |
| 3843 | *p++ = (char) ch; |
| 3844 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3845 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3846 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 3847 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 3848 | return NULL; |
| 3849 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3850 | } |
| 3851 | |
Alexandre Vassalotti | 2056bed | 2008-12-27 19:46:35 +0000 | [diff] [blame] | 3852 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3853 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3854 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3855 | if (!PyUnicode_Check(unicode)) { |
| 3856 | PyErr_BadArgument(); |
| 3857 | return NULL; |
| 3858 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3859 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3860 | PyUnicode_GET_SIZE(unicode)); |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3861 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3862 | } |
| 3863 | |
| 3864 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 3865 | |
| 3866 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3867 | Py_ssize_t size, |
| 3868 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3869 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3870 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3871 | Py_ssize_t startinpos; |
| 3872 | Py_ssize_t endinpos; |
| 3873 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3874 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3875 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3876 | const char *end; |
| 3877 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3878 | PyObject *errorHandler = NULL; |
| 3879 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3880 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3881 | /* Escaped strings will always be longer than the resulting |
| 3882 | 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] | 3883 | length after conversion to the true value. (But decoding error |
| 3884 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3885 | v = _PyUnicode_New(size); |
| 3886 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3887 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3888 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3889 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3890 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3891 | end = s + size; |
| 3892 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3893 | unsigned char c; |
| 3894 | Py_UCS4 x; |
| 3895 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3896 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3897 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3898 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3899 | if (*s != '\\') { |
| 3900 | *p++ = (unsigned char)*s++; |
| 3901 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3902 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3903 | startinpos = s-starts; |
| 3904 | |
| 3905 | /* \u-escapes are only interpreted iff the number of leading |
| 3906 | backslashes if odd */ |
| 3907 | bs = s; |
| 3908 | for (;s < end;) { |
| 3909 | if (*s != '\\') |
| 3910 | break; |
| 3911 | *p++ = (unsigned char)*s++; |
| 3912 | } |
| 3913 | if (((s - bs) & 1) == 0 || |
| 3914 | s >= end || |
| 3915 | (*s != 'u' && *s != 'U')) { |
| 3916 | continue; |
| 3917 | } |
| 3918 | p--; |
| 3919 | count = *s=='u' ? 4 : 8; |
| 3920 | s++; |
| 3921 | |
| 3922 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
| 3923 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3924 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 3925 | c = (unsigned char)*s; |
| 3926 | if (!ISXDIGIT(c)) { |
| 3927 | endinpos = s-starts; |
| 3928 | if (unicode_decode_call_errorhandler( |
| 3929 | errors, &errorHandler, |
| 3930 | "rawunicodeescape", "truncated \\uXXXX", |
| 3931 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3932 | &v, &outpos, &p)) |
| 3933 | goto onError; |
| 3934 | goto nextByte; |
| 3935 | } |
| 3936 | x = (x<<4) & ~0xF; |
| 3937 | if (c >= '0' && c <= '9') |
| 3938 | x += c - '0'; |
| 3939 | else if (c >= 'a' && c <= 'f') |
| 3940 | x += 10 + c - 'a'; |
| 3941 | else |
| 3942 | x += 10 + c - 'A'; |
| 3943 | } |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3944 | if (x <= 0xffff) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3945 | /* UCS-2 character */ |
| 3946 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3947 | else if (x <= 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3948 | /* UCS-4 character. Either store directly, or as |
| 3949 | surrogate pair. */ |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3950 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3951 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3952 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3953 | x -= 0x10000L; |
| 3954 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 3955 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3956 | #endif |
| 3957 | } else { |
| 3958 | endinpos = s-starts; |
| 3959 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3960 | if (unicode_decode_call_errorhandler( |
| 3961 | errors, &errorHandler, |
| 3962 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3963 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3964 | &v, &outpos, &p)) |
| 3965 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3966 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3967 | nextByte: |
| 3968 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3969 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3970 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3971 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3972 | Py_XDECREF(errorHandler); |
| 3973 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3974 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3975 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3976 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3977 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3978 | Py_XDECREF(errorHandler); |
| 3979 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3980 | return NULL; |
| 3981 | } |
| 3982 | |
| 3983 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3984 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3985 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3986 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3987 | char *p; |
| 3988 | char *q; |
| 3989 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3990 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3991 | const Py_ssize_t expandsize = 10; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3992 | #else |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3993 | const Py_ssize_t expandsize = 6; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3994 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3995 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3996 | if (size > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3997 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3998 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3999 | repr = PyBytes_FromStringAndSize(NULL, expandsize * size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4000 | if (repr == NULL) |
| 4001 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 4002 | if (size == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4003 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4004 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4005 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4006 | while (size-- > 0) { |
| 4007 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4008 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4009 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 4010 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4011 | *p++ = '\\'; |
| 4012 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 4013 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 4014 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 4015 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 4016 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 4017 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 4018 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 4019 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 4020 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4021 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4022 | else |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4023 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4024 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 4025 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 4026 | Py_UNICODE ch2; |
| 4027 | Py_UCS4 ucs; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4028 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4029 | ch2 = *s++; |
| 4030 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 4031 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4032 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 4033 | *p++ = '\\'; |
| 4034 | *p++ = 'U'; |
| 4035 | *p++ = hexdigits[(ucs >> 28) & 0xf]; |
| 4036 | *p++ = hexdigits[(ucs >> 24) & 0xf]; |
| 4037 | *p++ = hexdigits[(ucs >> 20) & 0xf]; |
| 4038 | *p++ = hexdigits[(ucs >> 16) & 0xf]; |
| 4039 | *p++ = hexdigits[(ucs >> 12) & 0xf]; |
| 4040 | *p++ = hexdigits[(ucs >> 8) & 0xf]; |
| 4041 | *p++ = hexdigits[(ucs >> 4) & 0xf]; |
| 4042 | *p++ = hexdigits[ucs & 0xf]; |
| 4043 | continue; |
| 4044 | } |
| 4045 | /* Fall through: isolated surrogates are copied as-is */ |
| 4046 | s--; |
| 4047 | size++; |
| 4048 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4049 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4050 | /* Map 16-bit characters to '\uxxxx' */ |
| 4051 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4052 | *p++ = '\\'; |
| 4053 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 4054 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 4055 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 4056 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 4057 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4058 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4059 | /* Copy everything else as-is */ |
| 4060 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4061 | *p++ = (char) ch; |
| 4062 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4063 | size = p - q; |
| 4064 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4065 | assert(size > 0); |
| 4066 | if (_PyBytes_Resize(&repr, size) < 0) |
| 4067 | return NULL; |
| 4068 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4069 | } |
| 4070 | |
| 4071 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 4072 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 4073 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4074 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 4075 | PyErr_BadArgument(); |
| 4076 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4077 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 4078 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 4079 | PyUnicode_GET_SIZE(unicode)); |
| 4080 | |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 4081 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4082 | } |
| 4083 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4084 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 4085 | |
| 4086 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4087 | Py_ssize_t size, |
| 4088 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4089 | { |
| 4090 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4091 | Py_ssize_t startinpos; |
| 4092 | Py_ssize_t endinpos; |
| 4093 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4094 | PyUnicodeObject *v; |
| 4095 | Py_UNICODE *p; |
| 4096 | const char *end; |
| 4097 | const char *reason; |
| 4098 | PyObject *errorHandler = NULL; |
| 4099 | PyObject *exc = NULL; |
| 4100 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 4101 | #ifdef Py_UNICODE_WIDE |
| 4102 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 4103 | #endif |
| 4104 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4105 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4106 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 4107 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4108 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4109 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4110 | return (PyObject *)v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4111 | p = PyUnicode_AS_UNICODE(v); |
| 4112 | end = s + size; |
| 4113 | |
| 4114 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4115 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4116 | /* We have to sanity check the raw data, otherwise doom looms for |
| 4117 | some malformed UCS-4 data. */ |
| 4118 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4119 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4120 | *p > unimax || *p < 0 || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4121 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4122 | end-s < Py_UNICODE_SIZE |
| 4123 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4124 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4125 | startinpos = s - starts; |
| 4126 | if (end-s < Py_UNICODE_SIZE) { |
| 4127 | endinpos = end-starts; |
| 4128 | reason = "truncated input"; |
| 4129 | } |
| 4130 | else { |
| 4131 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 4132 | reason = "illegal code point (> 0x10FFFF)"; |
| 4133 | } |
| 4134 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 4135 | if (unicode_decode_call_errorhandler( |
| 4136 | errors, &errorHandler, |
| 4137 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4138 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 4139 | &v, &outpos, &p)) { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4140 | goto onError; |
| 4141 | } |
| 4142 | } |
| 4143 | else { |
| 4144 | p++; |
| 4145 | s += Py_UNICODE_SIZE; |
| 4146 | } |
| 4147 | } |
| 4148 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4149 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4150 | goto onError; |
| 4151 | Py_XDECREF(errorHandler); |
| 4152 | Py_XDECREF(exc); |
| 4153 | return (PyObject *)v; |
| 4154 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4155 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4156 | Py_XDECREF(v); |
| 4157 | Py_XDECREF(errorHandler); |
| 4158 | Py_XDECREF(exc); |
| 4159 | return NULL; |
| 4160 | } |
| 4161 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4162 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 4163 | |
| 4164 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4165 | Py_ssize_t size, |
| 4166 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4167 | { |
| 4168 | PyUnicodeObject *v; |
| 4169 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4170 | const char *e, *unrolled_end; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4171 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4172 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4173 | if (size == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4174 | Py_UNICODE r = *(unsigned char*)s; |
| 4175 | return PyUnicode_FromUnicode(&r, 1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4176 | } |
| 4177 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4178 | v = _PyUnicode_New(size); |
| 4179 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4180 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4181 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4182 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4183 | p = PyUnicode_AS_UNICODE(v); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4184 | e = s + size; |
| 4185 | /* Unrolling the copy makes it much faster by reducing the looping |
| 4186 | overhead. This is similar to what many memcpy() implementations do. */ |
| 4187 | unrolled_end = e - 4; |
| 4188 | while (s < unrolled_end) { |
| 4189 | p[0] = (unsigned char) s[0]; |
| 4190 | p[1] = (unsigned char) s[1]; |
| 4191 | p[2] = (unsigned char) s[2]; |
| 4192 | p[3] = (unsigned char) s[3]; |
| 4193 | s += 4; |
| 4194 | p += 4; |
| 4195 | } |
| 4196 | while (s < e) |
| 4197 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4198 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4199 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4200 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4201 | Py_XDECREF(v); |
| 4202 | return NULL; |
| 4203 | } |
| 4204 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4205 | /* create or adjust a UnicodeEncodeError */ |
| 4206 | static void make_encode_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4207 | const char *encoding, |
| 4208 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4209 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4210 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4211 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4212 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4213 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 4214 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4215 | } |
| 4216 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4217 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 4218 | goto onError; |
| 4219 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 4220 | goto onError; |
| 4221 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 4222 | goto onError; |
| 4223 | return; |
| 4224 | onError: |
| 4225 | Py_DECREF(*exceptionObject); |
| 4226 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4227 | } |
| 4228 | } |
| 4229 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4230 | /* raises a UnicodeEncodeError */ |
| 4231 | static void raise_encode_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4232 | const char *encoding, |
| 4233 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4234 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4235 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4236 | { |
| 4237 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4238 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4239 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4240 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4241 | } |
| 4242 | |
| 4243 | /* error handling callback helper: |
| 4244 | build arguments, call the callback and check the arguments, |
| 4245 | put the result into newpos and return the replacement string, which |
| 4246 | has to be freed by the caller */ |
| 4247 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4248 | PyObject **errorHandler, |
| 4249 | const char *encoding, const char *reason, |
| 4250 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 4251 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4252 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4253 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4254 | 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] | 4255 | |
| 4256 | PyObject *restuple; |
| 4257 | PyObject *resunicode; |
| 4258 | |
| 4259 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4260 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4261 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4262 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4263 | } |
| 4264 | |
| 4265 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4266 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4267 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4268 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4269 | |
| 4270 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4271 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4272 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4273 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4274 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4275 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4276 | Py_DECREF(restuple); |
| 4277 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4278 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4279 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4280 | &resunicode, newpos)) { |
| 4281 | Py_DECREF(restuple); |
| 4282 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4283 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4284 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 4285 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 4286 | Py_DECREF(restuple); |
| 4287 | return NULL; |
| 4288 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4289 | if (*newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4290 | *newpos = size+*newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4291 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4292 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 4293 | Py_DECREF(restuple); |
| 4294 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4295 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4296 | Py_INCREF(resunicode); |
| 4297 | Py_DECREF(restuple); |
| 4298 | return resunicode; |
| 4299 | } |
| 4300 | |
| 4301 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4302 | Py_ssize_t size, |
| 4303 | const char *errors, |
| 4304 | int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4305 | { |
| 4306 | /* output object */ |
| 4307 | PyObject *res; |
| 4308 | /* pointers to the beginning and end+1 of input */ |
| 4309 | const Py_UNICODE *startp = p; |
| 4310 | const Py_UNICODE *endp = p + size; |
| 4311 | /* pointer to the beginning of the unencodable characters */ |
| 4312 | /* const Py_UNICODE *badp = NULL; */ |
| 4313 | /* pointer into the output */ |
| 4314 | char *str; |
| 4315 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4316 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4317 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 4318 | 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] | 4319 | PyObject *errorHandler = NULL; |
| 4320 | PyObject *exc = NULL; |
| 4321 | /* the following variable is used for caching string comparisons |
| 4322 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 4323 | int known_errorHandler = -1; |
| 4324 | |
| 4325 | /* allocate enough for a simple encoding without |
| 4326 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4327 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4328 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4329 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4330 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4331 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4332 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4333 | ressize = size; |
| 4334 | |
| 4335 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4336 | Py_UNICODE c = *p; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4337 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4338 | /* can we encode this? */ |
| 4339 | if (c<limit) { |
| 4340 | /* no overflow check, because we know that the space is enough */ |
| 4341 | *str++ = (char)c; |
| 4342 | ++p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4343 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4344 | else { |
| 4345 | Py_ssize_t unicodepos = p-startp; |
| 4346 | Py_ssize_t requiredsize; |
| 4347 | PyObject *repunicode; |
| 4348 | Py_ssize_t repsize; |
| 4349 | Py_ssize_t newpos; |
| 4350 | Py_ssize_t respos; |
| 4351 | Py_UNICODE *uni2; |
| 4352 | /* startpos for collecting unencodable chars */ |
| 4353 | const Py_UNICODE *collstart = p; |
| 4354 | const Py_UNICODE *collend = p; |
| 4355 | /* find all unecodable characters */ |
| 4356 | while ((collend < endp) && ((*collend)>=limit)) |
| 4357 | ++collend; |
| 4358 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 4359 | if (known_errorHandler==-1) { |
| 4360 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4361 | known_errorHandler = 1; |
| 4362 | else if (!strcmp(errors, "replace")) |
| 4363 | known_errorHandler = 2; |
| 4364 | else if (!strcmp(errors, "ignore")) |
| 4365 | known_errorHandler = 3; |
| 4366 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4367 | known_errorHandler = 4; |
| 4368 | else |
| 4369 | known_errorHandler = 0; |
| 4370 | } |
| 4371 | switch (known_errorHandler) { |
| 4372 | case 1: /* strict */ |
| 4373 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 4374 | goto onError; |
| 4375 | case 2: /* replace */ |
| 4376 | while (collstart++<collend) |
| 4377 | *str++ = '?'; /* fall through */ |
| 4378 | case 3: /* ignore */ |
| 4379 | p = collend; |
| 4380 | break; |
| 4381 | case 4: /* xmlcharrefreplace */ |
| 4382 | respos = str - PyBytes_AS_STRING(res); |
| 4383 | /* determine replacement size (temporarily (mis)uses p) */ |
| 4384 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 4385 | if (*p<10) |
| 4386 | repsize += 2+1+1; |
| 4387 | else if (*p<100) |
| 4388 | repsize += 2+2+1; |
| 4389 | else if (*p<1000) |
| 4390 | repsize += 2+3+1; |
| 4391 | else if (*p<10000) |
| 4392 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 4393 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4394 | else |
| 4395 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 4396 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4397 | else if (*p<100000) |
| 4398 | repsize += 2+5+1; |
| 4399 | else if (*p<1000000) |
| 4400 | repsize += 2+6+1; |
| 4401 | else |
| 4402 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4403 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4404 | } |
| 4405 | requiredsize = respos+repsize+(endp-collend); |
| 4406 | if (requiredsize > ressize) { |
| 4407 | if (requiredsize<2*ressize) |
| 4408 | requiredsize = 2*ressize; |
| 4409 | if (_PyBytes_Resize(&res, requiredsize)) |
| 4410 | goto onError; |
| 4411 | str = PyBytes_AS_STRING(res) + respos; |
| 4412 | ressize = requiredsize; |
| 4413 | } |
| 4414 | /* generate replacement (temporarily (mis)uses p) */ |
| 4415 | for (p = collstart; p < collend; ++p) { |
| 4416 | str += sprintf(str, "&#%d;", (int)*p); |
| 4417 | } |
| 4418 | p = collend; |
| 4419 | break; |
| 4420 | default: |
| 4421 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 4422 | encoding, reason, startp, size, &exc, |
| 4423 | collstart-startp, collend-startp, &newpos); |
| 4424 | if (repunicode == NULL) |
| 4425 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4426 | if (PyBytes_Check(repunicode)) { |
| 4427 | /* Directly copy bytes result to output. */ |
| 4428 | repsize = PyBytes_Size(repunicode); |
| 4429 | if (repsize > 1) { |
| 4430 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 4431 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4432 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 4433 | Py_DECREF(repunicode); |
| 4434 | goto onError; |
| 4435 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 4436 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4437 | ressize += repsize-1; |
| 4438 | } |
| 4439 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 4440 | str += repsize; |
| 4441 | p = startp + newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4442 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4443 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4444 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4445 | /* need more space? (at least enough for what we |
| 4446 | have+the replacement+the rest of the string, so |
| 4447 | we won't have to check space for encodable characters) */ |
| 4448 | respos = str - PyBytes_AS_STRING(res); |
| 4449 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4450 | requiredsize = respos+repsize+(endp-collend); |
| 4451 | if (requiredsize > ressize) { |
| 4452 | if (requiredsize<2*ressize) |
| 4453 | requiredsize = 2*ressize; |
| 4454 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 4455 | Py_DECREF(repunicode); |
| 4456 | goto onError; |
| 4457 | } |
| 4458 | str = PyBytes_AS_STRING(res) + respos; |
| 4459 | ressize = requiredsize; |
| 4460 | } |
| 4461 | /* check if there is anything unencodable in the replacement |
| 4462 | and copy it to the output */ |
| 4463 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 4464 | c = *uni2; |
| 4465 | if (c >= limit) { |
| 4466 | raise_encode_exception(&exc, encoding, startp, size, |
| 4467 | unicodepos, unicodepos+1, reason); |
| 4468 | Py_DECREF(repunicode); |
| 4469 | goto onError; |
| 4470 | } |
| 4471 | *str = (char)c; |
| 4472 | } |
| 4473 | p = startp + newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4474 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4475 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4476 | } |
| 4477 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4478 | /* Resize if we allocated to much */ |
| 4479 | size = str - PyBytes_AS_STRING(res); |
| 4480 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 4481 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4482 | if (_PyBytes_Resize(&res, size) < 0) |
| 4483 | goto onError; |
| 4484 | } |
| 4485 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4486 | Py_XDECREF(errorHandler); |
| 4487 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4488 | return res; |
| 4489 | |
| 4490 | onError: |
| 4491 | Py_XDECREF(res); |
| 4492 | Py_XDECREF(errorHandler); |
| 4493 | Py_XDECREF(exc); |
| 4494 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4495 | } |
| 4496 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4497 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4498 | Py_ssize_t size, |
| 4499 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4500 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4501 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4502 | } |
| 4503 | |
| 4504 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 4505 | { |
| 4506 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4507 | PyErr_BadArgument(); |
| 4508 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4509 | } |
| 4510 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4511 | PyUnicode_GET_SIZE(unicode), |
| 4512 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4513 | } |
| 4514 | |
| 4515 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 4516 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4517 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4518 | Py_ssize_t size, |
| 4519 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4520 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4521 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4522 | PyUnicodeObject *v; |
| 4523 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4524 | Py_ssize_t startinpos; |
| 4525 | Py_ssize_t endinpos; |
| 4526 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4527 | const char *e; |
| 4528 | PyObject *errorHandler = NULL; |
| 4529 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4530 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4531 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4532 | if (size == 1 && *(unsigned char*)s < 128) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4533 | Py_UNICODE r = *(unsigned char*)s; |
| 4534 | return PyUnicode_FromUnicode(&r, 1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4535 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4536 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4537 | v = _PyUnicode_New(size); |
| 4538 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4539 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4540 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4541 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4542 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4543 | e = s + size; |
| 4544 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4545 | register unsigned char c = (unsigned char)*s; |
| 4546 | if (c < 128) { |
| 4547 | *p++ = c; |
| 4548 | ++s; |
| 4549 | } |
| 4550 | else { |
| 4551 | startinpos = s-starts; |
| 4552 | endinpos = startinpos + 1; |
| 4553 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
| 4554 | if (unicode_decode_call_errorhandler( |
| 4555 | errors, &errorHandler, |
| 4556 | "ascii", "ordinal not in range(128)", |
| 4557 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 4558 | &v, &outpos, &p)) |
| 4559 | goto onError; |
| 4560 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4561 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 4562 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4563 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 4564 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4565 | Py_XDECREF(errorHandler); |
| 4566 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4567 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4568 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4569 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4570 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4571 | Py_XDECREF(errorHandler); |
| 4572 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4573 | return NULL; |
| 4574 | } |
| 4575 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4576 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4577 | Py_ssize_t size, |
| 4578 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4579 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4580 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4581 | } |
| 4582 | |
| 4583 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 4584 | { |
| 4585 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4586 | PyErr_BadArgument(); |
| 4587 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4588 | } |
| 4589 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4590 | PyUnicode_GET_SIZE(unicode), |
| 4591 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4592 | } |
| 4593 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4594 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4595 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4596 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4597 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 4598 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4599 | #define NEED_RETRY |
| 4600 | #endif |
| 4601 | |
| 4602 | /* XXX This code is limited to "true" double-byte encodings, as |
| 4603 | a) it assumes an incomplete character consists of a single byte, and |
| 4604 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4605 | encodings, see IsDBCSLeadByteEx documentation. */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4606 | |
| 4607 | static int is_dbcs_lead_byte(const char *s, int offset) |
| 4608 | { |
| 4609 | const char *curr = s + offset; |
| 4610 | |
| 4611 | if (IsDBCSLeadByte(*curr)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4612 | const char *prev = CharPrev(s, curr); |
| 4613 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4614 | } |
| 4615 | return 0; |
| 4616 | } |
| 4617 | |
| 4618 | /* |
| 4619 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 4620 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 4621 | */ |
| 4622 | static int decode_mbcs(PyUnicodeObject **v, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4623 | const char *s, /* MBCS string */ |
| 4624 | int size, /* sizeof MBCS string */ |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4625 | int final, |
| 4626 | const char *errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4627 | { |
| 4628 | Py_UNICODE *p; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4629 | Py_ssize_t n; |
| 4630 | DWORD usize; |
| 4631 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4632 | |
| 4633 | assert(size >= 0); |
| 4634 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4635 | /* check and handle 'errors' arg */ |
| 4636 | if (errors==NULL || strcmp(errors, "strict")==0) |
| 4637 | flags = MB_ERR_INVALID_CHARS; |
| 4638 | else if (strcmp(errors, "ignore")==0) |
| 4639 | flags = 0; |
| 4640 | else { |
| 4641 | PyErr_Format(PyExc_ValueError, |
| 4642 | "mbcs encoding does not support errors='%s'", |
| 4643 | errors); |
| 4644 | return -1; |
| 4645 | } |
| 4646 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4647 | /* Skip trailing lead-byte unless 'final' is set */ |
| 4648 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4649 | --size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4650 | |
| 4651 | /* First get the size of the result */ |
| 4652 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4653 | usize = MultiByteToWideChar(CP_ACP, flags, s, size, NULL, 0); |
| 4654 | if (usize==0) |
| 4655 | goto mbcs_decode_error; |
| 4656 | } else |
| 4657 | usize = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4658 | |
| 4659 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4660 | /* Create unicode object */ |
| 4661 | *v = _PyUnicode_New(usize); |
| 4662 | if (*v == NULL) |
| 4663 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4664 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4665 | } |
| 4666 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4667 | /* Extend unicode object */ |
| 4668 | n = PyUnicode_GET_SIZE(*v); |
| 4669 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 4670 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4671 | } |
| 4672 | |
| 4673 | /* Do the conversion */ |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4674 | if (usize > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4675 | p = PyUnicode_AS_UNICODE(*v) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4676 | if (0 == MultiByteToWideChar(CP_ACP, flags, s, size, p, usize)) { |
| 4677 | goto mbcs_decode_error; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4678 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4679 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4680 | return size; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4681 | |
| 4682 | mbcs_decode_error: |
| 4683 | /* If the last error was ERROR_NO_UNICODE_TRANSLATION, then |
| 4684 | we raise a UnicodeDecodeError - else it is a 'generic' |
| 4685 | windows error |
| 4686 | */ |
| 4687 | if (GetLastError()==ERROR_NO_UNICODE_TRANSLATION) { |
| 4688 | /* Ideally, we should get reason from FormatMessage - this |
| 4689 | is the Windows 2000 English version of the message |
| 4690 | */ |
| 4691 | PyObject *exc = NULL; |
| 4692 | const char *reason = "No mapping for the Unicode character exists " |
| 4693 | "in the target multi-byte code page."; |
| 4694 | make_decode_exception(&exc, "mbcs", s, size, 0, 0, reason); |
| 4695 | if (exc != NULL) { |
| 4696 | PyCodec_StrictErrors(exc); |
| 4697 | Py_DECREF(exc); |
| 4698 | } |
| 4699 | } else { |
| 4700 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4701 | } |
| 4702 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4703 | } |
| 4704 | |
| 4705 | PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4706 | Py_ssize_t size, |
| 4707 | const char *errors, |
| 4708 | Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4709 | { |
| 4710 | PyUnicodeObject *v = NULL; |
| 4711 | int done; |
| 4712 | |
| 4713 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4714 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4715 | |
| 4716 | #ifdef NEED_RETRY |
| 4717 | retry: |
| 4718 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4719 | done = decode_mbcs(&v, s, INT_MAX, 0, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4720 | else |
| 4721 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4722 | done = decode_mbcs(&v, s, (int)size, !consumed, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4723 | |
| 4724 | if (done < 0) { |
| 4725 | Py_XDECREF(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4726 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4727 | } |
| 4728 | |
| 4729 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4730 | *consumed += done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4731 | |
| 4732 | #ifdef NEED_RETRY |
| 4733 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4734 | s += done; |
| 4735 | size -= done; |
| 4736 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4737 | } |
| 4738 | #endif |
| 4739 | |
| 4740 | return (PyObject *)v; |
| 4741 | } |
| 4742 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4743 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4744 | Py_ssize_t size, |
| 4745 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4746 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4747 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 4748 | } |
| 4749 | |
| 4750 | /* |
| 4751 | * Convert unicode into string object (MBCS). |
| 4752 | * Returns 0 if succeed, -1 otherwise. |
| 4753 | */ |
| 4754 | static int encode_mbcs(PyObject **repr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4755 | const Py_UNICODE *p, /* unicode */ |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4756 | int size, /* size of unicode */ |
| 4757 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4758 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4759 | BOOL usedDefaultChar = FALSE; |
| 4760 | BOOL *pusedDefaultChar; |
| 4761 | int mbcssize; |
| 4762 | Py_ssize_t n; |
| 4763 | PyObject *exc = NULL; |
| 4764 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4765 | |
| 4766 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4767 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4768 | /* check and handle 'errors' arg */ |
| 4769 | if (errors==NULL || strcmp(errors, "strict")==0) { |
| 4770 | flags = WC_NO_BEST_FIT_CHARS; |
| 4771 | pusedDefaultChar = &usedDefaultChar; |
| 4772 | } else if (strcmp(errors, "replace")==0) { |
| 4773 | flags = 0; |
| 4774 | pusedDefaultChar = NULL; |
| 4775 | } else { |
| 4776 | PyErr_Format(PyExc_ValueError, |
| 4777 | "mbcs encoding does not support errors='%s'", |
| 4778 | errors); |
| 4779 | return -1; |
| 4780 | } |
| 4781 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4782 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4783 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4784 | mbcssize = WideCharToMultiByte(CP_ACP, flags, p, size, NULL, 0, |
| 4785 | NULL, pusedDefaultChar); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4786 | if (mbcssize == 0) { |
| 4787 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4788 | return -1; |
| 4789 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4790 | /* If we used a default char, then we failed! */ |
| 4791 | if (pusedDefaultChar && *pusedDefaultChar) |
| 4792 | goto mbcs_encode_error; |
| 4793 | } else { |
| 4794 | mbcssize = 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4795 | } |
| 4796 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4797 | if (*repr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4798 | /* Create string object */ |
| 4799 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); |
| 4800 | if (*repr == NULL) |
| 4801 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4802 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4803 | } |
| 4804 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4805 | /* Extend string object */ |
| 4806 | n = PyBytes_Size(*repr); |
| 4807 | if (_PyBytes_Resize(repr, n + mbcssize) < 0) |
| 4808 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4809 | } |
| 4810 | |
| 4811 | /* Do the conversion */ |
| 4812 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4813 | char *s = PyBytes_AS_STRING(*repr) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4814 | if (0 == WideCharToMultiByte(CP_ACP, flags, p, size, s, mbcssize, |
| 4815 | NULL, pusedDefaultChar)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4816 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4817 | return -1; |
| 4818 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4819 | if (pusedDefaultChar && *pusedDefaultChar) |
| 4820 | goto mbcs_encode_error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4821 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4822 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4823 | |
| 4824 | mbcs_encode_error: |
| 4825 | raise_encode_exception(&exc, "mbcs", p, size, 0, 0, "invalid character"); |
| 4826 | Py_XDECREF(exc); |
| 4827 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4828 | } |
| 4829 | |
| 4830 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4831 | Py_ssize_t size, |
| 4832 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4833 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4834 | PyObject *repr = NULL; |
| 4835 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 4836 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4837 | #ifdef NEED_RETRY |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4838 | retry: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4839 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4840 | ret = encode_mbcs(&repr, p, INT_MAX, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4841 | else |
| 4842 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4843 | ret = encode_mbcs(&repr, p, (int)size, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4844 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4845 | if (ret < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4846 | Py_XDECREF(repr); |
| 4847 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4848 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4849 | |
| 4850 | #ifdef NEED_RETRY |
| 4851 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4852 | p += INT_MAX; |
| 4853 | size -= INT_MAX; |
| 4854 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4855 | } |
| 4856 | #endif |
| 4857 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4858 | return repr; |
| 4859 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4860 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 4861 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 4862 | { |
| 4863 | if (!PyUnicode_Check(unicode)) { |
| 4864 | PyErr_BadArgument(); |
| 4865 | return NULL; |
| 4866 | } |
| 4867 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4868 | PyUnicode_GET_SIZE(unicode), |
| 4869 | NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 4870 | } |
| 4871 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4872 | #undef NEED_RETRY |
| 4873 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4874 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4875 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4876 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 4877 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4878 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4879 | Py_ssize_t size, |
| 4880 | PyObject *mapping, |
| 4881 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4882 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4883 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4884 | Py_ssize_t startinpos; |
| 4885 | Py_ssize_t endinpos; |
| 4886 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4887 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4888 | PyUnicodeObject *v; |
| 4889 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4890 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4891 | PyObject *errorHandler = NULL; |
| 4892 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4893 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4894 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4895 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4896 | /* Default to Latin-1 */ |
| 4897 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4898 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4899 | |
| 4900 | v = _PyUnicode_New(size); |
| 4901 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4902 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4903 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4904 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4905 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4906 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4907 | if (PyUnicode_CheckExact(mapping)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4908 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 4909 | maplen = PyUnicode_GET_SIZE(mapping); |
| 4910 | while (s < e) { |
| 4911 | unsigned char ch = *s; |
| 4912 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4913 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4914 | if (ch < maplen) |
| 4915 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4916 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4917 | if (x == 0xfffe) { |
| 4918 | /* undefined mapping */ |
| 4919 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4920 | startinpos = s-starts; |
| 4921 | endinpos = startinpos+1; |
| 4922 | if (unicode_decode_call_errorhandler( |
| 4923 | errors, &errorHandler, |
| 4924 | "charmap", "character maps to <undefined>", |
| 4925 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 4926 | &v, &outpos, &p)) { |
| 4927 | goto onError; |
| 4928 | } |
| 4929 | continue; |
| 4930 | } |
| 4931 | *p++ = x; |
| 4932 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4933 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4934 | } |
| 4935 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4936 | while (s < e) { |
| 4937 | unsigned char ch = *s; |
| 4938 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4939 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4940 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 4941 | w = PyLong_FromLong((long)ch); |
| 4942 | if (w == NULL) |
| 4943 | goto onError; |
| 4944 | x = PyObject_GetItem(mapping, w); |
| 4945 | Py_DECREF(w); |
| 4946 | if (x == NULL) { |
| 4947 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4948 | /* No mapping found means: mapping is undefined. */ |
| 4949 | PyErr_Clear(); |
| 4950 | x = Py_None; |
| 4951 | Py_INCREF(x); |
| 4952 | } else |
| 4953 | goto onError; |
| 4954 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4955 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4956 | /* Apply mapping */ |
| 4957 | if (PyLong_Check(x)) { |
| 4958 | long value = PyLong_AS_LONG(x); |
| 4959 | if (value < 0 || value > 65535) { |
| 4960 | PyErr_SetString(PyExc_TypeError, |
| 4961 | "character mapping must be in range(65536)"); |
| 4962 | Py_DECREF(x); |
| 4963 | goto onError; |
| 4964 | } |
| 4965 | *p++ = (Py_UNICODE)value; |
| 4966 | } |
| 4967 | else if (x == Py_None) { |
| 4968 | /* undefined mapping */ |
| 4969 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4970 | startinpos = s-starts; |
| 4971 | endinpos = startinpos+1; |
| 4972 | if (unicode_decode_call_errorhandler( |
| 4973 | errors, &errorHandler, |
| 4974 | "charmap", "character maps to <undefined>", |
| 4975 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 4976 | &v, &outpos, &p)) { |
| 4977 | Py_DECREF(x); |
| 4978 | goto onError; |
| 4979 | } |
| 4980 | Py_DECREF(x); |
| 4981 | continue; |
| 4982 | } |
| 4983 | else if (PyUnicode_Check(x)) { |
| 4984 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4985 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4986 | if (targetsize == 1) |
| 4987 | /* 1-1 mapping */ |
| 4988 | *p++ = *PyUnicode_AS_UNICODE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4989 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4990 | else if (targetsize > 1) { |
| 4991 | /* 1-n mapping */ |
| 4992 | if (targetsize > extrachars) { |
| 4993 | /* resize first */ |
| 4994 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 4995 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 4996 | (targetsize << 2); |
| 4997 | extrachars += needed; |
| 4998 | /* XXX overflow detection missing */ |
| 4999 | if (_PyUnicode_Resize(&v, |
| 5000 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 5001 | Py_DECREF(x); |
| 5002 | goto onError; |
| 5003 | } |
| 5004 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 5005 | } |
| 5006 | Py_UNICODE_COPY(p, |
| 5007 | PyUnicode_AS_UNICODE(x), |
| 5008 | targetsize); |
| 5009 | p += targetsize; |
| 5010 | extrachars -= targetsize; |
| 5011 | } |
| 5012 | /* 1-0 mapping: skip the character */ |
| 5013 | } |
| 5014 | else { |
| 5015 | /* wrong return value */ |
| 5016 | PyErr_SetString(PyExc_TypeError, |
| 5017 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5018 | Py_DECREF(x); |
| 5019 | goto onError; |
| 5020 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5021 | Py_DECREF(x); |
| 5022 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5023 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5024 | } |
| 5025 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5026 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 5027 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5028 | Py_XDECREF(errorHandler); |
| 5029 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5030 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5031 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5032 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5033 | Py_XDECREF(errorHandler); |
| 5034 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5035 | Py_XDECREF(v); |
| 5036 | return NULL; |
| 5037 | } |
| 5038 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5039 | /* Charmap encoding: the lookup table */ |
| 5040 | |
| 5041 | struct encoding_map{ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5042 | PyObject_HEAD |
| 5043 | unsigned char level1[32]; |
| 5044 | int count2, count3; |
| 5045 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5046 | }; |
| 5047 | |
| 5048 | static PyObject* |
| 5049 | encoding_map_size(PyObject *obj, PyObject* args) |
| 5050 | { |
| 5051 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5052 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5053 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5054 | } |
| 5055 | |
| 5056 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5057 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5058 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 5059 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5060 | }; |
| 5061 | |
| 5062 | static void |
| 5063 | encoding_map_dealloc(PyObject* o) |
| 5064 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5065 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5066 | } |
| 5067 | |
| 5068 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5069 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5070 | "EncodingMap", /*tp_name*/ |
| 5071 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 5072 | 0, /*tp_itemsize*/ |
| 5073 | /* methods */ |
| 5074 | encoding_map_dealloc, /*tp_dealloc*/ |
| 5075 | 0, /*tp_print*/ |
| 5076 | 0, /*tp_getattr*/ |
| 5077 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 5078 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5079 | 0, /*tp_repr*/ |
| 5080 | 0, /*tp_as_number*/ |
| 5081 | 0, /*tp_as_sequence*/ |
| 5082 | 0, /*tp_as_mapping*/ |
| 5083 | 0, /*tp_hash*/ |
| 5084 | 0, /*tp_call*/ |
| 5085 | 0, /*tp_str*/ |
| 5086 | 0, /*tp_getattro*/ |
| 5087 | 0, /*tp_setattro*/ |
| 5088 | 0, /*tp_as_buffer*/ |
| 5089 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 5090 | 0, /*tp_doc*/ |
| 5091 | 0, /*tp_traverse*/ |
| 5092 | 0, /*tp_clear*/ |
| 5093 | 0, /*tp_richcompare*/ |
| 5094 | 0, /*tp_weaklistoffset*/ |
| 5095 | 0, /*tp_iter*/ |
| 5096 | 0, /*tp_iternext*/ |
| 5097 | encoding_map_methods, /*tp_methods*/ |
| 5098 | 0, /*tp_members*/ |
| 5099 | 0, /*tp_getset*/ |
| 5100 | 0, /*tp_base*/ |
| 5101 | 0, /*tp_dict*/ |
| 5102 | 0, /*tp_descr_get*/ |
| 5103 | 0, /*tp_descr_set*/ |
| 5104 | 0, /*tp_dictoffset*/ |
| 5105 | 0, /*tp_init*/ |
| 5106 | 0, /*tp_alloc*/ |
| 5107 | 0, /*tp_new*/ |
| 5108 | 0, /*tp_free*/ |
| 5109 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5110 | }; |
| 5111 | |
| 5112 | PyObject* |
| 5113 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 5114 | { |
| 5115 | Py_UNICODE *decode; |
| 5116 | PyObject *result; |
| 5117 | struct encoding_map *mresult; |
| 5118 | int i; |
| 5119 | int need_dict = 0; |
| 5120 | unsigned char level1[32]; |
| 5121 | unsigned char level2[512]; |
| 5122 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 5123 | int count2 = 0, count3 = 0; |
| 5124 | |
| 5125 | if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { |
| 5126 | PyErr_BadArgument(); |
| 5127 | return NULL; |
| 5128 | } |
| 5129 | decode = PyUnicode_AS_UNICODE(string); |
| 5130 | memset(level1, 0xFF, sizeof level1); |
| 5131 | memset(level2, 0xFF, sizeof level2); |
| 5132 | |
| 5133 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 5134 | or if there are non-BMP characters, we need to use |
| 5135 | a mapping dictionary. */ |
| 5136 | if (decode[0] != 0) |
| 5137 | need_dict = 1; |
| 5138 | for (i = 1; i < 256; i++) { |
| 5139 | int l1, l2; |
| 5140 | if (decode[i] == 0 |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5141 | #ifdef Py_UNICODE_WIDE |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5142 | || decode[i] > 0xFFFF |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5143 | #endif |
| 5144 | ) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5145 | need_dict = 1; |
| 5146 | break; |
| 5147 | } |
| 5148 | if (decode[i] == 0xFFFE) |
| 5149 | /* unmapped character */ |
| 5150 | continue; |
| 5151 | l1 = decode[i] >> 11; |
| 5152 | l2 = decode[i] >> 7; |
| 5153 | if (level1[l1] == 0xFF) |
| 5154 | level1[l1] = count2++; |
| 5155 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5156 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5157 | } |
| 5158 | |
| 5159 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 5160 | need_dict = 1; |
| 5161 | |
| 5162 | if (need_dict) { |
| 5163 | PyObject *result = PyDict_New(); |
| 5164 | PyObject *key, *value; |
| 5165 | if (!result) |
| 5166 | return NULL; |
| 5167 | for (i = 0; i < 256; i++) { |
| 5168 | key = value = NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5169 | key = PyLong_FromLong(decode[i]); |
| 5170 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5171 | if (!key || !value) |
| 5172 | goto failed1; |
| 5173 | if (PyDict_SetItem(result, key, value) == -1) |
| 5174 | goto failed1; |
| 5175 | Py_DECREF(key); |
| 5176 | Py_DECREF(value); |
| 5177 | } |
| 5178 | return result; |
| 5179 | failed1: |
| 5180 | Py_XDECREF(key); |
| 5181 | Py_XDECREF(value); |
| 5182 | Py_DECREF(result); |
| 5183 | return NULL; |
| 5184 | } |
| 5185 | |
| 5186 | /* Create a three-level trie */ |
| 5187 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 5188 | 16*count2 + 128*count3 - 1); |
| 5189 | if (!result) |
| 5190 | return PyErr_NoMemory(); |
| 5191 | PyObject_Init(result, &EncodingMapType); |
| 5192 | mresult = (struct encoding_map*)result; |
| 5193 | mresult->count2 = count2; |
| 5194 | mresult->count3 = count3; |
| 5195 | mlevel1 = mresult->level1; |
| 5196 | mlevel2 = mresult->level23; |
| 5197 | mlevel3 = mresult->level23 + 16*count2; |
| 5198 | memcpy(mlevel1, level1, 32); |
| 5199 | memset(mlevel2, 0xFF, 16*count2); |
| 5200 | memset(mlevel3, 0, 128*count3); |
| 5201 | count3 = 0; |
| 5202 | for (i = 1; i < 256; i++) { |
| 5203 | int o1, o2, o3, i2, i3; |
| 5204 | if (decode[i] == 0xFFFE) |
| 5205 | /* unmapped character */ |
| 5206 | continue; |
| 5207 | o1 = decode[i]>>11; |
| 5208 | o2 = (decode[i]>>7) & 0xF; |
| 5209 | i2 = 16*mlevel1[o1] + o2; |
| 5210 | if (mlevel2[i2] == 0xFF) |
| 5211 | mlevel2[i2] = count3++; |
| 5212 | o3 = decode[i] & 0x7F; |
| 5213 | i3 = 128*mlevel2[i2] + o3; |
| 5214 | mlevel3[i3] = i; |
| 5215 | } |
| 5216 | return result; |
| 5217 | } |
| 5218 | |
| 5219 | static int |
| 5220 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 5221 | { |
| 5222 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 5223 | int l1 = c>>11; |
| 5224 | int l2 = (c>>7) & 0xF; |
| 5225 | int l3 = c & 0x7F; |
| 5226 | int i; |
| 5227 | |
| 5228 | #ifdef Py_UNICODE_WIDE |
| 5229 | if (c > 0xFFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5230 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5231 | } |
| 5232 | #endif |
| 5233 | if (c == 0) |
| 5234 | return 0; |
| 5235 | /* level 1*/ |
| 5236 | i = map->level1[l1]; |
| 5237 | if (i == 0xFF) { |
| 5238 | return -1; |
| 5239 | } |
| 5240 | /* level 2*/ |
| 5241 | i = map->level23[16*i+l2]; |
| 5242 | if (i == 0xFF) { |
| 5243 | return -1; |
| 5244 | } |
| 5245 | /* level 3 */ |
| 5246 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 5247 | if (i == 0) { |
| 5248 | return -1; |
| 5249 | } |
| 5250 | return i; |
| 5251 | } |
| 5252 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5253 | /* Lookup the character ch in the mapping. If the character |
| 5254 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 5255 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5256 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5257 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5258 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5259 | PyObject *x; |
| 5260 | |
| 5261 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5262 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5263 | x = PyObject_GetItem(mapping, w); |
| 5264 | Py_DECREF(w); |
| 5265 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5266 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 5267 | /* No mapping found means: mapping is undefined. */ |
| 5268 | PyErr_Clear(); |
| 5269 | x = Py_None; |
| 5270 | Py_INCREF(x); |
| 5271 | return x; |
| 5272 | } else |
| 5273 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5274 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 5275 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5276 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5277 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5278 | long value = PyLong_AS_LONG(x); |
| 5279 | if (value < 0 || value > 255) { |
| 5280 | PyErr_SetString(PyExc_TypeError, |
| 5281 | "character mapping must be in range(256)"); |
| 5282 | Py_DECREF(x); |
| 5283 | return NULL; |
| 5284 | } |
| 5285 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5286 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5287 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5288 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5289 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5290 | /* wrong return value */ |
| 5291 | PyErr_Format(PyExc_TypeError, |
| 5292 | "character mapping must return integer, bytes or None, not %.400s", |
| 5293 | x->ob_type->tp_name); |
| 5294 | Py_DECREF(x); |
| 5295 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5296 | } |
| 5297 | } |
| 5298 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5299 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5300 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5301 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5302 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 5303 | /* exponentially overallocate to minimize reallocations */ |
| 5304 | if (requiredsize < 2*outsize) |
| 5305 | requiredsize = 2*outsize; |
| 5306 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 5307 | return -1; |
| 5308 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5309 | } |
| 5310 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5311 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5312 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5313 | }charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5314 | /* 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] | 5315 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5316 | space is available. Return a new reference to the object that |
| 5317 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 5318 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 5319 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5320 | static |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5321 | charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5322 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5323 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5324 | PyObject *rep; |
| 5325 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5326 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5327 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5328 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5329 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5330 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5331 | if (res == -1) |
| 5332 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5333 | if (outsize<requiredsize) |
| 5334 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 5335 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5336 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5337 | outstart[(*outpos)++] = (char)res; |
| 5338 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5339 | } |
| 5340 | |
| 5341 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5342 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5343 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5344 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5345 | Py_DECREF(rep); |
| 5346 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5347 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5348 | if (PyLong_Check(rep)) { |
| 5349 | Py_ssize_t requiredsize = *outpos+1; |
| 5350 | if (outsize<requiredsize) |
| 5351 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 5352 | Py_DECREF(rep); |
| 5353 | return enc_EXCEPTION; |
| 5354 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5355 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5356 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5357 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5358 | else { |
| 5359 | const char *repchars = PyBytes_AS_STRING(rep); |
| 5360 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 5361 | Py_ssize_t requiredsize = *outpos+repsize; |
| 5362 | if (outsize<requiredsize) |
| 5363 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 5364 | Py_DECREF(rep); |
| 5365 | return enc_EXCEPTION; |
| 5366 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5367 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5368 | memcpy(outstart + *outpos, repchars, repsize); |
| 5369 | *outpos += repsize; |
| 5370 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5371 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5372 | Py_DECREF(rep); |
| 5373 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5374 | } |
| 5375 | |
| 5376 | /* handle an error in PyUnicode_EncodeCharmap |
| 5377 | Return 0 on success, -1 on error */ |
| 5378 | static |
| 5379 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5380 | 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] | 5381 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 5382 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5383 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5384 | { |
| 5385 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5386 | Py_ssize_t repsize; |
| 5387 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5388 | Py_UNICODE *uni2; |
| 5389 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5390 | Py_ssize_t collstartpos = *inpos; |
| 5391 | Py_ssize_t collendpos = *inpos+1; |
| 5392 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5393 | char *encoding = "charmap"; |
| 5394 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5395 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5396 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5397 | /* find all unencodable characters */ |
| 5398 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5399 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5400 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5401 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 5402 | if (res != -1) |
| 5403 | break; |
| 5404 | ++collendpos; |
| 5405 | continue; |
| 5406 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5407 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5408 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 5409 | if (rep==NULL) |
| 5410 | return -1; |
| 5411 | else if (rep!=Py_None) { |
| 5412 | Py_DECREF(rep); |
| 5413 | break; |
| 5414 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5415 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5416 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5417 | } |
| 5418 | /* cache callback name lookup |
| 5419 | * (if not done yet, i.e. it's the first error) */ |
| 5420 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5421 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5422 | *known_errorHandler = 1; |
| 5423 | else if (!strcmp(errors, "replace")) |
| 5424 | *known_errorHandler = 2; |
| 5425 | else if (!strcmp(errors, "ignore")) |
| 5426 | *known_errorHandler = 3; |
| 5427 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5428 | *known_errorHandler = 4; |
| 5429 | else |
| 5430 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5431 | } |
| 5432 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5433 | case 1: /* strict */ |
| 5434 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5435 | return -1; |
| 5436 | case 2: /* replace */ |
| 5437 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5438 | x = charmapencode_output('?', mapping, res, respos); |
| 5439 | if (x==enc_EXCEPTION) { |
| 5440 | return -1; |
| 5441 | } |
| 5442 | else if (x==enc_FAILED) { |
| 5443 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5444 | return -1; |
| 5445 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5446 | } |
| 5447 | /* fall through */ |
| 5448 | case 3: /* ignore */ |
| 5449 | *inpos = collendpos; |
| 5450 | break; |
| 5451 | case 4: /* xmlcharrefreplace */ |
| 5452 | /* generate replacement (temporarily (mis)uses p) */ |
| 5453 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5454 | char buffer[2+29+1+1]; |
| 5455 | char *cp; |
| 5456 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 5457 | for (cp = buffer; *cp; ++cp) { |
| 5458 | x = charmapencode_output(*cp, mapping, res, respos); |
| 5459 | if (x==enc_EXCEPTION) |
| 5460 | return -1; |
| 5461 | else if (x==enc_FAILED) { |
| 5462 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5463 | return -1; |
| 5464 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5465 | } |
| 5466 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5467 | *inpos = collendpos; |
| 5468 | break; |
| 5469 | default: |
| 5470 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5471 | encoding, reason, p, size, exceptionObject, |
| 5472 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5473 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5474 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5475 | if (PyBytes_Check(repunicode)) { |
| 5476 | /* Directly copy bytes result to output. */ |
| 5477 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 5478 | Py_ssize_t requiredsize; |
| 5479 | repsize = PyBytes_Size(repunicode); |
| 5480 | requiredsize = *respos + repsize; |
| 5481 | if (requiredsize > outsize) |
| 5482 | /* Make room for all additional bytes. */ |
| 5483 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 5484 | Py_DECREF(repunicode); |
| 5485 | return -1; |
| 5486 | } |
| 5487 | memcpy(PyBytes_AsString(*res) + *respos, |
| 5488 | PyBytes_AsString(repunicode), repsize); |
| 5489 | *respos += repsize; |
| 5490 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5491 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5492 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5493 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5494 | /* generate replacement */ |
| 5495 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5496 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5497 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 5498 | if (x==enc_EXCEPTION) { |
| 5499 | return -1; |
| 5500 | } |
| 5501 | else if (x==enc_FAILED) { |
| 5502 | Py_DECREF(repunicode); |
| 5503 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5504 | return -1; |
| 5505 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5506 | } |
| 5507 | *inpos = newpos; |
| 5508 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5509 | } |
| 5510 | return 0; |
| 5511 | } |
| 5512 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5513 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5514 | Py_ssize_t size, |
| 5515 | PyObject *mapping, |
| 5516 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5517 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5518 | /* output object */ |
| 5519 | PyObject *res = NULL; |
| 5520 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5521 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5522 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5523 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5524 | PyObject *errorHandler = NULL; |
| 5525 | PyObject *exc = NULL; |
| 5526 | /* the following variable is used for caching string comparisons |
| 5527 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 5528 | * 3=ignore, 4=xmlcharrefreplace */ |
| 5529 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5530 | |
| 5531 | /* Default to Latin-1 */ |
| 5532 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5533 | return PyUnicode_EncodeLatin1(p, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5534 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5535 | /* allocate enough for a simple encoding without |
| 5536 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5537 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5538 | if (res == NULL) |
| 5539 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 5540 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5541 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5542 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5543 | while (inpos<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5544 | /* try to encode it */ |
| 5545 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 5546 | if (x==enc_EXCEPTION) /* error */ |
| 5547 | goto onError; |
| 5548 | if (x==enc_FAILED) { /* unencodable character */ |
| 5549 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 5550 | &exc, |
| 5551 | &known_errorHandler, &errorHandler, errors, |
| 5552 | &res, &respos)) { |
| 5553 | goto onError; |
| 5554 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5555 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5556 | else |
| 5557 | /* done with this character => adjust input position */ |
| 5558 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5559 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5560 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5561 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5562 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5563 | if (_PyBytes_Resize(&res, respos) < 0) |
| 5564 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5565 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5566 | Py_XDECREF(exc); |
| 5567 | Py_XDECREF(errorHandler); |
| 5568 | return res; |
| 5569 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5570 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5571 | Py_XDECREF(res); |
| 5572 | Py_XDECREF(exc); |
| 5573 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5574 | return NULL; |
| 5575 | } |
| 5576 | |
| 5577 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5578 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5579 | { |
| 5580 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5581 | PyErr_BadArgument(); |
| 5582 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5583 | } |
| 5584 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5585 | PyUnicode_GET_SIZE(unicode), |
| 5586 | mapping, |
| 5587 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5588 | } |
| 5589 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5590 | /* create or adjust a UnicodeTranslateError */ |
| 5591 | static void make_translate_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5592 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5593 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5594 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5595 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5596 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5597 | *exceptionObject = PyUnicodeTranslateError_Create( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5598 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5599 | } |
| 5600 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5601 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 5602 | goto onError; |
| 5603 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 5604 | goto onError; |
| 5605 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 5606 | goto onError; |
| 5607 | return; |
| 5608 | onError: |
| 5609 | Py_DECREF(*exceptionObject); |
| 5610 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5611 | } |
| 5612 | } |
| 5613 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5614 | /* raises a UnicodeTranslateError */ |
| 5615 | static void raise_translate_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5616 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5617 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5618 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5619 | { |
| 5620 | make_translate_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5621 | unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5622 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5623 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5624 | } |
| 5625 | |
| 5626 | /* error handling callback helper: |
| 5627 | build arguments, call the callback and check the arguments, |
| 5628 | put the result into newpos and return the replacement string, which |
| 5629 | has to be freed by the caller */ |
| 5630 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5631 | PyObject **errorHandler, |
| 5632 | const char *reason, |
| 5633 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 5634 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5635 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5636 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 5637 | 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] | 5638 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5639 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5640 | PyObject *restuple; |
| 5641 | PyObject *resunicode; |
| 5642 | |
| 5643 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5644 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5645 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5646 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5647 | } |
| 5648 | |
| 5649 | make_translate_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5650 | unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5651 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5652 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5653 | |
| 5654 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5655 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5656 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5657 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5658 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 5659 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5660 | Py_DECREF(restuple); |
| 5661 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5662 | } |
| 5663 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5664 | &resunicode, &i_newpos)) { |
| 5665 | Py_DECREF(restuple); |
| 5666 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5667 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5668 | if (i_newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5669 | *newpos = size+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5670 | else |
| 5671 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5672 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5673 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 5674 | Py_DECREF(restuple); |
| 5675 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5676 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5677 | Py_INCREF(resunicode); |
| 5678 | Py_DECREF(restuple); |
| 5679 | return resunicode; |
| 5680 | } |
| 5681 | |
| 5682 | /* Lookup the character ch in the mapping and put the result in result, |
| 5683 | which must be decrefed by the caller. |
| 5684 | Return 0 on success, -1 on error */ |
| 5685 | static |
| 5686 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 5687 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5688 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5689 | PyObject *x; |
| 5690 | |
| 5691 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5692 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5693 | x = PyObject_GetItem(mapping, w); |
| 5694 | Py_DECREF(w); |
| 5695 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5696 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 5697 | /* No mapping found means: use 1:1 mapping. */ |
| 5698 | PyErr_Clear(); |
| 5699 | *result = NULL; |
| 5700 | return 0; |
| 5701 | } else |
| 5702 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5703 | } |
| 5704 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5705 | *result = x; |
| 5706 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5707 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5708 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5709 | long value = PyLong_AS_LONG(x); |
| 5710 | long max = PyUnicode_GetMax(); |
| 5711 | if (value < 0 || value > max) { |
| 5712 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 5713 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5714 | Py_DECREF(x); |
| 5715 | return -1; |
| 5716 | } |
| 5717 | *result = x; |
| 5718 | return 0; |
| 5719 | } |
| 5720 | else if (PyUnicode_Check(x)) { |
| 5721 | *result = x; |
| 5722 | return 0; |
| 5723 | } |
| 5724 | else { |
| 5725 | /* wrong return value */ |
| 5726 | PyErr_SetString(PyExc_TypeError, |
| 5727 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5728 | Py_DECREF(x); |
| 5729 | return -1; |
| 5730 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5731 | } |
| 5732 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5733 | if not reallocate and adjust various state variables. |
| 5734 | Return 0 on success, -1 on error */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5735 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5736 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5737 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5738 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5739 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5740 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5741 | /* remember old output position */ |
| 5742 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
| 5743 | /* exponentially overallocate to minimize reallocations */ |
| 5744 | if (requiredsize < 2 * oldsize) |
| 5745 | requiredsize = 2 * oldsize; |
| 5746 | if (PyUnicode_Resize(outobj, requiredsize) < 0) |
| 5747 | return -1; |
| 5748 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5749 | } |
| 5750 | return 0; |
| 5751 | } |
| 5752 | /* lookup the character, put the result in the output string and adjust |
| 5753 | various state variables. Return a new reference to the object that |
| 5754 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 5755 | undefined (in which case no character was written). |
| 5756 | The called must decref result. |
| 5757 | Return 0 on success, -1 on error. */ |
| 5758 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5759 | int charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5760 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
| 5761 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5762 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5763 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5764 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5765 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5766 | /* not found => default to 1:1 mapping */ |
| 5767 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5768 | } |
| 5769 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5770 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5771 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5772 | /* no overflow check, because we know that the space is enough */ |
| 5773 | *(*outp)++ = (Py_UNICODE)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5774 | } |
| 5775 | else if (PyUnicode_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5776 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
| 5777 | if (repsize==1) { |
| 5778 | /* no overflow check, because we know that the space is enough */ |
| 5779 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 5780 | } |
| 5781 | else if (repsize!=0) { |
| 5782 | /* more than one character */ |
| 5783 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
| 5784 | (insize - (curinp-startinp)) + |
| 5785 | repsize - 1; |
| 5786 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
| 5787 | return -1; |
| 5788 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 5789 | *outp += repsize; |
| 5790 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5791 | } |
| 5792 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5793 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5794 | return 0; |
| 5795 | } |
| 5796 | |
| 5797 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5798 | Py_ssize_t size, |
| 5799 | PyObject *mapping, |
| 5800 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5801 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5802 | /* output object */ |
| 5803 | PyObject *res = NULL; |
| 5804 | /* pointers to the beginning and end+1 of input */ |
| 5805 | const Py_UNICODE *startp = p; |
| 5806 | const Py_UNICODE *endp = p + size; |
| 5807 | /* pointer into the output */ |
| 5808 | Py_UNICODE *str; |
| 5809 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5810 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5811 | char *reason = "character maps to <undefined>"; |
| 5812 | PyObject *errorHandler = NULL; |
| 5813 | PyObject *exc = NULL; |
| 5814 | /* the following variable is used for caching string comparisons |
| 5815 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 5816 | * 3=ignore, 4=xmlcharrefreplace */ |
| 5817 | int known_errorHandler = -1; |
| 5818 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5819 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5820 | PyErr_BadArgument(); |
| 5821 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5822 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5823 | |
| 5824 | /* allocate enough for a simple 1:1 translation without |
| 5825 | replacements, if we need more, we'll resize */ |
| 5826 | res = PyUnicode_FromUnicode(NULL, size); |
| 5827 | if (res == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5828 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5829 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5830 | return res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5831 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5832 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5833 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5834 | /* try to encode it */ |
| 5835 | PyObject *x = NULL; |
| 5836 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
| 5837 | Py_XDECREF(x); |
| 5838 | goto onError; |
| 5839 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5840 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5841 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 5842 | ++p; |
| 5843 | else { /* untranslatable character */ |
| 5844 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 5845 | Py_ssize_t repsize; |
| 5846 | Py_ssize_t newpos; |
| 5847 | Py_UNICODE *uni2; |
| 5848 | /* startpos for collecting untranslatable chars */ |
| 5849 | const Py_UNICODE *collstart = p; |
| 5850 | const Py_UNICODE *collend = p+1; |
| 5851 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5852 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5853 | /* find all untranslatable characters */ |
| 5854 | while (collend < endp) { |
| 5855 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
| 5856 | goto onError; |
| 5857 | Py_XDECREF(x); |
| 5858 | if (x!=Py_None) |
| 5859 | break; |
| 5860 | ++collend; |
| 5861 | } |
| 5862 | /* cache callback name lookup |
| 5863 | * (if not done yet, i.e. it's the first error) */ |
| 5864 | if (known_errorHandler==-1) { |
| 5865 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5866 | known_errorHandler = 1; |
| 5867 | else if (!strcmp(errors, "replace")) |
| 5868 | known_errorHandler = 2; |
| 5869 | else if (!strcmp(errors, "ignore")) |
| 5870 | known_errorHandler = 3; |
| 5871 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5872 | known_errorHandler = 4; |
| 5873 | else |
| 5874 | known_errorHandler = 0; |
| 5875 | } |
| 5876 | switch (known_errorHandler) { |
| 5877 | case 1: /* strict */ |
| 5878 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5879 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5880 | case 2: /* replace */ |
| 5881 | /* No need to check for space, this is a 1:1 replacement */ |
| 5882 | for (coll = collstart; coll<collend; ++coll) |
| 5883 | *str++ = '?'; |
| 5884 | /* fall through */ |
| 5885 | case 3: /* ignore */ |
| 5886 | p = collend; |
| 5887 | break; |
| 5888 | case 4: /* xmlcharrefreplace */ |
| 5889 | /* generate replacement (temporarily (mis)uses p) */ |
| 5890 | for (p = collstart; p < collend; ++p) { |
| 5891 | char buffer[2+29+1+1]; |
| 5892 | char *cp; |
| 5893 | sprintf(buffer, "&#%d;", (int)*p); |
| 5894 | if (charmaptranslate_makespace(&res, &str, |
| 5895 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 5896 | goto onError; |
| 5897 | for (cp = buffer; *cp; ++cp) |
| 5898 | *str++ = *cp; |
| 5899 | } |
| 5900 | p = collend; |
| 5901 | break; |
| 5902 | default: |
| 5903 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 5904 | reason, startp, size, &exc, |
| 5905 | collstart-startp, collend-startp, &newpos); |
| 5906 | if (repunicode == NULL) |
| 5907 | goto onError; |
| 5908 | /* generate replacement */ |
| 5909 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5910 | if (charmaptranslate_makespace(&res, &str, |
| 5911 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 5912 | Py_DECREF(repunicode); |
| 5913 | goto onError; |
| 5914 | } |
| 5915 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 5916 | *str++ = *uni2; |
| 5917 | p = startp + newpos; |
| 5918 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5919 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5920 | } |
| 5921 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5922 | /* Resize if we allocated to much */ |
| 5923 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5924 | if (respos<PyUnicode_GET_SIZE(res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5925 | if (PyUnicode_Resize(&res, respos) < 0) |
| 5926 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5927 | } |
| 5928 | Py_XDECREF(exc); |
| 5929 | Py_XDECREF(errorHandler); |
| 5930 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5931 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5932 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5933 | Py_XDECREF(res); |
| 5934 | Py_XDECREF(exc); |
| 5935 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5936 | return NULL; |
| 5937 | } |
| 5938 | |
| 5939 | PyObject *PyUnicode_Translate(PyObject *str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5940 | PyObject *mapping, |
| 5941 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5942 | { |
| 5943 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5944 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5945 | str = PyUnicode_FromObject(str); |
| 5946 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5947 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5948 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5949 | PyUnicode_GET_SIZE(str), |
| 5950 | mapping, |
| 5951 | errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5952 | Py_DECREF(str); |
| 5953 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5954 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5955 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5956 | Py_XDECREF(str); |
| 5957 | return NULL; |
| 5958 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5959 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5960 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 5961 | |
| 5962 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5963 | Py_ssize_t length, |
| 5964 | char *output, |
| 5965 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5966 | { |
| 5967 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5968 | PyObject *errorHandler = NULL; |
| 5969 | PyObject *exc = NULL; |
| 5970 | const char *encoding = "decimal"; |
| 5971 | const char *reason = "invalid decimal Unicode string"; |
| 5972 | /* the following variable is used for caching string comparisons |
| 5973 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 5974 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5975 | |
| 5976 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5977 | PyErr_BadArgument(); |
| 5978 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5979 | } |
| 5980 | |
| 5981 | p = s; |
| 5982 | end = s + length; |
| 5983 | while (p < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5984 | register Py_UNICODE ch = *p; |
| 5985 | int decimal; |
| 5986 | PyObject *repunicode; |
| 5987 | Py_ssize_t repsize; |
| 5988 | Py_ssize_t newpos; |
| 5989 | Py_UNICODE *uni2; |
| 5990 | Py_UNICODE *collstart; |
| 5991 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5992 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5993 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5994 | *output++ = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5995 | ++p; |
| 5996 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5997 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5998 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5999 | if (decimal >= 0) { |
| 6000 | *output++ = '0' + decimal; |
| 6001 | ++p; |
| 6002 | continue; |
| 6003 | } |
| 6004 | if (0 < ch && ch < 256) { |
| 6005 | *output++ = (char)ch; |
| 6006 | ++p; |
| 6007 | continue; |
| 6008 | } |
| 6009 | /* All other characters are considered unencodable */ |
| 6010 | collstart = p; |
| 6011 | collend = p+1; |
| 6012 | while (collend < end) { |
| 6013 | if ((0 < *collend && *collend < 256) || |
| 6014 | !Py_UNICODE_ISSPACE(*collend) || |
| 6015 | Py_UNICODE_TODECIMAL(*collend)) |
| 6016 | break; |
| 6017 | } |
| 6018 | /* cache callback name lookup |
| 6019 | * (if not done yet, i.e. it's the first error) */ |
| 6020 | if (known_errorHandler==-1) { |
| 6021 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6022 | known_errorHandler = 1; |
| 6023 | else if (!strcmp(errors, "replace")) |
| 6024 | known_errorHandler = 2; |
| 6025 | else if (!strcmp(errors, "ignore")) |
| 6026 | known_errorHandler = 3; |
| 6027 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6028 | known_errorHandler = 4; |
| 6029 | else |
| 6030 | known_errorHandler = 0; |
| 6031 | } |
| 6032 | switch (known_errorHandler) { |
| 6033 | case 1: /* strict */ |
| 6034 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 6035 | goto onError; |
| 6036 | case 2: /* replace */ |
| 6037 | for (p = collstart; p < collend; ++p) |
| 6038 | *output++ = '?'; |
| 6039 | /* fall through */ |
| 6040 | case 3: /* ignore */ |
| 6041 | p = collend; |
| 6042 | break; |
| 6043 | case 4: /* xmlcharrefreplace */ |
| 6044 | /* generate replacement (temporarily (mis)uses p) */ |
| 6045 | for (p = collstart; p < collend; ++p) |
| 6046 | output += sprintf(output, "&#%d;", (int)*p); |
| 6047 | p = collend; |
| 6048 | break; |
| 6049 | default: |
| 6050 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 6051 | encoding, reason, s, length, &exc, |
| 6052 | collstart-s, collend-s, &newpos); |
| 6053 | if (repunicode == NULL) |
| 6054 | goto onError; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6055 | if (!PyUnicode_Check(repunicode)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6056 | /* Byte results not supported, since they have no decimal property. */ |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6057 | PyErr_SetString(PyExc_TypeError, "error handler should return unicode"); |
| 6058 | Py_DECREF(repunicode); |
| 6059 | goto onError; |
| 6060 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6061 | /* generate replacement */ |
| 6062 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 6063 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 6064 | Py_UNICODE ch = *uni2; |
| 6065 | if (Py_UNICODE_ISSPACE(ch)) |
| 6066 | *output++ = ' '; |
| 6067 | else { |
| 6068 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 6069 | if (decimal >= 0) |
| 6070 | *output++ = '0' + decimal; |
| 6071 | else if (0 < ch && ch < 256) |
| 6072 | *output++ = (char)ch; |
| 6073 | else { |
| 6074 | Py_DECREF(repunicode); |
| 6075 | raise_encode_exception(&exc, encoding, |
| 6076 | s, length, collstart-s, collend-s, reason); |
| 6077 | goto onError; |
| 6078 | } |
| 6079 | } |
| 6080 | } |
| 6081 | p = s + newpos; |
| 6082 | Py_DECREF(repunicode); |
| 6083 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6084 | } |
| 6085 | /* 0-terminate the output string */ |
| 6086 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6087 | Py_XDECREF(exc); |
| 6088 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6089 | return 0; |
| 6090 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6091 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6092 | Py_XDECREF(exc); |
| 6093 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6094 | return -1; |
| 6095 | } |
| 6096 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6097 | /* --- Helpers ------------------------------------------------------------ */ |
| 6098 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 6099 | #include "stringlib/unicodedefs.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6100 | #include "stringlib/fastsearch.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6101 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6102 | #include "stringlib/count.h" |
| 6103 | #include "stringlib/find.h" |
| 6104 | #include "stringlib/partition.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6105 | #include "stringlib/split.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6106 | |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 6107 | #define _Py_InsertThousandsGrouping _PyUnicode_InsertThousandsGrouping |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame] | 6108 | #define _Py_InsertThousandsGroupingLocale _PyUnicode_InsertThousandsGroupingLocale |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 6109 | #include "stringlib/localeutil.h" |
| 6110 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6111 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6112 | #define ADJUST_INDICES(start, end, len) \ |
| 6113 | if (end > len) \ |
| 6114 | end = len; \ |
| 6115 | else if (end < 0) { \ |
| 6116 | end += len; \ |
| 6117 | if (end < 0) \ |
| 6118 | end = 0; \ |
| 6119 | } \ |
| 6120 | if (start < 0) { \ |
| 6121 | start += len; \ |
| 6122 | if (start < 0) \ |
| 6123 | start = 0; \ |
| 6124 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6125 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6126 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6127 | PyObject *substr, |
| 6128 | Py_ssize_t start, |
| 6129 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6130 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6131 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6132 | PyUnicodeObject* str_obj; |
| 6133 | PyUnicodeObject* sub_obj; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6134 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6135 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
| 6136 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6137 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6138 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
| 6139 | if (!sub_obj) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6140 | Py_DECREF(str_obj); |
| 6141 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6142 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6143 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6144 | ADJUST_INDICES(start, end, str_obj->length); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6145 | result = stringlib_count( |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6146 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length, |
| 6147 | PY_SSIZE_T_MAX |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6148 | ); |
| 6149 | |
| 6150 | Py_DECREF(sub_obj); |
| 6151 | Py_DECREF(str_obj); |
| 6152 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6153 | return result; |
| 6154 | } |
| 6155 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6156 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6157 | PyObject *sub, |
| 6158 | Py_ssize_t start, |
| 6159 | Py_ssize_t end, |
| 6160 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6161 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6162 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6163 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6164 | str = PyUnicode_FromObject(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6165 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6166 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6167 | sub = PyUnicode_FromObject(sub); |
| 6168 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6169 | Py_DECREF(str); |
| 6170 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6171 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6172 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6173 | if (direction > 0) |
| 6174 | result = stringlib_find_slice( |
| 6175 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 6176 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 6177 | start, end |
| 6178 | ); |
| 6179 | else |
| 6180 | result = stringlib_rfind_slice( |
| 6181 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 6182 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 6183 | start, end |
| 6184 | ); |
| 6185 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6186 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6187 | Py_DECREF(sub); |
| 6188 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6189 | return result; |
| 6190 | } |
| 6191 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6192 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6193 | int tailmatch(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6194 | PyUnicodeObject *substring, |
| 6195 | Py_ssize_t start, |
| 6196 | Py_ssize_t end, |
| 6197 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6198 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6199 | if (substring->length == 0) |
| 6200 | return 1; |
| 6201 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6202 | ADJUST_INDICES(start, end, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6203 | end -= substring->length; |
| 6204 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6205 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6206 | |
| 6207 | if (direction > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6208 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 6209 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6210 | } else { |
| 6211 | if (Py_UNICODE_MATCH(self, start, substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6212 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6213 | } |
| 6214 | |
| 6215 | return 0; |
| 6216 | } |
| 6217 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6218 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6219 | PyObject *substr, |
| 6220 | Py_ssize_t start, |
| 6221 | Py_ssize_t end, |
| 6222 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6223 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6224 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6225 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6226 | str = PyUnicode_FromObject(str); |
| 6227 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6228 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6229 | substr = PyUnicode_FromObject(substr); |
| 6230 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6231 | Py_DECREF(str); |
| 6232 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6233 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6234 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6235 | result = tailmatch((PyUnicodeObject *)str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6236 | (PyUnicodeObject *)substr, |
| 6237 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6238 | Py_DECREF(str); |
| 6239 | Py_DECREF(substr); |
| 6240 | return result; |
| 6241 | } |
| 6242 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6243 | /* Apply fixfct filter to the Unicode object self and return a |
| 6244 | reference to the modified object */ |
| 6245 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6246 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6247 | PyObject *fixup(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6248 | int (*fixfct)(PyUnicodeObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6249 | { |
| 6250 | |
| 6251 | PyUnicodeObject *u; |
| 6252 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6253 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6254 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6255 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6256 | |
| 6257 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6258 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6259 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6260 | /* fixfct should return TRUE if it modified the buffer. If |
| 6261 | FALSE, return a reference to the original buffer instead |
| 6262 | (to save space, not time) */ |
| 6263 | Py_INCREF(self); |
| 6264 | Py_DECREF(u); |
| 6265 | return (PyObject*) self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6266 | } |
| 6267 | return (PyObject*) u; |
| 6268 | } |
| 6269 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6270 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6271 | int fixupper(PyUnicodeObject *self) |
| 6272 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6273 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6274 | Py_UNICODE *s = self->str; |
| 6275 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6276 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6277 | while (len-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6278 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6279 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6280 | ch = Py_UNICODE_TOUPPER(*s); |
| 6281 | if (ch != *s) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6282 | status = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6283 | *s = ch; |
| 6284 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6285 | s++; |
| 6286 | } |
| 6287 | |
| 6288 | return status; |
| 6289 | } |
| 6290 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6291 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6292 | int fixlower(PyUnicodeObject *self) |
| 6293 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6294 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6295 | Py_UNICODE *s = self->str; |
| 6296 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6297 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6298 | while (len-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6299 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6300 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6301 | ch = Py_UNICODE_TOLOWER(*s); |
| 6302 | if (ch != *s) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6303 | status = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6304 | *s = ch; |
| 6305 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6306 | s++; |
| 6307 | } |
| 6308 | |
| 6309 | return status; |
| 6310 | } |
| 6311 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6312 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6313 | int fixswapcase(PyUnicodeObject *self) |
| 6314 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6315 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6316 | Py_UNICODE *s = self->str; |
| 6317 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6318 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6319 | while (len-- > 0) { |
| 6320 | if (Py_UNICODE_ISUPPER(*s)) { |
| 6321 | *s = Py_UNICODE_TOLOWER(*s); |
| 6322 | status = 1; |
| 6323 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 6324 | *s = Py_UNICODE_TOUPPER(*s); |
| 6325 | status = 1; |
| 6326 | } |
| 6327 | s++; |
| 6328 | } |
| 6329 | |
| 6330 | return status; |
| 6331 | } |
| 6332 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6333 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6334 | int fixcapitalize(PyUnicodeObject *self) |
| 6335 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6336 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6337 | Py_UNICODE *s = self->str; |
| 6338 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6339 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6340 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6341 | return 0; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6342 | if (Py_UNICODE_ISLOWER(*s)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6343 | *s = Py_UNICODE_TOUPPER(*s); |
| 6344 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6345 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6346 | s++; |
| 6347 | while (--len > 0) { |
| 6348 | if (Py_UNICODE_ISUPPER(*s)) { |
| 6349 | *s = Py_UNICODE_TOLOWER(*s); |
| 6350 | status = 1; |
| 6351 | } |
| 6352 | s++; |
| 6353 | } |
| 6354 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6355 | } |
| 6356 | |
| 6357 | static |
| 6358 | int fixtitle(PyUnicodeObject *self) |
| 6359 | { |
| 6360 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6361 | register Py_UNICODE *e; |
| 6362 | int previous_is_cased; |
| 6363 | |
| 6364 | /* Shortcut for single character strings */ |
| 6365 | if (PyUnicode_GET_SIZE(self) == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6366 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 6367 | if (*p != ch) { |
| 6368 | *p = ch; |
| 6369 | return 1; |
| 6370 | } |
| 6371 | else |
| 6372 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6373 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6374 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6375 | e = p + PyUnicode_GET_SIZE(self); |
| 6376 | previous_is_cased = 0; |
| 6377 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6378 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6379 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6380 | if (previous_is_cased) |
| 6381 | *p = Py_UNICODE_TOLOWER(ch); |
| 6382 | else |
| 6383 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6384 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6385 | if (Py_UNICODE_ISLOWER(ch) || |
| 6386 | Py_UNICODE_ISUPPER(ch) || |
| 6387 | Py_UNICODE_ISTITLE(ch)) |
| 6388 | previous_is_cased = 1; |
| 6389 | else |
| 6390 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6391 | } |
| 6392 | return 1; |
| 6393 | } |
| 6394 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6395 | PyObject * |
| 6396 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6397 | { |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 6398 | const Py_UNICODE blank = ' '; |
| 6399 | const Py_UNICODE *sep = ␣ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6400 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6401 | PyUnicodeObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6402 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 6403 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6404 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 6405 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6406 | PyObject *item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6407 | Py_ssize_t sz, i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6408 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6409 | fseq = PySequence_Fast(seq, ""); |
| 6410 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6411 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6412 | } |
| 6413 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6414 | /* NOTE: the following code can't call back into Python code, |
| 6415 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 6416 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6417 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6418 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 6419 | /* If empty sequence, return u"". */ |
| 6420 | if (seqlen == 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6421 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 6422 | goto Done; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6423 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6424 | items = PySequence_Fast_ITEMS(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6425 | /* If singleton sequence with an exact Unicode, return that. */ |
| 6426 | if (seqlen == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6427 | item = items[0]; |
| 6428 | if (PyUnicode_CheckExact(item)) { |
| 6429 | Py_INCREF(item); |
| 6430 | res = (PyUnicodeObject *)item; |
| 6431 | goto Done; |
| 6432 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6433 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6434 | else { |
| 6435 | /* Set up sep and seplen */ |
| 6436 | if (separator == NULL) { |
| 6437 | sep = ␣ |
| 6438 | seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6439 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6440 | else { |
| 6441 | if (!PyUnicode_Check(separator)) { |
| 6442 | PyErr_Format(PyExc_TypeError, |
| 6443 | "separator: expected str instance," |
| 6444 | " %.80s found", |
| 6445 | Py_TYPE(separator)->tp_name); |
| 6446 | goto onError; |
| 6447 | } |
| 6448 | sep = PyUnicode_AS_UNICODE(separator); |
| 6449 | seplen = PyUnicode_GET_SIZE(separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6450 | } |
| 6451 | } |
| 6452 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6453 | /* There are at least two things to join, or else we have a subclass |
| 6454 | * of str in the sequence. |
| 6455 | * Do a pre-pass to figure out the total amount of space we'll |
| 6456 | * need (sz), and see whether all argument are strings. |
| 6457 | */ |
| 6458 | sz = 0; |
| 6459 | for (i = 0; i < seqlen; i++) { |
| 6460 | const Py_ssize_t old_sz = sz; |
| 6461 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6462 | if (!PyUnicode_Check(item)) { |
| 6463 | PyErr_Format(PyExc_TypeError, |
| 6464 | "sequence item %zd: expected str instance," |
| 6465 | " %.80s found", |
| 6466 | i, Py_TYPE(item)->tp_name); |
| 6467 | goto onError; |
| 6468 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6469 | sz += PyUnicode_GET_SIZE(item); |
| 6470 | if (i != 0) |
| 6471 | sz += seplen; |
| 6472 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 6473 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6474 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6475 | goto onError; |
| 6476 | } |
| 6477 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6478 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6479 | res = _PyUnicode_New(sz); |
| 6480 | if (res == NULL) |
| 6481 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 6482 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6483 | /* Catenate everything. */ |
| 6484 | res_p = PyUnicode_AS_UNICODE(res); |
| 6485 | for (i = 0; i < seqlen; ++i) { |
| 6486 | Py_ssize_t itemlen; |
| 6487 | item = items[i]; |
| 6488 | itemlen = PyUnicode_GET_SIZE(item); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6489 | /* Copy item, and maybe the separator. */ |
| 6490 | if (i) { |
| 6491 | Py_UNICODE_COPY(res_p, sep, seplen); |
| 6492 | res_p += seplen; |
| 6493 | } |
| 6494 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
| 6495 | res_p += itemlen; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6496 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6497 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6498 | Done: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6499 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6500 | return (PyObject *)res; |
| 6501 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6502 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6503 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6504 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6505 | return NULL; |
| 6506 | } |
| 6507 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6508 | static |
| 6509 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6510 | Py_ssize_t left, |
| 6511 | Py_ssize_t right, |
| 6512 | Py_UNICODE fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6513 | { |
| 6514 | PyUnicodeObject *u; |
| 6515 | |
| 6516 | if (left < 0) |
| 6517 | left = 0; |
| 6518 | if (right < 0) |
| 6519 | right = 0; |
| 6520 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6521 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6522 | Py_INCREF(self); |
| 6523 | return self; |
| 6524 | } |
| 6525 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6526 | if (left > PY_SSIZE_T_MAX - self->length || |
| 6527 | right > PY_SSIZE_T_MAX - (left + self->length)) { |
| 6528 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 6529 | return NULL; |
| 6530 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6531 | u = _PyUnicode_New(left + self->length + right); |
| 6532 | if (u) { |
| 6533 | if (left) |
| 6534 | Py_UNICODE_FILL(u->str, fill, left); |
| 6535 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 6536 | if (right) |
| 6537 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 6538 | } |
| 6539 | |
| 6540 | return u; |
| 6541 | } |
| 6542 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6543 | PyObject *PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6544 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6545 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6546 | |
| 6547 | string = PyUnicode_FromObject(string); |
| 6548 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6549 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6550 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6551 | list = stringlib_splitlines( |
| 6552 | (PyObject*) string, PyUnicode_AS_UNICODE(string), |
| 6553 | PyUnicode_GET_SIZE(string), keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6554 | |
| 6555 | Py_DECREF(string); |
| 6556 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6557 | } |
| 6558 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6559 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6560 | PyObject *split(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6561 | PyUnicodeObject *substring, |
| 6562 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6563 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6564 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6565 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6566 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6567 | if (substring == NULL) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6568 | return stringlib_split_whitespace( |
| 6569 | (PyObject*) self, self->str, self->length, maxcount |
| 6570 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6571 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6572 | return stringlib_split( |
| 6573 | (PyObject*) self, self->str, self->length, |
| 6574 | substring->str, substring->length, |
| 6575 | maxcount |
| 6576 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6577 | } |
| 6578 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6579 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6580 | PyObject *rsplit(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6581 | PyUnicodeObject *substring, |
| 6582 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6583 | { |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6584 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6585 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6586 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6587 | if (substring == NULL) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6588 | return stringlib_rsplit_whitespace( |
| 6589 | (PyObject*) self, self->str, self->length, maxcount |
| 6590 | ); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6591 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6592 | return stringlib_rsplit( |
| 6593 | (PyObject*) self, self->str, self->length, |
| 6594 | substring->str, substring->length, |
| 6595 | maxcount |
| 6596 | ); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6597 | } |
| 6598 | |
| 6599 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6600 | PyObject *replace(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6601 | PyUnicodeObject *str1, |
| 6602 | PyUnicodeObject *str2, |
| 6603 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6604 | { |
| 6605 | PyUnicodeObject *u; |
| 6606 | |
| 6607 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6608 | maxcount = PY_SSIZE_T_MAX; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6609 | else if (maxcount == 0 || self->length == 0) |
| 6610 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6611 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6612 | if (str1->length == str2->length) { |
Antoine Pitrou | cbfdee3 | 2010-01-13 08:58:08 +0000 | [diff] [blame] | 6613 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6614 | /* same length */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6615 | if (str1->length == 0) |
| 6616 | goto nothing; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6617 | if (str1->length == 1) { |
| 6618 | /* replace characters */ |
| 6619 | Py_UNICODE u1, u2; |
| 6620 | if (!findchar(self->str, self->length, str1->str[0])) |
| 6621 | goto nothing; |
| 6622 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6623 | if (!u) |
| 6624 | return NULL; |
| 6625 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6626 | u1 = str1->str[0]; |
| 6627 | u2 = str2->str[0]; |
| 6628 | for (i = 0; i < u->length; i++) |
| 6629 | if (u->str[i] == u1) { |
| 6630 | if (--maxcount < 0) |
| 6631 | break; |
| 6632 | u->str[i] = u2; |
| 6633 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6634 | } else { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6635 | i = stringlib_find( |
| 6636 | self->str, self->length, str1->str, str1->length, 0 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6637 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6638 | if (i < 0) |
| 6639 | goto nothing; |
| 6640 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6641 | if (!u) |
| 6642 | return NULL; |
| 6643 | Py_UNICODE_COPY(u->str, self->str, self->length); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6644 | |
| 6645 | /* change everything in-place, starting with this one */ |
| 6646 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 6647 | i += str1->length; |
| 6648 | |
| 6649 | while ( --maxcount > 0) { |
| 6650 | i = stringlib_find(self->str+i, self->length-i, |
| 6651 | str1->str, str1->length, |
| 6652 | i); |
| 6653 | if (i == -1) |
| 6654 | break; |
| 6655 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 6656 | i += str1->length; |
| 6657 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6658 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6659 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6660 | |
| 6661 | Py_ssize_t n, i, j, e; |
| 6662 | Py_ssize_t product, new_size, delta; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6663 | Py_UNICODE *p; |
| 6664 | |
| 6665 | /* replace strings */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6666 | n = stringlib_count(self->str, self->length, str1->str, str1->length, |
| 6667 | maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6668 | if (n == 0) |
| 6669 | goto nothing; |
| 6670 | /* new_size = self->length + n * (str2->length - str1->length)); */ |
| 6671 | delta = (str2->length - str1->length); |
| 6672 | if (delta == 0) { |
| 6673 | new_size = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6674 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6675 | product = n * (str2->length - str1->length); |
| 6676 | if ((product / (str2->length - str1->length)) != n) { |
| 6677 | PyErr_SetString(PyExc_OverflowError, |
| 6678 | "replace string is too long"); |
| 6679 | return NULL; |
| 6680 | } |
| 6681 | new_size = self->length + product; |
| 6682 | if (new_size < 0) { |
| 6683 | PyErr_SetString(PyExc_OverflowError, |
| 6684 | "replace string is too long"); |
| 6685 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6686 | } |
| 6687 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6688 | u = _PyUnicode_New(new_size); |
| 6689 | if (!u) |
| 6690 | return NULL; |
| 6691 | i = 0; |
| 6692 | p = u->str; |
| 6693 | e = self->length - str1->length; |
| 6694 | if (str1->length > 0) { |
| 6695 | while (n-- > 0) { |
| 6696 | /* look for next match */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6697 | j = stringlib_find(self->str+i, self->length-i, |
| 6698 | str1->str, str1->length, |
| 6699 | i); |
| 6700 | if (j == -1) |
| 6701 | break; |
| 6702 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6703 | /* copy unchanged part [i:j] */ |
| 6704 | Py_UNICODE_COPY(p, self->str+i, j-i); |
| 6705 | p += j - i; |
| 6706 | } |
| 6707 | /* copy substitution string */ |
| 6708 | if (str2->length > 0) { |
| 6709 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6710 | p += str2->length; |
| 6711 | } |
| 6712 | i = j + str1->length; |
| 6713 | } |
| 6714 | if (i < self->length) |
| 6715 | /* copy tail [i:] */ |
| 6716 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6717 | } else { |
| 6718 | /* interleave */ |
| 6719 | while (n > 0) { |
| 6720 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6721 | p += str2->length; |
| 6722 | if (--n <= 0) |
| 6723 | break; |
| 6724 | *p++ = self->str[i++]; |
| 6725 | } |
| 6726 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6727 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6728 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6729 | return (PyObject *) u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6730 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6731 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6732 | /* nothing to replace; return original string (when possible) */ |
| 6733 | if (PyUnicode_CheckExact(self)) { |
| 6734 | Py_INCREF(self); |
| 6735 | return (PyObject *) self; |
| 6736 | } |
| 6737 | return PyUnicode_FromUnicode(self->str, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6738 | } |
| 6739 | |
| 6740 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 6741 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6742 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6743 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6744 | \n\ |
| 6745 | 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] | 6746 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6747 | |
| 6748 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6749 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6750 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6751 | return fixup(self, fixtitle); |
| 6752 | } |
| 6753 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6754 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6755 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6756 | \n\ |
| 6757 | 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] | 6758 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6759 | |
| 6760 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6761 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6762 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6763 | return fixup(self, fixcapitalize); |
| 6764 | } |
| 6765 | |
| 6766 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6767 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6768 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6769 | \n\ |
| 6770 | 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] | 6771 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6772 | |
| 6773 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6774 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6775 | { |
| 6776 | PyObject *list; |
| 6777 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6778 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6779 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6780 | /* Split into words */ |
| 6781 | list = split(self, NULL, -1); |
| 6782 | if (!list) |
| 6783 | return NULL; |
| 6784 | |
| 6785 | /* Capitalize each word */ |
| 6786 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 6787 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6788 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6789 | if (item == NULL) |
| 6790 | goto onError; |
| 6791 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 6792 | PyList_SET_ITEM(list, i, item); |
| 6793 | } |
| 6794 | |
| 6795 | /* Join the words to form a new string */ |
| 6796 | item = PyUnicode_Join(NULL, list); |
| 6797 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6798 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6799 | Py_DECREF(list); |
| 6800 | return (PyObject *)item; |
| 6801 | } |
| 6802 | #endif |
| 6803 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6804 | /* Argument converter. Coerces to a single unicode character */ |
| 6805 | |
| 6806 | static int |
| 6807 | convert_uc(PyObject *obj, void *addr) |
| 6808 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6809 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 6810 | PyObject *uniobj; |
| 6811 | Py_UNICODE *unistr; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6812 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6813 | uniobj = PyUnicode_FromObject(obj); |
| 6814 | if (uniobj == NULL) { |
| 6815 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6816 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6817 | return 0; |
| 6818 | } |
| 6819 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 6820 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6821 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6822 | Py_DECREF(uniobj); |
| 6823 | return 0; |
| 6824 | } |
| 6825 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 6826 | *fillcharloc = unistr[0]; |
| 6827 | Py_DECREF(uniobj); |
| 6828 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6829 | } |
| 6830 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6831 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6832 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6833 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 6834 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6835 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6836 | |
| 6837 | static PyObject * |
| 6838 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 6839 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6840 | Py_ssize_t marg, left; |
| 6841 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6842 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6843 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 6844 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6845 | return NULL; |
| 6846 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6847 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6848 | Py_INCREF(self); |
| 6849 | return (PyObject*) self; |
| 6850 | } |
| 6851 | |
| 6852 | marg = width - self->length; |
| 6853 | left = marg / 2 + (marg & width & 1); |
| 6854 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6855 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6856 | } |
| 6857 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6858 | #if 0 |
| 6859 | |
| 6860 | /* This code should go into some future Unicode collation support |
| 6861 | module. The basic comparison should compare ordinals on a naive |
Georg Brandl | c6c3178 | 2009-06-08 13:41:29 +0000 | [diff] [blame] | 6862 | basis (this is what Java does and thus Jython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6863 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6864 | /* speedy UTF-16 code point order comparison */ |
| 6865 | /* gleaned from: */ |
| 6866 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 6867 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6868 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6869 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6870 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6871 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 6872 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6873 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6874 | }; |
| 6875 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6876 | static int |
| 6877 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6878 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6879 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6880 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6881 | Py_UNICODE *s1 = str1->str; |
| 6882 | Py_UNICODE *s2 = str2->str; |
| 6883 | |
| 6884 | len1 = str1->length; |
| 6885 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6886 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6887 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6888 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6889 | |
| 6890 | c1 = *s1++; |
| 6891 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6892 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6893 | if (c1 > (1<<11) * 26) |
| 6894 | c1 += utf16Fixup[c1>>11]; |
| 6895 | if (c2 > (1<<11) * 26) |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6896 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6897 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6898 | |
| 6899 | if (c1 != c2) |
| 6900 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6901 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6902 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6903 | } |
| 6904 | |
| 6905 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6906 | } |
| 6907 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6908 | #else |
| 6909 | |
| 6910 | static int |
| 6911 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6912 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6913 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6914 | |
| 6915 | Py_UNICODE *s1 = str1->str; |
| 6916 | Py_UNICODE *s2 = str2->str; |
| 6917 | |
| 6918 | len1 = str1->length; |
| 6919 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6920 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6921 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6922 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6923 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6924 | c1 = *s1++; |
| 6925 | c2 = *s2++; |
| 6926 | |
| 6927 | if (c1 != c2) |
| 6928 | return (c1 < c2) ? -1 : 1; |
| 6929 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6930 | len1--; len2--; |
| 6931 | } |
| 6932 | |
| 6933 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6934 | } |
| 6935 | |
| 6936 | #endif |
| 6937 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6938 | int PyUnicode_Compare(PyObject *left, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6939 | PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6940 | { |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6941 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) |
| 6942 | return unicode_compare((PyUnicodeObject *)left, |
| 6943 | (PyUnicodeObject *)right); |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6944 | PyErr_Format(PyExc_TypeError, |
| 6945 | "Can't compare %.100s and %.100s", |
| 6946 | left->ob_type->tp_name, |
| 6947 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6948 | return -1; |
| 6949 | } |
| 6950 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6951 | int |
| 6952 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 6953 | { |
| 6954 | int i; |
| 6955 | Py_UNICODE *id; |
| 6956 | assert(PyUnicode_Check(uni)); |
| 6957 | id = PyUnicode_AS_UNICODE(uni); |
| 6958 | /* Compare Unicode string and source character set string */ |
| 6959 | for (i = 0; id[i] && str[i]; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6960 | if (id[i] != str[i]) |
| 6961 | return ((int)id[i] < (int)str[i]) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 6962 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 6963 | to C strings identical up to that point. */ |
Benjamin Peterson | a23831f | 2010-04-25 21:54:00 +0000 | [diff] [blame] | 6964 | if (PyUnicode_GET_SIZE(uni) != i || id[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6965 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6966 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6967 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6968 | return 0; |
| 6969 | } |
| 6970 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6971 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6972 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6973 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6974 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6975 | PyObject *PyUnicode_RichCompare(PyObject *left, |
| 6976 | PyObject *right, |
| 6977 | int op) |
| 6978 | { |
| 6979 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6980 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6981 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 6982 | PyObject *v; |
| 6983 | if (((PyUnicodeObject *) left)->length != |
| 6984 | ((PyUnicodeObject *) right)->length) { |
| 6985 | if (op == Py_EQ) { |
| 6986 | Py_INCREF(Py_False); |
| 6987 | return Py_False; |
| 6988 | } |
| 6989 | if (op == Py_NE) { |
| 6990 | Py_INCREF(Py_True); |
| 6991 | return Py_True; |
| 6992 | } |
| 6993 | } |
| 6994 | if (left == right) |
| 6995 | result = 0; |
| 6996 | else |
| 6997 | result = unicode_compare((PyUnicodeObject *)left, |
| 6998 | (PyUnicodeObject *)right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6999 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 7000 | /* Convert the return value to a Boolean */ |
| 7001 | switch (op) { |
| 7002 | case Py_EQ: |
| 7003 | v = TEST_COND(result == 0); |
| 7004 | break; |
| 7005 | case Py_NE: |
| 7006 | v = TEST_COND(result != 0); |
| 7007 | break; |
| 7008 | case Py_LE: |
| 7009 | v = TEST_COND(result <= 0); |
| 7010 | break; |
| 7011 | case Py_GE: |
| 7012 | v = TEST_COND(result >= 0); |
| 7013 | break; |
| 7014 | case Py_LT: |
| 7015 | v = TEST_COND(result == -1); |
| 7016 | break; |
| 7017 | case Py_GT: |
| 7018 | v = TEST_COND(result == 1); |
| 7019 | break; |
| 7020 | default: |
| 7021 | PyErr_BadArgument(); |
| 7022 | return NULL; |
| 7023 | } |
| 7024 | Py_INCREF(v); |
| 7025 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 7026 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7027 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 7028 | Py_INCREF(Py_NotImplemented); |
| 7029 | return Py_NotImplemented; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 7030 | } |
| 7031 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7032 | int PyUnicode_Contains(PyObject *container, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7033 | PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7034 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7035 | PyObject *str, *sub; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7036 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7037 | |
| 7038 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7039 | sub = PyUnicode_FromObject(element); |
| 7040 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7041 | PyErr_Format(PyExc_TypeError, |
| 7042 | "'in <string>' requires string as left operand, not %s", |
| 7043 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7044 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7045 | } |
| 7046 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7047 | str = PyUnicode_FromObject(container); |
| 7048 | if (!str) { |
| 7049 | Py_DECREF(sub); |
| 7050 | return -1; |
| 7051 | } |
| 7052 | |
| 7053 | result = stringlib_contains_obj(str, sub); |
| 7054 | |
| 7055 | Py_DECREF(str); |
| 7056 | Py_DECREF(sub); |
| 7057 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7058 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7059 | } |
| 7060 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7061 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 7062 | |
| 7063 | PyObject *PyUnicode_Concat(PyObject *left, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7064 | PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7065 | { |
| 7066 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 7067 | |
| 7068 | /* Coerce the two arguments */ |
| 7069 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 7070 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7071 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7072 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 7073 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7074 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7075 | |
| 7076 | /* Shortcuts */ |
| 7077 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7078 | Py_DECREF(v); |
| 7079 | return (PyObject *)u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7080 | } |
| 7081 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7082 | Py_DECREF(u); |
| 7083 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7084 | } |
| 7085 | |
| 7086 | /* Concat the two Unicode strings */ |
| 7087 | w = _PyUnicode_New(u->length + v->length); |
| 7088 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7089 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7090 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 7091 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 7092 | |
| 7093 | Py_DECREF(u); |
| 7094 | Py_DECREF(v); |
| 7095 | return (PyObject *)w; |
| 7096 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7097 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7098 | Py_XDECREF(u); |
| 7099 | Py_XDECREF(v); |
| 7100 | return NULL; |
| 7101 | } |
| 7102 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7103 | void |
| 7104 | PyUnicode_Append(PyObject **pleft, PyObject *right) |
| 7105 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7106 | PyObject *new; |
| 7107 | if (*pleft == NULL) |
| 7108 | return; |
| 7109 | if (right == NULL || !PyUnicode_Check(*pleft)) { |
| 7110 | Py_DECREF(*pleft); |
| 7111 | *pleft = NULL; |
| 7112 | return; |
| 7113 | } |
| 7114 | new = PyUnicode_Concat(*pleft, right); |
| 7115 | Py_DECREF(*pleft); |
| 7116 | *pleft = new; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7117 | } |
| 7118 | |
| 7119 | void |
| 7120 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 7121 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7122 | PyUnicode_Append(pleft, right); |
| 7123 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7124 | } |
| 7125 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7126 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7127 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7128 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7129 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7130 | 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] | 7131 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7132 | |
| 7133 | static PyObject * |
| 7134 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 7135 | { |
| 7136 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7137 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7138 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7139 | PyObject *result; |
| 7140 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 7141 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7142 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7143 | return NULL; |
| 7144 | |
| 7145 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7146 | (PyObject *)substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7147 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7148 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7149 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 7150 | ADJUST_INDICES(start, end, self->length); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7151 | result = PyLong_FromSsize_t( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7152 | stringlib_count(self->str + start, end - start, |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 7153 | substring->str, substring->length, |
| 7154 | PY_SSIZE_T_MAX) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7155 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7156 | |
| 7157 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7158 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7159 | return result; |
| 7160 | } |
| 7161 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7162 | PyDoc_STRVAR(encode__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7163 | "S.encode([encoding[, errors]]) -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7164 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7165 | Encode S using the codec registered for encoding. encoding defaults\n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7166 | to the default encoding. errors may be given to set a different error\n\ |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7167 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7168 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 7169 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 7170 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7171 | |
| 7172 | static PyObject * |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 7173 | unicode_encode(PyUnicodeObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7174 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 7175 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7176 | char *encoding = NULL; |
| 7177 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7178 | PyObject *v; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 7179 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 7180 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 7181 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7182 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 7183 | v = PyUnicode_AsEncodedString((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 7184 | if (v == NULL) |
| 7185 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7186 | if (!PyBytes_Check(v)) { |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7187 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 7188 | "encoder did not return a bytes object " |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7189 | "(type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7190 | Py_TYPE(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7191 | Py_DECREF(v); |
| 7192 | return NULL; |
| 7193 | } |
| 7194 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 7195 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7196 | onError: |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 7197 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7198 | } |
| 7199 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7200 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7201 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7202 | \n\ |
| 7203 | 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] | 7204 | 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] | 7205 | |
| 7206 | static PyObject* |
| 7207 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 7208 | { |
| 7209 | Py_UNICODE *e; |
| 7210 | Py_UNICODE *p; |
| 7211 | Py_UNICODE *q; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7212 | Py_UNICODE *qe; |
| 7213 | Py_ssize_t i, j, incr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7214 | PyUnicodeObject *u; |
| 7215 | int tabsize = 8; |
| 7216 | |
| 7217 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7218 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7219 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 7220 | /* First pass: determine size of output string */ |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7221 | i = 0; /* chars up to and including most recent \n or \r */ |
| 7222 | j = 0; /* chars since most recent \n or \r (use in tab calculations) */ |
| 7223 | e = self->str + self->length; /* end of input */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7224 | for (p = self->str; p < e; p++) |
| 7225 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7226 | if (tabsize > 0) { |
| 7227 | incr = tabsize - (j % tabsize); /* cannot overflow */ |
| 7228 | if (j > PY_SSIZE_T_MAX - incr) |
| 7229 | goto overflow1; |
| 7230 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7231 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7232 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7233 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7234 | if (j > PY_SSIZE_T_MAX - 1) |
| 7235 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7236 | j++; |
| 7237 | if (*p == '\n' || *p == '\r') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7238 | if (i > PY_SSIZE_T_MAX - j) |
| 7239 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7240 | i += j; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7241 | j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7242 | } |
| 7243 | } |
| 7244 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7245 | if (i > PY_SSIZE_T_MAX - j) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7246 | goto overflow1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 7247 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7248 | /* Second pass: create output string and fill it */ |
| 7249 | u = _PyUnicode_New(i + j); |
| 7250 | if (!u) |
| 7251 | return NULL; |
| 7252 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7253 | j = 0; /* same as in first pass */ |
| 7254 | q = u->str; /* next output char */ |
| 7255 | qe = u->str + u->length; /* end of output */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7256 | |
| 7257 | for (p = self->str; p < e; p++) |
| 7258 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7259 | if (tabsize > 0) { |
| 7260 | i = tabsize - (j % tabsize); |
| 7261 | j += i; |
| 7262 | while (i--) { |
| 7263 | if (q >= qe) |
| 7264 | goto overflow2; |
| 7265 | *q++ = ' '; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7266 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7267 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7268 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7269 | else { |
| 7270 | if (q >= qe) |
| 7271 | goto overflow2; |
| 7272 | *q++ = *p; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7273 | j++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7274 | if (*p == '\n' || *p == '\r') |
| 7275 | j = 0; |
| 7276 | } |
| 7277 | |
| 7278 | return (PyObject*) u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7279 | |
| 7280 | overflow2: |
| 7281 | Py_DECREF(u); |
| 7282 | overflow1: |
| 7283 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 7284 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7285 | } |
| 7286 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7287 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7288 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7289 | \n\ |
| 7290 | 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] | 7291 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7292 | arguments start and end are interpreted as in slice notation.\n\ |
| 7293 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7294 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7295 | |
| 7296 | static PyObject * |
| 7297 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 7298 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7299 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7300 | Py_ssize_t start; |
| 7301 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7302 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7303 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7304 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7305 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7306 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7307 | result = stringlib_find_slice( |
| 7308 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7309 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7310 | start, end |
| 7311 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7312 | |
| 7313 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7314 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7315 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7316 | } |
| 7317 | |
| 7318 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7319 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7320 | { |
| 7321 | if (index < 0 || index >= self->length) { |
| 7322 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 7323 | return NULL; |
| 7324 | } |
| 7325 | |
| 7326 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 7327 | } |
| 7328 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7329 | /* Believe it or not, this produces the same value for ASCII strings |
| 7330 | as string_hash(). */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7331 | static long |
Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 7332 | unicode_hash(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7333 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7334 | Py_ssize_t len; |
| 7335 | Py_UNICODE *p; |
| 7336 | long x; |
| 7337 | |
| 7338 | if (self->hash != -1) |
| 7339 | return self->hash; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7340 | len = Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7341 | p = self->str; |
| 7342 | x = *p << 7; |
| 7343 | while (--len >= 0) |
| 7344 | x = (1000003*x) ^ *p++; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7345 | x ^= Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7346 | if (x == -1) |
| 7347 | x = -2; |
| 7348 | self->hash = x; |
| 7349 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7350 | } |
| 7351 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7352 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7353 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7354 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7355 | 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] | 7356 | |
| 7357 | static PyObject * |
| 7358 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 7359 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7360 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7361 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7362 | Py_ssize_t start; |
| 7363 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7364 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7365 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7366 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7367 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7368 | result = stringlib_find_slice( |
| 7369 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7370 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7371 | start, end |
| 7372 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7373 | |
| 7374 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7375 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7376 | if (result < 0) { |
| 7377 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 7378 | return NULL; |
| 7379 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7380 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7381 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7382 | } |
| 7383 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7384 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7385 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7386 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7387 | 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] | 7388 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7389 | |
| 7390 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7391 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7392 | { |
| 7393 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7394 | register const Py_UNICODE *e; |
| 7395 | int cased; |
| 7396 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7397 | /* Shortcut for single character strings */ |
| 7398 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7399 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7400 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7401 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7402 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7403 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7404 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7405 | e = p + PyUnicode_GET_SIZE(self); |
| 7406 | cased = 0; |
| 7407 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7408 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7409 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7410 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 7411 | return PyBool_FromLong(0); |
| 7412 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 7413 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7414 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7415 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7416 | } |
| 7417 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7418 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7419 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7420 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7421 | 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] | 7422 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7423 | |
| 7424 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7425 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7426 | { |
| 7427 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7428 | register const Py_UNICODE *e; |
| 7429 | int cased; |
| 7430 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7431 | /* Shortcut for single character strings */ |
| 7432 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7433 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7434 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7435 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7436 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7437 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7438 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7439 | e = p + PyUnicode_GET_SIZE(self); |
| 7440 | cased = 0; |
| 7441 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7442 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7443 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7444 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 7445 | return PyBool_FromLong(0); |
| 7446 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 7447 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7448 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7449 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7450 | } |
| 7451 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7452 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7453 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7454 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7455 | Return True if S is a titlecased string and there is at least one\n\ |
| 7456 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 7457 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 7458 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7459 | |
| 7460 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7461 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7462 | { |
| 7463 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7464 | register const Py_UNICODE *e; |
| 7465 | int cased, previous_is_cased; |
| 7466 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7467 | /* Shortcut for single character strings */ |
| 7468 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7469 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 7470 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7471 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7472 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7473 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7474 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7475 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7476 | e = p + PyUnicode_GET_SIZE(self); |
| 7477 | cased = 0; |
| 7478 | previous_is_cased = 0; |
| 7479 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7480 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7481 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7482 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 7483 | if (previous_is_cased) |
| 7484 | return PyBool_FromLong(0); |
| 7485 | previous_is_cased = 1; |
| 7486 | cased = 1; |
| 7487 | } |
| 7488 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 7489 | if (!previous_is_cased) |
| 7490 | return PyBool_FromLong(0); |
| 7491 | previous_is_cased = 1; |
| 7492 | cased = 1; |
| 7493 | } |
| 7494 | else |
| 7495 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7496 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7497 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7498 | } |
| 7499 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7500 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7501 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7502 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7503 | Return True if all characters in S are whitespace\n\ |
| 7504 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7505 | |
| 7506 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7507 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7508 | { |
| 7509 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7510 | register const Py_UNICODE *e; |
| 7511 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7512 | /* Shortcut for single character strings */ |
| 7513 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7514 | Py_UNICODE_ISSPACE(*p)) |
| 7515 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7516 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7517 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7518 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7519 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7520 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7521 | e = p + PyUnicode_GET_SIZE(self); |
| 7522 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7523 | if (!Py_UNICODE_ISSPACE(*p)) |
| 7524 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7525 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7526 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7527 | } |
| 7528 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7529 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7530 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7531 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7532 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7533 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7534 | |
| 7535 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7536 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7537 | { |
| 7538 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7539 | register const Py_UNICODE *e; |
| 7540 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7541 | /* Shortcut for single character strings */ |
| 7542 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7543 | Py_UNICODE_ISALPHA(*p)) |
| 7544 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7545 | |
| 7546 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7547 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7548 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7549 | |
| 7550 | e = p + PyUnicode_GET_SIZE(self); |
| 7551 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7552 | if (!Py_UNICODE_ISALPHA(*p)) |
| 7553 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7554 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7555 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7556 | } |
| 7557 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7558 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7559 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7560 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7561 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7562 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7563 | |
| 7564 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7565 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7566 | { |
| 7567 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7568 | register const Py_UNICODE *e; |
| 7569 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7570 | /* Shortcut for single character strings */ |
| 7571 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7572 | Py_UNICODE_ISALNUM(*p)) |
| 7573 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7574 | |
| 7575 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7576 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7577 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7578 | |
| 7579 | e = p + PyUnicode_GET_SIZE(self); |
| 7580 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7581 | if (!Py_UNICODE_ISALNUM(*p)) |
| 7582 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7583 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7584 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7585 | } |
| 7586 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7587 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7588 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7589 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7590 | 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] | 7591 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7592 | |
| 7593 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7594 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7595 | { |
| 7596 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7597 | register const Py_UNICODE *e; |
| 7598 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7599 | /* Shortcut for single character strings */ |
| 7600 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7601 | Py_UNICODE_ISDECIMAL(*p)) |
| 7602 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7603 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7604 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7605 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7606 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7607 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7608 | e = p + PyUnicode_GET_SIZE(self); |
| 7609 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7610 | if (!Py_UNICODE_ISDECIMAL(*p)) |
| 7611 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7612 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7613 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7614 | } |
| 7615 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7616 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7617 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7618 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7619 | Return True if all characters in S are digits\n\ |
| 7620 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7621 | |
| 7622 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7623 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7624 | { |
| 7625 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7626 | register const Py_UNICODE *e; |
| 7627 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7628 | /* Shortcut for single character strings */ |
| 7629 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7630 | Py_UNICODE_ISDIGIT(*p)) |
| 7631 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7632 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7633 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7634 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7635 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7636 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7637 | e = p + PyUnicode_GET_SIZE(self); |
| 7638 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7639 | if (!Py_UNICODE_ISDIGIT(*p)) |
| 7640 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7641 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7642 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7643 | } |
| 7644 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7645 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7646 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7647 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7648 | 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] | 7649 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7650 | |
| 7651 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7652 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7653 | { |
| 7654 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7655 | register const Py_UNICODE *e; |
| 7656 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7657 | /* Shortcut for single character strings */ |
| 7658 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7659 | Py_UNICODE_ISNUMERIC(*p)) |
| 7660 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7661 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7662 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7663 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7664 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7665 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7666 | e = p + PyUnicode_GET_SIZE(self); |
| 7667 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7668 | if (!Py_UNICODE_ISNUMERIC(*p)) |
| 7669 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7670 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7671 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7672 | } |
| 7673 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7674 | int |
| 7675 | PyUnicode_IsIdentifier(PyObject *self) |
| 7676 | { |
| 7677 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE((PyUnicodeObject*)self); |
| 7678 | register const Py_UNICODE *e; |
| 7679 | |
| 7680 | /* Special case for empty strings */ |
| 7681 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7682 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7683 | |
| 7684 | /* PEP 3131 says that the first character must be in |
| 7685 | XID_Start and subsequent characters in XID_Continue, |
| 7686 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7687 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7688 | letters, digits, underscore). However, given the current |
| 7689 | definition of XID_Start and XID_Continue, it is sufficient |
| 7690 | to check just for these, except that _ must be allowed |
| 7691 | as starting an identifier. */ |
| 7692 | if (!_PyUnicode_IsXidStart(*p) && *p != 0x5F /* LOW LINE */) |
| 7693 | return 0; |
| 7694 | |
| 7695 | e = p + PyUnicode_GET_SIZE(self); |
| 7696 | for (p++; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7697 | if (!_PyUnicode_IsXidContinue(*p)) |
| 7698 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7699 | } |
| 7700 | return 1; |
| 7701 | } |
| 7702 | |
| 7703 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7704 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7705 | \n\ |
| 7706 | Return True if S is a valid identifier according\n\ |
| 7707 | to the language definition."); |
| 7708 | |
| 7709 | static PyObject* |
| 7710 | unicode_isidentifier(PyObject *self) |
| 7711 | { |
| 7712 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 7713 | } |
| 7714 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7715 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7716 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7717 | \n\ |
| 7718 | Return True if all characters in S are considered\n\ |
| 7719 | printable in repr() or S is empty, False otherwise."); |
| 7720 | |
| 7721 | static PyObject* |
| 7722 | unicode_isprintable(PyObject *self) |
| 7723 | { |
| 7724 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7725 | register const Py_UNICODE *e; |
| 7726 | |
| 7727 | /* Shortcut for single character strings */ |
| 7728 | if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISPRINTABLE(*p)) { |
| 7729 | Py_RETURN_TRUE; |
| 7730 | } |
| 7731 | |
| 7732 | e = p + PyUnicode_GET_SIZE(self); |
| 7733 | for (; p < e; p++) { |
| 7734 | if (!Py_UNICODE_ISPRINTABLE(*p)) { |
| 7735 | Py_RETURN_FALSE; |
| 7736 | } |
| 7737 | } |
| 7738 | Py_RETURN_TRUE; |
| 7739 | } |
| 7740 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7741 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 7742 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7743 | \n\ |
| 7744 | 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] | 7745 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7746 | |
| 7747 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7748 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7749 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7750 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7751 | } |
| 7752 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7753 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7754 | unicode_length(PyUnicodeObject *self) |
| 7755 | { |
| 7756 | return self->length; |
| 7757 | } |
| 7758 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7759 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7760 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7761 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 7762 | 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] | 7763 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7764 | |
| 7765 | static PyObject * |
| 7766 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 7767 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7768 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7769 | Py_UNICODE fillchar = ' '; |
| 7770 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7771 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7772 | return NULL; |
| 7773 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7774 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7775 | Py_INCREF(self); |
| 7776 | return (PyObject*) self; |
| 7777 | } |
| 7778 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7779 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7780 | } |
| 7781 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7782 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7783 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7784 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7785 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7786 | |
| 7787 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7788 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7789 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7790 | return fixup(self, fixlower); |
| 7791 | } |
| 7792 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7793 | #define LEFTSTRIP 0 |
| 7794 | #define RIGHTSTRIP 1 |
| 7795 | #define BOTHSTRIP 2 |
| 7796 | |
| 7797 | /* Arrays indexed by above */ |
| 7798 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 7799 | |
| 7800 | #define STRIPNAME(i) (stripformat[i]+3) |
| 7801 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7802 | /* externally visible for str.strip(unicode) */ |
| 7803 | PyObject * |
| 7804 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 7805 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7806 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 7807 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
| 7808 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
| 7809 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 7810 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7811 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7812 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7813 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7814 | i = 0; |
| 7815 | if (striptype != RIGHTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7816 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 7817 | i++; |
| 7818 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7819 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7820 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7821 | j = len; |
| 7822 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7823 | do { |
| 7824 | j--; |
| 7825 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 7826 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7827 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7828 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7829 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7830 | Py_INCREF(self); |
| 7831 | return (PyObject*)self; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7832 | } |
| 7833 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7834 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7835 | } |
| 7836 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7837 | |
| 7838 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7839 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7840 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7841 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 7842 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7843 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7844 | i = 0; |
| 7845 | if (striptype != RIGHTSTRIP) { |
| 7846 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 7847 | i++; |
| 7848 | } |
| 7849 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7850 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7851 | j = len; |
| 7852 | if (striptype != LEFTSTRIP) { |
| 7853 | do { |
| 7854 | j--; |
| 7855 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 7856 | j++; |
| 7857 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7858 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7859 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 7860 | Py_INCREF(self); |
| 7861 | return (PyObject*)self; |
| 7862 | } |
| 7863 | else |
| 7864 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7865 | } |
| 7866 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7867 | |
| 7868 | static PyObject * |
| 7869 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 7870 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7871 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7872 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7873 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 7874 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7875 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7876 | if (sep != NULL && sep != Py_None) { |
| 7877 | if (PyUnicode_Check(sep)) |
| 7878 | return _PyUnicode_XStrip(self, striptype, sep); |
| 7879 | else { |
| 7880 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7881 | "%s arg must be None or str", |
| 7882 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7883 | return NULL; |
| 7884 | } |
| 7885 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7886 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7887 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7888 | } |
| 7889 | |
| 7890 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7891 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7892 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7893 | \n\ |
| 7894 | Return a copy of the string S with leading and trailing\n\ |
| 7895 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7896 | 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] | 7897 | |
| 7898 | static PyObject * |
| 7899 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 7900 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7901 | if (PyTuple_GET_SIZE(args) == 0) |
| 7902 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 7903 | else |
| 7904 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7905 | } |
| 7906 | |
| 7907 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7908 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7909 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7910 | \n\ |
| 7911 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7912 | 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] | 7913 | |
| 7914 | static PyObject * |
| 7915 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 7916 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7917 | if (PyTuple_GET_SIZE(args) == 0) |
| 7918 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 7919 | else |
| 7920 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7921 | } |
| 7922 | |
| 7923 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7924 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7925 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7926 | \n\ |
| 7927 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7928 | 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] | 7929 | |
| 7930 | static PyObject * |
| 7931 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 7932 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7933 | if (PyTuple_GET_SIZE(args) == 0) |
| 7934 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 7935 | else |
| 7936 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7937 | } |
| 7938 | |
| 7939 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7940 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7941 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7942 | { |
| 7943 | PyUnicodeObject *u; |
| 7944 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7945 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7946 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7947 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7948 | if (len < 1) { |
| 7949 | Py_INCREF(unicode_empty); |
| 7950 | return (PyObject *)unicode_empty; |
| 7951 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7952 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7953 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7954 | /* no repeat, return original string */ |
| 7955 | Py_INCREF(str); |
| 7956 | return (PyObject*) str; |
| 7957 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7958 | |
| 7959 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 7960 | * needed doesn't overflow size_t |
| 7961 | */ |
| 7962 | nchars = len * str->length; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7963 | if (nchars / len != str->length) { |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7964 | PyErr_SetString(PyExc_OverflowError, |
| 7965 | "repeated string is too long"); |
| 7966 | return NULL; |
| 7967 | } |
| 7968 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 7969 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 7970 | PyErr_SetString(PyExc_OverflowError, |
| 7971 | "repeated string is too long"); |
| 7972 | return NULL; |
| 7973 | } |
| 7974 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7975 | if (!u) |
| 7976 | return NULL; |
| 7977 | |
| 7978 | p = u->str; |
| 7979 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7980 | if (str->length == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7981 | Py_UNICODE_FILL(p, str->str[0], len); |
| 7982 | } else { |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7983 | Py_ssize_t done = str->length; /* number of characters copied this far */ |
| 7984 | Py_UNICODE_COPY(p, str->str, str->length); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7985 | while (done < nchars) { |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 7986 | Py_ssize_t n = (done <= nchars-done) ? done : nchars-done; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7987 | Py_UNICODE_COPY(p+done, p, n); |
| 7988 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7989 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7990 | } |
| 7991 | |
| 7992 | return (PyObject*) u; |
| 7993 | } |
| 7994 | |
| 7995 | PyObject *PyUnicode_Replace(PyObject *obj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7996 | PyObject *subobj, |
| 7997 | PyObject *replobj, |
| 7998 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7999 | { |
| 8000 | PyObject *self; |
| 8001 | PyObject *str1; |
| 8002 | PyObject *str2; |
| 8003 | PyObject *result; |
| 8004 | |
| 8005 | self = PyUnicode_FromObject(obj); |
| 8006 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8007 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8008 | str1 = PyUnicode_FromObject(subobj); |
| 8009 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8010 | Py_DECREF(self); |
| 8011 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8012 | } |
| 8013 | str2 = PyUnicode_FromObject(replobj); |
| 8014 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8015 | Py_DECREF(self); |
| 8016 | Py_DECREF(str1); |
| 8017 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8018 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8019 | result = replace((PyUnicodeObject *)self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8020 | (PyUnicodeObject *)str1, |
| 8021 | (PyUnicodeObject *)str2, |
| 8022 | maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8023 | Py_DECREF(self); |
| 8024 | Py_DECREF(str1); |
| 8025 | Py_DECREF(str2); |
| 8026 | return result; |
| 8027 | } |
| 8028 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8029 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 8030 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8031 | \n\ |
| 8032 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 8033 | old replaced by new. If the optional argument count is\n\ |
| 8034 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8035 | |
| 8036 | static PyObject* |
| 8037 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 8038 | { |
| 8039 | PyUnicodeObject *str1; |
| 8040 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8041 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8042 | PyObject *result; |
| 8043 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8044 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8045 | return NULL; |
| 8046 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 8047 | if (str1 == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8048 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8049 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 8050 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8051 | Py_DECREF(str1); |
| 8052 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 8053 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8054 | |
| 8055 | result = replace(self, str1, str2, maxcount); |
| 8056 | |
| 8057 | Py_DECREF(str1); |
| 8058 | Py_DECREF(str2); |
| 8059 | return result; |
| 8060 | } |
| 8061 | |
| 8062 | static |
| 8063 | PyObject *unicode_repr(PyObject *unicode) |
| 8064 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8065 | PyObject *repr; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8066 | Py_UNICODE *p; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8067 | Py_UNICODE *s = PyUnicode_AS_UNICODE(unicode); |
| 8068 | Py_ssize_t size = PyUnicode_GET_SIZE(unicode); |
| 8069 | |
| 8070 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 8071 | better to choose a different scheme. Perhaps scan the |
| 8072 | first N-chars of the string and allocate based on that size. |
| 8073 | */ |
| 8074 | /* Initial allocation is based on the longest-possible unichr |
| 8075 | escape. |
| 8076 | |
| 8077 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 8078 | unichr, so in this case it's the longest unichr escape. In |
| 8079 | narrow (UTF-16) builds this is five chars per source unichr |
| 8080 | since there are two unichrs in the surrogate pair, so in narrow |
| 8081 | (UTF-16) builds it's not the longest unichr escape. |
| 8082 | |
| 8083 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 8084 | so in the narrow (UTF-16) build case it's the longest unichr |
| 8085 | escape. |
| 8086 | */ |
| 8087 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8088 | repr = PyUnicode_FromUnicode(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8089 | 2 /* quotes */ |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8090 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8091 | + 10*size |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8092 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8093 | + 6*size |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8094 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8095 | + 1); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8096 | if (repr == NULL) |
| 8097 | return NULL; |
| 8098 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8099 | p = PyUnicode_AS_UNICODE(repr); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8100 | |
| 8101 | /* Add quote */ |
| 8102 | *p++ = (findchar(s, size, '\'') && |
| 8103 | !findchar(s, size, '"')) ? '"' : '\''; |
| 8104 | while (size-- > 0) { |
| 8105 | Py_UNICODE ch = *s++; |
| 8106 | |
| 8107 | /* Escape quotes and backslashes */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8108 | if ((ch == PyUnicode_AS_UNICODE(repr)[0]) || (ch == '\\')) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8109 | *p++ = '\\'; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8110 | *p++ = ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8111 | continue; |
| 8112 | } |
| 8113 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8114 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8115 | if (ch == '\t') { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8116 | *p++ = '\\'; |
| 8117 | *p++ = 't'; |
| 8118 | } |
| 8119 | else if (ch == '\n') { |
| 8120 | *p++ = '\\'; |
| 8121 | *p++ = 'n'; |
| 8122 | } |
| 8123 | else if (ch == '\r') { |
| 8124 | *p++ = '\\'; |
| 8125 | *p++ = 'r'; |
| 8126 | } |
| 8127 | |
| 8128 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8129 | else if (ch < ' ' || ch == 0x7F) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8130 | *p++ = '\\'; |
| 8131 | *p++ = 'x'; |
| 8132 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 8133 | *p++ = hexdigits[ch & 0x000F]; |
| 8134 | } |
| 8135 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8136 | /* Copy ASCII characters as-is */ |
| 8137 | else if (ch < 0x7F) { |
| 8138 | *p++ = ch; |
| 8139 | } |
| 8140 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8141 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8142 | else { |
| 8143 | Py_UCS4 ucs = ch; |
| 8144 | |
| 8145 | #ifndef Py_UNICODE_WIDE |
| 8146 | Py_UNICODE ch2 = 0; |
| 8147 | /* Get code point from surrogate pair */ |
| 8148 | if (size > 0) { |
| 8149 | ch2 = *s; |
| 8150 | if (ch >= 0xD800 && ch < 0xDC00 && ch2 >= 0xDC00 |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8151 | && ch2 <= 0xDFFF) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8152 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8153 | + 0x00010000; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8154 | s++; |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8155 | size--; |
| 8156 | } |
| 8157 | } |
| 8158 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8159 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8160 | (categories Z* and C* except ASCII space) |
| 8161 | */ |
| 8162 | if (!Py_UNICODE_ISPRINTABLE(ucs)) { |
| 8163 | /* Map 8-bit characters to '\xhh' */ |
| 8164 | if (ucs <= 0xff) { |
| 8165 | *p++ = '\\'; |
| 8166 | *p++ = 'x'; |
| 8167 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 8168 | *p++ = hexdigits[ch & 0x000F]; |
| 8169 | } |
| 8170 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 8171 | else if (ucs >= 0x10000) { |
| 8172 | *p++ = '\\'; |
| 8173 | *p++ = 'U'; |
| 8174 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 8175 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 8176 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 8177 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 8178 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 8179 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 8180 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 8181 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 8182 | } |
| 8183 | /* Map 16-bit characters to '\uxxxx' */ |
| 8184 | else { |
| 8185 | *p++ = '\\'; |
| 8186 | *p++ = 'u'; |
| 8187 | *p++ = hexdigits[(ucs >> 12) & 0x000F]; |
| 8188 | *p++ = hexdigits[(ucs >> 8) & 0x000F]; |
| 8189 | *p++ = hexdigits[(ucs >> 4) & 0x000F]; |
| 8190 | *p++ = hexdigits[ucs & 0x000F]; |
| 8191 | } |
| 8192 | } |
| 8193 | /* Copy characters as-is */ |
| 8194 | else { |
| 8195 | *p++ = ch; |
| 8196 | #ifndef Py_UNICODE_WIDE |
| 8197 | if (ucs >= 0x10000) |
| 8198 | *p++ = ch2; |
| 8199 | #endif |
| 8200 | } |
| 8201 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8202 | } |
| 8203 | /* Add quote */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8204 | *p++ = PyUnicode_AS_UNICODE(repr)[0]; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8205 | |
| 8206 | *p = '\0'; |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 8207 | PyUnicode_Resize(&repr, p - PyUnicode_AS_UNICODE(repr)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8208 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8209 | } |
| 8210 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8211 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8212 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8213 | \n\ |
| 8214 | 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] | 8215 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8216 | arguments start and end are interpreted as in slice notation.\n\ |
| 8217 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8218 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8219 | |
| 8220 | static PyObject * |
| 8221 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 8222 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8223 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8224 | Py_ssize_t start; |
| 8225 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8226 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8227 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8228 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8229 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8230 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8231 | result = stringlib_rfind_slice( |
| 8232 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 8233 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 8234 | start, end |
| 8235 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8236 | |
| 8237 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8238 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8239 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8240 | } |
| 8241 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8242 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8243 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8244 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8245 | 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] | 8246 | |
| 8247 | static PyObject * |
| 8248 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 8249 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8250 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8251 | Py_ssize_t start; |
| 8252 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8253 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8254 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8255 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8256 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8257 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8258 | result = stringlib_rfind_slice( |
| 8259 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 8260 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 8261 | start, end |
| 8262 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8263 | |
| 8264 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8265 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8266 | if (result < 0) { |
| 8267 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 8268 | return NULL; |
| 8269 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8270 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8271 | } |
| 8272 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8273 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8274 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8275 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 8276 | 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] | 8277 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8278 | |
| 8279 | static PyObject * |
| 8280 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 8281 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8282 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 8283 | Py_UNICODE fillchar = ' '; |
| 8284 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8285 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8286 | return NULL; |
| 8287 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 8288 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8289 | Py_INCREF(self); |
| 8290 | return (PyObject*) self; |
| 8291 | } |
| 8292 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 8293 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8294 | } |
| 8295 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8296 | PyObject *PyUnicode_Split(PyObject *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8297 | PyObject *sep, |
| 8298 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8299 | { |
| 8300 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8301 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8302 | s = PyUnicode_FromObject(s); |
| 8303 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8304 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8305 | if (sep != NULL) { |
| 8306 | sep = PyUnicode_FromObject(sep); |
| 8307 | if (sep == NULL) { |
| 8308 | Py_DECREF(s); |
| 8309 | return NULL; |
| 8310 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8311 | } |
| 8312 | |
| 8313 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 8314 | |
| 8315 | Py_DECREF(s); |
| 8316 | Py_XDECREF(sep); |
| 8317 | return result; |
| 8318 | } |
| 8319 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8320 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8321 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8322 | \n\ |
| 8323 | Return a list of the words in S, using sep as the\n\ |
| 8324 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 8325 | 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] | 8326 | whitespace string is a separator and empty strings are\n\ |
| 8327 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8328 | |
| 8329 | static PyObject* |
| 8330 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 8331 | { |
| 8332 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8333 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8334 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8335 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8336 | return NULL; |
| 8337 | |
| 8338 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8339 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8340 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8341 | return split(self, (PyUnicodeObject *)substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8342 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8343 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8344 | } |
| 8345 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8346 | PyObject * |
| 8347 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 8348 | { |
| 8349 | PyObject* str_obj; |
| 8350 | PyObject* sep_obj; |
| 8351 | PyObject* out; |
| 8352 | |
| 8353 | str_obj = PyUnicode_FromObject(str_in); |
| 8354 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8355 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8356 | sep_obj = PyUnicode_FromObject(sep_in); |
| 8357 | if (!sep_obj) { |
| 8358 | Py_DECREF(str_obj); |
| 8359 | return NULL; |
| 8360 | } |
| 8361 | |
| 8362 | out = stringlib_partition( |
| 8363 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 8364 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 8365 | ); |
| 8366 | |
| 8367 | Py_DECREF(sep_obj); |
| 8368 | Py_DECREF(str_obj); |
| 8369 | |
| 8370 | return out; |
| 8371 | } |
| 8372 | |
| 8373 | |
| 8374 | PyObject * |
| 8375 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 8376 | { |
| 8377 | PyObject* str_obj; |
| 8378 | PyObject* sep_obj; |
| 8379 | PyObject* out; |
| 8380 | |
| 8381 | str_obj = PyUnicode_FromObject(str_in); |
| 8382 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8383 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8384 | sep_obj = PyUnicode_FromObject(sep_in); |
| 8385 | if (!sep_obj) { |
| 8386 | Py_DECREF(str_obj); |
| 8387 | return NULL; |
| 8388 | } |
| 8389 | |
| 8390 | out = stringlib_rpartition( |
| 8391 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 8392 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 8393 | ); |
| 8394 | |
| 8395 | Py_DECREF(sep_obj); |
| 8396 | Py_DECREF(str_obj); |
| 8397 | |
| 8398 | return out; |
| 8399 | } |
| 8400 | |
| 8401 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8402 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8403 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8404 | 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] | 8405 | 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] | 8406 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8407 | |
| 8408 | static PyObject* |
| 8409 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
| 8410 | { |
| 8411 | return PyUnicode_Partition((PyObject *)self, separator); |
| 8412 | } |
| 8413 | |
| 8414 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 8415 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8416 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8417 | 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] | 8418 | 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] | 8419 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8420 | |
| 8421 | static PyObject* |
| 8422 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 8423 | { |
| 8424 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 8425 | } |
| 8426 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8427 | PyObject *PyUnicode_RSplit(PyObject *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8428 | PyObject *sep, |
| 8429 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8430 | { |
| 8431 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8432 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8433 | s = PyUnicode_FromObject(s); |
| 8434 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8435 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8436 | if (sep != NULL) { |
| 8437 | sep = PyUnicode_FromObject(sep); |
| 8438 | if (sep == NULL) { |
| 8439 | Py_DECREF(s); |
| 8440 | return NULL; |
| 8441 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8442 | } |
| 8443 | |
| 8444 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 8445 | |
| 8446 | Py_DECREF(s); |
| 8447 | Py_XDECREF(sep); |
| 8448 | return result; |
| 8449 | } |
| 8450 | |
| 8451 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8452 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8453 | \n\ |
| 8454 | Return a list of the words in S, using sep as the\n\ |
| 8455 | delimiter string, starting at the end of the string and\n\ |
| 8456 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 8457 | splits are done. If sep is not specified, any whitespace string\n\ |
| 8458 | is a separator."); |
| 8459 | |
| 8460 | static PyObject* |
| 8461 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 8462 | { |
| 8463 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8464 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8465 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8466 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8467 | return NULL; |
| 8468 | |
| 8469 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8470 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8471 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8472 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8473 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8474 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8475 | } |
| 8476 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8477 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8478 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8479 | \n\ |
| 8480 | 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] | 8481 | 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] | 8482 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8483 | |
| 8484 | static PyObject* |
| 8485 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 8486 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8487 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8488 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8489 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8490 | return NULL; |
| 8491 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8492 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8493 | } |
| 8494 | |
| 8495 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 8496 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8497 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 8498 | if (PyUnicode_CheckExact(self)) { |
| 8499 | Py_INCREF(self); |
| 8500 | return self; |
| 8501 | } else |
| 8502 | /* Subtype -- return genuine unicode string with the same value. */ |
| 8503 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(self), |
| 8504 | PyUnicode_GET_SIZE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8505 | } |
| 8506 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8507 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8508 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8509 | \n\ |
| 8510 | 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] | 8511 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8512 | |
| 8513 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8514 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8515 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8516 | return fixup(self, fixswapcase); |
| 8517 | } |
| 8518 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8519 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8520 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8521 | \n\ |
| 8522 | Return a translation table usable for str.translate().\n\ |
| 8523 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 8524 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8525 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8526 | If there are two arguments, they must be strings of equal length, and\n\ |
| 8527 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 8528 | character at the same position in y. If there is a third argument, it\n\ |
| 8529 | must be a string, whose characters will be mapped to None in the result."); |
| 8530 | |
| 8531 | static PyObject* |
| 8532 | unicode_maketrans(PyUnicodeObject *null, PyObject *args) |
| 8533 | { |
| 8534 | PyObject *x, *y = NULL, *z = NULL; |
| 8535 | PyObject *new = NULL, *key, *value; |
| 8536 | Py_ssize_t i = 0; |
| 8537 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8538 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8539 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 8540 | return NULL; |
| 8541 | new = PyDict_New(); |
| 8542 | if (!new) |
| 8543 | return NULL; |
| 8544 | if (y != NULL) { |
| 8545 | /* x must be a string too, of equal length */ |
| 8546 | Py_ssize_t ylen = PyUnicode_GET_SIZE(y); |
| 8547 | if (!PyUnicode_Check(x)) { |
| 8548 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 8549 | "be a string if there is a second argument"); |
| 8550 | goto err; |
| 8551 | } |
| 8552 | if (PyUnicode_GET_SIZE(x) != ylen) { |
| 8553 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 8554 | "arguments must have equal length"); |
| 8555 | goto err; |
| 8556 | } |
| 8557 | /* create entries for translating chars in x to those in y */ |
| 8558 | for (i = 0; i < PyUnicode_GET_SIZE(x); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8559 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(x)[i]); |
| 8560 | value = PyLong_FromLong(PyUnicode_AS_UNICODE(y)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8561 | if (!key || !value) |
| 8562 | goto err; |
| 8563 | res = PyDict_SetItem(new, key, value); |
| 8564 | Py_DECREF(key); |
| 8565 | Py_DECREF(value); |
| 8566 | if (res < 0) |
| 8567 | goto err; |
| 8568 | } |
| 8569 | /* create entries for deleting chars in z */ |
| 8570 | if (z != NULL) { |
| 8571 | for (i = 0; i < PyUnicode_GET_SIZE(z); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8572 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(z)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8573 | if (!key) |
| 8574 | goto err; |
| 8575 | res = PyDict_SetItem(new, key, Py_None); |
| 8576 | Py_DECREF(key); |
| 8577 | if (res < 0) |
| 8578 | goto err; |
| 8579 | } |
| 8580 | } |
| 8581 | } else { |
| 8582 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 8583 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8584 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 8585 | "to maketrans it must be a dict"); |
| 8586 | goto err; |
| 8587 | } |
| 8588 | /* copy entries into the new dict, converting string keys to int keys */ |
| 8589 | while (PyDict_Next(x, &i, &key, &value)) { |
| 8590 | if (PyUnicode_Check(key)) { |
| 8591 | /* convert string keys to integer keys */ |
| 8592 | PyObject *newkey; |
| 8593 | if (PyUnicode_GET_SIZE(key) != 1) { |
| 8594 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 8595 | "table must be of length 1"); |
| 8596 | goto err; |
| 8597 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8598 | newkey = PyLong_FromLong(PyUnicode_AS_UNICODE(key)[0]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8599 | if (!newkey) |
| 8600 | goto err; |
| 8601 | res = PyDict_SetItem(new, newkey, value); |
| 8602 | Py_DECREF(newkey); |
| 8603 | if (res < 0) |
| 8604 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8605 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8606 | /* just keep integer keys */ |
| 8607 | if (PyDict_SetItem(new, key, value) < 0) |
| 8608 | goto err; |
| 8609 | } else { |
| 8610 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 8611 | "be strings or integers"); |
| 8612 | goto err; |
| 8613 | } |
| 8614 | } |
| 8615 | } |
| 8616 | return new; |
| 8617 | err: |
| 8618 | Py_DECREF(new); |
| 8619 | return NULL; |
| 8620 | } |
| 8621 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8622 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8623 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8624 | \n\ |
| 8625 | Return a copy of the string S, where all characters have been mapped\n\ |
| 8626 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8627 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 8628 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 8629 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8630 | |
| 8631 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8632 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8633 | { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8634 | return PyUnicode_TranslateCharmap(self->str, self->length, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8635 | } |
| 8636 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8637 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8638 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8639 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8640 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8641 | |
| 8642 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8643 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8644 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8645 | return fixup(self, fixupper); |
| 8646 | } |
| 8647 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8648 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8649 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8650 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 8651 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 8652 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8653 | |
| 8654 | static PyObject * |
| 8655 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 8656 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8657 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8658 | PyUnicodeObject *u; |
| 8659 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8660 | Py_ssize_t width; |
| 8661 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8662 | return NULL; |
| 8663 | |
| 8664 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 8665 | if (PyUnicode_CheckExact(self)) { |
| 8666 | Py_INCREF(self); |
| 8667 | return (PyObject*) self; |
| 8668 | } |
| 8669 | else |
| 8670 | return PyUnicode_FromUnicode( |
| 8671 | PyUnicode_AS_UNICODE(self), |
| 8672 | PyUnicode_GET_SIZE(self) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8673 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8674 | } |
| 8675 | |
| 8676 | fill = width - self->length; |
| 8677 | |
| 8678 | u = pad(self, fill, 0, '0'); |
| 8679 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8680 | if (u == NULL) |
| 8681 | return NULL; |
| 8682 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8683 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 8684 | /* move sign to beginning of string */ |
| 8685 | u->str[0] = u->str[fill]; |
| 8686 | u->str[fill] = '0'; |
| 8687 | } |
| 8688 | |
| 8689 | return (PyObject*) u; |
| 8690 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8691 | |
| 8692 | #if 0 |
| 8693 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8694 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8695 | { |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 8696 | return PyLong_FromLong(numfree); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8697 | } |
| 8698 | #endif |
| 8699 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8700 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8701 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8702 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 8703 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 8704 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8705 | With optional end, stop comparing S at that position.\n\ |
| 8706 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8707 | |
| 8708 | static PyObject * |
| 8709 | unicode_startswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8710 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8711 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8712 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8713 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8714 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8715 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8716 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8717 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8718 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8719 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 8720 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8721 | if (PyTuple_Check(subobj)) { |
| 8722 | Py_ssize_t i; |
| 8723 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 8724 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8725 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8726 | if (substring == NULL) |
| 8727 | return NULL; |
| 8728 | result = tailmatch(self, substring, start, end, -1); |
| 8729 | Py_DECREF(substring); |
| 8730 | if (result) { |
| 8731 | Py_RETURN_TRUE; |
| 8732 | } |
| 8733 | } |
| 8734 | /* nothing matched */ |
| 8735 | Py_RETURN_FALSE; |
| 8736 | } |
| 8737 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8738 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8739 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8740 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8741 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8742 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8743 | } |
| 8744 | |
| 8745 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8746 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8747 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8748 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 8749 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 8750 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8751 | With optional end, stop comparing S at that position.\n\ |
| 8752 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8753 | |
| 8754 | static PyObject * |
| 8755 | unicode_endswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8756 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8757 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8758 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8759 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8760 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8761 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8762 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8763 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8764 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8765 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 8766 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8767 | if (PyTuple_Check(subobj)) { |
| 8768 | Py_ssize_t i; |
| 8769 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 8770 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8771 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8772 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8773 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8774 | result = tailmatch(self, substring, start, end, +1); |
| 8775 | Py_DECREF(substring); |
| 8776 | if (result) { |
| 8777 | Py_RETURN_TRUE; |
| 8778 | } |
| 8779 | } |
| 8780 | Py_RETURN_FALSE; |
| 8781 | } |
| 8782 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8783 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8784 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8785 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8786 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8787 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8788 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8789 | } |
| 8790 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8791 | #include "stringlib/string_format.h" |
| 8792 | |
| 8793 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8794 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8795 | \n\ |
| 8796 | "); |
| 8797 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 8798 | static PyObject * |
| 8799 | unicode__format__(PyObject* self, PyObject* args) |
| 8800 | { |
| 8801 | PyObject *format_spec; |
| 8802 | |
| 8803 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 8804 | return NULL; |
| 8805 | |
| 8806 | return _PyUnicode_FormatAdvanced(self, |
| 8807 | PyUnicode_AS_UNICODE(format_spec), |
| 8808 | PyUnicode_GET_SIZE(format_spec)); |
| 8809 | } |
| 8810 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8811 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8812 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8813 | \n\ |
| 8814 | "); |
| 8815 | |
| 8816 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8817 | unicode__sizeof__(PyUnicodeObject *v) |
| 8818 | { |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 8819 | return PyLong_FromSsize_t(sizeof(PyUnicodeObject) + |
| 8820 | sizeof(Py_UNICODE) * (v->length + 1)); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8821 | } |
| 8822 | |
| 8823 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8824 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8825 | |
| 8826 | static PyObject * |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8827 | unicode_getnewargs(PyUnicodeObject *v) |
| 8828 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8829 | return Py_BuildValue("(u#)", v->str, v->length); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8830 | } |
| 8831 | |
| 8832 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8833 | static PyMethodDef unicode_methods[] = { |
| 8834 | |
| 8835 | /* Order is according to common usage: often used methods should |
| 8836 | appear first, since lookup is done sequentially. */ |
| 8837 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 8838 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8839 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 8840 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8841 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8842 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 8843 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 8844 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 8845 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 8846 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 8847 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 8848 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8849 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8850 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 8851 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 8852 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8853 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8854 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 8855 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 8856 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8857 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8858 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8859 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8860 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8861 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 8862 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 8863 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 8864 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 8865 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 8866 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 8867 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 8868 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 8869 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 8870 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 8871 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 8872 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 8873 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 8874 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 8875 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8876 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8877 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 8878 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 8879 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Eric Smith | f6db409 | 2007-08-27 23:52:26 +0000 | [diff] [blame] | 8880 | {"_formatter_field_name_split", (PyCFunction) formatter_field_name_split, METH_NOARGS}, |
| 8881 | {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8882 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 8883 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8884 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8885 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8886 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8887 | #endif |
| 8888 | |
| 8889 | #if 0 |
| 8890 | /* This one is just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8891 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8892 | #endif |
| 8893 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8894 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8895 | {NULL, NULL} |
| 8896 | }; |
| 8897 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8898 | static PyObject * |
| 8899 | unicode_mod(PyObject *v, PyObject *w) |
| 8900 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8901 | if (!PyUnicode_Check(v)) { |
| 8902 | Py_INCREF(Py_NotImplemented); |
| 8903 | return Py_NotImplemented; |
| 8904 | } |
| 8905 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8906 | } |
| 8907 | |
| 8908 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8909 | 0, /*nb_add*/ |
| 8910 | 0, /*nb_subtract*/ |
| 8911 | 0, /*nb_multiply*/ |
| 8912 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8913 | }; |
| 8914 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8915 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8916 | (lenfunc) unicode_length, /* sq_length */ |
| 8917 | PyUnicode_Concat, /* sq_concat */ |
| 8918 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 8919 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 8920 | 0, /* sq_slice */ |
| 8921 | 0, /* sq_ass_item */ |
| 8922 | 0, /* sq_ass_slice */ |
| 8923 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8924 | }; |
| 8925 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8926 | static PyObject* |
| 8927 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 8928 | { |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 8929 | if (PyIndex_Check(item)) { |
| 8930 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8931 | if (i == -1 && PyErr_Occurred()) |
| 8932 | return NULL; |
| 8933 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8934 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8935 | return unicode_getitem(self, i); |
| 8936 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8937 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8938 | Py_UNICODE* source_buf; |
| 8939 | Py_UNICODE* result_buf; |
| 8940 | PyObject* result; |
| 8941 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8942 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8943 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8944 | return NULL; |
| 8945 | } |
| 8946 | |
| 8947 | if (slicelength <= 0) { |
| 8948 | return PyUnicode_FromUnicode(NULL, 0); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 8949 | } else if (start == 0 && step == 1 && slicelength == self->length && |
| 8950 | PyUnicode_CheckExact(self)) { |
| 8951 | Py_INCREF(self); |
| 8952 | return (PyObject *)self; |
| 8953 | } else if (step == 1) { |
| 8954 | return PyUnicode_FromUnicode(self->str + start, slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8955 | } else { |
| 8956 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 8957 | result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* |
| 8958 | sizeof(Py_UNICODE)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8959 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8960 | if (result_buf == NULL) |
| 8961 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8962 | |
| 8963 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 8964 | result_buf[i] = source_buf[cur]; |
| 8965 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8966 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8967 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 8968 | PyObject_FREE(result_buf); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8969 | return result; |
| 8970 | } |
| 8971 | } else { |
| 8972 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 8973 | return NULL; |
| 8974 | } |
| 8975 | } |
| 8976 | |
| 8977 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8978 | (lenfunc)unicode_length, /* mp_length */ |
| 8979 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 8980 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8981 | }; |
| 8982 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8983 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8984 | /* Helpers for PyUnicode_Format() */ |
| 8985 | |
| 8986 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8987 | 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] | 8988 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8989 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8990 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8991 | (*p_argidx)++; |
| 8992 | if (arglen < 0) |
| 8993 | return args; |
| 8994 | else |
| 8995 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8996 | } |
| 8997 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8998 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8999 | return NULL; |
| 9000 | } |
| 9001 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9002 | /* 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] | 9003 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9004 | static PyObject * |
| 9005 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9006 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9007 | char *p; |
| 9008 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9009 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9010 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9011 | x = PyFloat_AsDouble(v); |
| 9012 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9013 | return NULL; |
| 9014 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9015 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9016 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 9017 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 9018 | p = PyOS_double_to_string(x, type, prec, |
| 9019 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9020 | if (p == NULL) |
| 9021 | return NULL; |
| 9022 | result = PyUnicode_FromStringAndSize(p, strlen(p)); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 9023 | PyMem_Free(p); |
| 9024 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9025 | } |
| 9026 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9027 | static PyObject* |
| 9028 | formatlong(PyObject *val, int flags, int prec, int type) |
| 9029 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9030 | char *buf; |
| 9031 | int len; |
| 9032 | PyObject *str; /* temporary string object. */ |
| 9033 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9034 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9035 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 9036 | if (!str) |
| 9037 | return NULL; |
| 9038 | result = PyUnicode_FromStringAndSize(buf, len); |
| 9039 | Py_DECREF(str); |
| 9040 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9041 | } |
| 9042 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9043 | static int |
| 9044 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9045 | size_t buflen, |
| 9046 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9047 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 9048 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 9049 | if (PyUnicode_Check(v)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9050 | if (PyUnicode_GET_SIZE(v) == 1) { |
| 9051 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
| 9052 | buf[1] = '\0'; |
| 9053 | return 1; |
| 9054 | } |
| 9055 | #ifndef Py_UNICODE_WIDE |
| 9056 | if (PyUnicode_GET_SIZE(v) == 2) { |
| 9057 | /* Decode a valid surrogate pair */ |
| 9058 | int c0 = PyUnicode_AS_UNICODE(v)[0]; |
| 9059 | int c1 = PyUnicode_AS_UNICODE(v)[1]; |
| 9060 | if (0xD800 <= c0 && c0 <= 0xDBFF && |
| 9061 | 0xDC00 <= c1 && c1 <= 0xDFFF) { |
| 9062 | buf[0] = c0; |
| 9063 | buf[1] = c1; |
| 9064 | buf[2] = '\0'; |
| 9065 | return 2; |
| 9066 | } |
| 9067 | } |
| 9068 | #endif |
| 9069 | goto onError; |
| 9070 | } |
| 9071 | else { |
| 9072 | /* Integer input truncated to a character */ |
| 9073 | long x; |
| 9074 | x = PyLong_AsLong(v); |
| 9075 | if (x == -1 && PyErr_Occurred()) |
| 9076 | goto onError; |
| 9077 | |
| 9078 | if (x < 0 || x > 0x10ffff) { |
| 9079 | PyErr_SetString(PyExc_OverflowError, |
| 9080 | "%c arg not in range(0x110000)"); |
| 9081 | return -1; |
| 9082 | } |
| 9083 | |
| 9084 | #ifndef Py_UNICODE_WIDE |
| 9085 | if (x > 0xffff) { |
| 9086 | x -= 0x10000; |
| 9087 | buf[0] = (Py_UNICODE)(0xD800 | (x >> 10)); |
| 9088 | buf[1] = (Py_UNICODE)(0xDC00 | (x & 0x3FF)); |
| 9089 | return 2; |
| 9090 | } |
| 9091 | #endif |
| 9092 | buf[0] = (Py_UNICODE) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9093 | buf[1] = '\0'; |
| 9094 | return 1; |
| 9095 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 9096 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9097 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 9098 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9099 | "%c requires int or char"); |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 9100 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9101 | } |
| 9102 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9103 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9104 | 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] | 9105 | */ |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9106 | #define FORMATBUFLEN (size_t)10 |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9107 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9108 | PyObject *PyUnicode_Format(PyObject *format, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9109 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9110 | { |
| 9111 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9112 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9113 | int args_owned = 0; |
| 9114 | PyUnicodeObject *result = NULL; |
| 9115 | PyObject *dict = NULL; |
| 9116 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9117 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9118 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9119 | PyErr_BadInternalCall(); |
| 9120 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9121 | } |
| 9122 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 9123 | if (uformat == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9124 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9125 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 9126 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 9127 | |
| 9128 | reslen = rescnt = fmtcnt + 100; |
| 9129 | result = _PyUnicode_New(reslen); |
| 9130 | if (result == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9131 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9132 | res = PyUnicode_AS_UNICODE(result); |
| 9133 | |
| 9134 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9135 | arglen = PyTuple_Size(args); |
| 9136 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9137 | } |
| 9138 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9139 | arglen = -1; |
| 9140 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9141 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 9142 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 9143 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9144 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9145 | |
| 9146 | while (--fmtcnt >= 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9147 | if (*fmt != '%') { |
| 9148 | if (--rescnt < 0) { |
| 9149 | rescnt = fmtcnt + 100; |
| 9150 | reslen += rescnt; |
| 9151 | if (_PyUnicode_Resize(&result, reslen) < 0) |
| 9152 | goto onError; |
| 9153 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 9154 | --rescnt; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9155 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9156 | *res++ = *fmt++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9157 | } |
| 9158 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9159 | /* Got a format specifier */ |
| 9160 | int flags = 0; |
| 9161 | Py_ssize_t width = -1; |
| 9162 | int prec = -1; |
| 9163 | Py_UNICODE c = '\0'; |
| 9164 | Py_UNICODE fill; |
| 9165 | int isnumok; |
| 9166 | PyObject *v = NULL; |
| 9167 | PyObject *temp = NULL; |
| 9168 | Py_UNICODE *pbuf; |
| 9169 | Py_UNICODE sign; |
| 9170 | Py_ssize_t len; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9171 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For formatchar() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9172 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9173 | fmt++; |
| 9174 | if (*fmt == '(') { |
| 9175 | Py_UNICODE *keystart; |
| 9176 | Py_ssize_t keylen; |
| 9177 | PyObject *key; |
| 9178 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 9179 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9180 | if (dict == NULL) { |
| 9181 | PyErr_SetString(PyExc_TypeError, |
| 9182 | "format requires a mapping"); |
| 9183 | goto onError; |
| 9184 | } |
| 9185 | ++fmt; |
| 9186 | --fmtcnt; |
| 9187 | keystart = fmt; |
| 9188 | /* Skip over balanced parentheses */ |
| 9189 | while (pcount > 0 && --fmtcnt >= 0) { |
| 9190 | if (*fmt == ')') |
| 9191 | --pcount; |
| 9192 | else if (*fmt == '(') |
| 9193 | ++pcount; |
| 9194 | fmt++; |
| 9195 | } |
| 9196 | keylen = fmt - keystart - 1; |
| 9197 | if (fmtcnt < 0 || pcount > 0) { |
| 9198 | PyErr_SetString(PyExc_ValueError, |
| 9199 | "incomplete format key"); |
| 9200 | goto onError; |
| 9201 | } |
| 9202 | #if 0 |
| 9203 | /* keys are converted to strings using UTF-8 and |
| 9204 | then looked up since Python uses strings to hold |
| 9205 | variables names etc. in its namespaces and we |
| 9206 | wouldn't want to break common idioms. */ |
| 9207 | key = PyUnicode_EncodeUTF8(keystart, |
| 9208 | keylen, |
| 9209 | NULL); |
| 9210 | #else |
| 9211 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 9212 | #endif |
| 9213 | if (key == NULL) |
| 9214 | goto onError; |
| 9215 | if (args_owned) { |
| 9216 | Py_DECREF(args); |
| 9217 | args_owned = 0; |
| 9218 | } |
| 9219 | args = PyObject_GetItem(dict, key); |
| 9220 | Py_DECREF(key); |
| 9221 | if (args == NULL) { |
| 9222 | goto onError; |
| 9223 | } |
| 9224 | args_owned = 1; |
| 9225 | arglen = -1; |
| 9226 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9227 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9228 | while (--fmtcnt >= 0) { |
| 9229 | switch (c = *fmt++) { |
| 9230 | case '-': flags |= F_LJUST; continue; |
| 9231 | case '+': flags |= F_SIGN; continue; |
| 9232 | case ' ': flags |= F_BLANK; continue; |
| 9233 | case '#': flags |= F_ALT; continue; |
| 9234 | case '0': flags |= F_ZERO; continue; |
| 9235 | } |
| 9236 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9237 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9238 | if (c == '*') { |
| 9239 | v = getnextarg(args, arglen, &argidx); |
| 9240 | if (v == NULL) |
| 9241 | goto onError; |
| 9242 | if (!PyLong_Check(v)) { |
| 9243 | PyErr_SetString(PyExc_TypeError, |
| 9244 | "* wants int"); |
| 9245 | goto onError; |
| 9246 | } |
| 9247 | width = PyLong_AsLong(v); |
| 9248 | if (width == -1 && PyErr_Occurred()) |
| 9249 | goto onError; |
| 9250 | if (width < 0) { |
| 9251 | flags |= F_LJUST; |
| 9252 | width = -width; |
| 9253 | } |
| 9254 | if (--fmtcnt >= 0) |
| 9255 | c = *fmt++; |
| 9256 | } |
| 9257 | else if (c >= '0' && c <= '9') { |
| 9258 | width = c - '0'; |
| 9259 | while (--fmtcnt >= 0) { |
| 9260 | c = *fmt++; |
| 9261 | if (c < '0' || c > '9') |
| 9262 | break; |
| 9263 | if ((width*10) / 10 != width) { |
| 9264 | PyErr_SetString(PyExc_ValueError, |
| 9265 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9266 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9267 | } |
| 9268 | width = width*10 + (c - '0'); |
| 9269 | } |
| 9270 | } |
| 9271 | if (c == '.') { |
| 9272 | prec = 0; |
| 9273 | if (--fmtcnt >= 0) |
| 9274 | c = *fmt++; |
| 9275 | if (c == '*') { |
| 9276 | v = getnextarg(args, arglen, &argidx); |
| 9277 | if (v == NULL) |
| 9278 | goto onError; |
| 9279 | if (!PyLong_Check(v)) { |
| 9280 | PyErr_SetString(PyExc_TypeError, |
| 9281 | "* wants int"); |
| 9282 | goto onError; |
| 9283 | } |
| 9284 | prec = PyLong_AsLong(v); |
| 9285 | if (prec == -1 && PyErr_Occurred()) |
| 9286 | goto onError; |
| 9287 | if (prec < 0) |
| 9288 | prec = 0; |
| 9289 | if (--fmtcnt >= 0) |
| 9290 | c = *fmt++; |
| 9291 | } |
| 9292 | else if (c >= '0' && c <= '9') { |
| 9293 | prec = c - '0'; |
| 9294 | while (--fmtcnt >= 0) { |
Stefan Krah | 99212f6 | 2010-07-19 17:58:26 +0000 | [diff] [blame] | 9295 | c = *fmt++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9296 | if (c < '0' || c > '9') |
| 9297 | break; |
| 9298 | if ((prec*10) / 10 != prec) { |
| 9299 | PyErr_SetString(PyExc_ValueError, |
| 9300 | "prec too big"); |
| 9301 | goto onError; |
| 9302 | } |
| 9303 | prec = prec*10 + (c - '0'); |
| 9304 | } |
| 9305 | } |
| 9306 | } /* prec */ |
| 9307 | if (fmtcnt >= 0) { |
| 9308 | if (c == 'h' || c == 'l' || c == 'L') { |
| 9309 | if (--fmtcnt >= 0) |
| 9310 | c = *fmt++; |
| 9311 | } |
| 9312 | } |
| 9313 | if (fmtcnt < 0) { |
| 9314 | PyErr_SetString(PyExc_ValueError, |
| 9315 | "incomplete format"); |
| 9316 | goto onError; |
| 9317 | } |
| 9318 | if (c != '%') { |
| 9319 | v = getnextarg(args, arglen, &argidx); |
| 9320 | if (v == NULL) |
| 9321 | goto onError; |
| 9322 | } |
| 9323 | sign = 0; |
| 9324 | fill = ' '; |
| 9325 | switch (c) { |
| 9326 | |
| 9327 | case '%': |
| 9328 | pbuf = formatbuf; |
| 9329 | /* presume that buffer length is at least 1 */ |
| 9330 | pbuf[0] = '%'; |
| 9331 | len = 1; |
| 9332 | break; |
| 9333 | |
| 9334 | case 's': |
| 9335 | case 'r': |
| 9336 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 9337 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9338 | temp = v; |
| 9339 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9340 | } |
| 9341 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9342 | if (c == 's') |
| 9343 | temp = PyObject_Str(v); |
| 9344 | else if (c == 'r') |
| 9345 | temp = PyObject_Repr(v); |
| 9346 | else |
| 9347 | temp = PyObject_ASCII(v); |
| 9348 | if (temp == NULL) |
| 9349 | goto onError; |
| 9350 | if (PyUnicode_Check(temp)) |
| 9351 | /* nothing to do */; |
| 9352 | else { |
| 9353 | Py_DECREF(temp); |
| 9354 | PyErr_SetString(PyExc_TypeError, |
| 9355 | "%s argument has non-string str()"); |
| 9356 | goto onError; |
| 9357 | } |
| 9358 | } |
| 9359 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9360 | len = PyUnicode_GET_SIZE(temp); |
| 9361 | if (prec >= 0 && len > prec) |
| 9362 | len = prec; |
| 9363 | break; |
| 9364 | |
| 9365 | case 'i': |
| 9366 | case 'd': |
| 9367 | case 'u': |
| 9368 | case 'o': |
| 9369 | case 'x': |
| 9370 | case 'X': |
| 9371 | if (c == 'i') |
| 9372 | c = 'd'; |
| 9373 | isnumok = 0; |
| 9374 | if (PyNumber_Check(v)) { |
| 9375 | PyObject *iobj=NULL; |
| 9376 | |
| 9377 | if (PyLong_Check(v)) { |
| 9378 | iobj = v; |
| 9379 | Py_INCREF(iobj); |
| 9380 | } |
| 9381 | else { |
| 9382 | iobj = PyNumber_Long(v); |
| 9383 | } |
| 9384 | if (iobj!=NULL) { |
| 9385 | if (PyLong_Check(iobj)) { |
| 9386 | isnumok = 1; |
| 9387 | temp = formatlong(iobj, flags, prec, c); |
| 9388 | Py_DECREF(iobj); |
| 9389 | if (!temp) |
| 9390 | goto onError; |
| 9391 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9392 | len = PyUnicode_GET_SIZE(temp); |
| 9393 | sign = 1; |
| 9394 | } |
| 9395 | else { |
| 9396 | Py_DECREF(iobj); |
| 9397 | } |
| 9398 | } |
| 9399 | } |
| 9400 | if (!isnumok) { |
| 9401 | PyErr_Format(PyExc_TypeError, |
| 9402 | "%%%c format: a number is required, " |
| 9403 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 9404 | goto onError; |
| 9405 | } |
| 9406 | if (flags & F_ZERO) |
| 9407 | fill = '0'; |
| 9408 | break; |
| 9409 | |
| 9410 | case 'e': |
| 9411 | case 'E': |
| 9412 | case 'f': |
| 9413 | case 'F': |
| 9414 | case 'g': |
| 9415 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9416 | temp = formatfloat(v, flags, prec, c); |
| 9417 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9418 | goto onError; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9419 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9420 | len = PyUnicode_GET_SIZE(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9421 | sign = 1; |
| 9422 | if (flags & F_ZERO) |
| 9423 | fill = '0'; |
| 9424 | break; |
| 9425 | |
| 9426 | case 'c': |
| 9427 | pbuf = formatbuf; |
| 9428 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
| 9429 | if (len < 0) |
| 9430 | goto onError; |
| 9431 | break; |
| 9432 | |
| 9433 | default: |
| 9434 | PyErr_Format(PyExc_ValueError, |
| 9435 | "unsupported format character '%c' (0x%x) " |
| 9436 | "at index %zd", |
| 9437 | (31<=c && c<=126) ? (char)c : '?', |
| 9438 | (int)c, |
| 9439 | (Py_ssize_t)(fmt - 1 - |
| 9440 | PyUnicode_AS_UNICODE(uformat))); |
| 9441 | goto onError; |
| 9442 | } |
| 9443 | if (sign) { |
| 9444 | if (*pbuf == '-' || *pbuf == '+') { |
| 9445 | sign = *pbuf++; |
| 9446 | len--; |
| 9447 | } |
| 9448 | else if (flags & F_SIGN) |
| 9449 | sign = '+'; |
| 9450 | else if (flags & F_BLANK) |
| 9451 | sign = ' '; |
| 9452 | else |
| 9453 | sign = 0; |
| 9454 | } |
| 9455 | if (width < len) |
| 9456 | width = len; |
| 9457 | if (rescnt - (sign != 0) < width) { |
| 9458 | reslen -= rescnt; |
| 9459 | rescnt = width + fmtcnt + 100; |
| 9460 | reslen += rescnt; |
| 9461 | if (reslen < 0) { |
| 9462 | Py_XDECREF(temp); |
| 9463 | PyErr_NoMemory(); |
| 9464 | goto onError; |
| 9465 | } |
| 9466 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 9467 | Py_XDECREF(temp); |
| 9468 | goto onError; |
| 9469 | } |
| 9470 | res = PyUnicode_AS_UNICODE(result) |
| 9471 | + reslen - rescnt; |
| 9472 | } |
| 9473 | if (sign) { |
| 9474 | if (fill != ' ') |
| 9475 | *res++ = sign; |
| 9476 | rescnt--; |
| 9477 | if (width > len) |
| 9478 | width--; |
| 9479 | } |
| 9480 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
| 9481 | assert(pbuf[0] == '0'); |
| 9482 | assert(pbuf[1] == c); |
| 9483 | if (fill != ' ') { |
| 9484 | *res++ = *pbuf++; |
| 9485 | *res++ = *pbuf++; |
| 9486 | } |
| 9487 | rescnt -= 2; |
| 9488 | width -= 2; |
| 9489 | if (width < 0) |
| 9490 | width = 0; |
| 9491 | len -= 2; |
| 9492 | } |
| 9493 | if (width > len && !(flags & F_LJUST)) { |
| 9494 | do { |
| 9495 | --rescnt; |
| 9496 | *res++ = fill; |
| 9497 | } while (--width > len); |
| 9498 | } |
| 9499 | if (fill == ' ') { |
| 9500 | if (sign) |
| 9501 | *res++ = sign; |
| 9502 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
| 9503 | assert(pbuf[0] == '0'); |
| 9504 | assert(pbuf[1] == c); |
| 9505 | *res++ = *pbuf++; |
| 9506 | *res++ = *pbuf++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9507 | } |
| 9508 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9509 | Py_UNICODE_COPY(res, pbuf, len); |
| 9510 | res += len; |
| 9511 | rescnt -= len; |
| 9512 | while (--width >= len) { |
| 9513 | --rescnt; |
| 9514 | *res++ = ' '; |
| 9515 | } |
| 9516 | if (dict && (argidx < arglen) && c != '%') { |
| 9517 | PyErr_SetString(PyExc_TypeError, |
| 9518 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9519 | Py_XDECREF(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9520 | goto onError; |
| 9521 | } |
| 9522 | Py_XDECREF(temp); |
| 9523 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9524 | } /* until end */ |
| 9525 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9526 | PyErr_SetString(PyExc_TypeError, |
| 9527 | "not all arguments converted during string formatting"); |
| 9528 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9529 | } |
| 9530 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9531 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9532 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9533 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9534 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9535 | } |
| 9536 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9537 | return (PyObject *)result; |
| 9538 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9539 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9540 | Py_XDECREF(result); |
| 9541 | Py_DECREF(uformat); |
| 9542 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9543 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9544 | } |
| 9545 | return NULL; |
| 9546 | } |
| 9547 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 9548 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9549 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 9550 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9551 | static PyObject * |
| 9552 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9553 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9554 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9555 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 9556 | char *encoding = NULL; |
| 9557 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9558 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9559 | if (type != &PyUnicode_Type) |
| 9560 | return unicode_subtype_new(type, args, kwds); |
| 9561 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9562 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9563 | return NULL; |
| 9564 | if (x == NULL) |
| 9565 | return (PyObject *)_PyUnicode_New(0); |
| 9566 | if (encoding == NULL && errors == NULL) |
| 9567 | return PyObject_Str(x); |
| 9568 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9569 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9570 | } |
| 9571 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9572 | static PyObject * |
| 9573 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9574 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9575 | PyUnicodeObject *tmp, *pnew; |
| 9576 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9577 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9578 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 9579 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 9580 | if (tmp == NULL) |
| 9581 | return NULL; |
| 9582 | assert(PyUnicode_Check(tmp)); |
| 9583 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
| 9584 | if (pnew == NULL) { |
| 9585 | Py_DECREF(tmp); |
| 9586 | return NULL; |
| 9587 | } |
| 9588 | pnew->str = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1)); |
| 9589 | if (pnew->str == NULL) { |
| 9590 | _Py_ForgetReference((PyObject *)pnew); |
| 9591 | PyObject_Del(pnew); |
| 9592 | Py_DECREF(tmp); |
| 9593 | return PyErr_NoMemory(); |
| 9594 | } |
| 9595 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 9596 | pnew->length = n; |
| 9597 | pnew->hash = tmp->hash; |
| 9598 | Py_DECREF(tmp); |
| 9599 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9600 | } |
| 9601 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9602 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9603 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9604 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 9605 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 9606 | encoding defaults to the current default string encoding.\n\ |
| 9607 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9608 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9609 | static PyObject *unicode_iter(PyObject *seq); |
| 9610 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9611 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 9612 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9613 | "str", /* tp_name */ |
| 9614 | sizeof(PyUnicodeObject), /* tp_size */ |
| 9615 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9616 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9617 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 9618 | 0, /* tp_print */ |
| 9619 | 0, /* tp_getattr */ |
| 9620 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 9621 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9622 | unicode_repr, /* tp_repr */ |
| 9623 | &unicode_as_number, /* tp_as_number */ |
| 9624 | &unicode_as_sequence, /* tp_as_sequence */ |
| 9625 | &unicode_as_mapping, /* tp_as_mapping */ |
| 9626 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 9627 | 0, /* tp_call*/ |
| 9628 | (reprfunc) unicode_str, /* tp_str */ |
| 9629 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9630 | 0, /* tp_setattro */ |
| 9631 | 0, /* tp_as_buffer */ |
| 9632 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9633 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9634 | unicode_doc, /* tp_doc */ |
| 9635 | 0, /* tp_traverse */ |
| 9636 | 0, /* tp_clear */ |
| 9637 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 9638 | 0, /* tp_weaklistoffset */ |
| 9639 | unicode_iter, /* tp_iter */ |
| 9640 | 0, /* tp_iternext */ |
| 9641 | unicode_methods, /* tp_methods */ |
| 9642 | 0, /* tp_members */ |
| 9643 | 0, /* tp_getset */ |
| 9644 | &PyBaseObject_Type, /* tp_base */ |
| 9645 | 0, /* tp_dict */ |
| 9646 | 0, /* tp_descr_get */ |
| 9647 | 0, /* tp_descr_set */ |
| 9648 | 0, /* tp_dictoffset */ |
| 9649 | 0, /* tp_init */ |
| 9650 | 0, /* tp_alloc */ |
| 9651 | unicode_new, /* tp_new */ |
| 9652 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9653 | }; |
| 9654 | |
| 9655 | /* Initialize the Unicode implementation */ |
| 9656 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9657 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9658 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9659 | int i; |
| 9660 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9661 | /* XXX - move this array to unicodectype.c ? */ |
| 9662 | Py_UNICODE linebreak[] = { |
| 9663 | 0x000A, /* LINE FEED */ |
| 9664 | 0x000D, /* CARRIAGE RETURN */ |
| 9665 | 0x001C, /* FILE SEPARATOR */ |
| 9666 | 0x001D, /* GROUP SEPARATOR */ |
| 9667 | 0x001E, /* RECORD SEPARATOR */ |
| 9668 | 0x0085, /* NEXT LINE */ |
| 9669 | 0x2028, /* LINE SEPARATOR */ |
| 9670 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 9671 | }; |
| 9672 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 9673 | /* Init the implementation */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 9674 | free_list = NULL; |
| 9675 | numfree = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9676 | unicode_empty = _PyUnicode_New(0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9677 | if (!unicode_empty) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9678 | return; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9679 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9680 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9681 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 9682 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9683 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9684 | |
| 9685 | /* initialize the linebreak bloom filter */ |
| 9686 | bloom_linebreak = make_bloom_mask( |
| 9687 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 9688 | ); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9689 | |
| 9690 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9691 | } |
| 9692 | |
| 9693 | /* Finalize the Unicode implementation */ |
| 9694 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9695 | int |
| 9696 | PyUnicode_ClearFreeList(void) |
| 9697 | { |
| 9698 | int freelist_size = numfree; |
| 9699 | PyUnicodeObject *u; |
| 9700 | |
| 9701 | for (u = free_list; u != NULL;) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9702 | PyUnicodeObject *v = u; |
| 9703 | u = *(PyUnicodeObject **)u; |
| 9704 | if (v->str) |
| 9705 | PyObject_DEL(v->str); |
| 9706 | Py_XDECREF(v->defenc); |
| 9707 | PyObject_Del(v); |
| 9708 | numfree--; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9709 | } |
| 9710 | free_list = NULL; |
| 9711 | assert(numfree == 0); |
| 9712 | return freelist_size; |
| 9713 | } |
| 9714 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9715 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9716 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9717 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9718 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9719 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 9720 | Py_XDECREF(unicode_empty); |
| 9721 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 9722 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9723 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9724 | if (unicode_latin1[i]) { |
| 9725 | Py_DECREF(unicode_latin1[i]); |
| 9726 | unicode_latin1[i] = NULL; |
| 9727 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9728 | } |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9729 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9730 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9731 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9732 | void |
| 9733 | PyUnicode_InternInPlace(PyObject **p) |
| 9734 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9735 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 9736 | PyObject *t; |
| 9737 | if (s == NULL || !PyUnicode_Check(s)) |
| 9738 | Py_FatalError( |
| 9739 | "PyUnicode_InternInPlace: unicode strings only please!"); |
| 9740 | /* If it's a subclass, we don't really know what putting |
| 9741 | it in the interned dict might do. */ |
| 9742 | if (!PyUnicode_CheckExact(s)) |
| 9743 | return; |
| 9744 | if (PyUnicode_CHECK_INTERNED(s)) |
| 9745 | return; |
| 9746 | if (interned == NULL) { |
| 9747 | interned = PyDict_New(); |
| 9748 | if (interned == NULL) { |
| 9749 | PyErr_Clear(); /* Don't leave an exception */ |
| 9750 | return; |
| 9751 | } |
| 9752 | } |
| 9753 | /* It might be that the GetItem call fails even |
| 9754 | though the key is present in the dictionary, |
| 9755 | namely when this happens during a stack overflow. */ |
| 9756 | Py_ALLOW_RECURSION |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9757 | t = PyDict_GetItem(interned, (PyObject *)s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9758 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9759 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9760 | if (t) { |
| 9761 | Py_INCREF(t); |
| 9762 | Py_DECREF(*p); |
| 9763 | *p = t; |
| 9764 | return; |
| 9765 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9766 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9767 | PyThreadState_GET()->recursion_critical = 1; |
| 9768 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 9769 | PyErr_Clear(); |
| 9770 | PyThreadState_GET()->recursion_critical = 0; |
| 9771 | return; |
| 9772 | } |
| 9773 | PyThreadState_GET()->recursion_critical = 0; |
| 9774 | /* The two references in interned are not counted by refcnt. |
| 9775 | The deallocator will take care of this */ |
| 9776 | Py_REFCNT(s) -= 2; |
| 9777 | PyUnicode_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9778 | } |
| 9779 | |
| 9780 | void |
| 9781 | PyUnicode_InternImmortal(PyObject **p) |
| 9782 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9783 | PyUnicode_InternInPlace(p); |
| 9784 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
| 9785 | PyUnicode_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL; |
| 9786 | Py_INCREF(*p); |
| 9787 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9788 | } |
| 9789 | |
| 9790 | PyObject * |
| 9791 | PyUnicode_InternFromString(const char *cp) |
| 9792 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9793 | PyObject *s = PyUnicode_FromString(cp); |
| 9794 | if (s == NULL) |
| 9795 | return NULL; |
| 9796 | PyUnicode_InternInPlace(&s); |
| 9797 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9798 | } |
| 9799 | |
| 9800 | void _Py_ReleaseInternedUnicodeStrings(void) |
| 9801 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9802 | PyObject *keys; |
| 9803 | PyUnicodeObject *s; |
| 9804 | Py_ssize_t i, n; |
| 9805 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9806 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9807 | if (interned == NULL || !PyDict_Check(interned)) |
| 9808 | return; |
| 9809 | keys = PyDict_Keys(interned); |
| 9810 | if (keys == NULL || !PyList_Check(keys)) { |
| 9811 | PyErr_Clear(); |
| 9812 | return; |
| 9813 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9814 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9815 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 9816 | detector, interned unicode strings are not forcibly deallocated; |
| 9817 | rather, we give them their stolen references back, and then clear |
| 9818 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9819 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9820 | n = PyList_GET_SIZE(keys); |
| 9821 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9822 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9823 | for (i = 0; i < n; i++) { |
| 9824 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
| 9825 | switch (s->state) { |
| 9826 | case SSTATE_NOT_INTERNED: |
| 9827 | /* XXX Shouldn't happen */ |
| 9828 | break; |
| 9829 | case SSTATE_INTERNED_IMMORTAL: |
| 9830 | Py_REFCNT(s) += 1; |
| 9831 | immortal_size += s->length; |
| 9832 | break; |
| 9833 | case SSTATE_INTERNED_MORTAL: |
| 9834 | Py_REFCNT(s) += 2; |
| 9835 | mortal_size += s->length; |
| 9836 | break; |
| 9837 | default: |
| 9838 | Py_FatalError("Inconsistent interned string state."); |
| 9839 | } |
| 9840 | s->state = SSTATE_NOT_INTERNED; |
| 9841 | } |
| 9842 | fprintf(stderr, "total size of all interned strings: " |
| 9843 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 9844 | "mortal/immortal\n", mortal_size, immortal_size); |
| 9845 | Py_DECREF(keys); |
| 9846 | PyDict_Clear(interned); |
| 9847 | Py_DECREF(interned); |
| 9848 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9849 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9850 | |
| 9851 | |
| 9852 | /********************* Unicode Iterator **************************/ |
| 9853 | |
| 9854 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9855 | PyObject_HEAD |
| 9856 | Py_ssize_t it_index; |
| 9857 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9858 | } unicodeiterobject; |
| 9859 | |
| 9860 | static void |
| 9861 | unicodeiter_dealloc(unicodeiterobject *it) |
| 9862 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9863 | _PyObject_GC_UNTRACK(it); |
| 9864 | Py_XDECREF(it->it_seq); |
| 9865 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9866 | } |
| 9867 | |
| 9868 | static int |
| 9869 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 9870 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9871 | Py_VISIT(it->it_seq); |
| 9872 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9873 | } |
| 9874 | |
| 9875 | static PyObject * |
| 9876 | unicodeiter_next(unicodeiterobject *it) |
| 9877 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9878 | PyUnicodeObject *seq; |
| 9879 | PyObject *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9880 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9881 | assert(it != NULL); |
| 9882 | seq = it->it_seq; |
| 9883 | if (seq == NULL) |
| 9884 | return NULL; |
| 9885 | assert(PyUnicode_Check(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9886 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9887 | if (it->it_index < PyUnicode_GET_SIZE(seq)) { |
| 9888 | item = PyUnicode_FromUnicode( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9889 | PyUnicode_AS_UNICODE(seq)+it->it_index, 1); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9890 | if (item != NULL) |
| 9891 | ++it->it_index; |
| 9892 | return item; |
| 9893 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9894 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9895 | Py_DECREF(seq); |
| 9896 | it->it_seq = NULL; |
| 9897 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9898 | } |
| 9899 | |
| 9900 | static PyObject * |
| 9901 | unicodeiter_len(unicodeiterobject *it) |
| 9902 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9903 | Py_ssize_t len = 0; |
| 9904 | if (it->it_seq) |
| 9905 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
| 9906 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9907 | } |
| 9908 | |
| 9909 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 9910 | |
| 9911 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9912 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9913 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9914 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9915 | }; |
| 9916 | |
| 9917 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9918 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 9919 | "str_iterator", /* tp_name */ |
| 9920 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 9921 | 0, /* tp_itemsize */ |
| 9922 | /* methods */ |
| 9923 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 9924 | 0, /* tp_print */ |
| 9925 | 0, /* tp_getattr */ |
| 9926 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 9927 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9928 | 0, /* tp_repr */ |
| 9929 | 0, /* tp_as_number */ |
| 9930 | 0, /* tp_as_sequence */ |
| 9931 | 0, /* tp_as_mapping */ |
| 9932 | 0, /* tp_hash */ |
| 9933 | 0, /* tp_call */ |
| 9934 | 0, /* tp_str */ |
| 9935 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9936 | 0, /* tp_setattro */ |
| 9937 | 0, /* tp_as_buffer */ |
| 9938 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 9939 | 0, /* tp_doc */ |
| 9940 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 9941 | 0, /* tp_clear */ |
| 9942 | 0, /* tp_richcompare */ |
| 9943 | 0, /* tp_weaklistoffset */ |
| 9944 | PyObject_SelfIter, /* tp_iter */ |
| 9945 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 9946 | unicodeiter_methods, /* tp_methods */ |
| 9947 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9948 | }; |
| 9949 | |
| 9950 | static PyObject * |
| 9951 | unicode_iter(PyObject *seq) |
| 9952 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9953 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9954 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9955 | if (!PyUnicode_Check(seq)) { |
| 9956 | PyErr_BadInternalCall(); |
| 9957 | return NULL; |
| 9958 | } |
| 9959 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 9960 | if (it == NULL) |
| 9961 | return NULL; |
| 9962 | it->it_index = 0; |
| 9963 | Py_INCREF(seq); |
| 9964 | it->it_seq = (PyUnicodeObject *)seq; |
| 9965 | _PyObject_GC_TRACK(it); |
| 9966 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9967 | } |
| 9968 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9969 | size_t |
| 9970 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 9971 | { |
| 9972 | int res = 0; |
| 9973 | while(*u++) |
| 9974 | res++; |
| 9975 | return res; |
| 9976 | } |
| 9977 | |
| 9978 | Py_UNICODE* |
| 9979 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 9980 | { |
| 9981 | Py_UNICODE *u = s1; |
| 9982 | while ((*u++ = *s2++)); |
| 9983 | return s1; |
| 9984 | } |
| 9985 | |
| 9986 | Py_UNICODE* |
| 9987 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 9988 | { |
| 9989 | Py_UNICODE *u = s1; |
| 9990 | while ((*u++ = *s2++)) |
| 9991 | if (n-- == 0) |
| 9992 | break; |
| 9993 | return s1; |
| 9994 | } |
| 9995 | |
Victor Stinner | c4eb765 | 2010-09-01 23:43:50 +0000 | [diff] [blame] | 9996 | Py_UNICODE* |
| 9997 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 9998 | { |
| 9999 | Py_UNICODE *u1 = s1; |
| 10000 | u1 += Py_UNICODE_strlen(u1); |
| 10001 | Py_UNICODE_strcpy(u1, s2); |
| 10002 | return s1; |
| 10003 | } |
| 10004 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10005 | int |
| 10006 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 10007 | { |
| 10008 | while (*s1 && *s2 && *s1 == *s2) |
| 10009 | s1++, s2++; |
| 10010 | if (*s1 && *s2) |
| 10011 | return (*s1 < *s2) ? -1 : +1; |
| 10012 | if (*s1) |
| 10013 | return 1; |
| 10014 | if (*s2) |
| 10015 | return -1; |
| 10016 | return 0; |
| 10017 | } |
| 10018 | |
Victor Stinner | ef8d95c | 2010-08-16 22:03:11 +0000 | [diff] [blame] | 10019 | int |
| 10020 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 10021 | { |
| 10022 | register Py_UNICODE u1, u2; |
| 10023 | for (; n != 0; n--) { |
| 10024 | u1 = *s1; |
| 10025 | u2 = *s2; |
| 10026 | if (u1 != u2) |
| 10027 | return (u1 < u2) ? -1 : +1; |
| 10028 | if (u1 == '\0') |
| 10029 | return 0; |
| 10030 | s1++; |
| 10031 | s2++; |
| 10032 | } |
| 10033 | return 0; |
| 10034 | } |
| 10035 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10036 | Py_UNICODE* |
| 10037 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 10038 | { |
| 10039 | const Py_UNICODE *p; |
| 10040 | for (p = s; *p; p++) |
| 10041 | if (*p == c) |
| 10042 | return (Py_UNICODE*)p; |
| 10043 | return NULL; |
| 10044 | } |
| 10045 | |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 10046 | Py_UNICODE* |
| 10047 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 10048 | { |
| 10049 | const Py_UNICODE *p; |
| 10050 | p = s + Py_UNICODE_strlen(s); |
| 10051 | while (p != s) { |
| 10052 | p--; |
| 10053 | if (*p == c) |
| 10054 | return (Py_UNICODE*)p; |
| 10055 | } |
| 10056 | return NULL; |
| 10057 | } |
| 10058 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 10059 | Py_UNICODE* |
Victor Stinner | 4640860 | 2010-09-03 16:18:00 +0000 | [diff] [blame] | 10060 | PyUnicode_AsUnicodeCopy(PyObject *object) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 10061 | { |
| 10062 | PyUnicodeObject *unicode = (PyUnicodeObject *)object; |
| 10063 | Py_UNICODE *copy; |
| 10064 | Py_ssize_t size; |
| 10065 | |
| 10066 | /* Ensure we won't overflow the size. */ |
| 10067 | if (PyUnicode_GET_SIZE(unicode) > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 10068 | PyErr_NoMemory(); |
| 10069 | return NULL; |
| 10070 | } |
| 10071 | size = PyUnicode_GET_SIZE(unicode) + 1; /* copy the nul character */ |
| 10072 | size *= sizeof(Py_UNICODE); |
| 10073 | copy = PyMem_Malloc(size); |
| 10074 | if (copy == NULL) { |
| 10075 | PyErr_NoMemory(); |
| 10076 | return NULL; |
| 10077 | } |
| 10078 | memcpy(copy, PyUnicode_AS_UNICODE(unicode), size); |
| 10079 | return copy; |
| 10080 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10081 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10082 | #ifdef __cplusplus |
| 10083 | } |
| 10084 | #endif |