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': |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 1077 | case 'A': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1078 | { |
| 1079 | Py_UNICODE *ucopy; |
| 1080 | Py_ssize_t usize; |
| 1081 | Py_ssize_t upos; |
| 1082 | /* unused, since we already have the result */ |
| 1083 | (void) va_arg(vargs, PyObject *); |
| 1084 | ucopy = PyUnicode_AS_UNICODE(*callresult); |
| 1085 | usize = PyUnicode_GET_SIZE(*callresult); |
| 1086 | for (upos = 0; upos<usize;) |
| 1087 | *s++ = ucopy[upos++]; |
| 1088 | /* We're done with the unicode()/repr() => forget it */ |
| 1089 | Py_DECREF(*callresult); |
| 1090 | /* switch to next unicode()/repr() result */ |
| 1091 | ++callresult; |
| 1092 | break; |
| 1093 | } |
| 1094 | case 'p': |
| 1095 | sprintf(buffer, "%p", va_arg(vargs, void*)); |
| 1096 | /* %p is ill-defined: ensure leading 0x. */ |
| 1097 | if (buffer[1] == 'X') |
| 1098 | buffer[1] = 'x'; |
| 1099 | else if (buffer[1] != 'x') { |
| 1100 | memmove(buffer+2, buffer, strlen(buffer)+1); |
| 1101 | buffer[0] = '0'; |
| 1102 | buffer[1] = 'x'; |
| 1103 | } |
| 1104 | appendstring(buffer); |
| 1105 | break; |
| 1106 | case '%': |
| 1107 | *s++ = '%'; |
| 1108 | break; |
| 1109 | default: |
| 1110 | appendstring(p); |
| 1111 | goto end; |
| 1112 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 1113 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 1114 | else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1115 | *s++ = *f; |
| 1116 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1117 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1118 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1119 | if (callresults) |
| 1120 | PyObject_Free(callresults); |
| 1121 | if (abuffer) |
| 1122 | PyObject_Free(abuffer); |
| 1123 | PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string)); |
| 1124 | return string; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1125 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1126 | if (callresults) { |
| 1127 | PyObject **callresult2 = callresults; |
| 1128 | while (callresult2 < callresult) { |
| 1129 | Py_DECREF(*callresult2); |
| 1130 | ++callresult2; |
| 1131 | } |
| 1132 | PyObject_Free(callresults); |
| 1133 | } |
| 1134 | if (abuffer) |
| 1135 | PyObject_Free(abuffer); |
| 1136 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | #undef appendstring |
| 1140 | |
| 1141 | PyObject * |
| 1142 | PyUnicode_FromFormat(const char *format, ...) |
| 1143 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1144 | PyObject* ret; |
| 1145 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1146 | |
| 1147 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1148 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1149 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1150 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1151 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1152 | ret = PyUnicode_FromFormatV(format, vargs); |
| 1153 | va_end(vargs); |
| 1154 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1157 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 1158 | convert a Unicode object to a wide character string. |
| 1159 | |
| 1160 | - If w is NULL: return the number of wide characters (including the nul |
| 1161 | character) required to convert the unicode object. Ignore size argument. |
| 1162 | |
| 1163 | - Otherwise: return the number of wide characters (excluding the nul |
| 1164 | character) written into w. Write at most size wide characters (including |
| 1165 | the nul character). */ |
| 1166 | static Py_ssize_t |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1167 | unicode_aswidechar(PyUnicodeObject *unicode, |
| 1168 | wchar_t *w, |
| 1169 | Py_ssize_t size) |
| 1170 | { |
| 1171 | #if Py_UNICODE_SIZE == SIZEOF_WCHAR_T |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1172 | Py_ssize_t res; |
| 1173 | if (w != NULL) { |
| 1174 | res = PyUnicode_GET_SIZE(unicode); |
| 1175 | if (size > res) |
| 1176 | size = res + 1; |
| 1177 | else |
| 1178 | res = size; |
| 1179 | memcpy(w, unicode->str, size * sizeof(wchar_t)); |
| 1180 | return res; |
| 1181 | } |
| 1182 | else |
| 1183 | return PyUnicode_GET_SIZE(unicode) + 1; |
| 1184 | #elif Py_UNICODE_SIZE == 2 && SIZEOF_WCHAR_T == 4 |
| 1185 | register const Py_UNICODE *u; |
| 1186 | const Py_UNICODE *uend; |
| 1187 | const wchar_t *worig, *wend; |
| 1188 | Py_ssize_t nchar; |
| 1189 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1190 | u = PyUnicode_AS_UNICODE(unicode); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1191 | uend = u + PyUnicode_GET_SIZE(unicode); |
| 1192 | if (w != NULL) { |
| 1193 | worig = w; |
| 1194 | wend = w + size; |
| 1195 | while (u != uend && w != wend) { |
| 1196 | if (0xD800 <= u[0] && u[0] <= 0xDBFF |
| 1197 | && 0xDC00 <= u[1] && u[1] <= 0xDFFF) |
| 1198 | { |
| 1199 | *w = (((u[0] & 0x3FF) << 10) | (u[1] & 0x3FF)) + 0x10000; |
| 1200 | u += 2; |
| 1201 | } |
| 1202 | else { |
| 1203 | *w = *u; |
| 1204 | u++; |
| 1205 | } |
| 1206 | w++; |
| 1207 | } |
| 1208 | if (w != wend) |
| 1209 | *w = L'\0'; |
| 1210 | return w - worig; |
| 1211 | } |
| 1212 | else { |
| 1213 | nchar = 1; /* nul character at the end */ |
| 1214 | while (u != uend) { |
| 1215 | if (0xD800 <= u[0] && u[0] <= 0xDBFF |
| 1216 | && 0xDC00 <= u[1] && u[1] <= 0xDFFF) |
| 1217 | u += 2; |
| 1218 | else |
| 1219 | u++; |
| 1220 | nchar++; |
| 1221 | } |
| 1222 | } |
| 1223 | return nchar; |
| 1224 | #elif Py_UNICODE_SIZE == 4 && SIZEOF_WCHAR_T == 2 |
| 1225 | register Py_UNICODE *u, *uend, ordinal; |
| 1226 | register Py_ssize_t i; |
| 1227 | wchar_t *worig, *wend; |
| 1228 | Py_ssize_t nchar; |
| 1229 | |
| 1230 | u = PyUnicode_AS_UNICODE(unicode); |
| 1231 | uend = u + PyUnicode_GET_SIZE(u); |
| 1232 | if (w != NULL) { |
| 1233 | worig = w; |
| 1234 | wend = w + size; |
| 1235 | while (u != uend && w != wend) { |
| 1236 | ordinal = *u; |
| 1237 | if (ordinal > 0xffff) { |
| 1238 | ordinal -= 0x10000; |
| 1239 | *w++ = 0xD800 | (ordinal >> 10); |
| 1240 | *w++ = 0xDC00 | (ordinal & 0x3FF); |
| 1241 | } |
| 1242 | else |
| 1243 | *w++ = ordinal; |
| 1244 | u++; |
| 1245 | } |
| 1246 | if (w != wend) |
| 1247 | *w = 0; |
| 1248 | return w - worig; |
| 1249 | } |
| 1250 | else { |
| 1251 | nchar = 1; /* nul character */ |
| 1252 | while (u != uend) { |
| 1253 | if (*u > 0xffff) |
| 1254 | nchar += 2; |
| 1255 | else |
| 1256 | nchar++; |
| 1257 | u++; |
| 1258 | } |
| 1259 | return nchar; |
| 1260 | } |
| 1261 | #else |
| 1262 | # error "unsupported wchar_t and Py_UNICODE sizes, see issue #8670" |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1263 | #endif |
| 1264 | } |
| 1265 | |
| 1266 | Py_ssize_t |
| 1267 | PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
| 1268 | wchar_t *w, |
| 1269 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1270 | { |
| 1271 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1272 | PyErr_BadInternalCall(); |
| 1273 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1274 | } |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1275 | return unicode_aswidechar(unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1278 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 1279 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1280 | Py_ssize_t *size) |
| 1281 | { |
| 1282 | wchar_t* buffer; |
| 1283 | Py_ssize_t buflen; |
| 1284 | |
| 1285 | if (unicode == NULL) { |
| 1286 | PyErr_BadInternalCall(); |
| 1287 | return NULL; |
| 1288 | } |
| 1289 | |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 1290 | buflen = unicode_aswidechar((PyUnicodeObject *)unicode, NULL, 0); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1291 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1292 | PyErr_NoMemory(); |
| 1293 | return NULL; |
| 1294 | } |
| 1295 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1296 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 1297 | if (buffer == NULL) { |
| 1298 | PyErr_NoMemory(); |
| 1299 | return NULL; |
| 1300 | } |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 1301 | buflen = unicode_aswidechar((PyUnicodeObject *)unicode, buffer, buflen); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1302 | if (size != NULL) |
| 1303 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1304 | return buffer; |
| 1305 | } |
| 1306 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1307 | #endif |
| 1308 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1309 | PyObject *PyUnicode_FromOrdinal(int ordinal) |
| 1310 | { |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1311 | Py_UNICODE s[2]; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1312 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1313 | if (ordinal < 0 || ordinal > 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1314 | PyErr_SetString(PyExc_ValueError, |
| 1315 | "chr() arg not in range(0x110000)"); |
| 1316 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1317 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1318 | |
| 1319 | #ifndef Py_UNICODE_WIDE |
| 1320 | if (ordinal > 0xffff) { |
| 1321 | ordinal -= 0x10000; |
| 1322 | s[0] = 0xD800 | (ordinal >> 10); |
| 1323 | s[1] = 0xDC00 | (ordinal & 0x3FF); |
| 1324 | return PyUnicode_FromUnicode(s, 2); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1325 | } |
| 1326 | #endif |
| 1327 | |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 1328 | s[0] = (Py_UNICODE)ordinal; |
| 1329 | return PyUnicode_FromUnicode(s, 1); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1330 | } |
| 1331 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1332 | PyObject *PyUnicode_FromObject(register PyObject *obj) |
| 1333 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1334 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1335 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1336 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1337 | Py_INCREF(obj); |
| 1338 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1339 | } |
| 1340 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1341 | /* For a Unicode subtype that's not a Unicode object, |
| 1342 | return a true Unicode object with the same data. */ |
| 1343 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 1344 | PyUnicode_GET_SIZE(obj)); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1345 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1346 | PyErr_Format(PyExc_TypeError, |
| 1347 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1348 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1349 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1353 | const char *encoding, |
| 1354 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1355 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 1356 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1357 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1358 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1359 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1360 | PyErr_BadInternalCall(); |
| 1361 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1362 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1363 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 1364 | /* Decoding bytes objects is the most common case and should be fast */ |
| 1365 | if (PyBytes_Check(obj)) { |
| 1366 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 1367 | Py_INCREF(unicode_empty); |
| 1368 | v = (PyObject *) unicode_empty; |
| 1369 | } |
| 1370 | else { |
| 1371 | v = PyUnicode_Decode( |
| 1372 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 1373 | encoding, errors); |
| 1374 | } |
| 1375 | return v; |
| 1376 | } |
| 1377 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1378 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1379 | PyErr_SetString(PyExc_TypeError, |
| 1380 | "decoding str is not supported"); |
| 1381 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1382 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1383 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 1384 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 1385 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 1386 | PyErr_Format(PyExc_TypeError, |
| 1387 | "coercing to str: need bytes, bytearray " |
| 1388 | "or buffer-like object, %.80s found", |
| 1389 | Py_TYPE(obj)->tp_name); |
| 1390 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1391 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1392 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 1393 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1394 | Py_INCREF(unicode_empty); |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 1395 | v = (PyObject *) unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1396 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1397 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 1398 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 1399 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 1400 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1401 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1402 | } |
| 1403 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 1404 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1405 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 1406 | 1 on success. */ |
| 1407 | static int |
| 1408 | normalize_encoding(const char *encoding, |
| 1409 | char *lower, |
| 1410 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1411 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1412 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 1413 | char *l; |
| 1414 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1415 | |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1416 | e = encoding; |
| 1417 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 1418 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1419 | while (*e) { |
| 1420 | if (l == l_end) |
| 1421 | return 0; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1422 | if (ISUPPER(*e)) { |
| 1423 | *l++ = TOLOWER(*e++); |
| 1424 | } |
| 1425 | else if (*e == '_') { |
| 1426 | *l++ = '-'; |
| 1427 | e++; |
| 1428 | } |
| 1429 | else { |
| 1430 | *l++ = *e++; |
| 1431 | } |
| 1432 | } |
| 1433 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1434 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | PyObject *PyUnicode_Decode(const char *s, |
| 1438 | Py_ssize_t size, |
| 1439 | const char *encoding, |
| 1440 | const char *errors) |
| 1441 | { |
| 1442 | PyObject *buffer = NULL, *unicode; |
| 1443 | Py_buffer info; |
| 1444 | char lower[11]; /* Enough for any encoding shortcut */ |
| 1445 | |
| 1446 | if (encoding == NULL) |
| 1447 | encoding = PyUnicode_GetDefaultEncoding(); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1448 | |
| 1449 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1450 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
| 1451 | if (strcmp(lower, "utf-8") == 0) |
| 1452 | return PyUnicode_DecodeUTF8(s, size, errors); |
| 1453 | else if ((strcmp(lower, "latin-1") == 0) || |
| 1454 | (strcmp(lower, "iso-8859-1") == 0)) |
| 1455 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1456 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1457 | else if (strcmp(lower, "mbcs") == 0) |
| 1458 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1459 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1460 | else if (strcmp(lower, "ascii") == 0) |
| 1461 | return PyUnicode_DecodeASCII(s, size, errors); |
| 1462 | else if (strcmp(lower, "utf-16") == 0) |
| 1463 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 1464 | else if (strcmp(lower, "utf-32") == 0) |
| 1465 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 1466 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1467 | |
| 1468 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1469 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 1470 | 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] | 1471 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 1472 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1473 | if (buffer == NULL) |
| 1474 | goto onError; |
| 1475 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 1476 | if (unicode == NULL) |
| 1477 | goto onError; |
| 1478 | if (!PyUnicode_Check(unicode)) { |
| 1479 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1480 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1481 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1482 | Py_DECREF(unicode); |
| 1483 | goto onError; |
| 1484 | } |
| 1485 | Py_DECREF(buffer); |
| 1486 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1487 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1488 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1489 | Py_XDECREF(buffer); |
| 1490 | return NULL; |
| 1491 | } |
| 1492 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1493 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 1494 | const char *encoding, |
| 1495 | const char *errors) |
| 1496 | { |
| 1497 | PyObject *v; |
| 1498 | |
| 1499 | if (!PyUnicode_Check(unicode)) { |
| 1500 | PyErr_BadArgument(); |
| 1501 | goto onError; |
| 1502 | } |
| 1503 | |
| 1504 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1505 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1506 | |
| 1507 | /* Decode via the codec registry */ |
| 1508 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1509 | if (v == NULL) |
| 1510 | goto onError; |
| 1511 | return v; |
| 1512 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1513 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1514 | return NULL; |
| 1515 | } |
| 1516 | |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1517 | PyObject *PyUnicode_AsDecodedUnicode(PyObject *unicode, |
| 1518 | const char *encoding, |
| 1519 | const char *errors) |
| 1520 | { |
| 1521 | PyObject *v; |
| 1522 | |
| 1523 | if (!PyUnicode_Check(unicode)) { |
| 1524 | PyErr_BadArgument(); |
| 1525 | goto onError; |
| 1526 | } |
| 1527 | |
| 1528 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1529 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1530 | |
| 1531 | /* Decode via the codec registry */ |
| 1532 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1533 | if (v == NULL) |
| 1534 | goto onError; |
| 1535 | if (!PyUnicode_Check(v)) { |
| 1536 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1537 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1538 | Py_TYPE(v)->tp_name); |
| 1539 | Py_DECREF(v); |
| 1540 | goto onError; |
| 1541 | } |
| 1542 | return v; |
| 1543 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1544 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1545 | return NULL; |
| 1546 | } |
| 1547 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1548 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1549 | Py_ssize_t size, |
| 1550 | const char *encoding, |
| 1551 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1552 | { |
| 1553 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1554 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1555 | unicode = PyUnicode_FromUnicode(s, size); |
| 1556 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1557 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1558 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 1559 | Py_DECREF(unicode); |
| 1560 | return v; |
| 1561 | } |
| 1562 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1563 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 1564 | const char *encoding, |
| 1565 | const char *errors) |
| 1566 | { |
| 1567 | PyObject *v; |
| 1568 | |
| 1569 | if (!PyUnicode_Check(unicode)) { |
| 1570 | PyErr_BadArgument(); |
| 1571 | goto onError; |
| 1572 | } |
| 1573 | |
| 1574 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1575 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1576 | |
| 1577 | /* Encode via the codec registry */ |
| 1578 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1579 | if (v == NULL) |
| 1580 | goto onError; |
| 1581 | return v; |
| 1582 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1583 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1584 | return NULL; |
| 1585 | } |
| 1586 | |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 1587 | PyObject *PyUnicode_EncodeFSDefault(PyObject *unicode) |
| 1588 | { |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 1589 | if (Py_FileSystemDefaultEncoding) { |
| 1590 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 1591 | if (strcmp(Py_FileSystemDefaultEncoding, "mbcs") == 0) |
| 1592 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 1593 | PyUnicode_GET_SIZE(unicode), |
| 1594 | NULL); |
| 1595 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 1596 | return PyUnicode_AsEncodedString(unicode, |
| 1597 | Py_FileSystemDefaultEncoding, |
| 1598 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 1599 | } |
| 1600 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 1601 | /* locale encoding with surrogateescape */ |
| 1602 | wchar_t *wchar; |
| 1603 | char *bytes; |
| 1604 | PyObject *bytes_obj; |
| 1605 | |
| 1606 | wchar = PyUnicode_AsWideCharString(unicode, NULL); |
| 1607 | if (wchar == NULL) |
| 1608 | return NULL; |
| 1609 | bytes = _Py_wchar2char(wchar); |
| 1610 | PyMem_Free(wchar); |
| 1611 | if (bytes == NULL) |
| 1612 | return NULL; |
| 1613 | |
| 1614 | bytes_obj = PyBytes_FromString(bytes); |
| 1615 | PyMem_Free(bytes); |
| 1616 | return bytes_obj; |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 1617 | } |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 1618 | } |
| 1619 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1620 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 1621 | const char *encoding, |
| 1622 | const char *errors) |
| 1623 | { |
| 1624 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 1625 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1626 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1627 | if (!PyUnicode_Check(unicode)) { |
| 1628 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1629 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1630 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1631 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1632 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1633 | encoding = PyUnicode_GetDefaultEncoding(); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1634 | |
| 1635 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1636 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
| 1637 | if (strcmp(lower, "utf-8") == 0) |
| 1638 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 1639 | PyUnicode_GET_SIZE(unicode), |
| 1640 | errors); |
| 1641 | else if ((strcmp(lower, "latin-1") == 0) || |
| 1642 | (strcmp(lower, "iso-8859-1") == 0)) |
| 1643 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 1644 | PyUnicode_GET_SIZE(unicode), |
| 1645 | errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1646 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1647 | else if (strcmp(lower, "mbcs") == 0) |
| 1648 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 1649 | PyUnicode_GET_SIZE(unicode), |
| 1650 | errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1651 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1652 | else if (strcmp(lower, "ascii") == 0) |
| 1653 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 1654 | PyUnicode_GET_SIZE(unicode), |
| 1655 | errors); |
| 1656 | } |
Victor Stinner | 59e62db | 2010-05-15 13:14:32 +0000 | [diff] [blame] | 1657 | /* During bootstrap, we may need to find the encodings |
| 1658 | package, to load the file system encoding, and require the |
| 1659 | file system encoding in order to load the encodings |
| 1660 | package. |
Christian Heimes | 6a27efa | 2008-10-30 21:48:26 +0000 | [diff] [blame] | 1661 | |
Victor Stinner | 59e62db | 2010-05-15 13:14:32 +0000 | [diff] [blame] | 1662 | Break out of this dependency by assuming that the path to |
| 1663 | the encodings module is ASCII-only. XXX could try wcstombs |
| 1664 | instead, if the file system encoding is the locale's |
| 1665 | encoding. */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1666 | if (Py_FileSystemDefaultEncoding && |
Victor Stinner | 59e62db | 2010-05-15 13:14:32 +0000 | [diff] [blame] | 1667 | strcmp(encoding, Py_FileSystemDefaultEncoding) == 0 && |
| 1668 | !PyThreadState_GET()->interp->codecs_initialized) |
| 1669 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 1670 | PyUnicode_GET_SIZE(unicode), |
| 1671 | errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1672 | |
| 1673 | /* Encode via the codec registry */ |
| 1674 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1675 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1676 | return NULL; |
| 1677 | |
| 1678 | /* The normal path */ |
| 1679 | if (PyBytes_Check(v)) |
| 1680 | return v; |
| 1681 | |
| 1682 | /* 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] | 1683 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 1684 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1685 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 1686 | |
| 1687 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 1688 | "encoder %s returned bytearray instead of bytes", |
| 1689 | encoding); |
| 1690 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1691 | Py_DECREF(v); |
| 1692 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1693 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1694 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1695 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 1696 | Py_DECREF(v); |
| 1697 | return b; |
| 1698 | } |
| 1699 | |
| 1700 | PyErr_Format(PyExc_TypeError, |
| 1701 | "encoder did not return a bytes object (type=%.400s)", |
| 1702 | Py_TYPE(v)->tp_name); |
| 1703 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1704 | return NULL; |
| 1705 | } |
| 1706 | |
| 1707 | PyObject *PyUnicode_AsEncodedUnicode(PyObject *unicode, |
| 1708 | const char *encoding, |
| 1709 | const char *errors) |
| 1710 | { |
| 1711 | PyObject *v; |
| 1712 | |
| 1713 | if (!PyUnicode_Check(unicode)) { |
| 1714 | PyErr_BadArgument(); |
| 1715 | goto onError; |
| 1716 | } |
| 1717 | |
| 1718 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1719 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1720 | |
| 1721 | /* Encode via the codec registry */ |
| 1722 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1723 | if (v == NULL) |
| 1724 | goto onError; |
| 1725 | if (!PyUnicode_Check(v)) { |
| 1726 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1727 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1728 | Py_TYPE(v)->tp_name); |
| 1729 | Py_DECREF(v); |
| 1730 | goto onError; |
| 1731 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1732 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1733 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1734 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1735 | return NULL; |
| 1736 | } |
| 1737 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1738 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1739 | const char *errors) |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1740 | { |
| 1741 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1742 | if (v) |
| 1743 | return v; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1744 | if (errors != NULL) |
| 1745 | Py_FatalError("non-NULL encoding in _PyUnicode_AsDefaultEncodedString"); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1746 | v = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Guido van Rossum | 0661009 | 2007-08-16 21:02:22 +0000 | [diff] [blame] | 1747 | PyUnicode_GET_SIZE(unicode), |
| 1748 | NULL); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1749 | if (!v) |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1750 | return NULL; |
Guido van Rossum | e7a0d39 | 2007-07-12 07:53:00 +0000 | [diff] [blame] | 1751 | ((PyUnicodeObject *)unicode)->defenc = v; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1752 | return v; |
| 1753 | } |
| 1754 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1755 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1756 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1757 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1758 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 1759 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1760 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1761 | PyObject* |
| 1762 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 1763 | { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1764 | /* During the early bootstrapping process, Py_FileSystemDefaultEncoding |
| 1765 | can be undefined. If it is case, decode using UTF-8. The following assumes |
| 1766 | that Py_FileSystemDefaultEncoding is set to a built-in encoding during the |
| 1767 | bootstrapping process where the codecs aren't ready yet. |
| 1768 | */ |
| 1769 | if (Py_FileSystemDefaultEncoding) { |
| 1770 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1771 | if (strcmp(Py_FileSystemDefaultEncoding, "mbcs") == 0) { |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 1772 | return PyUnicode_DecodeMBCS(s, size, NULL); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1773 | } |
| 1774 | #elif defined(__APPLE__) |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1775 | if (strcmp(Py_FileSystemDefaultEncoding, "utf-8") == 0) { |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 1776 | return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1777 | } |
| 1778 | #endif |
| 1779 | return PyUnicode_Decode(s, size, |
| 1780 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 1781 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1782 | } |
| 1783 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 1784 | /* locale encoding with surrogateescape */ |
| 1785 | wchar_t *wchar; |
| 1786 | PyObject *unicode; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 1787 | size_t len; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 1788 | |
| 1789 | if (s[size] != '\0' || size != strlen(s)) { |
| 1790 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 1791 | return NULL; |
| 1792 | } |
| 1793 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 1794 | wchar = _Py_char2wchar(s, &len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 1795 | if (wchar == NULL) |
| 1796 | return NULL; |
| 1797 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 1798 | unicode = PyUnicode_FromWideChar(wchar, len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 1799 | PyMem_Free(wchar); |
| 1800 | return unicode; |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1801 | } |
| 1802 | } |
| 1803 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1804 | |
| 1805 | int |
| 1806 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 1807 | { |
| 1808 | PyObject *output = NULL; |
| 1809 | Py_ssize_t size; |
| 1810 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 1811 | if (arg == NULL) { |
| 1812 | Py_DECREF(*(PyObject**)addr); |
| 1813 | return 1; |
| 1814 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 1815 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1816 | output = arg; |
| 1817 | Py_INCREF(output); |
| 1818 | } |
| 1819 | else { |
| 1820 | arg = PyUnicode_FromObject(arg); |
| 1821 | if (!arg) |
| 1822 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 1823 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1824 | Py_DECREF(arg); |
| 1825 | if (!output) |
| 1826 | return 0; |
| 1827 | if (!PyBytes_Check(output)) { |
| 1828 | Py_DECREF(output); |
| 1829 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 1830 | return 0; |
| 1831 | } |
| 1832 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 1833 | size = PyBytes_GET_SIZE(output); |
| 1834 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1835 | if (size != strlen(data)) { |
| 1836 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 1837 | Py_DECREF(output); |
| 1838 | return 0; |
| 1839 | } |
| 1840 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 1841 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1842 | } |
| 1843 | |
| 1844 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 1845 | int |
| 1846 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 1847 | { |
| 1848 | PyObject *output = NULL; |
| 1849 | Py_ssize_t size; |
| 1850 | void *data; |
| 1851 | if (arg == NULL) { |
| 1852 | Py_DECREF(*(PyObject**)addr); |
| 1853 | return 1; |
| 1854 | } |
| 1855 | if (PyUnicode_Check(arg)) { |
| 1856 | output = arg; |
| 1857 | Py_INCREF(output); |
| 1858 | } |
| 1859 | else { |
| 1860 | arg = PyBytes_FromObject(arg); |
| 1861 | if (!arg) |
| 1862 | return 0; |
| 1863 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 1864 | PyBytes_GET_SIZE(arg)); |
| 1865 | Py_DECREF(arg); |
| 1866 | if (!output) |
| 1867 | return 0; |
| 1868 | if (!PyUnicode_Check(output)) { |
| 1869 | Py_DECREF(output); |
| 1870 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 1871 | return 0; |
| 1872 | } |
| 1873 | } |
| 1874 | size = PyUnicode_GET_SIZE(output); |
| 1875 | data = PyUnicode_AS_UNICODE(output); |
| 1876 | if (size != Py_UNICODE_strlen(data)) { |
| 1877 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 1878 | Py_DECREF(output); |
| 1879 | return 0; |
| 1880 | } |
| 1881 | *(PyObject**)addr = output; |
| 1882 | return Py_CLEANUP_SUPPORTED; |
| 1883 | } |
| 1884 | |
| 1885 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1886 | char* |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1887 | _PyUnicode_AsStringAndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1888 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1889 | PyObject *bytes; |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 1890 | if (!PyUnicode_Check(unicode)) { |
| 1891 | PyErr_BadArgument(); |
| 1892 | return NULL; |
| 1893 | } |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1894 | bytes = _PyUnicode_AsDefaultEncodedString(unicode, NULL); |
| 1895 | if (bytes == NULL) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1896 | return NULL; |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1897 | if (psize != NULL) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1898 | *psize = PyBytes_GET_SIZE(bytes); |
| 1899 | return PyBytes_AS_STRING(bytes); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1900 | } |
| 1901 | |
| 1902 | char* |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1903 | _PyUnicode_AsString(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1904 | { |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1905 | return _PyUnicode_AsStringAndSize(unicode, NULL); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1906 | } |
| 1907 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1908 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 1909 | { |
| 1910 | if (!PyUnicode_Check(unicode)) { |
| 1911 | PyErr_BadArgument(); |
| 1912 | goto onError; |
| 1913 | } |
| 1914 | return PyUnicode_AS_UNICODE(unicode); |
| 1915 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1916 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1917 | return NULL; |
| 1918 | } |
| 1919 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1920 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1921 | { |
| 1922 | if (!PyUnicode_Check(unicode)) { |
| 1923 | PyErr_BadArgument(); |
| 1924 | goto onError; |
| 1925 | } |
| 1926 | return PyUnicode_GET_SIZE(unicode); |
| 1927 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1928 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1929 | return -1; |
| 1930 | } |
| 1931 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 1932 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1933 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 1934 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1935 | } |
| 1936 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 1937 | /* create or adjust a UnicodeDecodeError */ |
| 1938 | static void |
| 1939 | make_decode_exception(PyObject **exceptionObject, |
| 1940 | const char *encoding, |
| 1941 | const char *input, Py_ssize_t length, |
| 1942 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 1943 | const char *reason) |
| 1944 | { |
| 1945 | if (*exceptionObject == NULL) { |
| 1946 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 1947 | encoding, input, length, startpos, endpos, reason); |
| 1948 | } |
| 1949 | else { |
| 1950 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 1951 | goto onError; |
| 1952 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 1953 | goto onError; |
| 1954 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 1955 | goto onError; |
| 1956 | } |
| 1957 | return; |
| 1958 | |
| 1959 | onError: |
| 1960 | Py_DECREF(*exceptionObject); |
| 1961 | *exceptionObject = NULL; |
| 1962 | } |
| 1963 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1964 | /* error handling callback helper: |
| 1965 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 1966 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1967 | and adjust various state variables. |
| 1968 | return 0 on success, -1 on error |
| 1969 | */ |
| 1970 | |
| 1971 | static |
| 1972 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1973 | const char *encoding, const char *reason, |
| 1974 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 1975 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 1976 | PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1977 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1978 | 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] | 1979 | |
| 1980 | PyObject *restuple = NULL; |
| 1981 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1982 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1983 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1984 | Py_ssize_t requiredsize; |
| 1985 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1986 | Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1987 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1988 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1989 | int res = -1; |
| 1990 | |
| 1991 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1992 | *errorHandler = PyCodec_LookupError(errors); |
| 1993 | if (*errorHandler == NULL) |
| 1994 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1995 | } |
| 1996 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 1997 | make_decode_exception(exceptionObject, |
| 1998 | encoding, |
| 1999 | *input, *inend - *input, |
| 2000 | *startinpos, *endinpos, |
| 2001 | reason); |
| 2002 | if (*exceptionObject == NULL) |
| 2003 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2004 | |
| 2005 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 2006 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2007 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2008 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 2009 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2010 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2011 | } |
| 2012 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2013 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2014 | |
| 2015 | /* Copy back the bytes variables, which might have been modified by the |
| 2016 | callback */ |
| 2017 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 2018 | if (!inputobj) |
| 2019 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2020 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2021 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2022 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2023 | *input = PyBytes_AS_STRING(inputobj); |
| 2024 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2025 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 2026 | /* we can DECREF safely, as the exception has another reference, |
| 2027 | so the object won't go away. */ |
| 2028 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2029 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2030 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2031 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 2032 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2033 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 2034 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 2035 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2036 | |
| 2037 | /* need more space? (at least enough for what we |
| 2038 | have+the replacement+the rest of the string (starting |
| 2039 | at the new input position), so we won't have to check space |
| 2040 | when there are no errors in the rest of the string) */ |
| 2041 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 2042 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 2043 | requiredsize = *outpos + repsize + insize-newpos; |
| 2044 | if (requiredsize > outsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2045 | if (requiredsize<2*outsize) |
| 2046 | requiredsize = 2*outsize; |
| 2047 | if (_PyUnicode_Resize(output, requiredsize) < 0) |
| 2048 | goto onError; |
| 2049 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2050 | } |
| 2051 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2052 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2053 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 2054 | *outptr += repsize; |
| 2055 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2056 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2057 | /* we made it! */ |
| 2058 | res = 0; |
| 2059 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2060 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2061 | Py_XDECREF(restuple); |
| 2062 | return res; |
| 2063 | } |
| 2064 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2065 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 2066 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2067 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 2068 | |
| 2069 | /* Three simple macros defining base-64. */ |
| 2070 | |
| 2071 | /* Is c a base-64 character? */ |
| 2072 | |
| 2073 | #define IS_BASE64(c) \ |
| 2074 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 2075 | ((c) >= 'a' && (c) <= 'z') || \ |
| 2076 | ((c) >= '0' && (c) <= '9') || \ |
| 2077 | (c) == '+' || (c) == '/') |
| 2078 | |
| 2079 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 2080 | |
| 2081 | #define FROM_BASE64(c) \ |
| 2082 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 2083 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 2084 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 2085 | (c) == '+' ? 62 : 63) |
| 2086 | |
| 2087 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 2088 | |
| 2089 | #define TO_BASE64(n) \ |
| 2090 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 2091 | |
| 2092 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 2093 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 2094 | * byte not decoding to itself is the + which begins a base64 |
| 2095 | * string. */ |
| 2096 | |
| 2097 | #define DECODE_DIRECT(c) \ |
| 2098 | ((c) <= 127 && (c) != '+') |
| 2099 | |
| 2100 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 2101 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 2102 | * the above). See RFC2152. This array identifies these different |
| 2103 | * sets: |
| 2104 | * 0 : "Set D" |
| 2105 | * alphanumeric and '(),-./:? |
| 2106 | * 1 : "Set O" |
| 2107 | * !"#$%&*;<=>@[]^_`{|} |
| 2108 | * 2 : "whitespace" |
| 2109 | * ht nl cr sp |
| 2110 | * 3 : special (must be base64 encoded) |
| 2111 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 2112 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2113 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2114 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2115 | char utf7_category[128] = { |
| 2116 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 2117 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 2118 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 2119 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 2120 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 2121 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 2122 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 2123 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 2124 | /* @ A B C D E F G H I J K L M N O */ |
| 2125 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2126 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 2127 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 2128 | /* ` a b c d e f g h i j k l m n o */ |
| 2129 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2130 | /* p q r s t u v w x y z { | } ~ del */ |
| 2131 | 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] | 2132 | }; |
| 2133 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2134 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 2135 | * answer depends on whether we are encoding set O as itself, and also |
| 2136 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 2137 | * clear that the answers to these questions vary between |
| 2138 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 2139 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2140 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 2141 | ((c) < 128 && (c) > 0 && \ |
| 2142 | ((utf7_category[(c)] == 0) || \ |
| 2143 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 2144 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2145 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2146 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2147 | Py_ssize_t size, |
| 2148 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2149 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2150 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 2151 | } |
| 2152 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2153 | /* The decoder. The only state we preserve is our read position, |
| 2154 | * i.e. how many characters we have consumed. So if we end in the |
| 2155 | * middle of a shift sequence we have to back off the read position |
| 2156 | * and the output to the beginning of the sequence, otherwise we lose |
| 2157 | * all the shift state (seen bits, number of bits seen, high |
| 2158 | * surrogate). */ |
| 2159 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2160 | PyObject *PyUnicode_DecodeUTF7Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2161 | Py_ssize_t size, |
| 2162 | const char *errors, |
| 2163 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2164 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2165 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2166 | Py_ssize_t startinpos; |
| 2167 | Py_ssize_t endinpos; |
| 2168 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2169 | const char *e; |
| 2170 | PyUnicodeObject *unicode; |
| 2171 | Py_UNICODE *p; |
| 2172 | const char *errmsg = ""; |
| 2173 | int inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2174 | Py_UNICODE *shiftOutStart; |
| 2175 | unsigned int base64bits = 0; |
| 2176 | unsigned long base64buffer = 0; |
| 2177 | Py_UNICODE surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2178 | PyObject *errorHandler = NULL; |
| 2179 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2180 | |
| 2181 | unicode = _PyUnicode_New(size); |
| 2182 | if (!unicode) |
| 2183 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2184 | if (size == 0) { |
| 2185 | if (consumed) |
| 2186 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2187 | return (PyObject *)unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2188 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2189 | |
| 2190 | p = unicode->str; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2191 | shiftOutStart = p; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2192 | e = s + size; |
| 2193 | |
| 2194 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2195 | Py_UNICODE ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2196 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 2197 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2198 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2199 | if (inShift) { /* in a base-64 section */ |
| 2200 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 2201 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 2202 | base64bits += 6; |
| 2203 | s++; |
| 2204 | if (base64bits >= 16) { |
| 2205 | /* we have enough bits for a UTF-16 value */ |
| 2206 | Py_UNICODE outCh = (Py_UNICODE) |
| 2207 | (base64buffer >> (base64bits-16)); |
| 2208 | base64bits -= 16; |
| 2209 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 2210 | if (surrogate) { |
| 2211 | /* expecting a second surrogate */ |
| 2212 | if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 2213 | #ifdef Py_UNICODE_WIDE |
| 2214 | *p++ = (((surrogate & 0x3FF)<<10) |
| 2215 | | (outCh & 0x3FF)) + 0x10000; |
| 2216 | #else |
| 2217 | *p++ = surrogate; |
| 2218 | *p++ = outCh; |
| 2219 | #endif |
| 2220 | surrogate = 0; |
| 2221 | } |
| 2222 | else { |
| 2223 | surrogate = 0; |
| 2224 | errmsg = "second surrogate missing"; |
| 2225 | goto utf7Error; |
| 2226 | } |
| 2227 | } |
| 2228 | else if (outCh >= 0xD800 && outCh <= 0xDBFF) { |
| 2229 | /* first surrogate */ |
| 2230 | surrogate = outCh; |
| 2231 | } |
| 2232 | else if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 2233 | errmsg = "unexpected second surrogate"; |
| 2234 | goto utf7Error; |
| 2235 | } |
| 2236 | else { |
| 2237 | *p++ = outCh; |
| 2238 | } |
| 2239 | } |
| 2240 | } |
| 2241 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2242 | inShift = 0; |
| 2243 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2244 | if (surrogate) { |
| 2245 | errmsg = "second surrogate missing at end of shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2246 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2247 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2248 | if (base64bits > 0) { /* left-over bits */ |
| 2249 | if (base64bits >= 6) { |
| 2250 | /* We've seen at least one base-64 character */ |
| 2251 | errmsg = "partial character in shift sequence"; |
| 2252 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2253 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2254 | else { |
| 2255 | /* Some bits remain; they should be zero */ |
| 2256 | if (base64buffer != 0) { |
| 2257 | errmsg = "non-zero padding bits in shift sequence"; |
| 2258 | goto utf7Error; |
| 2259 | } |
| 2260 | } |
| 2261 | } |
| 2262 | if (ch != '-') { |
| 2263 | /* '-' is absorbed; other terminating |
| 2264 | characters are preserved */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2265 | *p++ = ch; |
| 2266 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2267 | } |
| 2268 | } |
| 2269 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2270 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2271 | s++; /* consume '+' */ |
| 2272 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2273 | s++; |
| 2274 | *p++ = '+'; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2275 | } |
| 2276 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2277 | inShift = 1; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2278 | shiftOutStart = p; |
| 2279 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2280 | } |
| 2281 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2282 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2283 | *p++ = ch; |
| 2284 | s++; |
| 2285 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2286 | else { |
| 2287 | startinpos = s-starts; |
| 2288 | s++; |
| 2289 | errmsg = "unexpected special character"; |
| 2290 | goto utf7Error; |
| 2291 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2292 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2293 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2294 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2295 | endinpos = s-starts; |
| 2296 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2297 | errors, &errorHandler, |
| 2298 | "utf7", errmsg, |
| 2299 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2300 | &unicode, &outpos, &p)) |
| 2301 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2302 | } |
| 2303 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2304 | /* end of string */ |
| 2305 | |
| 2306 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 2307 | /* if we're in an inconsistent state, that's an error */ |
| 2308 | if (surrogate || |
| 2309 | (base64bits >= 6) || |
| 2310 | (base64bits > 0 && base64buffer != 0)) { |
| 2311 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2312 | endinpos = size; |
| 2313 | if (unicode_decode_call_errorhandler( |
| 2314 | errors, &errorHandler, |
| 2315 | "utf7", "unterminated shift sequence", |
| 2316 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2317 | &unicode, &outpos, &p)) |
| 2318 | goto onError; |
| 2319 | if (s < e) |
| 2320 | goto restart; |
| 2321 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2322 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2323 | |
| 2324 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2325 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2326 | if (inShift) { |
| 2327 | p = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2328 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2329 | } |
| 2330 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2331 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2332 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2333 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2334 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2335 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2336 | goto onError; |
| 2337 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2338 | Py_XDECREF(errorHandler); |
| 2339 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2340 | return (PyObject *)unicode; |
| 2341 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2342 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2343 | Py_XDECREF(errorHandler); |
| 2344 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2345 | Py_DECREF(unicode); |
| 2346 | return NULL; |
| 2347 | } |
| 2348 | |
| 2349 | |
| 2350 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2351 | Py_ssize_t size, |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2352 | int base64SetO, |
| 2353 | int base64WhiteSpace, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2354 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2355 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2356 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2357 | /* It might be possible to tighten this worst case */ |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 2358 | Py_ssize_t allocated = 8 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2359 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2360 | Py_ssize_t i = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2361 | unsigned int base64bits = 0; |
| 2362 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2363 | char * out; |
| 2364 | char * start; |
| 2365 | |
| 2366 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2367 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2368 | |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 2369 | if (allocated / 8 != size) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2370 | return PyErr_NoMemory(); |
| 2371 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2372 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2373 | if (v == NULL) |
| 2374 | return NULL; |
| 2375 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2376 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2377 | for (;i < size; ++i) { |
| 2378 | Py_UNICODE ch = s[i]; |
| 2379 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2380 | if (inShift) { |
| 2381 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 2382 | /* shifting out */ |
| 2383 | if (base64bits) { /* output remaining bits */ |
| 2384 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 2385 | base64buffer = 0; |
| 2386 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2387 | } |
| 2388 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2389 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 2390 | so no '-' is required, except if the character is itself a '-' */ |
| 2391 | if (IS_BASE64(ch) || ch == '-') { |
| 2392 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2393 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2394 | *out++ = (char) ch; |
| 2395 | } |
| 2396 | else { |
| 2397 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2398 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2399 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2400 | else { /* not in a shift sequence */ |
| 2401 | if (ch == '+') { |
| 2402 | *out++ = '+'; |
| 2403 | *out++ = '-'; |
| 2404 | } |
| 2405 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 2406 | *out++ = (char) ch; |
| 2407 | } |
| 2408 | else { |
| 2409 | *out++ = '+'; |
| 2410 | inShift = 1; |
| 2411 | goto encode_char; |
| 2412 | } |
| 2413 | } |
| 2414 | continue; |
| 2415 | encode_char: |
| 2416 | #ifdef Py_UNICODE_WIDE |
| 2417 | if (ch >= 0x10000) { |
| 2418 | /* code first surrogate */ |
| 2419 | base64bits += 16; |
| 2420 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 2421 | while (base64bits >= 6) { |
| 2422 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 2423 | base64bits -= 6; |
| 2424 | } |
| 2425 | /* prepare second surrogate */ |
| 2426 | ch = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2427 | } |
| 2428 | #endif |
| 2429 | base64bits += 16; |
| 2430 | base64buffer = (base64buffer << 16) | ch; |
| 2431 | while (base64bits >= 6) { |
| 2432 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 2433 | base64bits -= 6; |
| 2434 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 2435 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2436 | if (base64bits) |
| 2437 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 2438 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2439 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2440 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 2441 | return NULL; |
| 2442 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2443 | } |
| 2444 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2445 | #undef IS_BASE64 |
| 2446 | #undef FROM_BASE64 |
| 2447 | #undef TO_BASE64 |
| 2448 | #undef DECODE_DIRECT |
| 2449 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2450 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2451 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 2452 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2453 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2454 | char utf8_code_length[256] = { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2455 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 2456 | illegal prefix. See RFC 3629 for details */ |
| 2457 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 2458 | 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] | 2459 | 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] | 2460 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2461 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2462 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2463 | 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] | 2464 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 2465 | 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] | 2466 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2467 | 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] | 2468 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 2469 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 2470 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 2471 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 2472 | 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] | 2473 | }; |
| 2474 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2475 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2476 | Py_ssize_t size, |
| 2477 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2478 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2479 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 2480 | } |
| 2481 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2482 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 2483 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 2484 | |
| 2485 | /* Mask to quickly check whether a C 'long' contains a |
| 2486 | non-ASCII, UTF8-encoded char. */ |
| 2487 | #if (SIZEOF_LONG == 8) |
| 2488 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 2489 | #elif (SIZEOF_LONG == 4) |
| 2490 | # define ASCII_CHAR_MASK 0x80808080L |
| 2491 | #else |
| 2492 | # error C 'long' size should be either 4 or 8! |
| 2493 | #endif |
| 2494 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2495 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2496 | Py_ssize_t size, |
| 2497 | const char *errors, |
| 2498 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2499 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2500 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2501 | int n; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2502 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2503 | Py_ssize_t startinpos; |
| 2504 | Py_ssize_t endinpos; |
| 2505 | Py_ssize_t outpos; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2506 | const char *e, *aligned_end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2507 | PyUnicodeObject *unicode; |
| 2508 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2509 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2510 | PyObject *errorHandler = NULL; |
| 2511 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2512 | |
| 2513 | /* Note: size will always be longer than the resulting Unicode |
| 2514 | character count */ |
| 2515 | unicode = _PyUnicode_New(size); |
| 2516 | if (!unicode) |
| 2517 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2518 | if (size == 0) { |
| 2519 | if (consumed) |
| 2520 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2521 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2522 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2523 | |
| 2524 | /* Unpack UTF-8 encoded data */ |
| 2525 | p = unicode->str; |
| 2526 | e = s + size; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2527 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2528 | |
| 2529 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2530 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2531 | |
| 2532 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2533 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 2534 | input will consist of an overwhelming majority of ASCII |
| 2535 | characters, we try to optimize for this case by checking |
| 2536 | as many characters as a C 'long' can contain. |
| 2537 | First, check if we can do an aligned read, as most CPUs have |
| 2538 | a penalty for unaligned reads. |
| 2539 | */ |
| 2540 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 2541 | /* Help register allocation */ |
| 2542 | register const char *_s = s; |
| 2543 | register Py_UNICODE *_p = p; |
| 2544 | while (_s < aligned_end) { |
| 2545 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 2546 | and do a fast unrolled copy if it only contains ASCII |
| 2547 | characters. */ |
| 2548 | unsigned long data = *(unsigned long *) _s; |
| 2549 | if (data & ASCII_CHAR_MASK) |
| 2550 | break; |
| 2551 | _p[0] = (unsigned char) _s[0]; |
| 2552 | _p[1] = (unsigned char) _s[1]; |
| 2553 | _p[2] = (unsigned char) _s[2]; |
| 2554 | _p[3] = (unsigned char) _s[3]; |
| 2555 | #if (SIZEOF_LONG == 8) |
| 2556 | _p[4] = (unsigned char) _s[4]; |
| 2557 | _p[5] = (unsigned char) _s[5]; |
| 2558 | _p[6] = (unsigned char) _s[6]; |
| 2559 | _p[7] = (unsigned char) _s[7]; |
| 2560 | #endif |
| 2561 | _s += SIZEOF_LONG; |
| 2562 | _p += SIZEOF_LONG; |
| 2563 | } |
| 2564 | s = _s; |
| 2565 | p = _p; |
| 2566 | if (s == e) |
| 2567 | break; |
| 2568 | ch = (unsigned char)*s; |
| 2569 | } |
| 2570 | } |
| 2571 | |
| 2572 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2573 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2574 | s++; |
| 2575 | continue; |
| 2576 | } |
| 2577 | |
| 2578 | n = utf8_code_length[ch]; |
| 2579 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2580 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2581 | if (consumed) |
| 2582 | break; |
| 2583 | else { |
| 2584 | errmsg = "unexpected end of data"; |
| 2585 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2586 | endinpos = startinpos+1; |
| 2587 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 2588 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2589 | goto utf8Error; |
| 2590 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2591 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2592 | |
| 2593 | switch (n) { |
| 2594 | |
| 2595 | case 0: |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2596 | errmsg = "invalid start byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2597 | startinpos = s-starts; |
| 2598 | endinpos = startinpos+1; |
| 2599 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2600 | |
| 2601 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2602 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2603 | startinpos = s-starts; |
| 2604 | endinpos = startinpos+1; |
| 2605 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2606 | |
| 2607 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2608 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2609 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2610 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2611 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2612 | goto utf8Error; |
| 2613 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2614 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2615 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 2616 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2617 | break; |
| 2618 | |
| 2619 | case 3: |
Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 2620 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 2621 | will result in surrogates in range d800-dfff. Surrogates are |
| 2622 | not valid UTF-8 so they are rejected. |
| 2623 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 2624 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2625 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2626 | (s[2] & 0xc0) != 0x80 || |
| 2627 | ((unsigned char)s[0] == 0xE0 && |
| 2628 | (unsigned char)s[1] < 0xA0) || |
| 2629 | ((unsigned char)s[0] == 0xED && |
| 2630 | (unsigned char)s[1] > 0x9F)) { |
| 2631 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2632 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2633 | endinpos = startinpos + 1; |
| 2634 | |
| 2635 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 2636 | continuation byte is s[2], so increment endinpos by 1, |
| 2637 | if not, s[1] is invalid and endinpos doesn't need to |
| 2638 | be incremented. */ |
| 2639 | if ((s[1] & 0xC0) == 0x80) |
| 2640 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2641 | goto utf8Error; |
| 2642 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2643 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2644 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
| 2645 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2646 | break; |
| 2647 | |
| 2648 | case 4: |
| 2649 | if ((s[1] & 0xc0) != 0x80 || |
| 2650 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2651 | (s[3] & 0xc0) != 0x80 || |
| 2652 | ((unsigned char)s[0] == 0xF0 && |
| 2653 | (unsigned char)s[1] < 0x90) || |
| 2654 | ((unsigned char)s[0] == 0xF4 && |
| 2655 | (unsigned char)s[1] > 0x8F)) { |
| 2656 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2657 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2658 | endinpos = startinpos + 1; |
| 2659 | if ((s[1] & 0xC0) == 0x80) { |
| 2660 | endinpos++; |
| 2661 | if ((s[2] & 0xC0) == 0x80) |
| 2662 | endinpos++; |
| 2663 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2664 | goto utf8Error; |
| 2665 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2666 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2667 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 2668 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 2669 | |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2670 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2671 | *p++ = (Py_UNICODE)ch; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2672 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2673 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2674 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2675 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 2676 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2677 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2678 | /* high surrogate = top 10 bits added to D800 */ |
| 2679 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2680 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2681 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2682 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2683 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2684 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2685 | } |
| 2686 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2687 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2688 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2689 | utf8Error: |
| 2690 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2691 | if (unicode_decode_call_errorhandler( |
| 2692 | errors, &errorHandler, |
| 2693 | "utf8", errmsg, |
| 2694 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2695 | &unicode, &outpos, &p)) |
| 2696 | goto onError; |
| 2697 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2698 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2699 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2700 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2701 | |
| 2702 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2703 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2704 | goto onError; |
| 2705 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2706 | Py_XDECREF(errorHandler); |
| 2707 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2708 | return (PyObject *)unicode; |
| 2709 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2710 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2711 | Py_XDECREF(errorHandler); |
| 2712 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2713 | Py_DECREF(unicode); |
| 2714 | return NULL; |
| 2715 | } |
| 2716 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2717 | #undef ASCII_CHAR_MASK |
| 2718 | |
| 2719 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2720 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 2721 | and allocate exactly as much space needed at the end. Else allocate the |
| 2722 | maximum possible needed (4 result bytes per Unicode character), and return |
| 2723 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2724 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 2725 | PyObject * |
| 2726 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2727 | Py_ssize_t size, |
| 2728 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2729 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2730 | #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] | 2731 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2732 | Py_ssize_t i; /* index into s of next input byte */ |
| 2733 | PyObject *result; /* result string object */ |
| 2734 | char *p; /* next free byte in output buffer */ |
| 2735 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 2736 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2737 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2738 | PyObject *errorHandler = NULL; |
| 2739 | PyObject *exc = NULL; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2740 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2741 | assert(s != NULL); |
| 2742 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2743 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2744 | if (size <= MAX_SHORT_UNICHARS) { |
| 2745 | /* Write into the stack buffer; nallocated can't overflow. |
| 2746 | * At the end, we'll allocate exactly as much heap space as it |
| 2747 | * turns out we need. |
| 2748 | */ |
| 2749 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2750 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2751 | p = stackbuf; |
| 2752 | } |
| 2753 | else { |
| 2754 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 2755 | nallocated = size * 4; |
| 2756 | if (nallocated / 4 != size) /* overflow! */ |
| 2757 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2758 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2759 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2760 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2761 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2762 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2763 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2764 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2765 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2766 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2767 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2768 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2769 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2770 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2771 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2772 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 2773 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 2774 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2775 | } else if (0xD800 <= ch && ch <= 0xDFFF) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2776 | #ifndef Py_UNICODE_WIDE |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2777 | /* Special case: check for high and low surrogate */ |
| 2778 | if (ch <= 0xDBFF && i != size && 0xDC00 <= s[i] && s[i] <= 0xDFFF) { |
| 2779 | Py_UCS4 ch2 = s[i]; |
| 2780 | /* Combine the two surrogates to form a UCS4 value */ |
| 2781 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
| 2782 | i++; |
| 2783 | |
| 2784 | /* Encode UCS4 Unicode ordinals */ |
| 2785 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2786 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2787 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2788 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2789 | } else { |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 2790 | #endif |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2791 | Py_ssize_t newpos; |
| 2792 | PyObject *rep; |
| 2793 | Py_ssize_t repsize, k; |
| 2794 | rep = unicode_encode_call_errorhandler |
| 2795 | (errors, &errorHandler, "utf-8", "surrogates not allowed", |
| 2796 | s, size, &exc, i-1, i, &newpos); |
| 2797 | if (!rep) |
| 2798 | goto error; |
| 2799 | |
| 2800 | if (PyBytes_Check(rep)) |
| 2801 | repsize = PyBytes_GET_SIZE(rep); |
| 2802 | else |
| 2803 | repsize = PyUnicode_GET_SIZE(rep); |
| 2804 | |
| 2805 | if (repsize > 4) { |
| 2806 | Py_ssize_t offset; |
| 2807 | |
| 2808 | if (result == NULL) |
| 2809 | offset = p - stackbuf; |
| 2810 | else |
| 2811 | offset = p - PyBytes_AS_STRING(result); |
| 2812 | |
| 2813 | if (nallocated > PY_SSIZE_T_MAX - repsize + 4) { |
| 2814 | /* integer overflow */ |
| 2815 | PyErr_NoMemory(); |
| 2816 | goto error; |
| 2817 | } |
| 2818 | nallocated += repsize - 4; |
| 2819 | if (result != NULL) { |
| 2820 | if (_PyBytes_Resize(&result, nallocated) < 0) |
| 2821 | goto error; |
| 2822 | } else { |
| 2823 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
| 2824 | if (result == NULL) |
| 2825 | goto error; |
| 2826 | Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset); |
| 2827 | } |
| 2828 | p = PyBytes_AS_STRING(result) + offset; |
| 2829 | } |
| 2830 | |
| 2831 | if (PyBytes_Check(rep)) { |
| 2832 | char *prep = PyBytes_AS_STRING(rep); |
| 2833 | for(k = repsize; k > 0; k--) |
| 2834 | *p++ = *prep++; |
| 2835 | } else /* rep is unicode */ { |
| 2836 | Py_UNICODE *prep = PyUnicode_AS_UNICODE(rep); |
| 2837 | Py_UNICODE c; |
| 2838 | |
| 2839 | for(k=0; k<repsize; k++) { |
| 2840 | c = prep[k]; |
| 2841 | if (0x80 <= c) { |
| 2842 | raise_encode_exception(&exc, "utf-8", s, size, |
| 2843 | i-1, i, "surrogates not allowed"); |
| 2844 | goto error; |
| 2845 | } |
| 2846 | *p++ = (char)prep[k]; |
| 2847 | } |
| 2848 | } |
| 2849 | Py_DECREF(rep); |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 2850 | #ifndef Py_UNICODE_WIDE |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2851 | } |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 2852 | #endif |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2853 | } else if (ch < 0x10000) { |
| 2854 | *p++ = (char)(0xe0 | (ch >> 12)); |
| 2855 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2856 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2857 | } else /* ch >= 0x10000 */ { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2858 | /* Encode UCS4 Unicode ordinals */ |
| 2859 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2860 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 2861 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2862 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2863 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2864 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 2865 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2866 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2867 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2868 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2869 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2870 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2871 | } |
| 2872 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 2873 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2874 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2875 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2876 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2877 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2878 | Py_XDECREF(errorHandler); |
| 2879 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2880 | return result; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2881 | error: |
| 2882 | Py_XDECREF(errorHandler); |
| 2883 | Py_XDECREF(exc); |
| 2884 | Py_XDECREF(result); |
| 2885 | return NULL; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2886 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2887 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2888 | } |
| 2889 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2890 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 2891 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2892 | if (!PyUnicode_Check(unicode)) { |
| 2893 | PyErr_BadArgument(); |
| 2894 | return NULL; |
| 2895 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 2896 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2897 | PyUnicode_GET_SIZE(unicode), |
| 2898 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2899 | } |
| 2900 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2901 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 2902 | |
| 2903 | PyObject * |
| 2904 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2905 | Py_ssize_t size, |
| 2906 | const char *errors, |
| 2907 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2908 | { |
| 2909 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 2910 | } |
| 2911 | |
| 2912 | PyObject * |
| 2913 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2914 | Py_ssize_t size, |
| 2915 | const char *errors, |
| 2916 | int *byteorder, |
| 2917 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2918 | { |
| 2919 | const char *starts = s; |
| 2920 | Py_ssize_t startinpos; |
| 2921 | Py_ssize_t endinpos; |
| 2922 | Py_ssize_t outpos; |
| 2923 | PyUnicodeObject *unicode; |
| 2924 | Py_UNICODE *p; |
| 2925 | #ifndef Py_UNICODE_WIDE |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 2926 | int pairs = 0; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 2927 | const unsigned char *qq; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2928 | #else |
| 2929 | const int pairs = 0; |
| 2930 | #endif |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 2931 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2932 | int bo = 0; /* assume native ordering by default */ |
| 2933 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2934 | /* Offsets from q for retrieving bytes in the right order. */ |
| 2935 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2936 | int iorder[] = {0, 1, 2, 3}; |
| 2937 | #else |
| 2938 | int iorder[] = {3, 2, 1, 0}; |
| 2939 | #endif |
| 2940 | PyObject *errorHandler = NULL; |
| 2941 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 2942 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2943 | q = (unsigned char *)s; |
| 2944 | e = q + size; |
| 2945 | |
| 2946 | if (byteorder) |
| 2947 | bo = *byteorder; |
| 2948 | |
| 2949 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2950 | byte order setting accordingly. In native mode, the leading BOM |
| 2951 | mark is skipped, in all other modes, it is copied to the output |
| 2952 | stream as-is (giving a ZWNBSP character). */ |
| 2953 | if (bo == 0) { |
| 2954 | if (size >= 4) { |
| 2955 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2956 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2957 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2958 | if (bom == 0x0000FEFF) { |
| 2959 | q += 4; |
| 2960 | bo = -1; |
| 2961 | } |
| 2962 | else if (bom == 0xFFFE0000) { |
| 2963 | q += 4; |
| 2964 | bo = 1; |
| 2965 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2966 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2967 | if (bom == 0x0000FEFF) { |
| 2968 | q += 4; |
| 2969 | bo = 1; |
| 2970 | } |
| 2971 | else if (bom == 0xFFFE0000) { |
| 2972 | q += 4; |
| 2973 | bo = -1; |
| 2974 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2975 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2976 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2977 | } |
| 2978 | |
| 2979 | if (bo == -1) { |
| 2980 | /* force LE */ |
| 2981 | iorder[0] = 0; |
| 2982 | iorder[1] = 1; |
| 2983 | iorder[2] = 2; |
| 2984 | iorder[3] = 3; |
| 2985 | } |
| 2986 | else if (bo == 1) { |
| 2987 | /* force BE */ |
| 2988 | iorder[0] = 3; |
| 2989 | iorder[1] = 2; |
| 2990 | iorder[2] = 1; |
| 2991 | iorder[3] = 0; |
| 2992 | } |
| 2993 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 2994 | /* On narrow builds we split characters outside the BMP into two |
| 2995 | codepoints => count how much extra space we need. */ |
| 2996 | #ifndef Py_UNICODE_WIDE |
| 2997 | for (qq = q; qq < e; qq += 4) |
| 2998 | if (qq[iorder[2]] != 0 || qq[iorder[3]] != 0) |
| 2999 | pairs++; |
| 3000 | #endif |
| 3001 | |
| 3002 | /* This might be one to much, because of a BOM */ |
| 3003 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 3004 | if (!unicode) |
| 3005 | return NULL; |
| 3006 | if (size == 0) |
| 3007 | return (PyObject *)unicode; |
| 3008 | |
| 3009 | /* Unpack UTF-32 encoded data */ |
| 3010 | p = unicode->str; |
| 3011 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3012 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3013 | Py_UCS4 ch; |
| 3014 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 3015 | if (e-q<4) { |
| 3016 | if (consumed) |
| 3017 | break; |
| 3018 | errmsg = "truncated data"; |
| 3019 | startinpos = ((const char *)q)-starts; |
| 3020 | endinpos = ((const char *)e)-starts; |
| 3021 | goto utf32Error; |
| 3022 | /* The remaining input chars are ignored if the callback |
| 3023 | chooses to skip the input */ |
| 3024 | } |
| 3025 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 3026 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3027 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3028 | if (ch >= 0x110000) |
| 3029 | { |
| 3030 | errmsg = "codepoint not in range(0x110000)"; |
| 3031 | startinpos = ((const char *)q)-starts; |
| 3032 | endinpos = startinpos+4; |
| 3033 | goto utf32Error; |
| 3034 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3035 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3036 | if (ch >= 0x10000) |
| 3037 | { |
| 3038 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 3039 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 3040 | } |
| 3041 | else |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3042 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3043 | *p++ = ch; |
| 3044 | q += 4; |
| 3045 | continue; |
| 3046 | utf32Error: |
| 3047 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 3048 | if (unicode_decode_call_errorhandler( |
| 3049 | errors, &errorHandler, |
| 3050 | "utf32", errmsg, |
| 3051 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
| 3052 | &unicode, &outpos, &p)) |
| 3053 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3054 | } |
| 3055 | |
| 3056 | if (byteorder) |
| 3057 | *byteorder = bo; |
| 3058 | |
| 3059 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3060 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3061 | |
| 3062 | /* Adjust length */ |
| 3063 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
| 3064 | goto onError; |
| 3065 | |
| 3066 | Py_XDECREF(errorHandler); |
| 3067 | Py_XDECREF(exc); |
| 3068 | return (PyObject *)unicode; |
| 3069 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3070 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3071 | Py_DECREF(unicode); |
| 3072 | Py_XDECREF(errorHandler); |
| 3073 | Py_XDECREF(exc); |
| 3074 | return NULL; |
| 3075 | } |
| 3076 | |
| 3077 | PyObject * |
| 3078 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3079 | Py_ssize_t size, |
| 3080 | const char *errors, |
| 3081 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3082 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3083 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3084 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3085 | Py_ssize_t nsize, bytesize; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3086 | #ifndef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3087 | Py_ssize_t i, pairs; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3088 | #else |
| 3089 | const int pairs = 0; |
| 3090 | #endif |
| 3091 | /* Offsets from p for storing byte pairs in the right order. */ |
| 3092 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3093 | int iorder[] = {0, 1, 2, 3}; |
| 3094 | #else |
| 3095 | int iorder[] = {3, 2, 1, 0}; |
| 3096 | #endif |
| 3097 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3098 | #define STORECHAR(CH) \ |
| 3099 | do { \ |
| 3100 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 3101 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 3102 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 3103 | p[iorder[0]] = (CH) & 0xff; \ |
| 3104 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3105 | } while(0) |
| 3106 | |
| 3107 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 3108 | so we need less space. */ |
| 3109 | #ifndef Py_UNICODE_WIDE |
| 3110 | for (i = pairs = 0; i < size-1; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3111 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 3112 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 3113 | pairs++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3114 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3115 | nsize = (size - pairs + (byteorder == 0)); |
| 3116 | bytesize = nsize * 4; |
| 3117 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3118 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3119 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3120 | if (v == NULL) |
| 3121 | return NULL; |
| 3122 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3123 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3124 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3125 | STORECHAR(0xFEFF); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3126 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3127 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3128 | |
| 3129 | if (byteorder == -1) { |
| 3130 | /* force LE */ |
| 3131 | iorder[0] = 0; |
| 3132 | iorder[1] = 1; |
| 3133 | iorder[2] = 2; |
| 3134 | iorder[3] = 3; |
| 3135 | } |
| 3136 | else if (byteorder == 1) { |
| 3137 | /* force BE */ |
| 3138 | iorder[0] = 3; |
| 3139 | iorder[1] = 2; |
| 3140 | iorder[2] = 1; |
| 3141 | iorder[3] = 0; |
| 3142 | } |
| 3143 | |
| 3144 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3145 | Py_UCS4 ch = *s++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3146 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3147 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 3148 | Py_UCS4 ch2 = *s; |
| 3149 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 3150 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 3151 | s++; |
| 3152 | size--; |
| 3153 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3154 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3155 | #endif |
| 3156 | STORECHAR(ch); |
| 3157 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3158 | |
| 3159 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3160 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3161 | #undef STORECHAR |
| 3162 | } |
| 3163 | |
| 3164 | PyObject *PyUnicode_AsUTF32String(PyObject *unicode) |
| 3165 | { |
| 3166 | if (!PyUnicode_Check(unicode)) { |
| 3167 | PyErr_BadArgument(); |
| 3168 | return NULL; |
| 3169 | } |
| 3170 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3171 | PyUnicode_GET_SIZE(unicode), |
| 3172 | NULL, |
| 3173 | 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3174 | } |
| 3175 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3176 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 3177 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3178 | PyObject * |
| 3179 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3180 | Py_ssize_t size, |
| 3181 | const char *errors, |
| 3182 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3183 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3184 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 3185 | } |
| 3186 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3187 | /* Two masks for fast checking of whether a C 'long' may contain |
| 3188 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 3189 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 3190 | rare in most input. |
| 3191 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 3192 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3193 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3194 | #if (SIZEOF_LONG == 8) |
| 3195 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 3196 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 3197 | #elif (SIZEOF_LONG == 4) |
| 3198 | # define FAST_CHAR_MASK 0x80008000L |
| 3199 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 3200 | #else |
| 3201 | # error C 'long' size should be either 4 or 8! |
| 3202 | #endif |
| 3203 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3204 | PyObject * |
| 3205 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3206 | Py_ssize_t size, |
| 3207 | const char *errors, |
| 3208 | int *byteorder, |
| 3209 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3210 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3211 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3212 | Py_ssize_t startinpos; |
| 3213 | Py_ssize_t endinpos; |
| 3214 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3215 | PyUnicodeObject *unicode; |
| 3216 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3217 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3218 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3219 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 3220 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3221 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 3222 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3223 | int ihi = 1, ilo = 0; |
| 3224 | #else |
| 3225 | int ihi = 0, ilo = 1; |
| 3226 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3227 | PyObject *errorHandler = NULL; |
| 3228 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3229 | |
| 3230 | /* Note: size will always be longer than the resulting Unicode |
| 3231 | character count */ |
| 3232 | unicode = _PyUnicode_New(size); |
| 3233 | if (!unicode) |
| 3234 | return NULL; |
| 3235 | if (size == 0) |
| 3236 | return (PyObject *)unicode; |
| 3237 | |
| 3238 | /* Unpack UTF-16 encoded data */ |
| 3239 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3240 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3241 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3242 | |
| 3243 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3244 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3245 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3246 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 3247 | byte order setting accordingly. In native mode, the leading BOM |
| 3248 | mark is skipped, in all other modes, it is copied to the output |
| 3249 | stream as-is (giving a ZWNBSP character). */ |
| 3250 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3251 | if (size >= 2) { |
| 3252 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3253 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3254 | if (bom == 0xFEFF) { |
| 3255 | q += 2; |
| 3256 | bo = -1; |
| 3257 | } |
| 3258 | else if (bom == 0xFFFE) { |
| 3259 | q += 2; |
| 3260 | bo = 1; |
| 3261 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3262 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3263 | if (bom == 0xFEFF) { |
| 3264 | q += 2; |
| 3265 | bo = 1; |
| 3266 | } |
| 3267 | else if (bom == 0xFFFE) { |
| 3268 | q += 2; |
| 3269 | bo = -1; |
| 3270 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3271 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3272 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3273 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3274 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3275 | if (bo == -1) { |
| 3276 | /* force LE */ |
| 3277 | ihi = 1; |
| 3278 | ilo = 0; |
| 3279 | } |
| 3280 | else if (bo == 1) { |
| 3281 | /* force BE */ |
| 3282 | ihi = 0; |
| 3283 | ilo = 1; |
| 3284 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3285 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3286 | native_ordering = ilo < ihi; |
| 3287 | #else |
| 3288 | native_ordering = ilo > ihi; |
| 3289 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3290 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3291 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3292 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3293 | Py_UNICODE ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3294 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 3295 | reads are more expensive, better to defer to another iteration. */ |
| 3296 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 3297 | /* Fast path for runs of non-surrogate chars. */ |
| 3298 | register const unsigned char *_q = q; |
| 3299 | Py_UNICODE *_p = p; |
| 3300 | if (native_ordering) { |
| 3301 | /* Native ordering is simple: as long as the input cannot |
| 3302 | possibly contain a surrogate char, do an unrolled copy |
| 3303 | of several 16-bit code points to the target object. |
| 3304 | The non-surrogate check is done on several input bytes |
| 3305 | at a time (as many as a C 'long' can contain). */ |
| 3306 | while (_q < aligned_end) { |
| 3307 | unsigned long data = * (unsigned long *) _q; |
| 3308 | if (data & FAST_CHAR_MASK) |
| 3309 | break; |
| 3310 | _p[0] = ((unsigned short *) _q)[0]; |
| 3311 | _p[1] = ((unsigned short *) _q)[1]; |
| 3312 | #if (SIZEOF_LONG == 8) |
| 3313 | _p[2] = ((unsigned short *) _q)[2]; |
| 3314 | _p[3] = ((unsigned short *) _q)[3]; |
| 3315 | #endif |
| 3316 | _q += SIZEOF_LONG; |
| 3317 | _p += SIZEOF_LONG / 2; |
| 3318 | } |
| 3319 | } |
| 3320 | else { |
| 3321 | /* Byteswapped ordering is similar, but we must decompose |
| 3322 | the copy bytewise, and take care of zero'ing out the |
| 3323 | upper bytes if the target object is in 32-bit units |
| 3324 | (that is, in UCS-4 builds). */ |
| 3325 | while (_q < aligned_end) { |
| 3326 | unsigned long data = * (unsigned long *) _q; |
| 3327 | if (data & SWAPPED_FAST_CHAR_MASK) |
| 3328 | break; |
| 3329 | /* Zero upper bytes in UCS-4 builds */ |
| 3330 | #if (Py_UNICODE_SIZE > 2) |
| 3331 | _p[0] = 0; |
| 3332 | _p[1] = 0; |
| 3333 | #if (SIZEOF_LONG == 8) |
| 3334 | _p[2] = 0; |
| 3335 | _p[3] = 0; |
| 3336 | #endif |
| 3337 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 3338 | /* Issue #4916; UCS-4 builds on big endian machines must |
| 3339 | fill the two last bytes of each 4-byte unit. */ |
| 3340 | #if (!defined(BYTEORDER_IS_LITTLE_ENDIAN) && Py_UNICODE_SIZE > 2) |
| 3341 | # define OFF 2 |
| 3342 | #else |
| 3343 | # define OFF 0 |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3344 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 3345 | ((unsigned char *) _p)[OFF + 1] = _q[0]; |
| 3346 | ((unsigned char *) _p)[OFF + 0] = _q[1]; |
| 3347 | ((unsigned char *) _p)[OFF + 1 + Py_UNICODE_SIZE] = _q[2]; |
| 3348 | ((unsigned char *) _p)[OFF + 0 + Py_UNICODE_SIZE] = _q[3]; |
| 3349 | #if (SIZEOF_LONG == 8) |
| 3350 | ((unsigned char *) _p)[OFF + 1 + 2 * Py_UNICODE_SIZE] = _q[4]; |
| 3351 | ((unsigned char *) _p)[OFF + 0 + 2 * Py_UNICODE_SIZE] = _q[5]; |
| 3352 | ((unsigned char *) _p)[OFF + 1 + 3 * Py_UNICODE_SIZE] = _q[6]; |
| 3353 | ((unsigned char *) _p)[OFF + 0 + 3 * Py_UNICODE_SIZE] = _q[7]; |
| 3354 | #endif |
| 3355 | #undef OFF |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3356 | _q += SIZEOF_LONG; |
| 3357 | _p += SIZEOF_LONG / 2; |
| 3358 | } |
| 3359 | } |
| 3360 | p = _p; |
| 3361 | q = _q; |
| 3362 | if (q >= e) |
| 3363 | break; |
| 3364 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3365 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3366 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3367 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3368 | |
| 3369 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 3370 | *p++ = ch; |
| 3371 | continue; |
| 3372 | } |
| 3373 | |
| 3374 | /* UTF-16 code pair: */ |
| 3375 | if (q > e) { |
| 3376 | errmsg = "unexpected end of data"; |
| 3377 | startinpos = (((const char *)q) - 2) - starts; |
| 3378 | endinpos = ((const char *)e) + 1 - starts; |
| 3379 | goto utf16Error; |
| 3380 | } |
| 3381 | if (0xD800 <= ch && ch <= 0xDBFF) { |
| 3382 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 3383 | q += 2; |
| 3384 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 3385 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3386 | *p++ = ch; |
| 3387 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3388 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3389 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3390 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3391 | continue; |
| 3392 | } |
| 3393 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3394 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3395 | startinpos = (((const char *)q)-4)-starts; |
| 3396 | endinpos = startinpos+2; |
| 3397 | goto utf16Error; |
| 3398 | } |
| 3399 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3400 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3401 | errmsg = "illegal encoding"; |
| 3402 | startinpos = (((const char *)q)-2)-starts; |
| 3403 | endinpos = startinpos+2; |
| 3404 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3405 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3406 | utf16Error: |
| 3407 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 3408 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3409 | errors, |
| 3410 | &errorHandler, |
| 3411 | "utf16", errmsg, |
| 3412 | &starts, |
| 3413 | (const char **)&e, |
| 3414 | &startinpos, |
| 3415 | &endinpos, |
| 3416 | &exc, |
| 3417 | (const char **)&q, |
| 3418 | &unicode, |
| 3419 | &outpos, |
| 3420 | &p)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3421 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3422 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3423 | /* remaining byte at the end? (size should be even) */ |
| 3424 | if (e == q) { |
| 3425 | if (!consumed) { |
| 3426 | errmsg = "truncated data"; |
| 3427 | startinpos = ((const char *)q) - starts; |
| 3428 | endinpos = ((const char *)e) + 1 - starts; |
| 3429 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 3430 | if (unicode_decode_call_errorhandler( |
| 3431 | errors, |
| 3432 | &errorHandler, |
| 3433 | "utf16", errmsg, |
| 3434 | &starts, |
| 3435 | (const char **)&e, |
| 3436 | &startinpos, |
| 3437 | &endinpos, |
| 3438 | &exc, |
| 3439 | (const char **)&q, |
| 3440 | &unicode, |
| 3441 | &outpos, |
| 3442 | &p)) |
| 3443 | goto onError; |
| 3444 | /* The remaining input chars are ignored if the callback |
| 3445 | chooses to skip the input */ |
| 3446 | } |
| 3447 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3448 | |
| 3449 | if (byteorder) |
| 3450 | *byteorder = bo; |
| 3451 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3452 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3453 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3454 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3455 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 3456 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3457 | goto onError; |
| 3458 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3459 | Py_XDECREF(errorHandler); |
| 3460 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3461 | return (PyObject *)unicode; |
| 3462 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3463 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3464 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3465 | Py_XDECREF(errorHandler); |
| 3466 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3467 | return NULL; |
| 3468 | } |
| 3469 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3470 | #undef FAST_CHAR_MASK |
| 3471 | #undef SWAPPED_FAST_CHAR_MASK |
| 3472 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3473 | PyObject * |
| 3474 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3475 | Py_ssize_t size, |
| 3476 | const char *errors, |
| 3477 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3478 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3479 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3480 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3481 | Py_ssize_t nsize, bytesize; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3482 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3483 | Py_ssize_t i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3484 | #else |
| 3485 | const int pairs = 0; |
| 3486 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3487 | /* Offsets from p for storing byte pairs in the right order. */ |
| 3488 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3489 | int ihi = 1, ilo = 0; |
| 3490 | #else |
| 3491 | int ihi = 0, ilo = 1; |
| 3492 | #endif |
| 3493 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3494 | #define STORECHAR(CH) \ |
| 3495 | do { \ |
| 3496 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 3497 | p[ilo] = (CH) & 0xff; \ |
| 3498 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3499 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3500 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3501 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3502 | for (i = pairs = 0; i < size; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3503 | if (s[i] >= 0x10000) |
| 3504 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3505 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3506 | /* 2 * (size + pairs + (byteorder == 0)) */ |
| 3507 | if (size > PY_SSIZE_T_MAX || |
| 3508 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3509 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3510 | nsize = size + pairs + (byteorder == 0); |
| 3511 | bytesize = nsize * 2; |
| 3512 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3513 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3514 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3515 | if (v == NULL) |
| 3516 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3517 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3518 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3519 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3520 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 3521 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3522 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3523 | |
| 3524 | if (byteorder == -1) { |
| 3525 | /* force LE */ |
| 3526 | ihi = 1; |
| 3527 | ilo = 0; |
| 3528 | } |
| 3529 | else if (byteorder == 1) { |
| 3530 | /* force BE */ |
| 3531 | ihi = 0; |
| 3532 | ilo = 1; |
| 3533 | } |
| 3534 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3535 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3536 | Py_UNICODE ch = *s++; |
| 3537 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3538 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3539 | if (ch >= 0x10000) { |
| 3540 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 3541 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 3542 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3543 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3544 | STORECHAR(ch); |
| 3545 | if (ch2) |
| 3546 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3547 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3548 | |
| 3549 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3550 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3551 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3552 | } |
| 3553 | |
| 3554 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 3555 | { |
| 3556 | if (!PyUnicode_Check(unicode)) { |
| 3557 | PyErr_BadArgument(); |
| 3558 | return NULL; |
| 3559 | } |
| 3560 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3561 | PyUnicode_GET_SIZE(unicode), |
| 3562 | NULL, |
| 3563 | 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3564 | } |
| 3565 | |
| 3566 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 3567 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 3568 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 3569 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3570 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3571 | Py_ssize_t size, |
| 3572 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3573 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3574 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3575 | Py_ssize_t startinpos; |
| 3576 | Py_ssize_t endinpos; |
| 3577 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3578 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3579 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3580 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3581 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3582 | char* message; |
| 3583 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3584 | PyObject *errorHandler = NULL; |
| 3585 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3586 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3587 | /* Escaped strings will always be longer than the resulting |
| 3588 | 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] | 3589 | length after conversion to the true value. |
| 3590 | (but if the error callback returns a long replacement string |
| 3591 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3592 | v = _PyUnicode_New(size); |
| 3593 | if (v == NULL) |
| 3594 | goto onError; |
| 3595 | if (size == 0) |
| 3596 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3597 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3598 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3599 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3600 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3601 | while (s < end) { |
| 3602 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 3603 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3604 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3605 | |
| 3606 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3607 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3608 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3609 | continue; |
| 3610 | } |
| 3611 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3612 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3613 | /* \ - Escapes */ |
| 3614 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3615 | c = *s++; |
| 3616 | if (s > end) |
| 3617 | c = '\0'; /* Invalid after \ */ |
| 3618 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3619 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3620 | /* \x escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3621 | case '\n': break; |
| 3622 | case '\\': *p++ = '\\'; break; |
| 3623 | case '\'': *p++ = '\''; break; |
| 3624 | case '\"': *p++ = '\"'; break; |
| 3625 | case 'b': *p++ = '\b'; break; |
| 3626 | case 'f': *p++ = '\014'; break; /* FF */ |
| 3627 | case 't': *p++ = '\t'; break; |
| 3628 | case 'n': *p++ = '\n'; break; |
| 3629 | case 'r': *p++ = '\r'; break; |
| 3630 | case 'v': *p++ = '\013'; break; /* VT */ |
| 3631 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 3632 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3633 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3634 | case '0': case '1': case '2': case '3': |
| 3635 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3636 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3637 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3638 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3639 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3640 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3641 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3642 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3643 | break; |
| 3644 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3645 | /* hex escapes */ |
| 3646 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3647 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3648 | digits = 2; |
| 3649 | message = "truncated \\xXX escape"; |
| 3650 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3651 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3652 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3653 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3654 | digits = 4; |
| 3655 | message = "truncated \\uXXXX escape"; |
| 3656 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3657 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3658 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3659 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3660 | digits = 8; |
| 3661 | message = "truncated \\UXXXXXXXX escape"; |
| 3662 | hexescape: |
| 3663 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3664 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3665 | if (s+digits>end) { |
| 3666 | endinpos = size; |
| 3667 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3668 | errors, &errorHandler, |
| 3669 | "unicodeescape", "end of string in escape sequence", |
| 3670 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3671 | &v, &outpos, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3672 | goto onError; |
| 3673 | goto nextByte; |
| 3674 | } |
| 3675 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3676 | c = (unsigned char) s[i]; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3677 | if (!ISXDIGIT(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3678 | endinpos = (s+i+1)-starts; |
| 3679 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3680 | errors, &errorHandler, |
| 3681 | "unicodeescape", message, |
| 3682 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3683 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3684 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3685 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3686 | } |
| 3687 | chr = (chr<<4) & ~0xF; |
| 3688 | if (c >= '0' && c <= '9') |
| 3689 | chr += c - '0'; |
| 3690 | else if (c >= 'a' && c <= 'f') |
| 3691 | chr += 10 + c - 'a'; |
| 3692 | else |
| 3693 | chr += 10 + c - 'A'; |
| 3694 | } |
| 3695 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 3696 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3697 | /* _decoding_error will have already written into the |
| 3698 | target buffer. */ |
| 3699 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3700 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3701 | /* when we get here, chr is a 32-bit unicode character */ |
| 3702 | if (chr <= 0xffff) |
| 3703 | /* UCS-2 character */ |
| 3704 | *p++ = (Py_UNICODE) chr; |
| 3705 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3706 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3707 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 3708 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3709 | *p++ = chr; |
| 3710 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3711 | chr -= 0x10000L; |
| 3712 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 3713 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3714 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3715 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3716 | endinpos = s-starts; |
| 3717 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3718 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3719 | errors, &errorHandler, |
| 3720 | "unicodeescape", "illegal Unicode character", |
| 3721 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3722 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3723 | goto onError; |
| 3724 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3725 | break; |
| 3726 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3727 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3728 | case 'N': |
| 3729 | message = "malformed \\N character escape"; |
| 3730 | if (ucnhash_CAPI == NULL) { |
| 3731 | /* load the unicode data module */ |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3732 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import(PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3733 | if (ucnhash_CAPI == NULL) |
| 3734 | goto ucnhashError; |
| 3735 | } |
| 3736 | if (*s == '{') { |
| 3737 | const char *start = s+1; |
| 3738 | /* look for the closing brace */ |
| 3739 | while (*s != '}' && s < end) |
| 3740 | s++; |
| 3741 | if (s > start && s < end && *s == '}') { |
| 3742 | /* found a name. look it up in the unicode database */ |
| 3743 | message = "unknown Unicode character name"; |
| 3744 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 3745 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3746 | goto store; |
| 3747 | } |
| 3748 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3749 | endinpos = s-starts; |
| 3750 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3751 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3752 | errors, &errorHandler, |
| 3753 | "unicodeescape", message, |
| 3754 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3755 | &v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3756 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3757 | break; |
| 3758 | |
| 3759 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3760 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3761 | message = "\\ at end of string"; |
| 3762 | s--; |
| 3763 | endinpos = s-starts; |
| 3764 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3765 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3766 | errors, &errorHandler, |
| 3767 | "unicodeescape", message, |
| 3768 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3769 | &v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3770 | goto onError; |
| 3771 | } |
| 3772 | else { |
| 3773 | *p++ = '\\'; |
| 3774 | *p++ = (unsigned char)s[-1]; |
| 3775 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3776 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3777 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3778 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3779 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3780 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3781 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3782 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 3783 | Py_XDECREF(errorHandler); |
| 3784 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3785 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3786 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3787 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 3788 | PyErr_SetString( |
| 3789 | PyExc_UnicodeError, |
| 3790 | "\\N escapes not supported (can't load unicodedata module)" |
| 3791 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3792 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3793 | Py_XDECREF(errorHandler); |
| 3794 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 3795 | return NULL; |
| 3796 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3797 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3798 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3799 | Py_XDECREF(errorHandler); |
| 3800 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3801 | return NULL; |
| 3802 | } |
| 3803 | |
| 3804 | /* Return a Unicode-Escape string version of the Unicode object. |
| 3805 | |
| 3806 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 3807 | appropriate. |
| 3808 | |
| 3809 | */ |
| 3810 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3811 | Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3812 | Py_ssize_t size, |
| 3813 | Py_UNICODE ch) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3814 | { |
| 3815 | /* like wcschr, but doesn't stop at NULL characters */ |
| 3816 | |
| 3817 | while (size-- > 0) { |
| 3818 | if (*s == ch) |
| 3819 | return s; |
| 3820 | s++; |
| 3821 | } |
| 3822 | |
| 3823 | return NULL; |
| 3824 | } |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 3825 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3826 | static const char *hexdigits = "0123456789abcdef"; |
| 3827 | |
| 3828 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3829 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3830 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3831 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3832 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3833 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3834 | #ifdef Py_UNICODE_WIDE |
| 3835 | const Py_ssize_t expandsize = 10; |
| 3836 | #else |
| 3837 | const Py_ssize_t expandsize = 6; |
| 3838 | #endif |
| 3839 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3840 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 3841 | better to choose a different scheme. Perhaps scan the |
| 3842 | first N-chars of the string and allocate based on that size. |
| 3843 | */ |
| 3844 | /* Initial allocation is based on the longest-possible unichr |
| 3845 | escape. |
| 3846 | |
| 3847 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 3848 | unichr, so in this case it's the longest unichr escape. In |
| 3849 | narrow (UTF-16) builds this is five chars per source unichr |
| 3850 | since there are two unichrs in the surrogate pair, so in narrow |
| 3851 | (UTF-16) builds it's not the longest unichr escape. |
| 3852 | |
| 3853 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 3854 | so in the narrow (UTF-16) build case it's the longest unichr |
| 3855 | escape. |
| 3856 | */ |
| 3857 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3858 | if (size == 0) |
| 3859 | return PyBytes_FromStringAndSize(NULL, 0); |
| 3860 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3861 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3862 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3863 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3864 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3865 | 2 |
| 3866 | + expandsize*size |
| 3867 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3868 | if (repr == NULL) |
| 3869 | return NULL; |
| 3870 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3871 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3872 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3873 | while (size-- > 0) { |
| 3874 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3875 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3876 | /* Escape backslashes */ |
| 3877 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3878 | *p++ = '\\'; |
| 3879 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3880 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3881 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3882 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 3883 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3884 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 3885 | else if (ch >= 0x10000) { |
| 3886 | *p++ = '\\'; |
| 3887 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3888 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 3889 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 3890 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 3891 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 3892 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 3893 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 3894 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 3895 | *p++ = hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3896 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3897 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3898 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3899 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 3900 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 3901 | Py_UNICODE ch2; |
| 3902 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3903 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3904 | ch2 = *s++; |
| 3905 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 3906 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3907 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 3908 | *p++ = '\\'; |
| 3909 | *p++ = 'U'; |
| 3910 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 3911 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 3912 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 3913 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 3914 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 3915 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 3916 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 3917 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 3918 | continue; |
| 3919 | } |
| 3920 | /* Fall through: isolated surrogates are copied as-is */ |
| 3921 | s--; |
| 3922 | size++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3923 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3924 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3925 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3926 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3927 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3928 | *p++ = '\\'; |
| 3929 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3930 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 3931 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 3932 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 3933 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3934 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3935 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3936 | /* Map special whitespace to '\t', \n', '\r' */ |
| 3937 | else if (ch == '\t') { |
| 3938 | *p++ = '\\'; |
| 3939 | *p++ = 't'; |
| 3940 | } |
| 3941 | else if (ch == '\n') { |
| 3942 | *p++ = '\\'; |
| 3943 | *p++ = 'n'; |
| 3944 | } |
| 3945 | else if (ch == '\r') { |
| 3946 | *p++ = '\\'; |
| 3947 | *p++ = 'r'; |
| 3948 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3949 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3950 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 3951 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3952 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3953 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3954 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 3955 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3956 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3957 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3958 | /* Copy everything else as-is */ |
| 3959 | else |
| 3960 | *p++ = (char) ch; |
| 3961 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3962 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3963 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 3964 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 3965 | return NULL; |
| 3966 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3967 | } |
| 3968 | |
Alexandre Vassalotti | 2056bed | 2008-12-27 19:46:35 +0000 | [diff] [blame] | 3969 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3970 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3971 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3972 | if (!PyUnicode_Check(unicode)) { |
| 3973 | PyErr_BadArgument(); |
| 3974 | return NULL; |
| 3975 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3976 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3977 | PyUnicode_GET_SIZE(unicode)); |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3978 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3979 | } |
| 3980 | |
| 3981 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 3982 | |
| 3983 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3984 | Py_ssize_t size, |
| 3985 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3986 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3987 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3988 | Py_ssize_t startinpos; |
| 3989 | Py_ssize_t endinpos; |
| 3990 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3991 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3992 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3993 | const char *end; |
| 3994 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3995 | PyObject *errorHandler = NULL; |
| 3996 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3997 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3998 | /* Escaped strings will always be longer than the resulting |
| 3999 | 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] | 4000 | length after conversion to the true value. (But decoding error |
| 4001 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4002 | v = _PyUnicode_New(size); |
| 4003 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4004 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4005 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4006 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4007 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4008 | end = s + size; |
| 4009 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4010 | unsigned char c; |
| 4011 | Py_UCS4 x; |
| 4012 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4013 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4014 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4015 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 4016 | if (*s != '\\') { |
| 4017 | *p++ = (unsigned char)*s++; |
| 4018 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4019 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4020 | startinpos = s-starts; |
| 4021 | |
| 4022 | /* \u-escapes are only interpreted iff the number of leading |
| 4023 | backslashes if odd */ |
| 4024 | bs = s; |
| 4025 | for (;s < end;) { |
| 4026 | if (*s != '\\') |
| 4027 | break; |
| 4028 | *p++ = (unsigned char)*s++; |
| 4029 | } |
| 4030 | if (((s - bs) & 1) == 0 || |
| 4031 | s >= end || |
| 4032 | (*s != 'u' && *s != 'U')) { |
| 4033 | continue; |
| 4034 | } |
| 4035 | p--; |
| 4036 | count = *s=='u' ? 4 : 8; |
| 4037 | s++; |
| 4038 | |
| 4039 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
| 4040 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4041 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 4042 | c = (unsigned char)*s; |
| 4043 | if (!ISXDIGIT(c)) { |
| 4044 | endinpos = s-starts; |
| 4045 | if (unicode_decode_call_errorhandler( |
| 4046 | errors, &errorHandler, |
| 4047 | "rawunicodeescape", "truncated \\uXXXX", |
| 4048 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 4049 | &v, &outpos, &p)) |
| 4050 | goto onError; |
| 4051 | goto nextByte; |
| 4052 | } |
| 4053 | x = (x<<4) & ~0xF; |
| 4054 | if (c >= '0' && c <= '9') |
| 4055 | x += c - '0'; |
| 4056 | else if (c >= 'a' && c <= 'f') |
| 4057 | x += 10 + c - 'a'; |
| 4058 | else |
| 4059 | x += 10 + c - 'A'; |
| 4060 | } |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4061 | if (x <= 0xffff) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4062 | /* UCS-2 character */ |
| 4063 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4064 | else if (x <= 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4065 | /* UCS-4 character. Either store directly, or as |
| 4066 | surrogate pair. */ |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4067 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4068 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4069 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4070 | x -= 0x10000L; |
| 4071 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 4072 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4073 | #endif |
| 4074 | } else { |
| 4075 | endinpos = s-starts; |
| 4076 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4077 | if (unicode_decode_call_errorhandler( |
| 4078 | errors, &errorHandler, |
| 4079 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4080 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 4081 | &v, &outpos, &p)) |
| 4082 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4083 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4084 | nextByte: |
| 4085 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4086 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4087 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4088 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4089 | Py_XDECREF(errorHandler); |
| 4090 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4091 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4092 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4093 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4094 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4095 | Py_XDECREF(errorHandler); |
| 4096 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4097 | return NULL; |
| 4098 | } |
| 4099 | |
| 4100 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4101 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4102 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4103 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4104 | char *p; |
| 4105 | char *q; |
| 4106 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4107 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4108 | const Py_ssize_t expandsize = 10; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4109 | #else |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4110 | const Py_ssize_t expandsize = 6; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4111 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4112 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4113 | if (size > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4114 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4115 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4116 | repr = PyBytes_FromStringAndSize(NULL, expandsize * size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4117 | if (repr == NULL) |
| 4118 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 4119 | if (size == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4120 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4121 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4122 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4123 | while (size-- > 0) { |
| 4124 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4125 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4126 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 4127 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4128 | *p++ = '\\'; |
| 4129 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 4130 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 4131 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 4132 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 4133 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 4134 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 4135 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 4136 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 4137 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4138 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4139 | else |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4140 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4141 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 4142 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 4143 | Py_UNICODE ch2; |
| 4144 | Py_UCS4 ucs; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4145 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4146 | ch2 = *s++; |
| 4147 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 4148 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4149 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 4150 | *p++ = '\\'; |
| 4151 | *p++ = 'U'; |
| 4152 | *p++ = hexdigits[(ucs >> 28) & 0xf]; |
| 4153 | *p++ = hexdigits[(ucs >> 24) & 0xf]; |
| 4154 | *p++ = hexdigits[(ucs >> 20) & 0xf]; |
| 4155 | *p++ = hexdigits[(ucs >> 16) & 0xf]; |
| 4156 | *p++ = hexdigits[(ucs >> 12) & 0xf]; |
| 4157 | *p++ = hexdigits[(ucs >> 8) & 0xf]; |
| 4158 | *p++ = hexdigits[(ucs >> 4) & 0xf]; |
| 4159 | *p++ = hexdigits[ucs & 0xf]; |
| 4160 | continue; |
| 4161 | } |
| 4162 | /* Fall through: isolated surrogates are copied as-is */ |
| 4163 | s--; |
| 4164 | size++; |
| 4165 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4166 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4167 | /* Map 16-bit characters to '\uxxxx' */ |
| 4168 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4169 | *p++ = '\\'; |
| 4170 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 4171 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 4172 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 4173 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 4174 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4175 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4176 | /* Copy everything else as-is */ |
| 4177 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4178 | *p++ = (char) ch; |
| 4179 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4180 | size = p - q; |
| 4181 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4182 | assert(size > 0); |
| 4183 | if (_PyBytes_Resize(&repr, size) < 0) |
| 4184 | return NULL; |
| 4185 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4186 | } |
| 4187 | |
| 4188 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 4189 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 4190 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4191 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 4192 | PyErr_BadArgument(); |
| 4193 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4194 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 4195 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 4196 | PyUnicode_GET_SIZE(unicode)); |
| 4197 | |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 4198 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4199 | } |
| 4200 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4201 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 4202 | |
| 4203 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4204 | Py_ssize_t size, |
| 4205 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4206 | { |
| 4207 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4208 | Py_ssize_t startinpos; |
| 4209 | Py_ssize_t endinpos; |
| 4210 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4211 | PyUnicodeObject *v; |
| 4212 | Py_UNICODE *p; |
| 4213 | const char *end; |
| 4214 | const char *reason; |
| 4215 | PyObject *errorHandler = NULL; |
| 4216 | PyObject *exc = NULL; |
| 4217 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 4218 | #ifdef Py_UNICODE_WIDE |
| 4219 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 4220 | #endif |
| 4221 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4222 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4223 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 4224 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4225 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4226 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4227 | return (PyObject *)v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4228 | p = PyUnicode_AS_UNICODE(v); |
| 4229 | end = s + size; |
| 4230 | |
| 4231 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4232 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4233 | /* We have to sanity check the raw data, otherwise doom looms for |
| 4234 | some malformed UCS-4 data. */ |
| 4235 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4236 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4237 | *p > unimax || *p < 0 || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4238 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4239 | end-s < Py_UNICODE_SIZE |
| 4240 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4241 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4242 | startinpos = s - starts; |
| 4243 | if (end-s < Py_UNICODE_SIZE) { |
| 4244 | endinpos = end-starts; |
| 4245 | reason = "truncated input"; |
| 4246 | } |
| 4247 | else { |
| 4248 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 4249 | reason = "illegal code point (> 0x10FFFF)"; |
| 4250 | } |
| 4251 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 4252 | if (unicode_decode_call_errorhandler( |
| 4253 | errors, &errorHandler, |
| 4254 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4255 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 4256 | &v, &outpos, &p)) { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4257 | goto onError; |
| 4258 | } |
| 4259 | } |
| 4260 | else { |
| 4261 | p++; |
| 4262 | s += Py_UNICODE_SIZE; |
| 4263 | } |
| 4264 | } |
| 4265 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4266 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4267 | goto onError; |
| 4268 | Py_XDECREF(errorHandler); |
| 4269 | Py_XDECREF(exc); |
| 4270 | return (PyObject *)v; |
| 4271 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4272 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4273 | Py_XDECREF(v); |
| 4274 | Py_XDECREF(errorHandler); |
| 4275 | Py_XDECREF(exc); |
| 4276 | return NULL; |
| 4277 | } |
| 4278 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4279 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 4280 | |
| 4281 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4282 | Py_ssize_t size, |
| 4283 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4284 | { |
| 4285 | PyUnicodeObject *v; |
| 4286 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4287 | const char *e, *unrolled_end; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4288 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4289 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4290 | if (size == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4291 | Py_UNICODE r = *(unsigned char*)s; |
| 4292 | return PyUnicode_FromUnicode(&r, 1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4293 | } |
| 4294 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4295 | v = _PyUnicode_New(size); |
| 4296 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4297 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4298 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4299 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4300 | p = PyUnicode_AS_UNICODE(v); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4301 | e = s + size; |
| 4302 | /* Unrolling the copy makes it much faster by reducing the looping |
| 4303 | overhead. This is similar to what many memcpy() implementations do. */ |
| 4304 | unrolled_end = e - 4; |
| 4305 | while (s < unrolled_end) { |
| 4306 | p[0] = (unsigned char) s[0]; |
| 4307 | p[1] = (unsigned char) s[1]; |
| 4308 | p[2] = (unsigned char) s[2]; |
| 4309 | p[3] = (unsigned char) s[3]; |
| 4310 | s += 4; |
| 4311 | p += 4; |
| 4312 | } |
| 4313 | while (s < e) |
| 4314 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4315 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4316 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4317 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4318 | Py_XDECREF(v); |
| 4319 | return NULL; |
| 4320 | } |
| 4321 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4322 | /* create or adjust a UnicodeEncodeError */ |
| 4323 | static void make_encode_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4324 | const char *encoding, |
| 4325 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4326 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4327 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4328 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4329 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4330 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 4331 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4332 | } |
| 4333 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4334 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 4335 | goto onError; |
| 4336 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 4337 | goto onError; |
| 4338 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 4339 | goto onError; |
| 4340 | return; |
| 4341 | onError: |
| 4342 | Py_DECREF(*exceptionObject); |
| 4343 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4344 | } |
| 4345 | } |
| 4346 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4347 | /* raises a UnicodeEncodeError */ |
| 4348 | static void raise_encode_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4349 | const char *encoding, |
| 4350 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4351 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4352 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4353 | { |
| 4354 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4355 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4356 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4357 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4358 | } |
| 4359 | |
| 4360 | /* error handling callback helper: |
| 4361 | build arguments, call the callback and check the arguments, |
| 4362 | put the result into newpos and return the replacement string, which |
| 4363 | has to be freed by the caller */ |
| 4364 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4365 | PyObject **errorHandler, |
| 4366 | const char *encoding, const char *reason, |
| 4367 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 4368 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4369 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4370 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4371 | 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] | 4372 | |
| 4373 | PyObject *restuple; |
| 4374 | PyObject *resunicode; |
| 4375 | |
| 4376 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4377 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4378 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4379 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4380 | } |
| 4381 | |
| 4382 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4383 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4384 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4385 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4386 | |
| 4387 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4388 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4389 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4390 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4391 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4392 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4393 | Py_DECREF(restuple); |
| 4394 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4395 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4396 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4397 | &resunicode, newpos)) { |
| 4398 | Py_DECREF(restuple); |
| 4399 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4400 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4401 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 4402 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 4403 | Py_DECREF(restuple); |
| 4404 | return NULL; |
| 4405 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4406 | if (*newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4407 | *newpos = size+*newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4408 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4409 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 4410 | Py_DECREF(restuple); |
| 4411 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4412 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4413 | Py_INCREF(resunicode); |
| 4414 | Py_DECREF(restuple); |
| 4415 | return resunicode; |
| 4416 | } |
| 4417 | |
| 4418 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4419 | Py_ssize_t size, |
| 4420 | const char *errors, |
| 4421 | int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4422 | { |
| 4423 | /* output object */ |
| 4424 | PyObject *res; |
| 4425 | /* pointers to the beginning and end+1 of input */ |
| 4426 | const Py_UNICODE *startp = p; |
| 4427 | const Py_UNICODE *endp = p + size; |
| 4428 | /* pointer to the beginning of the unencodable characters */ |
| 4429 | /* const Py_UNICODE *badp = NULL; */ |
| 4430 | /* pointer into the output */ |
| 4431 | char *str; |
| 4432 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4433 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4434 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 4435 | 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] | 4436 | PyObject *errorHandler = NULL; |
| 4437 | PyObject *exc = NULL; |
| 4438 | /* the following variable is used for caching string comparisons |
| 4439 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 4440 | int known_errorHandler = -1; |
| 4441 | |
| 4442 | /* allocate enough for a simple encoding without |
| 4443 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4444 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4445 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4446 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4447 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4448 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4449 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4450 | ressize = size; |
| 4451 | |
| 4452 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4453 | Py_UNICODE c = *p; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4454 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4455 | /* can we encode this? */ |
| 4456 | if (c<limit) { |
| 4457 | /* no overflow check, because we know that the space is enough */ |
| 4458 | *str++ = (char)c; |
| 4459 | ++p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4460 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4461 | else { |
| 4462 | Py_ssize_t unicodepos = p-startp; |
| 4463 | Py_ssize_t requiredsize; |
| 4464 | PyObject *repunicode; |
| 4465 | Py_ssize_t repsize; |
| 4466 | Py_ssize_t newpos; |
| 4467 | Py_ssize_t respos; |
| 4468 | Py_UNICODE *uni2; |
| 4469 | /* startpos for collecting unencodable chars */ |
| 4470 | const Py_UNICODE *collstart = p; |
| 4471 | const Py_UNICODE *collend = p; |
| 4472 | /* find all unecodable characters */ |
| 4473 | while ((collend < endp) && ((*collend)>=limit)) |
| 4474 | ++collend; |
| 4475 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 4476 | if (known_errorHandler==-1) { |
| 4477 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4478 | known_errorHandler = 1; |
| 4479 | else if (!strcmp(errors, "replace")) |
| 4480 | known_errorHandler = 2; |
| 4481 | else if (!strcmp(errors, "ignore")) |
| 4482 | known_errorHandler = 3; |
| 4483 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4484 | known_errorHandler = 4; |
| 4485 | else |
| 4486 | known_errorHandler = 0; |
| 4487 | } |
| 4488 | switch (known_errorHandler) { |
| 4489 | case 1: /* strict */ |
| 4490 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 4491 | goto onError; |
| 4492 | case 2: /* replace */ |
| 4493 | while (collstart++<collend) |
| 4494 | *str++ = '?'; /* fall through */ |
| 4495 | case 3: /* ignore */ |
| 4496 | p = collend; |
| 4497 | break; |
| 4498 | case 4: /* xmlcharrefreplace */ |
| 4499 | respos = str - PyBytes_AS_STRING(res); |
| 4500 | /* determine replacement size (temporarily (mis)uses p) */ |
| 4501 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 4502 | if (*p<10) |
| 4503 | repsize += 2+1+1; |
| 4504 | else if (*p<100) |
| 4505 | repsize += 2+2+1; |
| 4506 | else if (*p<1000) |
| 4507 | repsize += 2+3+1; |
| 4508 | else if (*p<10000) |
| 4509 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 4510 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4511 | else |
| 4512 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 4513 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4514 | else if (*p<100000) |
| 4515 | repsize += 2+5+1; |
| 4516 | else if (*p<1000000) |
| 4517 | repsize += 2+6+1; |
| 4518 | else |
| 4519 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4520 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4521 | } |
| 4522 | requiredsize = respos+repsize+(endp-collend); |
| 4523 | if (requiredsize > ressize) { |
| 4524 | if (requiredsize<2*ressize) |
| 4525 | requiredsize = 2*ressize; |
| 4526 | if (_PyBytes_Resize(&res, requiredsize)) |
| 4527 | goto onError; |
| 4528 | str = PyBytes_AS_STRING(res) + respos; |
| 4529 | ressize = requiredsize; |
| 4530 | } |
| 4531 | /* generate replacement (temporarily (mis)uses p) */ |
| 4532 | for (p = collstart; p < collend; ++p) { |
| 4533 | str += sprintf(str, "&#%d;", (int)*p); |
| 4534 | } |
| 4535 | p = collend; |
| 4536 | break; |
| 4537 | default: |
| 4538 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 4539 | encoding, reason, startp, size, &exc, |
| 4540 | collstart-startp, collend-startp, &newpos); |
| 4541 | if (repunicode == NULL) |
| 4542 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4543 | if (PyBytes_Check(repunicode)) { |
| 4544 | /* Directly copy bytes result to output. */ |
| 4545 | repsize = PyBytes_Size(repunicode); |
| 4546 | if (repsize > 1) { |
| 4547 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 4548 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4549 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 4550 | Py_DECREF(repunicode); |
| 4551 | goto onError; |
| 4552 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 4553 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4554 | ressize += repsize-1; |
| 4555 | } |
| 4556 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 4557 | str += repsize; |
| 4558 | p = startp + newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4559 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4560 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4561 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4562 | /* need more space? (at least enough for what we |
| 4563 | have+the replacement+the rest of the string, so |
| 4564 | we won't have to check space for encodable characters) */ |
| 4565 | respos = str - PyBytes_AS_STRING(res); |
| 4566 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4567 | requiredsize = respos+repsize+(endp-collend); |
| 4568 | if (requiredsize > ressize) { |
| 4569 | if (requiredsize<2*ressize) |
| 4570 | requiredsize = 2*ressize; |
| 4571 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 4572 | Py_DECREF(repunicode); |
| 4573 | goto onError; |
| 4574 | } |
| 4575 | str = PyBytes_AS_STRING(res) + respos; |
| 4576 | ressize = requiredsize; |
| 4577 | } |
| 4578 | /* check if there is anything unencodable in the replacement |
| 4579 | and copy it to the output */ |
| 4580 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 4581 | c = *uni2; |
| 4582 | if (c >= limit) { |
| 4583 | raise_encode_exception(&exc, encoding, startp, size, |
| 4584 | unicodepos, unicodepos+1, reason); |
| 4585 | Py_DECREF(repunicode); |
| 4586 | goto onError; |
| 4587 | } |
| 4588 | *str = (char)c; |
| 4589 | } |
| 4590 | p = startp + newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4591 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4592 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4593 | } |
| 4594 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4595 | /* Resize if we allocated to much */ |
| 4596 | size = str - PyBytes_AS_STRING(res); |
| 4597 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 4598 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4599 | if (_PyBytes_Resize(&res, size) < 0) |
| 4600 | goto onError; |
| 4601 | } |
| 4602 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4603 | Py_XDECREF(errorHandler); |
| 4604 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4605 | return res; |
| 4606 | |
| 4607 | onError: |
| 4608 | Py_XDECREF(res); |
| 4609 | Py_XDECREF(errorHandler); |
| 4610 | Py_XDECREF(exc); |
| 4611 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4612 | } |
| 4613 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4614 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4615 | Py_ssize_t size, |
| 4616 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4617 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4618 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4619 | } |
| 4620 | |
| 4621 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 4622 | { |
| 4623 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4624 | PyErr_BadArgument(); |
| 4625 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4626 | } |
| 4627 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4628 | PyUnicode_GET_SIZE(unicode), |
| 4629 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4630 | } |
| 4631 | |
| 4632 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 4633 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4634 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4635 | Py_ssize_t size, |
| 4636 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4637 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4638 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4639 | PyUnicodeObject *v; |
| 4640 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4641 | Py_ssize_t startinpos; |
| 4642 | Py_ssize_t endinpos; |
| 4643 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4644 | const char *e; |
| 4645 | PyObject *errorHandler = NULL; |
| 4646 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4647 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4648 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4649 | if (size == 1 && *(unsigned char*)s < 128) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4650 | Py_UNICODE r = *(unsigned char*)s; |
| 4651 | return PyUnicode_FromUnicode(&r, 1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4652 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4653 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4654 | v = _PyUnicode_New(size); |
| 4655 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4656 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4657 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4658 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4659 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4660 | e = s + size; |
| 4661 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4662 | register unsigned char c = (unsigned char)*s; |
| 4663 | if (c < 128) { |
| 4664 | *p++ = c; |
| 4665 | ++s; |
| 4666 | } |
| 4667 | else { |
| 4668 | startinpos = s-starts; |
| 4669 | endinpos = startinpos + 1; |
| 4670 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
| 4671 | if (unicode_decode_call_errorhandler( |
| 4672 | errors, &errorHandler, |
| 4673 | "ascii", "ordinal not in range(128)", |
| 4674 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 4675 | &v, &outpos, &p)) |
| 4676 | goto onError; |
| 4677 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4678 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 4679 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4680 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 4681 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4682 | Py_XDECREF(errorHandler); |
| 4683 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4684 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4685 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4686 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4687 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4688 | Py_XDECREF(errorHandler); |
| 4689 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4690 | return NULL; |
| 4691 | } |
| 4692 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4693 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4694 | Py_ssize_t size, |
| 4695 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4696 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4697 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4698 | } |
| 4699 | |
| 4700 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 4701 | { |
| 4702 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4703 | PyErr_BadArgument(); |
| 4704 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4705 | } |
| 4706 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4707 | PyUnicode_GET_SIZE(unicode), |
| 4708 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4709 | } |
| 4710 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4711 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4712 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4713 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4714 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 4715 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4716 | #define NEED_RETRY |
| 4717 | #endif |
| 4718 | |
| 4719 | /* XXX This code is limited to "true" double-byte encodings, as |
| 4720 | a) it assumes an incomplete character consists of a single byte, and |
| 4721 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4722 | encodings, see IsDBCSLeadByteEx documentation. */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4723 | |
| 4724 | static int is_dbcs_lead_byte(const char *s, int offset) |
| 4725 | { |
| 4726 | const char *curr = s + offset; |
| 4727 | |
| 4728 | if (IsDBCSLeadByte(*curr)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4729 | const char *prev = CharPrev(s, curr); |
| 4730 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4731 | } |
| 4732 | return 0; |
| 4733 | } |
| 4734 | |
| 4735 | /* |
| 4736 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 4737 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 4738 | */ |
| 4739 | static int decode_mbcs(PyUnicodeObject **v, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4740 | const char *s, /* MBCS string */ |
| 4741 | int size, /* sizeof MBCS string */ |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4742 | int final, |
| 4743 | const char *errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4744 | { |
| 4745 | Py_UNICODE *p; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4746 | Py_ssize_t n; |
| 4747 | DWORD usize; |
| 4748 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4749 | |
| 4750 | assert(size >= 0); |
| 4751 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4752 | /* check and handle 'errors' arg */ |
| 4753 | if (errors==NULL || strcmp(errors, "strict")==0) |
| 4754 | flags = MB_ERR_INVALID_CHARS; |
| 4755 | else if (strcmp(errors, "ignore")==0) |
| 4756 | flags = 0; |
| 4757 | else { |
| 4758 | PyErr_Format(PyExc_ValueError, |
| 4759 | "mbcs encoding does not support errors='%s'", |
| 4760 | errors); |
| 4761 | return -1; |
| 4762 | } |
| 4763 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4764 | /* Skip trailing lead-byte unless 'final' is set */ |
| 4765 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4766 | --size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4767 | |
| 4768 | /* First get the size of the result */ |
| 4769 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4770 | usize = MultiByteToWideChar(CP_ACP, flags, s, size, NULL, 0); |
| 4771 | if (usize==0) |
| 4772 | goto mbcs_decode_error; |
| 4773 | } else |
| 4774 | usize = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4775 | |
| 4776 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4777 | /* Create unicode object */ |
| 4778 | *v = _PyUnicode_New(usize); |
| 4779 | if (*v == NULL) |
| 4780 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4781 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4782 | } |
| 4783 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4784 | /* Extend unicode object */ |
| 4785 | n = PyUnicode_GET_SIZE(*v); |
| 4786 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 4787 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4788 | } |
| 4789 | |
| 4790 | /* Do the conversion */ |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4791 | if (usize > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4792 | p = PyUnicode_AS_UNICODE(*v) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4793 | if (0 == MultiByteToWideChar(CP_ACP, flags, s, size, p, usize)) { |
| 4794 | goto mbcs_decode_error; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4795 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4796 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4797 | return size; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4798 | |
| 4799 | mbcs_decode_error: |
| 4800 | /* If the last error was ERROR_NO_UNICODE_TRANSLATION, then |
| 4801 | we raise a UnicodeDecodeError - else it is a 'generic' |
| 4802 | windows error |
| 4803 | */ |
| 4804 | if (GetLastError()==ERROR_NO_UNICODE_TRANSLATION) { |
| 4805 | /* Ideally, we should get reason from FormatMessage - this |
| 4806 | is the Windows 2000 English version of the message |
| 4807 | */ |
| 4808 | PyObject *exc = NULL; |
| 4809 | const char *reason = "No mapping for the Unicode character exists " |
| 4810 | "in the target multi-byte code page."; |
| 4811 | make_decode_exception(&exc, "mbcs", s, size, 0, 0, reason); |
| 4812 | if (exc != NULL) { |
| 4813 | PyCodec_StrictErrors(exc); |
| 4814 | Py_DECREF(exc); |
| 4815 | } |
| 4816 | } else { |
| 4817 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4818 | } |
| 4819 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4820 | } |
| 4821 | |
| 4822 | PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4823 | Py_ssize_t size, |
| 4824 | const char *errors, |
| 4825 | Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4826 | { |
| 4827 | PyUnicodeObject *v = NULL; |
| 4828 | int done; |
| 4829 | |
| 4830 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4831 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4832 | |
| 4833 | #ifdef NEED_RETRY |
| 4834 | retry: |
| 4835 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4836 | done = decode_mbcs(&v, s, INT_MAX, 0, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4837 | else |
| 4838 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4839 | done = decode_mbcs(&v, s, (int)size, !consumed, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4840 | |
| 4841 | if (done < 0) { |
| 4842 | Py_XDECREF(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4843 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4844 | } |
| 4845 | |
| 4846 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4847 | *consumed += done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4848 | |
| 4849 | #ifdef NEED_RETRY |
| 4850 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4851 | s += done; |
| 4852 | size -= done; |
| 4853 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4854 | } |
| 4855 | #endif |
| 4856 | |
| 4857 | return (PyObject *)v; |
| 4858 | } |
| 4859 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4860 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4861 | Py_ssize_t size, |
| 4862 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4863 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4864 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 4865 | } |
| 4866 | |
| 4867 | /* |
| 4868 | * Convert unicode into string object (MBCS). |
| 4869 | * Returns 0 if succeed, -1 otherwise. |
| 4870 | */ |
| 4871 | static int encode_mbcs(PyObject **repr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4872 | const Py_UNICODE *p, /* unicode */ |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4873 | int size, /* size of unicode */ |
| 4874 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4875 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4876 | BOOL usedDefaultChar = FALSE; |
| 4877 | BOOL *pusedDefaultChar; |
| 4878 | int mbcssize; |
| 4879 | Py_ssize_t n; |
| 4880 | PyObject *exc = NULL; |
| 4881 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4882 | |
| 4883 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4884 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4885 | /* check and handle 'errors' arg */ |
| 4886 | if (errors==NULL || strcmp(errors, "strict")==0) { |
| 4887 | flags = WC_NO_BEST_FIT_CHARS; |
| 4888 | pusedDefaultChar = &usedDefaultChar; |
| 4889 | } else if (strcmp(errors, "replace")==0) { |
| 4890 | flags = 0; |
| 4891 | pusedDefaultChar = NULL; |
| 4892 | } else { |
| 4893 | PyErr_Format(PyExc_ValueError, |
| 4894 | "mbcs encoding does not support errors='%s'", |
| 4895 | errors); |
| 4896 | return -1; |
| 4897 | } |
| 4898 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4899 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4900 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4901 | mbcssize = WideCharToMultiByte(CP_ACP, flags, p, size, NULL, 0, |
| 4902 | NULL, pusedDefaultChar); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4903 | if (mbcssize == 0) { |
| 4904 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4905 | return -1; |
| 4906 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4907 | /* If we used a default char, then we failed! */ |
| 4908 | if (pusedDefaultChar && *pusedDefaultChar) |
| 4909 | goto mbcs_encode_error; |
| 4910 | } else { |
| 4911 | mbcssize = 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4912 | } |
| 4913 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4914 | if (*repr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4915 | /* Create string object */ |
| 4916 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); |
| 4917 | if (*repr == NULL) |
| 4918 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4919 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4920 | } |
| 4921 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4922 | /* Extend string object */ |
| 4923 | n = PyBytes_Size(*repr); |
| 4924 | if (_PyBytes_Resize(repr, n + mbcssize) < 0) |
| 4925 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4926 | } |
| 4927 | |
| 4928 | /* Do the conversion */ |
| 4929 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4930 | char *s = PyBytes_AS_STRING(*repr) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4931 | if (0 == WideCharToMultiByte(CP_ACP, flags, p, size, s, mbcssize, |
| 4932 | NULL, pusedDefaultChar)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4933 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4934 | return -1; |
| 4935 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4936 | if (pusedDefaultChar && *pusedDefaultChar) |
| 4937 | goto mbcs_encode_error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4938 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4939 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4940 | |
| 4941 | mbcs_encode_error: |
| 4942 | raise_encode_exception(&exc, "mbcs", p, size, 0, 0, "invalid character"); |
| 4943 | Py_XDECREF(exc); |
| 4944 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4945 | } |
| 4946 | |
| 4947 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4948 | Py_ssize_t size, |
| 4949 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4950 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4951 | PyObject *repr = NULL; |
| 4952 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 4953 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4954 | #ifdef NEED_RETRY |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4955 | retry: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4956 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4957 | ret = encode_mbcs(&repr, p, INT_MAX, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4958 | else |
| 4959 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4960 | ret = encode_mbcs(&repr, p, (int)size, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4961 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4962 | if (ret < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4963 | Py_XDECREF(repr); |
| 4964 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4965 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4966 | |
| 4967 | #ifdef NEED_RETRY |
| 4968 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4969 | p += INT_MAX; |
| 4970 | size -= INT_MAX; |
| 4971 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4972 | } |
| 4973 | #endif |
| 4974 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4975 | return repr; |
| 4976 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4977 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 4978 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 4979 | { |
| 4980 | if (!PyUnicode_Check(unicode)) { |
| 4981 | PyErr_BadArgument(); |
| 4982 | return NULL; |
| 4983 | } |
| 4984 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4985 | PyUnicode_GET_SIZE(unicode), |
| 4986 | NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 4987 | } |
| 4988 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4989 | #undef NEED_RETRY |
| 4990 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4991 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4992 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4993 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 4994 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4995 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4996 | Py_ssize_t size, |
| 4997 | PyObject *mapping, |
| 4998 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4999 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5000 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5001 | Py_ssize_t startinpos; |
| 5002 | Py_ssize_t endinpos; |
| 5003 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5004 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5005 | PyUnicodeObject *v; |
| 5006 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5007 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5008 | PyObject *errorHandler = NULL; |
| 5009 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 5010 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5011 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5012 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5013 | /* Default to Latin-1 */ |
| 5014 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5015 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5016 | |
| 5017 | v = _PyUnicode_New(size); |
| 5018 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5019 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5020 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5021 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5022 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5023 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 5024 | if (PyUnicode_CheckExact(mapping)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5025 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 5026 | maplen = PyUnicode_GET_SIZE(mapping); |
| 5027 | while (s < e) { |
| 5028 | unsigned char ch = *s; |
| 5029 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5030 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5031 | if (ch < maplen) |
| 5032 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5033 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5034 | if (x == 0xfffe) { |
| 5035 | /* undefined mapping */ |
| 5036 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 5037 | startinpos = s-starts; |
| 5038 | endinpos = startinpos+1; |
| 5039 | if (unicode_decode_call_errorhandler( |
| 5040 | errors, &errorHandler, |
| 5041 | "charmap", "character maps to <undefined>", |
| 5042 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 5043 | &v, &outpos, &p)) { |
| 5044 | goto onError; |
| 5045 | } |
| 5046 | continue; |
| 5047 | } |
| 5048 | *p++ = x; |
| 5049 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5050 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 5051 | } |
| 5052 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5053 | while (s < e) { |
| 5054 | unsigned char ch = *s; |
| 5055 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 5056 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5057 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 5058 | w = PyLong_FromLong((long)ch); |
| 5059 | if (w == NULL) |
| 5060 | goto onError; |
| 5061 | x = PyObject_GetItem(mapping, w); |
| 5062 | Py_DECREF(w); |
| 5063 | if (x == NULL) { |
| 5064 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 5065 | /* No mapping found means: mapping is undefined. */ |
| 5066 | PyErr_Clear(); |
| 5067 | x = Py_None; |
| 5068 | Py_INCREF(x); |
| 5069 | } else |
| 5070 | goto onError; |
| 5071 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5072 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5073 | /* Apply mapping */ |
| 5074 | if (PyLong_Check(x)) { |
| 5075 | long value = PyLong_AS_LONG(x); |
| 5076 | if (value < 0 || value > 65535) { |
| 5077 | PyErr_SetString(PyExc_TypeError, |
| 5078 | "character mapping must be in range(65536)"); |
| 5079 | Py_DECREF(x); |
| 5080 | goto onError; |
| 5081 | } |
| 5082 | *p++ = (Py_UNICODE)value; |
| 5083 | } |
| 5084 | else if (x == Py_None) { |
| 5085 | /* undefined mapping */ |
| 5086 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 5087 | startinpos = s-starts; |
| 5088 | endinpos = startinpos+1; |
| 5089 | if (unicode_decode_call_errorhandler( |
| 5090 | errors, &errorHandler, |
| 5091 | "charmap", "character maps to <undefined>", |
| 5092 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 5093 | &v, &outpos, &p)) { |
| 5094 | Py_DECREF(x); |
| 5095 | goto onError; |
| 5096 | } |
| 5097 | Py_DECREF(x); |
| 5098 | continue; |
| 5099 | } |
| 5100 | else if (PyUnicode_Check(x)) { |
| 5101 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5102 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5103 | if (targetsize == 1) |
| 5104 | /* 1-1 mapping */ |
| 5105 | *p++ = *PyUnicode_AS_UNICODE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5106 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5107 | else if (targetsize > 1) { |
| 5108 | /* 1-n mapping */ |
| 5109 | if (targetsize > extrachars) { |
| 5110 | /* resize first */ |
| 5111 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 5112 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 5113 | (targetsize << 2); |
| 5114 | extrachars += needed; |
| 5115 | /* XXX overflow detection missing */ |
| 5116 | if (_PyUnicode_Resize(&v, |
| 5117 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 5118 | Py_DECREF(x); |
| 5119 | goto onError; |
| 5120 | } |
| 5121 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 5122 | } |
| 5123 | Py_UNICODE_COPY(p, |
| 5124 | PyUnicode_AS_UNICODE(x), |
| 5125 | targetsize); |
| 5126 | p += targetsize; |
| 5127 | extrachars -= targetsize; |
| 5128 | } |
| 5129 | /* 1-0 mapping: skip the character */ |
| 5130 | } |
| 5131 | else { |
| 5132 | /* wrong return value */ |
| 5133 | PyErr_SetString(PyExc_TypeError, |
| 5134 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5135 | Py_DECREF(x); |
| 5136 | goto onError; |
| 5137 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5138 | Py_DECREF(x); |
| 5139 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5140 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5141 | } |
| 5142 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5143 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 5144 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5145 | Py_XDECREF(errorHandler); |
| 5146 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5147 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5148 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5149 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5150 | Py_XDECREF(errorHandler); |
| 5151 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5152 | Py_XDECREF(v); |
| 5153 | return NULL; |
| 5154 | } |
| 5155 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5156 | /* Charmap encoding: the lookup table */ |
| 5157 | |
| 5158 | struct encoding_map{ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5159 | PyObject_HEAD |
| 5160 | unsigned char level1[32]; |
| 5161 | int count2, count3; |
| 5162 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5163 | }; |
| 5164 | |
| 5165 | static PyObject* |
| 5166 | encoding_map_size(PyObject *obj, PyObject* args) |
| 5167 | { |
| 5168 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5169 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5170 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5171 | } |
| 5172 | |
| 5173 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5174 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5175 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 5176 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5177 | }; |
| 5178 | |
| 5179 | static void |
| 5180 | encoding_map_dealloc(PyObject* o) |
| 5181 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5182 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5183 | } |
| 5184 | |
| 5185 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5186 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5187 | "EncodingMap", /*tp_name*/ |
| 5188 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 5189 | 0, /*tp_itemsize*/ |
| 5190 | /* methods */ |
| 5191 | encoding_map_dealloc, /*tp_dealloc*/ |
| 5192 | 0, /*tp_print*/ |
| 5193 | 0, /*tp_getattr*/ |
| 5194 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 5195 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5196 | 0, /*tp_repr*/ |
| 5197 | 0, /*tp_as_number*/ |
| 5198 | 0, /*tp_as_sequence*/ |
| 5199 | 0, /*tp_as_mapping*/ |
| 5200 | 0, /*tp_hash*/ |
| 5201 | 0, /*tp_call*/ |
| 5202 | 0, /*tp_str*/ |
| 5203 | 0, /*tp_getattro*/ |
| 5204 | 0, /*tp_setattro*/ |
| 5205 | 0, /*tp_as_buffer*/ |
| 5206 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 5207 | 0, /*tp_doc*/ |
| 5208 | 0, /*tp_traverse*/ |
| 5209 | 0, /*tp_clear*/ |
| 5210 | 0, /*tp_richcompare*/ |
| 5211 | 0, /*tp_weaklistoffset*/ |
| 5212 | 0, /*tp_iter*/ |
| 5213 | 0, /*tp_iternext*/ |
| 5214 | encoding_map_methods, /*tp_methods*/ |
| 5215 | 0, /*tp_members*/ |
| 5216 | 0, /*tp_getset*/ |
| 5217 | 0, /*tp_base*/ |
| 5218 | 0, /*tp_dict*/ |
| 5219 | 0, /*tp_descr_get*/ |
| 5220 | 0, /*tp_descr_set*/ |
| 5221 | 0, /*tp_dictoffset*/ |
| 5222 | 0, /*tp_init*/ |
| 5223 | 0, /*tp_alloc*/ |
| 5224 | 0, /*tp_new*/ |
| 5225 | 0, /*tp_free*/ |
| 5226 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5227 | }; |
| 5228 | |
| 5229 | PyObject* |
| 5230 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 5231 | { |
| 5232 | Py_UNICODE *decode; |
| 5233 | PyObject *result; |
| 5234 | struct encoding_map *mresult; |
| 5235 | int i; |
| 5236 | int need_dict = 0; |
| 5237 | unsigned char level1[32]; |
| 5238 | unsigned char level2[512]; |
| 5239 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 5240 | int count2 = 0, count3 = 0; |
| 5241 | |
| 5242 | if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { |
| 5243 | PyErr_BadArgument(); |
| 5244 | return NULL; |
| 5245 | } |
| 5246 | decode = PyUnicode_AS_UNICODE(string); |
| 5247 | memset(level1, 0xFF, sizeof level1); |
| 5248 | memset(level2, 0xFF, sizeof level2); |
| 5249 | |
| 5250 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 5251 | or if there are non-BMP characters, we need to use |
| 5252 | a mapping dictionary. */ |
| 5253 | if (decode[0] != 0) |
| 5254 | need_dict = 1; |
| 5255 | for (i = 1; i < 256; i++) { |
| 5256 | int l1, l2; |
| 5257 | if (decode[i] == 0 |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5258 | #ifdef Py_UNICODE_WIDE |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5259 | || decode[i] > 0xFFFF |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5260 | #endif |
| 5261 | ) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5262 | need_dict = 1; |
| 5263 | break; |
| 5264 | } |
| 5265 | if (decode[i] == 0xFFFE) |
| 5266 | /* unmapped character */ |
| 5267 | continue; |
| 5268 | l1 = decode[i] >> 11; |
| 5269 | l2 = decode[i] >> 7; |
| 5270 | if (level1[l1] == 0xFF) |
| 5271 | level1[l1] = count2++; |
| 5272 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5273 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5274 | } |
| 5275 | |
| 5276 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 5277 | need_dict = 1; |
| 5278 | |
| 5279 | if (need_dict) { |
| 5280 | PyObject *result = PyDict_New(); |
| 5281 | PyObject *key, *value; |
| 5282 | if (!result) |
| 5283 | return NULL; |
| 5284 | for (i = 0; i < 256; i++) { |
| 5285 | key = value = NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5286 | key = PyLong_FromLong(decode[i]); |
| 5287 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5288 | if (!key || !value) |
| 5289 | goto failed1; |
| 5290 | if (PyDict_SetItem(result, key, value) == -1) |
| 5291 | goto failed1; |
| 5292 | Py_DECREF(key); |
| 5293 | Py_DECREF(value); |
| 5294 | } |
| 5295 | return result; |
| 5296 | failed1: |
| 5297 | Py_XDECREF(key); |
| 5298 | Py_XDECREF(value); |
| 5299 | Py_DECREF(result); |
| 5300 | return NULL; |
| 5301 | } |
| 5302 | |
| 5303 | /* Create a three-level trie */ |
| 5304 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 5305 | 16*count2 + 128*count3 - 1); |
| 5306 | if (!result) |
| 5307 | return PyErr_NoMemory(); |
| 5308 | PyObject_Init(result, &EncodingMapType); |
| 5309 | mresult = (struct encoding_map*)result; |
| 5310 | mresult->count2 = count2; |
| 5311 | mresult->count3 = count3; |
| 5312 | mlevel1 = mresult->level1; |
| 5313 | mlevel2 = mresult->level23; |
| 5314 | mlevel3 = mresult->level23 + 16*count2; |
| 5315 | memcpy(mlevel1, level1, 32); |
| 5316 | memset(mlevel2, 0xFF, 16*count2); |
| 5317 | memset(mlevel3, 0, 128*count3); |
| 5318 | count3 = 0; |
| 5319 | for (i = 1; i < 256; i++) { |
| 5320 | int o1, o2, o3, i2, i3; |
| 5321 | if (decode[i] == 0xFFFE) |
| 5322 | /* unmapped character */ |
| 5323 | continue; |
| 5324 | o1 = decode[i]>>11; |
| 5325 | o2 = (decode[i]>>7) & 0xF; |
| 5326 | i2 = 16*mlevel1[o1] + o2; |
| 5327 | if (mlevel2[i2] == 0xFF) |
| 5328 | mlevel2[i2] = count3++; |
| 5329 | o3 = decode[i] & 0x7F; |
| 5330 | i3 = 128*mlevel2[i2] + o3; |
| 5331 | mlevel3[i3] = i; |
| 5332 | } |
| 5333 | return result; |
| 5334 | } |
| 5335 | |
| 5336 | static int |
| 5337 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 5338 | { |
| 5339 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 5340 | int l1 = c>>11; |
| 5341 | int l2 = (c>>7) & 0xF; |
| 5342 | int l3 = c & 0x7F; |
| 5343 | int i; |
| 5344 | |
| 5345 | #ifdef Py_UNICODE_WIDE |
| 5346 | if (c > 0xFFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5347 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5348 | } |
| 5349 | #endif |
| 5350 | if (c == 0) |
| 5351 | return 0; |
| 5352 | /* level 1*/ |
| 5353 | i = map->level1[l1]; |
| 5354 | if (i == 0xFF) { |
| 5355 | return -1; |
| 5356 | } |
| 5357 | /* level 2*/ |
| 5358 | i = map->level23[16*i+l2]; |
| 5359 | if (i == 0xFF) { |
| 5360 | return -1; |
| 5361 | } |
| 5362 | /* level 3 */ |
| 5363 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 5364 | if (i == 0) { |
| 5365 | return -1; |
| 5366 | } |
| 5367 | return i; |
| 5368 | } |
| 5369 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5370 | /* Lookup the character ch in the mapping. If the character |
| 5371 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 5372 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5373 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5374 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5375 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5376 | PyObject *x; |
| 5377 | |
| 5378 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5379 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5380 | x = PyObject_GetItem(mapping, w); |
| 5381 | Py_DECREF(w); |
| 5382 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5383 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 5384 | /* No mapping found means: mapping is undefined. */ |
| 5385 | PyErr_Clear(); |
| 5386 | x = Py_None; |
| 5387 | Py_INCREF(x); |
| 5388 | return x; |
| 5389 | } else |
| 5390 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5391 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 5392 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5393 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5394 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5395 | long value = PyLong_AS_LONG(x); |
| 5396 | if (value < 0 || value > 255) { |
| 5397 | PyErr_SetString(PyExc_TypeError, |
| 5398 | "character mapping must be in range(256)"); |
| 5399 | Py_DECREF(x); |
| 5400 | return NULL; |
| 5401 | } |
| 5402 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5403 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5404 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5405 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5406 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5407 | /* wrong return value */ |
| 5408 | PyErr_Format(PyExc_TypeError, |
| 5409 | "character mapping must return integer, bytes or None, not %.400s", |
| 5410 | x->ob_type->tp_name); |
| 5411 | Py_DECREF(x); |
| 5412 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5413 | } |
| 5414 | } |
| 5415 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5416 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5417 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5418 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5419 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 5420 | /* exponentially overallocate to minimize reallocations */ |
| 5421 | if (requiredsize < 2*outsize) |
| 5422 | requiredsize = 2*outsize; |
| 5423 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 5424 | return -1; |
| 5425 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5426 | } |
| 5427 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5428 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5429 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5430 | }charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5431 | /* 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] | 5432 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5433 | space is available. Return a new reference to the object that |
| 5434 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 5435 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 5436 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5437 | static |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5438 | charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5439 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5440 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5441 | PyObject *rep; |
| 5442 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5443 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5444 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5445 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5446 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5447 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5448 | if (res == -1) |
| 5449 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5450 | if (outsize<requiredsize) |
| 5451 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 5452 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5453 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5454 | outstart[(*outpos)++] = (char)res; |
| 5455 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5456 | } |
| 5457 | |
| 5458 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5459 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5460 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5461 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5462 | Py_DECREF(rep); |
| 5463 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5464 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5465 | if (PyLong_Check(rep)) { |
| 5466 | Py_ssize_t requiredsize = *outpos+1; |
| 5467 | if (outsize<requiredsize) |
| 5468 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 5469 | Py_DECREF(rep); |
| 5470 | return enc_EXCEPTION; |
| 5471 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5472 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5473 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5474 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5475 | else { |
| 5476 | const char *repchars = PyBytes_AS_STRING(rep); |
| 5477 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 5478 | Py_ssize_t requiredsize = *outpos+repsize; |
| 5479 | if (outsize<requiredsize) |
| 5480 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 5481 | Py_DECREF(rep); |
| 5482 | return enc_EXCEPTION; |
| 5483 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5484 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5485 | memcpy(outstart + *outpos, repchars, repsize); |
| 5486 | *outpos += repsize; |
| 5487 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5488 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5489 | Py_DECREF(rep); |
| 5490 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5491 | } |
| 5492 | |
| 5493 | /* handle an error in PyUnicode_EncodeCharmap |
| 5494 | Return 0 on success, -1 on error */ |
| 5495 | static |
| 5496 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5497 | 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] | 5498 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 5499 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5500 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5501 | { |
| 5502 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5503 | Py_ssize_t repsize; |
| 5504 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5505 | Py_UNICODE *uni2; |
| 5506 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5507 | Py_ssize_t collstartpos = *inpos; |
| 5508 | Py_ssize_t collendpos = *inpos+1; |
| 5509 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5510 | char *encoding = "charmap"; |
| 5511 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5512 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5513 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5514 | /* find all unencodable characters */ |
| 5515 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5516 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5517 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5518 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 5519 | if (res != -1) |
| 5520 | break; |
| 5521 | ++collendpos; |
| 5522 | continue; |
| 5523 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5524 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5525 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 5526 | if (rep==NULL) |
| 5527 | return -1; |
| 5528 | else if (rep!=Py_None) { |
| 5529 | Py_DECREF(rep); |
| 5530 | break; |
| 5531 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5532 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5533 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5534 | } |
| 5535 | /* cache callback name lookup |
| 5536 | * (if not done yet, i.e. it's the first error) */ |
| 5537 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5538 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5539 | *known_errorHandler = 1; |
| 5540 | else if (!strcmp(errors, "replace")) |
| 5541 | *known_errorHandler = 2; |
| 5542 | else if (!strcmp(errors, "ignore")) |
| 5543 | *known_errorHandler = 3; |
| 5544 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5545 | *known_errorHandler = 4; |
| 5546 | else |
| 5547 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5548 | } |
| 5549 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5550 | case 1: /* strict */ |
| 5551 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5552 | return -1; |
| 5553 | case 2: /* replace */ |
| 5554 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5555 | x = charmapencode_output('?', mapping, res, respos); |
| 5556 | if (x==enc_EXCEPTION) { |
| 5557 | return -1; |
| 5558 | } |
| 5559 | else if (x==enc_FAILED) { |
| 5560 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5561 | return -1; |
| 5562 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5563 | } |
| 5564 | /* fall through */ |
| 5565 | case 3: /* ignore */ |
| 5566 | *inpos = collendpos; |
| 5567 | break; |
| 5568 | case 4: /* xmlcharrefreplace */ |
| 5569 | /* generate replacement (temporarily (mis)uses p) */ |
| 5570 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5571 | char buffer[2+29+1+1]; |
| 5572 | char *cp; |
| 5573 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 5574 | for (cp = buffer; *cp; ++cp) { |
| 5575 | x = charmapencode_output(*cp, mapping, res, respos); |
| 5576 | if (x==enc_EXCEPTION) |
| 5577 | return -1; |
| 5578 | else if (x==enc_FAILED) { |
| 5579 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5580 | return -1; |
| 5581 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5582 | } |
| 5583 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5584 | *inpos = collendpos; |
| 5585 | break; |
| 5586 | default: |
| 5587 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5588 | encoding, reason, p, size, exceptionObject, |
| 5589 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5590 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5591 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5592 | if (PyBytes_Check(repunicode)) { |
| 5593 | /* Directly copy bytes result to output. */ |
| 5594 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 5595 | Py_ssize_t requiredsize; |
| 5596 | repsize = PyBytes_Size(repunicode); |
| 5597 | requiredsize = *respos + repsize; |
| 5598 | if (requiredsize > outsize) |
| 5599 | /* Make room for all additional bytes. */ |
| 5600 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 5601 | Py_DECREF(repunicode); |
| 5602 | return -1; |
| 5603 | } |
| 5604 | memcpy(PyBytes_AsString(*res) + *respos, |
| 5605 | PyBytes_AsString(repunicode), repsize); |
| 5606 | *respos += repsize; |
| 5607 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5608 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5609 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5610 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5611 | /* generate replacement */ |
| 5612 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5613 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5614 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 5615 | if (x==enc_EXCEPTION) { |
| 5616 | return -1; |
| 5617 | } |
| 5618 | else if (x==enc_FAILED) { |
| 5619 | Py_DECREF(repunicode); |
| 5620 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5621 | return -1; |
| 5622 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5623 | } |
| 5624 | *inpos = newpos; |
| 5625 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5626 | } |
| 5627 | return 0; |
| 5628 | } |
| 5629 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5630 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5631 | Py_ssize_t size, |
| 5632 | PyObject *mapping, |
| 5633 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5634 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5635 | /* output object */ |
| 5636 | PyObject *res = NULL; |
| 5637 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5638 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5639 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5640 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5641 | PyObject *errorHandler = NULL; |
| 5642 | PyObject *exc = NULL; |
| 5643 | /* the following variable is used for caching string comparisons |
| 5644 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 5645 | * 3=ignore, 4=xmlcharrefreplace */ |
| 5646 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5647 | |
| 5648 | /* Default to Latin-1 */ |
| 5649 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5650 | return PyUnicode_EncodeLatin1(p, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5651 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5652 | /* allocate enough for a simple encoding without |
| 5653 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5654 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5655 | if (res == NULL) |
| 5656 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 5657 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5658 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5659 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5660 | while (inpos<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5661 | /* try to encode it */ |
| 5662 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 5663 | if (x==enc_EXCEPTION) /* error */ |
| 5664 | goto onError; |
| 5665 | if (x==enc_FAILED) { /* unencodable character */ |
| 5666 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 5667 | &exc, |
| 5668 | &known_errorHandler, &errorHandler, errors, |
| 5669 | &res, &respos)) { |
| 5670 | goto onError; |
| 5671 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5672 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5673 | else |
| 5674 | /* done with this character => adjust input position */ |
| 5675 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5676 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5677 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5678 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5679 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5680 | if (_PyBytes_Resize(&res, respos) < 0) |
| 5681 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5682 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5683 | Py_XDECREF(exc); |
| 5684 | Py_XDECREF(errorHandler); |
| 5685 | return res; |
| 5686 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5687 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5688 | Py_XDECREF(res); |
| 5689 | Py_XDECREF(exc); |
| 5690 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5691 | return NULL; |
| 5692 | } |
| 5693 | |
| 5694 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5695 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5696 | { |
| 5697 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5698 | PyErr_BadArgument(); |
| 5699 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5700 | } |
| 5701 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5702 | PyUnicode_GET_SIZE(unicode), |
| 5703 | mapping, |
| 5704 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5705 | } |
| 5706 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5707 | /* create or adjust a UnicodeTranslateError */ |
| 5708 | static void make_translate_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5709 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5710 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5711 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5712 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5713 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5714 | *exceptionObject = PyUnicodeTranslateError_Create( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5715 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5716 | } |
| 5717 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5718 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 5719 | goto onError; |
| 5720 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 5721 | goto onError; |
| 5722 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 5723 | goto onError; |
| 5724 | return; |
| 5725 | onError: |
| 5726 | Py_DECREF(*exceptionObject); |
| 5727 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5728 | } |
| 5729 | } |
| 5730 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5731 | /* raises a UnicodeTranslateError */ |
| 5732 | static void raise_translate_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5733 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5734 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5735 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5736 | { |
| 5737 | make_translate_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5738 | unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5739 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5740 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5741 | } |
| 5742 | |
| 5743 | /* error handling callback helper: |
| 5744 | build arguments, call the callback and check the arguments, |
| 5745 | put the result into newpos and return the replacement string, which |
| 5746 | has to be freed by the caller */ |
| 5747 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5748 | PyObject **errorHandler, |
| 5749 | const char *reason, |
| 5750 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 5751 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5752 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5753 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 5754 | 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] | 5755 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5756 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5757 | PyObject *restuple; |
| 5758 | PyObject *resunicode; |
| 5759 | |
| 5760 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5761 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5762 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5763 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5764 | } |
| 5765 | |
| 5766 | make_translate_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5767 | unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5768 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5769 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5770 | |
| 5771 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5772 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5773 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5774 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5775 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 5776 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5777 | Py_DECREF(restuple); |
| 5778 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5779 | } |
| 5780 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5781 | &resunicode, &i_newpos)) { |
| 5782 | Py_DECREF(restuple); |
| 5783 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5784 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5785 | if (i_newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5786 | *newpos = size+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5787 | else |
| 5788 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5789 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5790 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 5791 | Py_DECREF(restuple); |
| 5792 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5793 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5794 | Py_INCREF(resunicode); |
| 5795 | Py_DECREF(restuple); |
| 5796 | return resunicode; |
| 5797 | } |
| 5798 | |
| 5799 | /* Lookup the character ch in the mapping and put the result in result, |
| 5800 | which must be decrefed by the caller. |
| 5801 | Return 0 on success, -1 on error */ |
| 5802 | static |
| 5803 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 5804 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5805 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5806 | PyObject *x; |
| 5807 | |
| 5808 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5809 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5810 | x = PyObject_GetItem(mapping, w); |
| 5811 | Py_DECREF(w); |
| 5812 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5813 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 5814 | /* No mapping found means: use 1:1 mapping. */ |
| 5815 | PyErr_Clear(); |
| 5816 | *result = NULL; |
| 5817 | return 0; |
| 5818 | } else |
| 5819 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5820 | } |
| 5821 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5822 | *result = x; |
| 5823 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5824 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5825 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5826 | long value = PyLong_AS_LONG(x); |
| 5827 | long max = PyUnicode_GetMax(); |
| 5828 | if (value < 0 || value > max) { |
| 5829 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 5830 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5831 | Py_DECREF(x); |
| 5832 | return -1; |
| 5833 | } |
| 5834 | *result = x; |
| 5835 | return 0; |
| 5836 | } |
| 5837 | else if (PyUnicode_Check(x)) { |
| 5838 | *result = x; |
| 5839 | return 0; |
| 5840 | } |
| 5841 | else { |
| 5842 | /* wrong return value */ |
| 5843 | PyErr_SetString(PyExc_TypeError, |
| 5844 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5845 | Py_DECREF(x); |
| 5846 | return -1; |
| 5847 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5848 | } |
| 5849 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5850 | if not reallocate and adjust various state variables. |
| 5851 | Return 0 on success, -1 on error */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5852 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5853 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5854 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5855 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5856 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5857 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5858 | /* remember old output position */ |
| 5859 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
| 5860 | /* exponentially overallocate to minimize reallocations */ |
| 5861 | if (requiredsize < 2 * oldsize) |
| 5862 | requiredsize = 2 * oldsize; |
| 5863 | if (PyUnicode_Resize(outobj, requiredsize) < 0) |
| 5864 | return -1; |
| 5865 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5866 | } |
| 5867 | return 0; |
| 5868 | } |
| 5869 | /* lookup the character, put the result in the output string and adjust |
| 5870 | various state variables. Return a new reference to the object that |
| 5871 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 5872 | undefined (in which case no character was written). |
| 5873 | The called must decref result. |
| 5874 | Return 0 on success, -1 on error. */ |
| 5875 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5876 | int charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5877 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
| 5878 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5879 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5880 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5881 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5882 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5883 | /* not found => default to 1:1 mapping */ |
| 5884 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5885 | } |
| 5886 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5887 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5888 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5889 | /* no overflow check, because we know that the space is enough */ |
| 5890 | *(*outp)++ = (Py_UNICODE)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5891 | } |
| 5892 | else if (PyUnicode_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5893 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
| 5894 | if (repsize==1) { |
| 5895 | /* no overflow check, because we know that the space is enough */ |
| 5896 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 5897 | } |
| 5898 | else if (repsize!=0) { |
| 5899 | /* more than one character */ |
| 5900 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
| 5901 | (insize - (curinp-startinp)) + |
| 5902 | repsize - 1; |
| 5903 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
| 5904 | return -1; |
| 5905 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 5906 | *outp += repsize; |
| 5907 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5908 | } |
| 5909 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5910 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5911 | return 0; |
| 5912 | } |
| 5913 | |
| 5914 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5915 | Py_ssize_t size, |
| 5916 | PyObject *mapping, |
| 5917 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5918 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5919 | /* output object */ |
| 5920 | PyObject *res = NULL; |
| 5921 | /* pointers to the beginning and end+1 of input */ |
| 5922 | const Py_UNICODE *startp = p; |
| 5923 | const Py_UNICODE *endp = p + size; |
| 5924 | /* pointer into the output */ |
| 5925 | Py_UNICODE *str; |
| 5926 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5927 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5928 | char *reason = "character maps to <undefined>"; |
| 5929 | PyObject *errorHandler = NULL; |
| 5930 | PyObject *exc = NULL; |
| 5931 | /* the following variable is used for caching string comparisons |
| 5932 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 5933 | * 3=ignore, 4=xmlcharrefreplace */ |
| 5934 | int known_errorHandler = -1; |
| 5935 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5936 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5937 | PyErr_BadArgument(); |
| 5938 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5939 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5940 | |
| 5941 | /* allocate enough for a simple 1:1 translation without |
| 5942 | replacements, if we need more, we'll resize */ |
| 5943 | res = PyUnicode_FromUnicode(NULL, size); |
| 5944 | if (res == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5945 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5946 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5947 | return res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5948 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5949 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5950 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5951 | /* try to encode it */ |
| 5952 | PyObject *x = NULL; |
| 5953 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
| 5954 | Py_XDECREF(x); |
| 5955 | goto onError; |
| 5956 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5957 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5958 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 5959 | ++p; |
| 5960 | else { /* untranslatable character */ |
| 5961 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 5962 | Py_ssize_t repsize; |
| 5963 | Py_ssize_t newpos; |
| 5964 | Py_UNICODE *uni2; |
| 5965 | /* startpos for collecting untranslatable chars */ |
| 5966 | const Py_UNICODE *collstart = p; |
| 5967 | const Py_UNICODE *collend = p+1; |
| 5968 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5969 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5970 | /* find all untranslatable characters */ |
| 5971 | while (collend < endp) { |
| 5972 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
| 5973 | goto onError; |
| 5974 | Py_XDECREF(x); |
| 5975 | if (x!=Py_None) |
| 5976 | break; |
| 5977 | ++collend; |
| 5978 | } |
| 5979 | /* cache callback name lookup |
| 5980 | * (if not done yet, i.e. it's the first error) */ |
| 5981 | if (known_errorHandler==-1) { |
| 5982 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5983 | known_errorHandler = 1; |
| 5984 | else if (!strcmp(errors, "replace")) |
| 5985 | known_errorHandler = 2; |
| 5986 | else if (!strcmp(errors, "ignore")) |
| 5987 | known_errorHandler = 3; |
| 5988 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5989 | known_errorHandler = 4; |
| 5990 | else |
| 5991 | known_errorHandler = 0; |
| 5992 | } |
| 5993 | switch (known_errorHandler) { |
| 5994 | case 1: /* strict */ |
| 5995 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5996 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5997 | case 2: /* replace */ |
| 5998 | /* No need to check for space, this is a 1:1 replacement */ |
| 5999 | for (coll = collstart; coll<collend; ++coll) |
| 6000 | *str++ = '?'; |
| 6001 | /* fall through */ |
| 6002 | case 3: /* ignore */ |
| 6003 | p = collend; |
| 6004 | break; |
| 6005 | case 4: /* xmlcharrefreplace */ |
| 6006 | /* generate replacement (temporarily (mis)uses p) */ |
| 6007 | for (p = collstart; p < collend; ++p) { |
| 6008 | char buffer[2+29+1+1]; |
| 6009 | char *cp; |
| 6010 | sprintf(buffer, "&#%d;", (int)*p); |
| 6011 | if (charmaptranslate_makespace(&res, &str, |
| 6012 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 6013 | goto onError; |
| 6014 | for (cp = buffer; *cp; ++cp) |
| 6015 | *str++ = *cp; |
| 6016 | } |
| 6017 | p = collend; |
| 6018 | break; |
| 6019 | default: |
| 6020 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 6021 | reason, startp, size, &exc, |
| 6022 | collstart-startp, collend-startp, &newpos); |
| 6023 | if (repunicode == NULL) |
| 6024 | goto onError; |
| 6025 | /* generate replacement */ |
| 6026 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 6027 | if (charmaptranslate_makespace(&res, &str, |
| 6028 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 6029 | Py_DECREF(repunicode); |
| 6030 | goto onError; |
| 6031 | } |
| 6032 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 6033 | *str++ = *uni2; |
| 6034 | p = startp + newpos; |
| 6035 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6036 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6037 | } |
| 6038 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6039 | /* Resize if we allocated to much */ |
| 6040 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 6041 | if (respos<PyUnicode_GET_SIZE(res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6042 | if (PyUnicode_Resize(&res, respos) < 0) |
| 6043 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6044 | } |
| 6045 | Py_XDECREF(exc); |
| 6046 | Py_XDECREF(errorHandler); |
| 6047 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6048 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6049 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6050 | Py_XDECREF(res); |
| 6051 | Py_XDECREF(exc); |
| 6052 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6053 | return NULL; |
| 6054 | } |
| 6055 | |
| 6056 | PyObject *PyUnicode_Translate(PyObject *str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6057 | PyObject *mapping, |
| 6058 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6059 | { |
| 6060 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6061 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6062 | str = PyUnicode_FromObject(str); |
| 6063 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6064 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6065 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6066 | PyUnicode_GET_SIZE(str), |
| 6067 | mapping, |
| 6068 | errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6069 | Py_DECREF(str); |
| 6070 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6071 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6072 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6073 | Py_XDECREF(str); |
| 6074 | return NULL; |
| 6075 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6076 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6077 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 6078 | |
| 6079 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6080 | Py_ssize_t length, |
| 6081 | char *output, |
| 6082 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6083 | { |
| 6084 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6085 | PyObject *errorHandler = NULL; |
| 6086 | PyObject *exc = NULL; |
| 6087 | const char *encoding = "decimal"; |
| 6088 | const char *reason = "invalid decimal Unicode string"; |
| 6089 | /* the following variable is used for caching string comparisons |
| 6090 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6091 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6092 | |
| 6093 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6094 | PyErr_BadArgument(); |
| 6095 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6096 | } |
| 6097 | |
| 6098 | p = s; |
| 6099 | end = s + length; |
| 6100 | while (p < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6101 | register Py_UNICODE ch = *p; |
| 6102 | int decimal; |
| 6103 | PyObject *repunicode; |
| 6104 | Py_ssize_t repsize; |
| 6105 | Py_ssize_t newpos; |
| 6106 | Py_UNICODE *uni2; |
| 6107 | Py_UNICODE *collstart; |
| 6108 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6109 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6110 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6111 | *output++ = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6112 | ++p; |
| 6113 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6114 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6115 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 6116 | if (decimal >= 0) { |
| 6117 | *output++ = '0' + decimal; |
| 6118 | ++p; |
| 6119 | continue; |
| 6120 | } |
| 6121 | if (0 < ch && ch < 256) { |
| 6122 | *output++ = (char)ch; |
| 6123 | ++p; |
| 6124 | continue; |
| 6125 | } |
| 6126 | /* All other characters are considered unencodable */ |
| 6127 | collstart = p; |
| 6128 | collend = p+1; |
| 6129 | while (collend < end) { |
| 6130 | if ((0 < *collend && *collend < 256) || |
| 6131 | !Py_UNICODE_ISSPACE(*collend) || |
| 6132 | Py_UNICODE_TODECIMAL(*collend)) |
| 6133 | break; |
| 6134 | } |
| 6135 | /* cache callback name lookup |
| 6136 | * (if not done yet, i.e. it's the first error) */ |
| 6137 | if (known_errorHandler==-1) { |
| 6138 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6139 | known_errorHandler = 1; |
| 6140 | else if (!strcmp(errors, "replace")) |
| 6141 | known_errorHandler = 2; |
| 6142 | else if (!strcmp(errors, "ignore")) |
| 6143 | known_errorHandler = 3; |
| 6144 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6145 | known_errorHandler = 4; |
| 6146 | else |
| 6147 | known_errorHandler = 0; |
| 6148 | } |
| 6149 | switch (known_errorHandler) { |
| 6150 | case 1: /* strict */ |
| 6151 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 6152 | goto onError; |
| 6153 | case 2: /* replace */ |
| 6154 | for (p = collstart; p < collend; ++p) |
| 6155 | *output++ = '?'; |
| 6156 | /* fall through */ |
| 6157 | case 3: /* ignore */ |
| 6158 | p = collend; |
| 6159 | break; |
| 6160 | case 4: /* xmlcharrefreplace */ |
| 6161 | /* generate replacement (temporarily (mis)uses p) */ |
| 6162 | for (p = collstart; p < collend; ++p) |
| 6163 | output += sprintf(output, "&#%d;", (int)*p); |
| 6164 | p = collend; |
| 6165 | break; |
| 6166 | default: |
| 6167 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 6168 | encoding, reason, s, length, &exc, |
| 6169 | collstart-s, collend-s, &newpos); |
| 6170 | if (repunicode == NULL) |
| 6171 | goto onError; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6172 | if (!PyUnicode_Check(repunicode)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6173 | /* Byte results not supported, since they have no decimal property. */ |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6174 | PyErr_SetString(PyExc_TypeError, "error handler should return unicode"); |
| 6175 | Py_DECREF(repunicode); |
| 6176 | goto onError; |
| 6177 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6178 | /* generate replacement */ |
| 6179 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 6180 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 6181 | Py_UNICODE ch = *uni2; |
| 6182 | if (Py_UNICODE_ISSPACE(ch)) |
| 6183 | *output++ = ' '; |
| 6184 | else { |
| 6185 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 6186 | if (decimal >= 0) |
| 6187 | *output++ = '0' + decimal; |
| 6188 | else if (0 < ch && ch < 256) |
| 6189 | *output++ = (char)ch; |
| 6190 | else { |
| 6191 | Py_DECREF(repunicode); |
| 6192 | raise_encode_exception(&exc, encoding, |
| 6193 | s, length, collstart-s, collend-s, reason); |
| 6194 | goto onError; |
| 6195 | } |
| 6196 | } |
| 6197 | } |
| 6198 | p = s + newpos; |
| 6199 | Py_DECREF(repunicode); |
| 6200 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6201 | } |
| 6202 | /* 0-terminate the output string */ |
| 6203 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6204 | Py_XDECREF(exc); |
| 6205 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6206 | return 0; |
| 6207 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6208 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6209 | Py_XDECREF(exc); |
| 6210 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6211 | return -1; |
| 6212 | } |
| 6213 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6214 | /* --- Helpers ------------------------------------------------------------ */ |
| 6215 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 6216 | #include "stringlib/unicodedefs.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6217 | #include "stringlib/fastsearch.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6218 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6219 | #include "stringlib/count.h" |
| 6220 | #include "stringlib/find.h" |
| 6221 | #include "stringlib/partition.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6222 | #include "stringlib/split.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6223 | |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 6224 | #define _Py_InsertThousandsGrouping _PyUnicode_InsertThousandsGrouping |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame] | 6225 | #define _Py_InsertThousandsGroupingLocale _PyUnicode_InsertThousandsGroupingLocale |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 6226 | #include "stringlib/localeutil.h" |
| 6227 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6228 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6229 | #define ADJUST_INDICES(start, end, len) \ |
| 6230 | if (end > len) \ |
| 6231 | end = len; \ |
| 6232 | else if (end < 0) { \ |
| 6233 | end += len; \ |
| 6234 | if (end < 0) \ |
| 6235 | end = 0; \ |
| 6236 | } \ |
| 6237 | if (start < 0) { \ |
| 6238 | start += len; \ |
| 6239 | if (start < 0) \ |
| 6240 | start = 0; \ |
| 6241 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6242 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6243 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6244 | PyObject *substr, |
| 6245 | Py_ssize_t start, |
| 6246 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6247 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6248 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6249 | PyUnicodeObject* str_obj; |
| 6250 | PyUnicodeObject* sub_obj; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6251 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6252 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
| 6253 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6254 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6255 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
| 6256 | if (!sub_obj) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6257 | Py_DECREF(str_obj); |
| 6258 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6259 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6260 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6261 | ADJUST_INDICES(start, end, str_obj->length); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6262 | result = stringlib_count( |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6263 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length, |
| 6264 | PY_SSIZE_T_MAX |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6265 | ); |
| 6266 | |
| 6267 | Py_DECREF(sub_obj); |
| 6268 | Py_DECREF(str_obj); |
| 6269 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6270 | return result; |
| 6271 | } |
| 6272 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6273 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6274 | PyObject *sub, |
| 6275 | Py_ssize_t start, |
| 6276 | Py_ssize_t end, |
| 6277 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6278 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6279 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6280 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6281 | str = PyUnicode_FromObject(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6282 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6283 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6284 | sub = PyUnicode_FromObject(sub); |
| 6285 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6286 | Py_DECREF(str); |
| 6287 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6288 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6289 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6290 | if (direction > 0) |
| 6291 | result = stringlib_find_slice( |
| 6292 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 6293 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 6294 | start, end |
| 6295 | ); |
| 6296 | else |
| 6297 | result = stringlib_rfind_slice( |
| 6298 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 6299 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 6300 | start, end |
| 6301 | ); |
| 6302 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6303 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6304 | Py_DECREF(sub); |
| 6305 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6306 | return result; |
| 6307 | } |
| 6308 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6309 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6310 | int tailmatch(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6311 | PyUnicodeObject *substring, |
| 6312 | Py_ssize_t start, |
| 6313 | Py_ssize_t end, |
| 6314 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6315 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6316 | if (substring->length == 0) |
| 6317 | return 1; |
| 6318 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6319 | ADJUST_INDICES(start, end, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6320 | end -= substring->length; |
| 6321 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6322 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6323 | |
| 6324 | if (direction > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6325 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 6326 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6327 | } else { |
| 6328 | if (Py_UNICODE_MATCH(self, start, substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6329 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6330 | } |
| 6331 | |
| 6332 | return 0; |
| 6333 | } |
| 6334 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6335 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6336 | PyObject *substr, |
| 6337 | Py_ssize_t start, |
| 6338 | Py_ssize_t end, |
| 6339 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6340 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6341 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6342 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6343 | str = PyUnicode_FromObject(str); |
| 6344 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6345 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6346 | substr = PyUnicode_FromObject(substr); |
| 6347 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6348 | Py_DECREF(str); |
| 6349 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6350 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6351 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6352 | result = tailmatch((PyUnicodeObject *)str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6353 | (PyUnicodeObject *)substr, |
| 6354 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6355 | Py_DECREF(str); |
| 6356 | Py_DECREF(substr); |
| 6357 | return result; |
| 6358 | } |
| 6359 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6360 | /* Apply fixfct filter to the Unicode object self and return a |
| 6361 | reference to the modified object */ |
| 6362 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6363 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6364 | PyObject *fixup(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6365 | int (*fixfct)(PyUnicodeObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6366 | { |
| 6367 | |
| 6368 | PyUnicodeObject *u; |
| 6369 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6370 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6371 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6372 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6373 | |
| 6374 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6375 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6376 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6377 | /* fixfct should return TRUE if it modified the buffer. If |
| 6378 | FALSE, return a reference to the original buffer instead |
| 6379 | (to save space, not time) */ |
| 6380 | Py_INCREF(self); |
| 6381 | Py_DECREF(u); |
| 6382 | return (PyObject*) self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6383 | } |
| 6384 | return (PyObject*) u; |
| 6385 | } |
| 6386 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6387 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6388 | int fixupper(PyUnicodeObject *self) |
| 6389 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6390 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6391 | Py_UNICODE *s = self->str; |
| 6392 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6393 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6394 | while (len-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6395 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6396 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6397 | ch = Py_UNICODE_TOUPPER(*s); |
| 6398 | if (ch != *s) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6399 | status = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6400 | *s = ch; |
| 6401 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6402 | s++; |
| 6403 | } |
| 6404 | |
| 6405 | return status; |
| 6406 | } |
| 6407 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6408 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6409 | int fixlower(PyUnicodeObject *self) |
| 6410 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6411 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6412 | Py_UNICODE *s = self->str; |
| 6413 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6414 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6415 | while (len-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6416 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6417 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6418 | ch = Py_UNICODE_TOLOWER(*s); |
| 6419 | if (ch != *s) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6420 | status = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6421 | *s = ch; |
| 6422 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6423 | s++; |
| 6424 | } |
| 6425 | |
| 6426 | return status; |
| 6427 | } |
| 6428 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6429 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6430 | int fixswapcase(PyUnicodeObject *self) |
| 6431 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6432 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6433 | Py_UNICODE *s = self->str; |
| 6434 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6435 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6436 | while (len-- > 0) { |
| 6437 | if (Py_UNICODE_ISUPPER(*s)) { |
| 6438 | *s = Py_UNICODE_TOLOWER(*s); |
| 6439 | status = 1; |
| 6440 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 6441 | *s = Py_UNICODE_TOUPPER(*s); |
| 6442 | status = 1; |
| 6443 | } |
| 6444 | s++; |
| 6445 | } |
| 6446 | |
| 6447 | return status; |
| 6448 | } |
| 6449 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6450 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6451 | int fixcapitalize(PyUnicodeObject *self) |
| 6452 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6453 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6454 | Py_UNICODE *s = self->str; |
| 6455 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6456 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6457 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6458 | return 0; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6459 | if (Py_UNICODE_ISLOWER(*s)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6460 | *s = Py_UNICODE_TOUPPER(*s); |
| 6461 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6462 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6463 | s++; |
| 6464 | while (--len > 0) { |
| 6465 | if (Py_UNICODE_ISUPPER(*s)) { |
| 6466 | *s = Py_UNICODE_TOLOWER(*s); |
| 6467 | status = 1; |
| 6468 | } |
| 6469 | s++; |
| 6470 | } |
| 6471 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6472 | } |
| 6473 | |
| 6474 | static |
| 6475 | int fixtitle(PyUnicodeObject *self) |
| 6476 | { |
| 6477 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6478 | register Py_UNICODE *e; |
| 6479 | int previous_is_cased; |
| 6480 | |
| 6481 | /* Shortcut for single character strings */ |
| 6482 | if (PyUnicode_GET_SIZE(self) == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6483 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 6484 | if (*p != ch) { |
| 6485 | *p = ch; |
| 6486 | return 1; |
| 6487 | } |
| 6488 | else |
| 6489 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6490 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6491 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6492 | e = p + PyUnicode_GET_SIZE(self); |
| 6493 | previous_is_cased = 0; |
| 6494 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6495 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6496 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6497 | if (previous_is_cased) |
| 6498 | *p = Py_UNICODE_TOLOWER(ch); |
| 6499 | else |
| 6500 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6501 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6502 | if (Py_UNICODE_ISLOWER(ch) || |
| 6503 | Py_UNICODE_ISUPPER(ch) || |
| 6504 | Py_UNICODE_ISTITLE(ch)) |
| 6505 | previous_is_cased = 1; |
| 6506 | else |
| 6507 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6508 | } |
| 6509 | return 1; |
| 6510 | } |
| 6511 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6512 | PyObject * |
| 6513 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6514 | { |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 6515 | const Py_UNICODE blank = ' '; |
| 6516 | const Py_UNICODE *sep = ␣ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6517 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6518 | PyUnicodeObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6519 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 6520 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6521 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 6522 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6523 | PyObject *item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6524 | Py_ssize_t sz, i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6525 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6526 | fseq = PySequence_Fast(seq, ""); |
| 6527 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6528 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6529 | } |
| 6530 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6531 | /* NOTE: the following code can't call back into Python code, |
| 6532 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 6533 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6534 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6535 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 6536 | /* If empty sequence, return u"". */ |
| 6537 | if (seqlen == 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6538 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 6539 | goto Done; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6540 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6541 | items = PySequence_Fast_ITEMS(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6542 | /* If singleton sequence with an exact Unicode, return that. */ |
| 6543 | if (seqlen == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6544 | item = items[0]; |
| 6545 | if (PyUnicode_CheckExact(item)) { |
| 6546 | Py_INCREF(item); |
| 6547 | res = (PyUnicodeObject *)item; |
| 6548 | goto Done; |
| 6549 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6550 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6551 | else { |
| 6552 | /* Set up sep and seplen */ |
| 6553 | if (separator == NULL) { |
| 6554 | sep = ␣ |
| 6555 | seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6556 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6557 | else { |
| 6558 | if (!PyUnicode_Check(separator)) { |
| 6559 | PyErr_Format(PyExc_TypeError, |
| 6560 | "separator: expected str instance," |
| 6561 | " %.80s found", |
| 6562 | Py_TYPE(separator)->tp_name); |
| 6563 | goto onError; |
| 6564 | } |
| 6565 | sep = PyUnicode_AS_UNICODE(separator); |
| 6566 | seplen = PyUnicode_GET_SIZE(separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6567 | } |
| 6568 | } |
| 6569 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6570 | /* There are at least two things to join, or else we have a subclass |
| 6571 | * of str in the sequence. |
| 6572 | * Do a pre-pass to figure out the total amount of space we'll |
| 6573 | * need (sz), and see whether all argument are strings. |
| 6574 | */ |
| 6575 | sz = 0; |
| 6576 | for (i = 0; i < seqlen; i++) { |
| 6577 | const Py_ssize_t old_sz = sz; |
| 6578 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6579 | if (!PyUnicode_Check(item)) { |
| 6580 | PyErr_Format(PyExc_TypeError, |
| 6581 | "sequence item %zd: expected str instance," |
| 6582 | " %.80s found", |
| 6583 | i, Py_TYPE(item)->tp_name); |
| 6584 | goto onError; |
| 6585 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6586 | sz += PyUnicode_GET_SIZE(item); |
| 6587 | if (i != 0) |
| 6588 | sz += seplen; |
| 6589 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 6590 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6591 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6592 | goto onError; |
| 6593 | } |
| 6594 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6595 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6596 | res = _PyUnicode_New(sz); |
| 6597 | if (res == NULL) |
| 6598 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 6599 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6600 | /* Catenate everything. */ |
| 6601 | res_p = PyUnicode_AS_UNICODE(res); |
| 6602 | for (i = 0; i < seqlen; ++i) { |
| 6603 | Py_ssize_t itemlen; |
| 6604 | item = items[i]; |
| 6605 | itemlen = PyUnicode_GET_SIZE(item); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6606 | /* Copy item, and maybe the separator. */ |
| 6607 | if (i) { |
| 6608 | Py_UNICODE_COPY(res_p, sep, seplen); |
| 6609 | res_p += seplen; |
| 6610 | } |
| 6611 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
| 6612 | res_p += itemlen; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6613 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6614 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6615 | Done: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6616 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6617 | return (PyObject *)res; |
| 6618 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6619 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6620 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6621 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6622 | return NULL; |
| 6623 | } |
| 6624 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6625 | static |
| 6626 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6627 | Py_ssize_t left, |
| 6628 | Py_ssize_t right, |
| 6629 | Py_UNICODE fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6630 | { |
| 6631 | PyUnicodeObject *u; |
| 6632 | |
| 6633 | if (left < 0) |
| 6634 | left = 0; |
| 6635 | if (right < 0) |
| 6636 | right = 0; |
| 6637 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6638 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6639 | Py_INCREF(self); |
| 6640 | return self; |
| 6641 | } |
| 6642 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6643 | if (left > PY_SSIZE_T_MAX - self->length || |
| 6644 | right > PY_SSIZE_T_MAX - (left + self->length)) { |
| 6645 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 6646 | return NULL; |
| 6647 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6648 | u = _PyUnicode_New(left + self->length + right); |
| 6649 | if (u) { |
| 6650 | if (left) |
| 6651 | Py_UNICODE_FILL(u->str, fill, left); |
| 6652 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 6653 | if (right) |
| 6654 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 6655 | } |
| 6656 | |
| 6657 | return u; |
| 6658 | } |
| 6659 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6660 | PyObject *PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6661 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6662 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6663 | |
| 6664 | string = PyUnicode_FromObject(string); |
| 6665 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6666 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6667 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6668 | list = stringlib_splitlines( |
| 6669 | (PyObject*) string, PyUnicode_AS_UNICODE(string), |
| 6670 | PyUnicode_GET_SIZE(string), keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6671 | |
| 6672 | Py_DECREF(string); |
| 6673 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6674 | } |
| 6675 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6676 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6677 | PyObject *split(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6678 | PyUnicodeObject *substring, |
| 6679 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6680 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6681 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6682 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6683 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6684 | if (substring == NULL) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6685 | return stringlib_split_whitespace( |
| 6686 | (PyObject*) self, self->str, self->length, maxcount |
| 6687 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6688 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6689 | return stringlib_split( |
| 6690 | (PyObject*) self, self->str, self->length, |
| 6691 | substring->str, substring->length, |
| 6692 | maxcount |
| 6693 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6694 | } |
| 6695 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6696 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6697 | PyObject *rsplit(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6698 | PyUnicodeObject *substring, |
| 6699 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6700 | { |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6701 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6702 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6703 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6704 | if (substring == NULL) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6705 | return stringlib_rsplit_whitespace( |
| 6706 | (PyObject*) self, self->str, self->length, maxcount |
| 6707 | ); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6708 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6709 | return stringlib_rsplit( |
| 6710 | (PyObject*) self, self->str, self->length, |
| 6711 | substring->str, substring->length, |
| 6712 | maxcount |
| 6713 | ); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6714 | } |
| 6715 | |
| 6716 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6717 | PyObject *replace(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6718 | PyUnicodeObject *str1, |
| 6719 | PyUnicodeObject *str2, |
| 6720 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6721 | { |
| 6722 | PyUnicodeObject *u; |
| 6723 | |
| 6724 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6725 | maxcount = PY_SSIZE_T_MAX; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6726 | else if (maxcount == 0 || self->length == 0) |
| 6727 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6728 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6729 | if (str1->length == str2->length) { |
Antoine Pitrou | cbfdee3 | 2010-01-13 08:58:08 +0000 | [diff] [blame] | 6730 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6731 | /* same length */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6732 | if (str1->length == 0) |
| 6733 | goto nothing; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6734 | if (str1->length == 1) { |
| 6735 | /* replace characters */ |
| 6736 | Py_UNICODE u1, u2; |
| 6737 | if (!findchar(self->str, self->length, str1->str[0])) |
| 6738 | goto nothing; |
| 6739 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6740 | if (!u) |
| 6741 | return NULL; |
| 6742 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6743 | u1 = str1->str[0]; |
| 6744 | u2 = str2->str[0]; |
| 6745 | for (i = 0; i < u->length; i++) |
| 6746 | if (u->str[i] == u1) { |
| 6747 | if (--maxcount < 0) |
| 6748 | break; |
| 6749 | u->str[i] = u2; |
| 6750 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6751 | } else { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6752 | i = stringlib_find( |
| 6753 | self->str, self->length, str1->str, str1->length, 0 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6754 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6755 | if (i < 0) |
| 6756 | goto nothing; |
| 6757 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6758 | if (!u) |
| 6759 | return NULL; |
| 6760 | Py_UNICODE_COPY(u->str, self->str, self->length); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6761 | |
| 6762 | /* change everything in-place, starting with this one */ |
| 6763 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 6764 | i += str1->length; |
| 6765 | |
| 6766 | while ( --maxcount > 0) { |
| 6767 | i = stringlib_find(self->str+i, self->length-i, |
| 6768 | str1->str, str1->length, |
| 6769 | i); |
| 6770 | if (i == -1) |
| 6771 | break; |
| 6772 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 6773 | i += str1->length; |
| 6774 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6775 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6776 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6777 | |
| 6778 | Py_ssize_t n, i, j, e; |
| 6779 | Py_ssize_t product, new_size, delta; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6780 | Py_UNICODE *p; |
| 6781 | |
| 6782 | /* replace strings */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6783 | n = stringlib_count(self->str, self->length, str1->str, str1->length, |
| 6784 | maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6785 | if (n == 0) |
| 6786 | goto nothing; |
| 6787 | /* new_size = self->length + n * (str2->length - str1->length)); */ |
| 6788 | delta = (str2->length - str1->length); |
| 6789 | if (delta == 0) { |
| 6790 | new_size = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6791 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6792 | product = n * (str2->length - str1->length); |
| 6793 | if ((product / (str2->length - str1->length)) != n) { |
| 6794 | PyErr_SetString(PyExc_OverflowError, |
| 6795 | "replace string is too long"); |
| 6796 | return NULL; |
| 6797 | } |
| 6798 | new_size = self->length + product; |
| 6799 | if (new_size < 0) { |
| 6800 | PyErr_SetString(PyExc_OverflowError, |
| 6801 | "replace string is too long"); |
| 6802 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6803 | } |
| 6804 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6805 | u = _PyUnicode_New(new_size); |
| 6806 | if (!u) |
| 6807 | return NULL; |
| 6808 | i = 0; |
| 6809 | p = u->str; |
| 6810 | e = self->length - str1->length; |
| 6811 | if (str1->length > 0) { |
| 6812 | while (n-- > 0) { |
| 6813 | /* look for next match */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6814 | j = stringlib_find(self->str+i, self->length-i, |
| 6815 | str1->str, str1->length, |
| 6816 | i); |
| 6817 | if (j == -1) |
| 6818 | break; |
| 6819 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6820 | /* copy unchanged part [i:j] */ |
| 6821 | Py_UNICODE_COPY(p, self->str+i, j-i); |
| 6822 | p += j - i; |
| 6823 | } |
| 6824 | /* copy substitution string */ |
| 6825 | if (str2->length > 0) { |
| 6826 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6827 | p += str2->length; |
| 6828 | } |
| 6829 | i = j + str1->length; |
| 6830 | } |
| 6831 | if (i < self->length) |
| 6832 | /* copy tail [i:] */ |
| 6833 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6834 | } else { |
| 6835 | /* interleave */ |
| 6836 | while (n > 0) { |
| 6837 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6838 | p += str2->length; |
| 6839 | if (--n <= 0) |
| 6840 | break; |
| 6841 | *p++ = self->str[i++]; |
| 6842 | } |
| 6843 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6844 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6845 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6846 | return (PyObject *) u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6847 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6848 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6849 | /* nothing to replace; return original string (when possible) */ |
| 6850 | if (PyUnicode_CheckExact(self)) { |
| 6851 | Py_INCREF(self); |
| 6852 | return (PyObject *) self; |
| 6853 | } |
| 6854 | return PyUnicode_FromUnicode(self->str, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6855 | } |
| 6856 | |
| 6857 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 6858 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6859 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6860 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6861 | \n\ |
| 6862 | 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] | 6863 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6864 | |
| 6865 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6866 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6867 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6868 | return fixup(self, fixtitle); |
| 6869 | } |
| 6870 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6871 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6872 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6873 | \n\ |
| 6874 | 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] | 6875 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6876 | |
| 6877 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6878 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6879 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6880 | return fixup(self, fixcapitalize); |
| 6881 | } |
| 6882 | |
| 6883 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6884 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6885 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6886 | \n\ |
| 6887 | 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] | 6888 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6889 | |
| 6890 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6891 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6892 | { |
| 6893 | PyObject *list; |
| 6894 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6895 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6896 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6897 | /* Split into words */ |
| 6898 | list = split(self, NULL, -1); |
| 6899 | if (!list) |
| 6900 | return NULL; |
| 6901 | |
| 6902 | /* Capitalize each word */ |
| 6903 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 6904 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6905 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6906 | if (item == NULL) |
| 6907 | goto onError; |
| 6908 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 6909 | PyList_SET_ITEM(list, i, item); |
| 6910 | } |
| 6911 | |
| 6912 | /* Join the words to form a new string */ |
| 6913 | item = PyUnicode_Join(NULL, list); |
| 6914 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6915 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6916 | Py_DECREF(list); |
| 6917 | return (PyObject *)item; |
| 6918 | } |
| 6919 | #endif |
| 6920 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6921 | /* Argument converter. Coerces to a single unicode character */ |
| 6922 | |
| 6923 | static int |
| 6924 | convert_uc(PyObject *obj, void *addr) |
| 6925 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6926 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 6927 | PyObject *uniobj; |
| 6928 | Py_UNICODE *unistr; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6929 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6930 | uniobj = PyUnicode_FromObject(obj); |
| 6931 | if (uniobj == NULL) { |
| 6932 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6933 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6934 | return 0; |
| 6935 | } |
| 6936 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 6937 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6938 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6939 | Py_DECREF(uniobj); |
| 6940 | return 0; |
| 6941 | } |
| 6942 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 6943 | *fillcharloc = unistr[0]; |
| 6944 | Py_DECREF(uniobj); |
| 6945 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6946 | } |
| 6947 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6948 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6949 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6950 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 6951 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6952 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6953 | |
| 6954 | static PyObject * |
| 6955 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 6956 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6957 | Py_ssize_t marg, left; |
| 6958 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6959 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6960 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 6961 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6962 | return NULL; |
| 6963 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6964 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6965 | Py_INCREF(self); |
| 6966 | return (PyObject*) self; |
| 6967 | } |
| 6968 | |
| 6969 | marg = width - self->length; |
| 6970 | left = marg / 2 + (marg & width & 1); |
| 6971 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6972 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6973 | } |
| 6974 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6975 | #if 0 |
| 6976 | |
| 6977 | /* This code should go into some future Unicode collation support |
| 6978 | module. The basic comparison should compare ordinals on a naive |
Georg Brandl | c6c3178 | 2009-06-08 13:41:29 +0000 | [diff] [blame] | 6979 | basis (this is what Java does and thus Jython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6980 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6981 | /* speedy UTF-16 code point order comparison */ |
| 6982 | /* gleaned from: */ |
| 6983 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 6984 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6985 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6986 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6987 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6988 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 6989 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6990 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6991 | }; |
| 6992 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6993 | static int |
| 6994 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6995 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6996 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6997 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6998 | Py_UNICODE *s1 = str1->str; |
| 6999 | Py_UNICODE *s2 = str2->str; |
| 7000 | |
| 7001 | len1 = str1->length; |
| 7002 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7003 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7004 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7005 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 7006 | |
| 7007 | c1 = *s1++; |
| 7008 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 7009 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7010 | if (c1 > (1<<11) * 26) |
| 7011 | c1 += utf16Fixup[c1>>11]; |
| 7012 | if (c2 > (1<<11) * 26) |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 7013 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 7014 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 7015 | |
| 7016 | if (c1 != c2) |
| 7017 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7018 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 7019 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7020 | } |
| 7021 | |
| 7022 | return (len1 < len2) ? -1 : (len1 != len2); |
| 7023 | } |
| 7024 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 7025 | #else |
| 7026 | |
| 7027 | static int |
| 7028 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 7029 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7030 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 7031 | |
| 7032 | Py_UNICODE *s1 = str1->str; |
| 7033 | Py_UNICODE *s2 = str2->str; |
| 7034 | |
| 7035 | len1 = str1->length; |
| 7036 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7037 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 7038 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7039 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 7040 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 7041 | c1 = *s1++; |
| 7042 | c2 = *s2++; |
| 7043 | |
| 7044 | if (c1 != c2) |
| 7045 | return (c1 < c2) ? -1 : 1; |
| 7046 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 7047 | len1--; len2--; |
| 7048 | } |
| 7049 | |
| 7050 | return (len1 < len2) ? -1 : (len1 != len2); |
| 7051 | } |
| 7052 | |
| 7053 | #endif |
| 7054 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7055 | int PyUnicode_Compare(PyObject *left, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7056 | PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7057 | { |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 7058 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) |
| 7059 | return unicode_compare((PyUnicodeObject *)left, |
| 7060 | (PyUnicodeObject *)right); |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 7061 | PyErr_Format(PyExc_TypeError, |
| 7062 | "Can't compare %.100s and %.100s", |
| 7063 | left->ob_type->tp_name, |
| 7064 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7065 | return -1; |
| 7066 | } |
| 7067 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 7068 | int |
| 7069 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 7070 | { |
| 7071 | int i; |
| 7072 | Py_UNICODE *id; |
| 7073 | assert(PyUnicode_Check(uni)); |
| 7074 | id = PyUnicode_AS_UNICODE(uni); |
| 7075 | /* Compare Unicode string and source character set string */ |
| 7076 | for (i = 0; id[i] && str[i]; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7077 | if (id[i] != str[i]) |
| 7078 | return ((int)id[i] < (int)str[i]) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 7079 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 7080 | to C strings identical up to that point. */ |
Benjamin Peterson | a23831f | 2010-04-25 21:54:00 +0000 | [diff] [blame] | 7081 | if (PyUnicode_GET_SIZE(uni) != i || id[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7082 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 7083 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7084 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 7085 | return 0; |
| 7086 | } |
| 7087 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 7088 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7089 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7090 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 7091 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 7092 | PyObject *PyUnicode_RichCompare(PyObject *left, |
| 7093 | PyObject *right, |
| 7094 | int op) |
| 7095 | { |
| 7096 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7097 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 7098 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 7099 | PyObject *v; |
| 7100 | if (((PyUnicodeObject *) left)->length != |
| 7101 | ((PyUnicodeObject *) right)->length) { |
| 7102 | if (op == Py_EQ) { |
| 7103 | Py_INCREF(Py_False); |
| 7104 | return Py_False; |
| 7105 | } |
| 7106 | if (op == Py_NE) { |
| 7107 | Py_INCREF(Py_True); |
| 7108 | return Py_True; |
| 7109 | } |
| 7110 | } |
| 7111 | if (left == right) |
| 7112 | result = 0; |
| 7113 | else |
| 7114 | result = unicode_compare((PyUnicodeObject *)left, |
| 7115 | (PyUnicodeObject *)right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7116 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 7117 | /* Convert the return value to a Boolean */ |
| 7118 | switch (op) { |
| 7119 | case Py_EQ: |
| 7120 | v = TEST_COND(result == 0); |
| 7121 | break; |
| 7122 | case Py_NE: |
| 7123 | v = TEST_COND(result != 0); |
| 7124 | break; |
| 7125 | case Py_LE: |
| 7126 | v = TEST_COND(result <= 0); |
| 7127 | break; |
| 7128 | case Py_GE: |
| 7129 | v = TEST_COND(result >= 0); |
| 7130 | break; |
| 7131 | case Py_LT: |
| 7132 | v = TEST_COND(result == -1); |
| 7133 | break; |
| 7134 | case Py_GT: |
| 7135 | v = TEST_COND(result == 1); |
| 7136 | break; |
| 7137 | default: |
| 7138 | PyErr_BadArgument(); |
| 7139 | return NULL; |
| 7140 | } |
| 7141 | Py_INCREF(v); |
| 7142 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 7143 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7144 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 7145 | Py_INCREF(Py_NotImplemented); |
| 7146 | return Py_NotImplemented; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 7147 | } |
| 7148 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7149 | int PyUnicode_Contains(PyObject *container, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7150 | PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7151 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7152 | PyObject *str, *sub; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7153 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7154 | |
| 7155 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7156 | sub = PyUnicode_FromObject(element); |
| 7157 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7158 | PyErr_Format(PyExc_TypeError, |
| 7159 | "'in <string>' requires string as left operand, not %s", |
| 7160 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7161 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7162 | } |
| 7163 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7164 | str = PyUnicode_FromObject(container); |
| 7165 | if (!str) { |
| 7166 | Py_DECREF(sub); |
| 7167 | return -1; |
| 7168 | } |
| 7169 | |
| 7170 | result = stringlib_contains_obj(str, sub); |
| 7171 | |
| 7172 | Py_DECREF(str); |
| 7173 | Py_DECREF(sub); |
| 7174 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7175 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7176 | } |
| 7177 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7178 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 7179 | |
| 7180 | PyObject *PyUnicode_Concat(PyObject *left, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7181 | PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7182 | { |
| 7183 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 7184 | |
| 7185 | /* Coerce the two arguments */ |
| 7186 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 7187 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7188 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7189 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 7190 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7191 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7192 | |
| 7193 | /* Shortcuts */ |
| 7194 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7195 | Py_DECREF(v); |
| 7196 | return (PyObject *)u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7197 | } |
| 7198 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7199 | Py_DECREF(u); |
| 7200 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7201 | } |
| 7202 | |
| 7203 | /* Concat the two Unicode strings */ |
| 7204 | w = _PyUnicode_New(u->length + v->length); |
| 7205 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7206 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7207 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 7208 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 7209 | |
| 7210 | Py_DECREF(u); |
| 7211 | Py_DECREF(v); |
| 7212 | return (PyObject *)w; |
| 7213 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7214 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7215 | Py_XDECREF(u); |
| 7216 | Py_XDECREF(v); |
| 7217 | return NULL; |
| 7218 | } |
| 7219 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7220 | void |
| 7221 | PyUnicode_Append(PyObject **pleft, PyObject *right) |
| 7222 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7223 | PyObject *new; |
| 7224 | if (*pleft == NULL) |
| 7225 | return; |
| 7226 | if (right == NULL || !PyUnicode_Check(*pleft)) { |
| 7227 | Py_DECREF(*pleft); |
| 7228 | *pleft = NULL; |
| 7229 | return; |
| 7230 | } |
| 7231 | new = PyUnicode_Concat(*pleft, right); |
| 7232 | Py_DECREF(*pleft); |
| 7233 | *pleft = new; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7234 | } |
| 7235 | |
| 7236 | void |
| 7237 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 7238 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7239 | PyUnicode_Append(pleft, right); |
| 7240 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7241 | } |
| 7242 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7243 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7244 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7245 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7246 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7247 | 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] | 7248 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7249 | |
| 7250 | static PyObject * |
| 7251 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 7252 | { |
| 7253 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7254 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7255 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7256 | PyObject *result; |
| 7257 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 7258 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7259 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7260 | return NULL; |
| 7261 | |
| 7262 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7263 | (PyObject *)substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7264 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7265 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7266 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 7267 | ADJUST_INDICES(start, end, self->length); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7268 | result = PyLong_FromSsize_t( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7269 | stringlib_count(self->str + start, end - start, |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 7270 | substring->str, substring->length, |
| 7271 | PY_SSIZE_T_MAX) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7272 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7273 | |
| 7274 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7275 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7276 | return result; |
| 7277 | } |
| 7278 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7279 | PyDoc_STRVAR(encode__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7280 | "S.encode([encoding[, errors]]) -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7281 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7282 | Encode S using the codec registered for encoding. encoding defaults\n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7283 | 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] | 7284 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7285 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 7286 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 7287 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7288 | |
| 7289 | static PyObject * |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 7290 | unicode_encode(PyUnicodeObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7291 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 7292 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7293 | char *encoding = NULL; |
| 7294 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7295 | PyObject *v; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 7296 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 7297 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 7298 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7299 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 7300 | v = PyUnicode_AsEncodedString((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 7301 | if (v == NULL) |
| 7302 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7303 | if (!PyBytes_Check(v)) { |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7304 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 7305 | "encoder did not return a bytes object " |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7306 | "(type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7307 | Py_TYPE(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7308 | Py_DECREF(v); |
| 7309 | return NULL; |
| 7310 | } |
| 7311 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 7312 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7313 | onError: |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 7314 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7315 | } |
| 7316 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7317 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7318 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7319 | \n\ |
| 7320 | 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] | 7321 | 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] | 7322 | |
| 7323 | static PyObject* |
| 7324 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 7325 | { |
| 7326 | Py_UNICODE *e; |
| 7327 | Py_UNICODE *p; |
| 7328 | Py_UNICODE *q; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7329 | Py_UNICODE *qe; |
| 7330 | Py_ssize_t i, j, incr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7331 | PyUnicodeObject *u; |
| 7332 | int tabsize = 8; |
| 7333 | |
| 7334 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7335 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7336 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 7337 | /* First pass: determine size of output string */ |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7338 | i = 0; /* chars up to and including most recent \n or \r */ |
| 7339 | j = 0; /* chars since most recent \n or \r (use in tab calculations) */ |
| 7340 | e = self->str + self->length; /* end of input */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7341 | for (p = self->str; p < e; p++) |
| 7342 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7343 | if (tabsize > 0) { |
| 7344 | incr = tabsize - (j % tabsize); /* cannot overflow */ |
| 7345 | if (j > PY_SSIZE_T_MAX - incr) |
| 7346 | goto overflow1; |
| 7347 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7348 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7349 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7350 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7351 | if (j > PY_SSIZE_T_MAX - 1) |
| 7352 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7353 | j++; |
| 7354 | if (*p == '\n' || *p == '\r') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7355 | if (i > PY_SSIZE_T_MAX - j) |
| 7356 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7357 | i += j; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7358 | j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7359 | } |
| 7360 | } |
| 7361 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7362 | if (i > PY_SSIZE_T_MAX - j) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7363 | goto overflow1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 7364 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7365 | /* Second pass: create output string and fill it */ |
| 7366 | u = _PyUnicode_New(i + j); |
| 7367 | if (!u) |
| 7368 | return NULL; |
| 7369 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7370 | j = 0; /* same as in first pass */ |
| 7371 | q = u->str; /* next output char */ |
| 7372 | qe = u->str + u->length; /* end of output */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7373 | |
| 7374 | for (p = self->str; p < e; p++) |
| 7375 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7376 | if (tabsize > 0) { |
| 7377 | i = tabsize - (j % tabsize); |
| 7378 | j += i; |
| 7379 | while (i--) { |
| 7380 | if (q >= qe) |
| 7381 | goto overflow2; |
| 7382 | *q++ = ' '; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7383 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7384 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7385 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7386 | else { |
| 7387 | if (q >= qe) |
| 7388 | goto overflow2; |
| 7389 | *q++ = *p; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7390 | j++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7391 | if (*p == '\n' || *p == '\r') |
| 7392 | j = 0; |
| 7393 | } |
| 7394 | |
| 7395 | return (PyObject*) u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7396 | |
| 7397 | overflow2: |
| 7398 | Py_DECREF(u); |
| 7399 | overflow1: |
| 7400 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 7401 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7402 | } |
| 7403 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7404 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7405 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7406 | \n\ |
| 7407 | 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] | 7408 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7409 | arguments start and end are interpreted as in slice notation.\n\ |
| 7410 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7411 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7412 | |
| 7413 | static PyObject * |
| 7414 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 7415 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7416 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7417 | Py_ssize_t start; |
| 7418 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7419 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7420 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7421 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7422 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7423 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7424 | result = stringlib_find_slice( |
| 7425 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7426 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7427 | start, end |
| 7428 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7429 | |
| 7430 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7431 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7432 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7433 | } |
| 7434 | |
| 7435 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7436 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7437 | { |
| 7438 | if (index < 0 || index >= self->length) { |
| 7439 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 7440 | return NULL; |
| 7441 | } |
| 7442 | |
| 7443 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 7444 | } |
| 7445 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7446 | /* Believe it or not, this produces the same value for ASCII strings |
| 7447 | as string_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 7448 | static Py_hash_t |
Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 7449 | unicode_hash(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7450 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7451 | Py_ssize_t len; |
| 7452 | Py_UNICODE *p; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 7453 | Py_hash_t x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7454 | |
| 7455 | if (self->hash != -1) |
| 7456 | return self->hash; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7457 | len = Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7458 | p = self->str; |
| 7459 | x = *p << 7; |
| 7460 | while (--len >= 0) |
| 7461 | x = (1000003*x) ^ *p++; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7462 | x ^= Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7463 | if (x == -1) |
| 7464 | x = -2; |
| 7465 | self->hash = x; |
| 7466 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7467 | } |
| 7468 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7469 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7470 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7471 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7472 | 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] | 7473 | |
| 7474 | static PyObject * |
| 7475 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 7476 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7477 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7478 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7479 | Py_ssize_t start; |
| 7480 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7481 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7482 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7483 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7484 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7485 | result = stringlib_find_slice( |
| 7486 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7487 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7488 | start, end |
| 7489 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7490 | |
| 7491 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7492 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7493 | if (result < 0) { |
| 7494 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 7495 | return NULL; |
| 7496 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7497 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7498 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7499 | } |
| 7500 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7501 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7502 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7503 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7504 | 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] | 7505 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7506 | |
| 7507 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7508 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7509 | { |
| 7510 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7511 | register const Py_UNICODE *e; |
| 7512 | int cased; |
| 7513 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7514 | /* Shortcut for single character strings */ |
| 7515 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7516 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7517 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7518 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7519 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7520 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7521 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7522 | e = p + PyUnicode_GET_SIZE(self); |
| 7523 | cased = 0; |
| 7524 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7525 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7526 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7527 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 7528 | return PyBool_FromLong(0); |
| 7529 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 7530 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7531 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7532 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7533 | } |
| 7534 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7535 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7536 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7537 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7538 | 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] | 7539 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7540 | |
| 7541 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7542 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7543 | { |
| 7544 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7545 | register const Py_UNICODE *e; |
| 7546 | int cased; |
| 7547 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7548 | /* Shortcut for single character strings */ |
| 7549 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7550 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7551 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7552 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7553 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7554 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7555 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7556 | e = p + PyUnicode_GET_SIZE(self); |
| 7557 | cased = 0; |
| 7558 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7559 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7560 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7561 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 7562 | return PyBool_FromLong(0); |
| 7563 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 7564 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7565 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7566 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7567 | } |
| 7568 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7569 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7570 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7571 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7572 | Return True if S is a titlecased string and there is at least one\n\ |
| 7573 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 7574 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 7575 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7576 | |
| 7577 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7578 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7579 | { |
| 7580 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7581 | register const Py_UNICODE *e; |
| 7582 | int cased, previous_is_cased; |
| 7583 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7584 | /* Shortcut for single character strings */ |
| 7585 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7586 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 7587 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7588 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7589 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7590 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7591 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7592 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7593 | e = p + PyUnicode_GET_SIZE(self); |
| 7594 | cased = 0; |
| 7595 | previous_is_cased = 0; |
| 7596 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7597 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7598 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7599 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 7600 | if (previous_is_cased) |
| 7601 | return PyBool_FromLong(0); |
| 7602 | previous_is_cased = 1; |
| 7603 | cased = 1; |
| 7604 | } |
| 7605 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 7606 | if (!previous_is_cased) |
| 7607 | return PyBool_FromLong(0); |
| 7608 | previous_is_cased = 1; |
| 7609 | cased = 1; |
| 7610 | } |
| 7611 | else |
| 7612 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7613 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7614 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7615 | } |
| 7616 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7617 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7618 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7619 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7620 | Return True if all characters in S are whitespace\n\ |
| 7621 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7622 | |
| 7623 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7624 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7625 | { |
| 7626 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7627 | register const Py_UNICODE *e; |
| 7628 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7629 | /* Shortcut for single character strings */ |
| 7630 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7631 | Py_UNICODE_ISSPACE(*p)) |
| 7632 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7633 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7634 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7635 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7636 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7637 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7638 | e = p + PyUnicode_GET_SIZE(self); |
| 7639 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7640 | if (!Py_UNICODE_ISSPACE(*p)) |
| 7641 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7642 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7643 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7644 | } |
| 7645 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7646 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7647 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7648 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7649 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7650 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7651 | |
| 7652 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7653 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7654 | { |
| 7655 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7656 | register const Py_UNICODE *e; |
| 7657 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7658 | /* Shortcut for single character strings */ |
| 7659 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7660 | Py_UNICODE_ISALPHA(*p)) |
| 7661 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7662 | |
| 7663 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7664 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7665 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7666 | |
| 7667 | e = p + PyUnicode_GET_SIZE(self); |
| 7668 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7669 | if (!Py_UNICODE_ISALPHA(*p)) |
| 7670 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7671 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7672 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7673 | } |
| 7674 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7675 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7676 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7677 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7678 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7679 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7680 | |
| 7681 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7682 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7683 | { |
| 7684 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7685 | register const Py_UNICODE *e; |
| 7686 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7687 | /* Shortcut for single character strings */ |
| 7688 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7689 | Py_UNICODE_ISALNUM(*p)) |
| 7690 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7691 | |
| 7692 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7693 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7694 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7695 | |
| 7696 | e = p + PyUnicode_GET_SIZE(self); |
| 7697 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7698 | if (!Py_UNICODE_ISALNUM(*p)) |
| 7699 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7700 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7701 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7702 | } |
| 7703 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7704 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7705 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7706 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7707 | 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] | 7708 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7709 | |
| 7710 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7711 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7712 | { |
| 7713 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7714 | register const Py_UNICODE *e; |
| 7715 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7716 | /* Shortcut for single character strings */ |
| 7717 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7718 | Py_UNICODE_ISDECIMAL(*p)) |
| 7719 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7720 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7721 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7722 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7723 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7724 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7725 | e = p + PyUnicode_GET_SIZE(self); |
| 7726 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7727 | if (!Py_UNICODE_ISDECIMAL(*p)) |
| 7728 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7729 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7730 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7731 | } |
| 7732 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7733 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7734 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7735 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7736 | Return True if all characters in S are digits\n\ |
| 7737 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7738 | |
| 7739 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7740 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7741 | { |
| 7742 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7743 | register const Py_UNICODE *e; |
| 7744 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7745 | /* Shortcut for single character strings */ |
| 7746 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7747 | Py_UNICODE_ISDIGIT(*p)) |
| 7748 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7749 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7750 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7751 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7752 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7753 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7754 | e = p + PyUnicode_GET_SIZE(self); |
| 7755 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7756 | if (!Py_UNICODE_ISDIGIT(*p)) |
| 7757 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7758 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7759 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7760 | } |
| 7761 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7762 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7763 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7764 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7765 | 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] | 7766 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7767 | |
| 7768 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7769 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7770 | { |
| 7771 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7772 | register const Py_UNICODE *e; |
| 7773 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7774 | /* Shortcut for single character strings */ |
| 7775 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7776 | Py_UNICODE_ISNUMERIC(*p)) |
| 7777 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7778 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7779 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7780 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7781 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7782 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7783 | e = p + PyUnicode_GET_SIZE(self); |
| 7784 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7785 | if (!Py_UNICODE_ISNUMERIC(*p)) |
| 7786 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7787 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7788 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7789 | } |
| 7790 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7791 | int |
| 7792 | PyUnicode_IsIdentifier(PyObject *self) |
| 7793 | { |
| 7794 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE((PyUnicodeObject*)self); |
| 7795 | register const Py_UNICODE *e; |
| 7796 | |
| 7797 | /* Special case for empty strings */ |
| 7798 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7799 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7800 | |
| 7801 | /* PEP 3131 says that the first character must be in |
| 7802 | XID_Start and subsequent characters in XID_Continue, |
| 7803 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7804 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7805 | letters, digits, underscore). However, given the current |
| 7806 | definition of XID_Start and XID_Continue, it is sufficient |
| 7807 | to check just for these, except that _ must be allowed |
| 7808 | as starting an identifier. */ |
| 7809 | if (!_PyUnicode_IsXidStart(*p) && *p != 0x5F /* LOW LINE */) |
| 7810 | return 0; |
| 7811 | |
| 7812 | e = p + PyUnicode_GET_SIZE(self); |
| 7813 | for (p++; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7814 | if (!_PyUnicode_IsXidContinue(*p)) |
| 7815 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7816 | } |
| 7817 | return 1; |
| 7818 | } |
| 7819 | |
| 7820 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7821 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7822 | \n\ |
| 7823 | Return True if S is a valid identifier according\n\ |
| 7824 | to the language definition."); |
| 7825 | |
| 7826 | static PyObject* |
| 7827 | unicode_isidentifier(PyObject *self) |
| 7828 | { |
| 7829 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 7830 | } |
| 7831 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7832 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7833 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7834 | \n\ |
| 7835 | Return True if all characters in S are considered\n\ |
| 7836 | printable in repr() or S is empty, False otherwise."); |
| 7837 | |
| 7838 | static PyObject* |
| 7839 | unicode_isprintable(PyObject *self) |
| 7840 | { |
| 7841 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7842 | register const Py_UNICODE *e; |
| 7843 | |
| 7844 | /* Shortcut for single character strings */ |
| 7845 | if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISPRINTABLE(*p)) { |
| 7846 | Py_RETURN_TRUE; |
| 7847 | } |
| 7848 | |
| 7849 | e = p + PyUnicode_GET_SIZE(self); |
| 7850 | for (; p < e; p++) { |
| 7851 | if (!Py_UNICODE_ISPRINTABLE(*p)) { |
| 7852 | Py_RETURN_FALSE; |
| 7853 | } |
| 7854 | } |
| 7855 | Py_RETURN_TRUE; |
| 7856 | } |
| 7857 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7858 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 7859 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7860 | \n\ |
| 7861 | 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] | 7862 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7863 | |
| 7864 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7865 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7866 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7867 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7868 | } |
| 7869 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7870 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7871 | unicode_length(PyUnicodeObject *self) |
| 7872 | { |
| 7873 | return self->length; |
| 7874 | } |
| 7875 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7876 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7877 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7878 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 7879 | 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] | 7880 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7881 | |
| 7882 | static PyObject * |
| 7883 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 7884 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7885 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7886 | Py_UNICODE fillchar = ' '; |
| 7887 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7888 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7889 | return NULL; |
| 7890 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7891 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7892 | Py_INCREF(self); |
| 7893 | return (PyObject*) self; |
| 7894 | } |
| 7895 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7896 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7897 | } |
| 7898 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7899 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7900 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7901 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7902 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7903 | |
| 7904 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7905 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7906 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7907 | return fixup(self, fixlower); |
| 7908 | } |
| 7909 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7910 | #define LEFTSTRIP 0 |
| 7911 | #define RIGHTSTRIP 1 |
| 7912 | #define BOTHSTRIP 2 |
| 7913 | |
| 7914 | /* Arrays indexed by above */ |
| 7915 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 7916 | |
| 7917 | #define STRIPNAME(i) (stripformat[i]+3) |
| 7918 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7919 | /* externally visible for str.strip(unicode) */ |
| 7920 | PyObject * |
| 7921 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 7922 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7923 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 7924 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
| 7925 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
| 7926 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 7927 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7928 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7929 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7930 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7931 | i = 0; |
| 7932 | if (striptype != RIGHTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7933 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 7934 | i++; |
| 7935 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7936 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7937 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7938 | j = len; |
| 7939 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7940 | do { |
| 7941 | j--; |
| 7942 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 7943 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7944 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7945 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7946 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7947 | Py_INCREF(self); |
| 7948 | return (PyObject*)self; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7949 | } |
| 7950 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7951 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7952 | } |
| 7953 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7954 | |
| 7955 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7956 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7957 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7958 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 7959 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7960 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7961 | i = 0; |
| 7962 | if (striptype != RIGHTSTRIP) { |
| 7963 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 7964 | i++; |
| 7965 | } |
| 7966 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7967 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7968 | j = len; |
| 7969 | if (striptype != LEFTSTRIP) { |
| 7970 | do { |
| 7971 | j--; |
| 7972 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 7973 | j++; |
| 7974 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7975 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7976 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 7977 | Py_INCREF(self); |
| 7978 | return (PyObject*)self; |
| 7979 | } |
| 7980 | else |
| 7981 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7982 | } |
| 7983 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7984 | |
| 7985 | static PyObject * |
| 7986 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 7987 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7988 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7989 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7990 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 7991 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7992 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7993 | if (sep != NULL && sep != Py_None) { |
| 7994 | if (PyUnicode_Check(sep)) |
| 7995 | return _PyUnicode_XStrip(self, striptype, sep); |
| 7996 | else { |
| 7997 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7998 | "%s arg must be None or str", |
| 7999 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8000 | return NULL; |
| 8001 | } |
| 8002 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8003 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8004 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8005 | } |
| 8006 | |
| 8007 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8008 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8009 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8010 | \n\ |
| 8011 | Return a copy of the string S with leading and trailing\n\ |
| 8012 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8013 | 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] | 8014 | |
| 8015 | static PyObject * |
| 8016 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 8017 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8018 | if (PyTuple_GET_SIZE(args) == 0) |
| 8019 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 8020 | else |
| 8021 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8022 | } |
| 8023 | |
| 8024 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8025 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8026 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8027 | \n\ |
| 8028 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8029 | 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] | 8030 | |
| 8031 | static PyObject * |
| 8032 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 8033 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8034 | if (PyTuple_GET_SIZE(args) == 0) |
| 8035 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 8036 | else |
| 8037 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8038 | } |
| 8039 | |
| 8040 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8041 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8042 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8043 | \n\ |
| 8044 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8045 | 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] | 8046 | |
| 8047 | static PyObject * |
| 8048 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 8049 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8050 | if (PyTuple_GET_SIZE(args) == 0) |
| 8051 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 8052 | else |
| 8053 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8054 | } |
| 8055 | |
| 8056 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8057 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8058 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8059 | { |
| 8060 | PyUnicodeObject *u; |
| 8061 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8062 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 8063 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8064 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 8065 | if (len < 1) { |
| 8066 | Py_INCREF(unicode_empty); |
| 8067 | return (PyObject *)unicode_empty; |
| 8068 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8069 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 8070 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8071 | /* no repeat, return original string */ |
| 8072 | Py_INCREF(str); |
| 8073 | return (PyObject*) str; |
| 8074 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 8075 | |
| 8076 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 8077 | * needed doesn't overflow size_t |
| 8078 | */ |
| 8079 | nchars = len * str->length; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 8080 | if (nchars / len != str->length) { |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 8081 | PyErr_SetString(PyExc_OverflowError, |
| 8082 | "repeated string is too long"); |
| 8083 | return NULL; |
| 8084 | } |
| 8085 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 8086 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 8087 | PyErr_SetString(PyExc_OverflowError, |
| 8088 | "repeated string is too long"); |
| 8089 | return NULL; |
| 8090 | } |
| 8091 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8092 | if (!u) |
| 8093 | return NULL; |
| 8094 | |
| 8095 | p = u->str; |
| 8096 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 8097 | if (str->length == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8098 | Py_UNICODE_FILL(p, str->str[0], len); |
| 8099 | } else { |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 8100 | Py_ssize_t done = str->length; /* number of characters copied this far */ |
| 8101 | Py_UNICODE_COPY(p, str->str, str->length); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8102 | while (done < nchars) { |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 8103 | Py_ssize_t n = (done <= nchars-done) ? done : nchars-done; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8104 | Py_UNICODE_COPY(p+done, p, n); |
| 8105 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8106 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8107 | } |
| 8108 | |
| 8109 | return (PyObject*) u; |
| 8110 | } |
| 8111 | |
| 8112 | PyObject *PyUnicode_Replace(PyObject *obj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8113 | PyObject *subobj, |
| 8114 | PyObject *replobj, |
| 8115 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8116 | { |
| 8117 | PyObject *self; |
| 8118 | PyObject *str1; |
| 8119 | PyObject *str2; |
| 8120 | PyObject *result; |
| 8121 | |
| 8122 | self = PyUnicode_FromObject(obj); |
| 8123 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8124 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8125 | str1 = PyUnicode_FromObject(subobj); |
| 8126 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8127 | Py_DECREF(self); |
| 8128 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8129 | } |
| 8130 | str2 = PyUnicode_FromObject(replobj); |
| 8131 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8132 | Py_DECREF(self); |
| 8133 | Py_DECREF(str1); |
| 8134 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8135 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8136 | result = replace((PyUnicodeObject *)self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8137 | (PyUnicodeObject *)str1, |
| 8138 | (PyUnicodeObject *)str2, |
| 8139 | maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8140 | Py_DECREF(self); |
| 8141 | Py_DECREF(str1); |
| 8142 | Py_DECREF(str2); |
| 8143 | return result; |
| 8144 | } |
| 8145 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8146 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 8147 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8148 | \n\ |
| 8149 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 8150 | old replaced by new. If the optional argument count is\n\ |
| 8151 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8152 | |
| 8153 | static PyObject* |
| 8154 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 8155 | { |
| 8156 | PyUnicodeObject *str1; |
| 8157 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8158 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8159 | PyObject *result; |
| 8160 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8161 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8162 | return NULL; |
| 8163 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 8164 | if (str1 == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8165 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8166 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 8167 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8168 | Py_DECREF(str1); |
| 8169 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 8170 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8171 | |
| 8172 | result = replace(self, str1, str2, maxcount); |
| 8173 | |
| 8174 | Py_DECREF(str1); |
| 8175 | Py_DECREF(str2); |
| 8176 | return result; |
| 8177 | } |
| 8178 | |
| 8179 | static |
| 8180 | PyObject *unicode_repr(PyObject *unicode) |
| 8181 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8182 | PyObject *repr; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8183 | Py_UNICODE *p; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8184 | Py_UNICODE *s = PyUnicode_AS_UNICODE(unicode); |
| 8185 | Py_ssize_t size = PyUnicode_GET_SIZE(unicode); |
| 8186 | |
| 8187 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 8188 | better to choose a different scheme. Perhaps scan the |
| 8189 | first N-chars of the string and allocate based on that size. |
| 8190 | */ |
| 8191 | /* Initial allocation is based on the longest-possible unichr |
| 8192 | escape. |
| 8193 | |
| 8194 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 8195 | unichr, so in this case it's the longest unichr escape. In |
| 8196 | narrow (UTF-16) builds this is five chars per source unichr |
| 8197 | since there are two unichrs in the surrogate pair, so in narrow |
| 8198 | (UTF-16) builds it's not the longest unichr escape. |
| 8199 | |
| 8200 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 8201 | so in the narrow (UTF-16) build case it's the longest unichr |
| 8202 | escape. |
| 8203 | */ |
| 8204 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8205 | repr = PyUnicode_FromUnicode(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8206 | 2 /* quotes */ |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8207 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8208 | + 10*size |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8209 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8210 | + 6*size |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8211 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8212 | + 1); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8213 | if (repr == NULL) |
| 8214 | return NULL; |
| 8215 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8216 | p = PyUnicode_AS_UNICODE(repr); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8217 | |
| 8218 | /* Add quote */ |
| 8219 | *p++ = (findchar(s, size, '\'') && |
| 8220 | !findchar(s, size, '"')) ? '"' : '\''; |
| 8221 | while (size-- > 0) { |
| 8222 | Py_UNICODE ch = *s++; |
| 8223 | |
| 8224 | /* Escape quotes and backslashes */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8225 | if ((ch == PyUnicode_AS_UNICODE(repr)[0]) || (ch == '\\')) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8226 | *p++ = '\\'; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8227 | *p++ = ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8228 | continue; |
| 8229 | } |
| 8230 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8231 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8232 | if (ch == '\t') { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8233 | *p++ = '\\'; |
| 8234 | *p++ = 't'; |
| 8235 | } |
| 8236 | else if (ch == '\n') { |
| 8237 | *p++ = '\\'; |
| 8238 | *p++ = 'n'; |
| 8239 | } |
| 8240 | else if (ch == '\r') { |
| 8241 | *p++ = '\\'; |
| 8242 | *p++ = 'r'; |
| 8243 | } |
| 8244 | |
| 8245 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8246 | else if (ch < ' ' || ch == 0x7F) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8247 | *p++ = '\\'; |
| 8248 | *p++ = 'x'; |
| 8249 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 8250 | *p++ = hexdigits[ch & 0x000F]; |
| 8251 | } |
| 8252 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8253 | /* Copy ASCII characters as-is */ |
| 8254 | else if (ch < 0x7F) { |
| 8255 | *p++ = ch; |
| 8256 | } |
| 8257 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8258 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8259 | else { |
| 8260 | Py_UCS4 ucs = ch; |
| 8261 | |
| 8262 | #ifndef Py_UNICODE_WIDE |
| 8263 | Py_UNICODE ch2 = 0; |
| 8264 | /* Get code point from surrogate pair */ |
| 8265 | if (size > 0) { |
| 8266 | ch2 = *s; |
| 8267 | if (ch >= 0xD800 && ch < 0xDC00 && ch2 >= 0xDC00 |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8268 | && ch2 <= 0xDFFF) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8269 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8270 | + 0x00010000; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8271 | s++; |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8272 | size--; |
| 8273 | } |
| 8274 | } |
| 8275 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8276 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8277 | (categories Z* and C* except ASCII space) |
| 8278 | */ |
| 8279 | if (!Py_UNICODE_ISPRINTABLE(ucs)) { |
| 8280 | /* Map 8-bit characters to '\xhh' */ |
| 8281 | if (ucs <= 0xff) { |
| 8282 | *p++ = '\\'; |
| 8283 | *p++ = 'x'; |
| 8284 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 8285 | *p++ = hexdigits[ch & 0x000F]; |
| 8286 | } |
| 8287 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 8288 | else if (ucs >= 0x10000) { |
| 8289 | *p++ = '\\'; |
| 8290 | *p++ = 'U'; |
| 8291 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 8292 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 8293 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 8294 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 8295 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 8296 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 8297 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 8298 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 8299 | } |
| 8300 | /* Map 16-bit characters to '\uxxxx' */ |
| 8301 | else { |
| 8302 | *p++ = '\\'; |
| 8303 | *p++ = 'u'; |
| 8304 | *p++ = hexdigits[(ucs >> 12) & 0x000F]; |
| 8305 | *p++ = hexdigits[(ucs >> 8) & 0x000F]; |
| 8306 | *p++ = hexdigits[(ucs >> 4) & 0x000F]; |
| 8307 | *p++ = hexdigits[ucs & 0x000F]; |
| 8308 | } |
| 8309 | } |
| 8310 | /* Copy characters as-is */ |
| 8311 | else { |
| 8312 | *p++ = ch; |
| 8313 | #ifndef Py_UNICODE_WIDE |
| 8314 | if (ucs >= 0x10000) |
| 8315 | *p++ = ch2; |
| 8316 | #endif |
| 8317 | } |
| 8318 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8319 | } |
| 8320 | /* Add quote */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8321 | *p++ = PyUnicode_AS_UNICODE(repr)[0]; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8322 | |
| 8323 | *p = '\0'; |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 8324 | PyUnicode_Resize(&repr, p - PyUnicode_AS_UNICODE(repr)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8325 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8326 | } |
| 8327 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8328 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8329 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8330 | \n\ |
| 8331 | 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] | 8332 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8333 | arguments start and end are interpreted as in slice notation.\n\ |
| 8334 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8335 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8336 | |
| 8337 | static PyObject * |
| 8338 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 8339 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8340 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8341 | Py_ssize_t start; |
| 8342 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8343 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8344 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8345 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8346 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8347 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8348 | result = stringlib_rfind_slice( |
| 8349 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 8350 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 8351 | start, end |
| 8352 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8353 | |
| 8354 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8355 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8356 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8357 | } |
| 8358 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8359 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8360 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8361 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8362 | 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] | 8363 | |
| 8364 | static PyObject * |
| 8365 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 8366 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8367 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8368 | Py_ssize_t start; |
| 8369 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8370 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8371 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8372 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8373 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8374 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8375 | result = stringlib_rfind_slice( |
| 8376 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 8377 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 8378 | start, end |
| 8379 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8380 | |
| 8381 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8382 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8383 | if (result < 0) { |
| 8384 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 8385 | return NULL; |
| 8386 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8387 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8388 | } |
| 8389 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8390 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8391 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8392 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 8393 | 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] | 8394 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8395 | |
| 8396 | static PyObject * |
| 8397 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 8398 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8399 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 8400 | Py_UNICODE fillchar = ' '; |
| 8401 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8402 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8403 | return NULL; |
| 8404 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 8405 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8406 | Py_INCREF(self); |
| 8407 | return (PyObject*) self; |
| 8408 | } |
| 8409 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 8410 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8411 | } |
| 8412 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8413 | PyObject *PyUnicode_Split(PyObject *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8414 | PyObject *sep, |
| 8415 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8416 | { |
| 8417 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8418 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8419 | s = PyUnicode_FromObject(s); |
| 8420 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8421 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8422 | if (sep != NULL) { |
| 8423 | sep = PyUnicode_FromObject(sep); |
| 8424 | if (sep == NULL) { |
| 8425 | Py_DECREF(s); |
| 8426 | return NULL; |
| 8427 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8428 | } |
| 8429 | |
| 8430 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 8431 | |
| 8432 | Py_DECREF(s); |
| 8433 | Py_XDECREF(sep); |
| 8434 | return result; |
| 8435 | } |
| 8436 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8437 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8438 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8439 | \n\ |
| 8440 | Return a list of the words in S, using sep as the\n\ |
| 8441 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 8442 | 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] | 8443 | whitespace string is a separator and empty strings are\n\ |
| 8444 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8445 | |
| 8446 | static PyObject* |
| 8447 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 8448 | { |
| 8449 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8450 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8451 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8452 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8453 | return NULL; |
| 8454 | |
| 8455 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8456 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8457 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8458 | return split(self, (PyUnicodeObject *)substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8459 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8460 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8461 | } |
| 8462 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8463 | PyObject * |
| 8464 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 8465 | { |
| 8466 | PyObject* str_obj; |
| 8467 | PyObject* sep_obj; |
| 8468 | PyObject* out; |
| 8469 | |
| 8470 | str_obj = PyUnicode_FromObject(str_in); |
| 8471 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8472 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8473 | sep_obj = PyUnicode_FromObject(sep_in); |
| 8474 | if (!sep_obj) { |
| 8475 | Py_DECREF(str_obj); |
| 8476 | return NULL; |
| 8477 | } |
| 8478 | |
| 8479 | out = stringlib_partition( |
| 8480 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 8481 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 8482 | ); |
| 8483 | |
| 8484 | Py_DECREF(sep_obj); |
| 8485 | Py_DECREF(str_obj); |
| 8486 | |
| 8487 | return out; |
| 8488 | } |
| 8489 | |
| 8490 | |
| 8491 | PyObject * |
| 8492 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 8493 | { |
| 8494 | PyObject* str_obj; |
| 8495 | PyObject* sep_obj; |
| 8496 | PyObject* out; |
| 8497 | |
| 8498 | str_obj = PyUnicode_FromObject(str_in); |
| 8499 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8500 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8501 | sep_obj = PyUnicode_FromObject(sep_in); |
| 8502 | if (!sep_obj) { |
| 8503 | Py_DECREF(str_obj); |
| 8504 | return NULL; |
| 8505 | } |
| 8506 | |
| 8507 | out = stringlib_rpartition( |
| 8508 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 8509 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 8510 | ); |
| 8511 | |
| 8512 | Py_DECREF(sep_obj); |
| 8513 | Py_DECREF(str_obj); |
| 8514 | |
| 8515 | return out; |
| 8516 | } |
| 8517 | |
| 8518 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8519 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8520 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8521 | 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] | 8522 | 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] | 8523 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8524 | |
| 8525 | static PyObject* |
| 8526 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
| 8527 | { |
| 8528 | return PyUnicode_Partition((PyObject *)self, separator); |
| 8529 | } |
| 8530 | |
| 8531 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 8532 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8533 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8534 | 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] | 8535 | 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] | 8536 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8537 | |
| 8538 | static PyObject* |
| 8539 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 8540 | { |
| 8541 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 8542 | } |
| 8543 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8544 | PyObject *PyUnicode_RSplit(PyObject *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8545 | PyObject *sep, |
| 8546 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8547 | { |
| 8548 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8549 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8550 | s = PyUnicode_FromObject(s); |
| 8551 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8552 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8553 | if (sep != NULL) { |
| 8554 | sep = PyUnicode_FromObject(sep); |
| 8555 | if (sep == NULL) { |
| 8556 | Py_DECREF(s); |
| 8557 | return NULL; |
| 8558 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8559 | } |
| 8560 | |
| 8561 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 8562 | |
| 8563 | Py_DECREF(s); |
| 8564 | Py_XDECREF(sep); |
| 8565 | return result; |
| 8566 | } |
| 8567 | |
| 8568 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8569 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8570 | \n\ |
| 8571 | Return a list of the words in S, using sep as the\n\ |
| 8572 | delimiter string, starting at the end of the string and\n\ |
| 8573 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 8574 | splits are done. If sep is not specified, any whitespace string\n\ |
| 8575 | is a separator."); |
| 8576 | |
| 8577 | static PyObject* |
| 8578 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 8579 | { |
| 8580 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8581 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8582 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8583 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8584 | return NULL; |
| 8585 | |
| 8586 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8587 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8588 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8589 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8590 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8591 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8592 | } |
| 8593 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8594 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8595 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8596 | \n\ |
| 8597 | 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] | 8598 | 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] | 8599 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8600 | |
| 8601 | static PyObject* |
| 8602 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 8603 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8604 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8605 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8606 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8607 | return NULL; |
| 8608 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8609 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8610 | } |
| 8611 | |
| 8612 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 8613 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8614 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 8615 | if (PyUnicode_CheckExact(self)) { |
| 8616 | Py_INCREF(self); |
| 8617 | return self; |
| 8618 | } else |
| 8619 | /* Subtype -- return genuine unicode string with the same value. */ |
| 8620 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(self), |
| 8621 | PyUnicode_GET_SIZE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8622 | } |
| 8623 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8624 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8625 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8626 | \n\ |
| 8627 | 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] | 8628 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8629 | |
| 8630 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8631 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8632 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8633 | return fixup(self, fixswapcase); |
| 8634 | } |
| 8635 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8636 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8637 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8638 | \n\ |
| 8639 | Return a translation table usable for str.translate().\n\ |
| 8640 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 8641 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8642 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8643 | If there are two arguments, they must be strings of equal length, and\n\ |
| 8644 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 8645 | character at the same position in y. If there is a third argument, it\n\ |
| 8646 | must be a string, whose characters will be mapped to None in the result."); |
| 8647 | |
| 8648 | static PyObject* |
| 8649 | unicode_maketrans(PyUnicodeObject *null, PyObject *args) |
| 8650 | { |
| 8651 | PyObject *x, *y = NULL, *z = NULL; |
| 8652 | PyObject *new = NULL, *key, *value; |
| 8653 | Py_ssize_t i = 0; |
| 8654 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8655 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8656 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 8657 | return NULL; |
| 8658 | new = PyDict_New(); |
| 8659 | if (!new) |
| 8660 | return NULL; |
| 8661 | if (y != NULL) { |
| 8662 | /* x must be a string too, of equal length */ |
| 8663 | Py_ssize_t ylen = PyUnicode_GET_SIZE(y); |
| 8664 | if (!PyUnicode_Check(x)) { |
| 8665 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 8666 | "be a string if there is a second argument"); |
| 8667 | goto err; |
| 8668 | } |
| 8669 | if (PyUnicode_GET_SIZE(x) != ylen) { |
| 8670 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 8671 | "arguments must have equal length"); |
| 8672 | goto err; |
| 8673 | } |
| 8674 | /* create entries for translating chars in x to those in y */ |
| 8675 | for (i = 0; i < PyUnicode_GET_SIZE(x); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8676 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(x)[i]); |
| 8677 | value = PyLong_FromLong(PyUnicode_AS_UNICODE(y)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8678 | if (!key || !value) |
| 8679 | goto err; |
| 8680 | res = PyDict_SetItem(new, key, value); |
| 8681 | Py_DECREF(key); |
| 8682 | Py_DECREF(value); |
| 8683 | if (res < 0) |
| 8684 | goto err; |
| 8685 | } |
| 8686 | /* create entries for deleting chars in z */ |
| 8687 | if (z != NULL) { |
| 8688 | for (i = 0; i < PyUnicode_GET_SIZE(z); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8689 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(z)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8690 | if (!key) |
| 8691 | goto err; |
| 8692 | res = PyDict_SetItem(new, key, Py_None); |
| 8693 | Py_DECREF(key); |
| 8694 | if (res < 0) |
| 8695 | goto err; |
| 8696 | } |
| 8697 | } |
| 8698 | } else { |
| 8699 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 8700 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8701 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 8702 | "to maketrans it must be a dict"); |
| 8703 | goto err; |
| 8704 | } |
| 8705 | /* copy entries into the new dict, converting string keys to int keys */ |
| 8706 | while (PyDict_Next(x, &i, &key, &value)) { |
| 8707 | if (PyUnicode_Check(key)) { |
| 8708 | /* convert string keys to integer keys */ |
| 8709 | PyObject *newkey; |
| 8710 | if (PyUnicode_GET_SIZE(key) != 1) { |
| 8711 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 8712 | "table must be of length 1"); |
| 8713 | goto err; |
| 8714 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8715 | newkey = PyLong_FromLong(PyUnicode_AS_UNICODE(key)[0]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8716 | if (!newkey) |
| 8717 | goto err; |
| 8718 | res = PyDict_SetItem(new, newkey, value); |
| 8719 | Py_DECREF(newkey); |
| 8720 | if (res < 0) |
| 8721 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8722 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8723 | /* just keep integer keys */ |
| 8724 | if (PyDict_SetItem(new, key, value) < 0) |
| 8725 | goto err; |
| 8726 | } else { |
| 8727 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 8728 | "be strings or integers"); |
| 8729 | goto err; |
| 8730 | } |
| 8731 | } |
| 8732 | } |
| 8733 | return new; |
| 8734 | err: |
| 8735 | Py_DECREF(new); |
| 8736 | return NULL; |
| 8737 | } |
| 8738 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8739 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8740 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8741 | \n\ |
| 8742 | Return a copy of the string S, where all characters have been mapped\n\ |
| 8743 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8744 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 8745 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 8746 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8747 | |
| 8748 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8749 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8750 | { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8751 | return PyUnicode_TranslateCharmap(self->str, self->length, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8752 | } |
| 8753 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8754 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8755 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8756 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8757 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8758 | |
| 8759 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8760 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8761 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8762 | return fixup(self, fixupper); |
| 8763 | } |
| 8764 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8765 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8766 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8767 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 8768 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 8769 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8770 | |
| 8771 | static PyObject * |
| 8772 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 8773 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8774 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8775 | PyUnicodeObject *u; |
| 8776 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8777 | Py_ssize_t width; |
| 8778 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8779 | return NULL; |
| 8780 | |
| 8781 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 8782 | if (PyUnicode_CheckExact(self)) { |
| 8783 | Py_INCREF(self); |
| 8784 | return (PyObject*) self; |
| 8785 | } |
| 8786 | else |
| 8787 | return PyUnicode_FromUnicode( |
| 8788 | PyUnicode_AS_UNICODE(self), |
| 8789 | PyUnicode_GET_SIZE(self) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8790 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8791 | } |
| 8792 | |
| 8793 | fill = width - self->length; |
| 8794 | |
| 8795 | u = pad(self, fill, 0, '0'); |
| 8796 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8797 | if (u == NULL) |
| 8798 | return NULL; |
| 8799 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8800 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 8801 | /* move sign to beginning of string */ |
| 8802 | u->str[0] = u->str[fill]; |
| 8803 | u->str[fill] = '0'; |
| 8804 | } |
| 8805 | |
| 8806 | return (PyObject*) u; |
| 8807 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8808 | |
| 8809 | #if 0 |
| 8810 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8811 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8812 | { |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 8813 | return PyLong_FromLong(numfree); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8814 | } |
| 8815 | #endif |
| 8816 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8817 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8818 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8819 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 8820 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 8821 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8822 | With optional end, stop comparing S at that position.\n\ |
| 8823 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8824 | |
| 8825 | static PyObject * |
| 8826 | unicode_startswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8827 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8828 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8829 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8830 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8831 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8832 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8833 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8834 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8835 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8836 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 8837 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8838 | if (PyTuple_Check(subobj)) { |
| 8839 | Py_ssize_t i; |
| 8840 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 8841 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8842 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8843 | if (substring == NULL) |
| 8844 | return NULL; |
| 8845 | result = tailmatch(self, substring, start, end, -1); |
| 8846 | Py_DECREF(substring); |
| 8847 | if (result) { |
| 8848 | Py_RETURN_TRUE; |
| 8849 | } |
| 8850 | } |
| 8851 | /* nothing matched */ |
| 8852 | Py_RETURN_FALSE; |
| 8853 | } |
| 8854 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8855 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8856 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8857 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8858 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8859 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8860 | } |
| 8861 | |
| 8862 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8863 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8864 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8865 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 8866 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 8867 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8868 | With optional end, stop comparing S at that position.\n\ |
| 8869 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8870 | |
| 8871 | static PyObject * |
| 8872 | unicode_endswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8873 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8874 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8875 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8876 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8877 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8878 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8879 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8880 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8881 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8882 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 8883 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8884 | if (PyTuple_Check(subobj)) { |
| 8885 | Py_ssize_t i; |
| 8886 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 8887 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8888 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8889 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8890 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8891 | result = tailmatch(self, substring, start, end, +1); |
| 8892 | Py_DECREF(substring); |
| 8893 | if (result) { |
| 8894 | Py_RETURN_TRUE; |
| 8895 | } |
| 8896 | } |
| 8897 | Py_RETURN_FALSE; |
| 8898 | } |
| 8899 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8900 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8901 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8902 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8903 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8904 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8905 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8906 | } |
| 8907 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8908 | #include "stringlib/string_format.h" |
| 8909 | |
| 8910 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8911 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8912 | \n\ |
| 8913 | "); |
| 8914 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 8915 | static PyObject * |
| 8916 | unicode__format__(PyObject* self, PyObject* args) |
| 8917 | { |
| 8918 | PyObject *format_spec; |
| 8919 | |
| 8920 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 8921 | return NULL; |
| 8922 | |
| 8923 | return _PyUnicode_FormatAdvanced(self, |
| 8924 | PyUnicode_AS_UNICODE(format_spec), |
| 8925 | PyUnicode_GET_SIZE(format_spec)); |
| 8926 | } |
| 8927 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8928 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8929 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8930 | \n\ |
| 8931 | "); |
| 8932 | |
| 8933 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8934 | unicode__sizeof__(PyUnicodeObject *v) |
| 8935 | { |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 8936 | return PyLong_FromSsize_t(sizeof(PyUnicodeObject) + |
| 8937 | sizeof(Py_UNICODE) * (v->length + 1)); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8938 | } |
| 8939 | |
| 8940 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8941 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8942 | |
| 8943 | static PyObject * |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8944 | unicode_getnewargs(PyUnicodeObject *v) |
| 8945 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8946 | return Py_BuildValue("(u#)", v->str, v->length); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8947 | } |
| 8948 | |
| 8949 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8950 | static PyMethodDef unicode_methods[] = { |
| 8951 | |
| 8952 | /* Order is according to common usage: often used methods should |
| 8953 | appear first, since lookup is done sequentially. */ |
| 8954 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 8955 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8956 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 8957 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8958 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8959 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 8960 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 8961 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 8962 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 8963 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 8964 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 8965 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8966 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8967 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 8968 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 8969 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8970 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8971 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 8972 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 8973 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8974 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8975 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8976 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8977 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8978 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 8979 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 8980 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 8981 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 8982 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 8983 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 8984 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 8985 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 8986 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 8987 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 8988 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 8989 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 8990 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 8991 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 8992 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8993 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8994 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 8995 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 8996 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8997 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 8998 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8999 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 9000 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9001 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9002 | #endif |
| 9003 | |
| 9004 | #if 0 |
| 9005 | /* This one is just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9006 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9007 | #endif |
| 9008 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9009 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9010 | {NULL, NULL} |
| 9011 | }; |
| 9012 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 9013 | static PyObject * |
| 9014 | unicode_mod(PyObject *v, PyObject *w) |
| 9015 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9016 | if (!PyUnicode_Check(v)) { |
| 9017 | Py_INCREF(Py_NotImplemented); |
| 9018 | return Py_NotImplemented; |
| 9019 | } |
| 9020 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 9021 | } |
| 9022 | |
| 9023 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9024 | 0, /*nb_add*/ |
| 9025 | 0, /*nb_subtract*/ |
| 9026 | 0, /*nb_multiply*/ |
| 9027 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 9028 | }; |
| 9029 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9030 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9031 | (lenfunc) unicode_length, /* sq_length */ |
| 9032 | PyUnicode_Concat, /* sq_concat */ |
| 9033 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 9034 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 9035 | 0, /* sq_slice */ |
| 9036 | 0, /* sq_ass_item */ |
| 9037 | 0, /* sq_ass_slice */ |
| 9038 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9039 | }; |
| 9040 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9041 | static PyObject* |
| 9042 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 9043 | { |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9044 | if (PyIndex_Check(item)) { |
| 9045 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9046 | if (i == -1 && PyErr_Occurred()) |
| 9047 | return NULL; |
| 9048 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 9049 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9050 | return unicode_getitem(self, i); |
| 9051 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9052 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9053 | Py_UNICODE* source_buf; |
| 9054 | Py_UNICODE* result_buf; |
| 9055 | PyObject* result; |
| 9056 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 9057 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9058 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9059 | return NULL; |
| 9060 | } |
| 9061 | |
| 9062 | if (slicelength <= 0) { |
| 9063 | return PyUnicode_FromUnicode(NULL, 0); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 9064 | } else if (start == 0 && step == 1 && slicelength == self->length && |
| 9065 | PyUnicode_CheckExact(self)) { |
| 9066 | Py_INCREF(self); |
| 9067 | return (PyObject *)self; |
| 9068 | } else if (step == 1) { |
| 9069 | return PyUnicode_FromUnicode(self->str + start, slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9070 | } else { |
| 9071 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 9072 | result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* |
| 9073 | sizeof(Py_UNICODE)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9074 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9075 | if (result_buf == NULL) |
| 9076 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9077 | |
| 9078 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 9079 | result_buf[i] = source_buf[cur]; |
| 9080 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9081 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9082 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 9083 | PyObject_FREE(result_buf); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9084 | return result; |
| 9085 | } |
| 9086 | } else { |
| 9087 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 9088 | return NULL; |
| 9089 | } |
| 9090 | } |
| 9091 | |
| 9092 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9093 | (lenfunc)unicode_length, /* mp_length */ |
| 9094 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 9095 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9096 | }; |
| 9097 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9098 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9099 | /* Helpers for PyUnicode_Format() */ |
| 9100 | |
| 9101 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9102 | 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] | 9103 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9104 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9105 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9106 | (*p_argidx)++; |
| 9107 | if (arglen < 0) |
| 9108 | return args; |
| 9109 | else |
| 9110 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9111 | } |
| 9112 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9113 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9114 | return NULL; |
| 9115 | } |
| 9116 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9117 | /* 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] | 9118 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9119 | static PyObject * |
| 9120 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9121 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9122 | char *p; |
| 9123 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9124 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9125 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9126 | x = PyFloat_AsDouble(v); |
| 9127 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9128 | return NULL; |
| 9129 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9130 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9131 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 9132 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 9133 | p = PyOS_double_to_string(x, type, prec, |
| 9134 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9135 | if (p == NULL) |
| 9136 | return NULL; |
| 9137 | result = PyUnicode_FromStringAndSize(p, strlen(p)); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 9138 | PyMem_Free(p); |
| 9139 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9140 | } |
| 9141 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9142 | static PyObject* |
| 9143 | formatlong(PyObject *val, int flags, int prec, int type) |
| 9144 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9145 | char *buf; |
| 9146 | int len; |
| 9147 | PyObject *str; /* temporary string object. */ |
| 9148 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9149 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9150 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 9151 | if (!str) |
| 9152 | return NULL; |
| 9153 | result = PyUnicode_FromStringAndSize(buf, len); |
| 9154 | Py_DECREF(str); |
| 9155 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9156 | } |
| 9157 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9158 | static int |
| 9159 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9160 | size_t buflen, |
| 9161 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9162 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 9163 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 9164 | if (PyUnicode_Check(v)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9165 | if (PyUnicode_GET_SIZE(v) == 1) { |
| 9166 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
| 9167 | buf[1] = '\0'; |
| 9168 | return 1; |
| 9169 | } |
| 9170 | #ifndef Py_UNICODE_WIDE |
| 9171 | if (PyUnicode_GET_SIZE(v) == 2) { |
| 9172 | /* Decode a valid surrogate pair */ |
| 9173 | int c0 = PyUnicode_AS_UNICODE(v)[0]; |
| 9174 | int c1 = PyUnicode_AS_UNICODE(v)[1]; |
| 9175 | if (0xD800 <= c0 && c0 <= 0xDBFF && |
| 9176 | 0xDC00 <= c1 && c1 <= 0xDFFF) { |
| 9177 | buf[0] = c0; |
| 9178 | buf[1] = c1; |
| 9179 | buf[2] = '\0'; |
| 9180 | return 2; |
| 9181 | } |
| 9182 | } |
| 9183 | #endif |
| 9184 | goto onError; |
| 9185 | } |
| 9186 | else { |
| 9187 | /* Integer input truncated to a character */ |
| 9188 | long x; |
| 9189 | x = PyLong_AsLong(v); |
| 9190 | if (x == -1 && PyErr_Occurred()) |
| 9191 | goto onError; |
| 9192 | |
| 9193 | if (x < 0 || x > 0x10ffff) { |
| 9194 | PyErr_SetString(PyExc_OverflowError, |
| 9195 | "%c arg not in range(0x110000)"); |
| 9196 | return -1; |
| 9197 | } |
| 9198 | |
| 9199 | #ifndef Py_UNICODE_WIDE |
| 9200 | if (x > 0xffff) { |
| 9201 | x -= 0x10000; |
| 9202 | buf[0] = (Py_UNICODE)(0xD800 | (x >> 10)); |
| 9203 | buf[1] = (Py_UNICODE)(0xDC00 | (x & 0x3FF)); |
| 9204 | return 2; |
| 9205 | } |
| 9206 | #endif |
| 9207 | buf[0] = (Py_UNICODE) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9208 | buf[1] = '\0'; |
| 9209 | return 1; |
| 9210 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 9211 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9212 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 9213 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9214 | "%c requires int or char"); |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 9215 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9216 | } |
| 9217 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9218 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9219 | 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] | 9220 | */ |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9221 | #define FORMATBUFLEN (size_t)10 |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9222 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9223 | PyObject *PyUnicode_Format(PyObject *format, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9224 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9225 | { |
| 9226 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9227 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9228 | int args_owned = 0; |
| 9229 | PyUnicodeObject *result = NULL; |
| 9230 | PyObject *dict = NULL; |
| 9231 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9232 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9233 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9234 | PyErr_BadInternalCall(); |
| 9235 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9236 | } |
| 9237 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 9238 | if (uformat == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9239 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9240 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 9241 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 9242 | |
| 9243 | reslen = rescnt = fmtcnt + 100; |
| 9244 | result = _PyUnicode_New(reslen); |
| 9245 | if (result == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9246 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9247 | res = PyUnicode_AS_UNICODE(result); |
| 9248 | |
| 9249 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9250 | arglen = PyTuple_Size(args); |
| 9251 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9252 | } |
| 9253 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9254 | arglen = -1; |
| 9255 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9256 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 9257 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 9258 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9259 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9260 | |
| 9261 | while (--fmtcnt >= 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9262 | if (*fmt != '%') { |
| 9263 | if (--rescnt < 0) { |
| 9264 | rescnt = fmtcnt + 100; |
| 9265 | reslen += rescnt; |
| 9266 | if (_PyUnicode_Resize(&result, reslen) < 0) |
| 9267 | goto onError; |
| 9268 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 9269 | --rescnt; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9270 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9271 | *res++ = *fmt++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9272 | } |
| 9273 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9274 | /* Got a format specifier */ |
| 9275 | int flags = 0; |
| 9276 | Py_ssize_t width = -1; |
| 9277 | int prec = -1; |
| 9278 | Py_UNICODE c = '\0'; |
| 9279 | Py_UNICODE fill; |
| 9280 | int isnumok; |
| 9281 | PyObject *v = NULL; |
| 9282 | PyObject *temp = NULL; |
| 9283 | Py_UNICODE *pbuf; |
| 9284 | Py_UNICODE sign; |
| 9285 | Py_ssize_t len; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9286 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For formatchar() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9287 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9288 | fmt++; |
| 9289 | if (*fmt == '(') { |
| 9290 | Py_UNICODE *keystart; |
| 9291 | Py_ssize_t keylen; |
| 9292 | PyObject *key; |
| 9293 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 9294 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9295 | if (dict == NULL) { |
| 9296 | PyErr_SetString(PyExc_TypeError, |
| 9297 | "format requires a mapping"); |
| 9298 | goto onError; |
| 9299 | } |
| 9300 | ++fmt; |
| 9301 | --fmtcnt; |
| 9302 | keystart = fmt; |
| 9303 | /* Skip over balanced parentheses */ |
| 9304 | while (pcount > 0 && --fmtcnt >= 0) { |
| 9305 | if (*fmt == ')') |
| 9306 | --pcount; |
| 9307 | else if (*fmt == '(') |
| 9308 | ++pcount; |
| 9309 | fmt++; |
| 9310 | } |
| 9311 | keylen = fmt - keystart - 1; |
| 9312 | if (fmtcnt < 0 || pcount > 0) { |
| 9313 | PyErr_SetString(PyExc_ValueError, |
| 9314 | "incomplete format key"); |
| 9315 | goto onError; |
| 9316 | } |
| 9317 | #if 0 |
| 9318 | /* keys are converted to strings using UTF-8 and |
| 9319 | then looked up since Python uses strings to hold |
| 9320 | variables names etc. in its namespaces and we |
| 9321 | wouldn't want to break common idioms. */ |
| 9322 | key = PyUnicode_EncodeUTF8(keystart, |
| 9323 | keylen, |
| 9324 | NULL); |
| 9325 | #else |
| 9326 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 9327 | #endif |
| 9328 | if (key == NULL) |
| 9329 | goto onError; |
| 9330 | if (args_owned) { |
| 9331 | Py_DECREF(args); |
| 9332 | args_owned = 0; |
| 9333 | } |
| 9334 | args = PyObject_GetItem(dict, key); |
| 9335 | Py_DECREF(key); |
| 9336 | if (args == NULL) { |
| 9337 | goto onError; |
| 9338 | } |
| 9339 | args_owned = 1; |
| 9340 | arglen = -1; |
| 9341 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9342 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9343 | while (--fmtcnt >= 0) { |
| 9344 | switch (c = *fmt++) { |
| 9345 | case '-': flags |= F_LJUST; continue; |
| 9346 | case '+': flags |= F_SIGN; continue; |
| 9347 | case ' ': flags |= F_BLANK; continue; |
| 9348 | case '#': flags |= F_ALT; continue; |
| 9349 | case '0': flags |= F_ZERO; continue; |
| 9350 | } |
| 9351 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9352 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9353 | if (c == '*') { |
| 9354 | v = getnextarg(args, arglen, &argidx); |
| 9355 | if (v == NULL) |
| 9356 | goto onError; |
| 9357 | if (!PyLong_Check(v)) { |
| 9358 | PyErr_SetString(PyExc_TypeError, |
| 9359 | "* wants int"); |
| 9360 | goto onError; |
| 9361 | } |
| 9362 | width = PyLong_AsLong(v); |
| 9363 | if (width == -1 && PyErr_Occurred()) |
| 9364 | goto onError; |
| 9365 | if (width < 0) { |
| 9366 | flags |= F_LJUST; |
| 9367 | width = -width; |
| 9368 | } |
| 9369 | if (--fmtcnt >= 0) |
| 9370 | c = *fmt++; |
| 9371 | } |
| 9372 | else if (c >= '0' && c <= '9') { |
| 9373 | width = c - '0'; |
| 9374 | while (--fmtcnt >= 0) { |
| 9375 | c = *fmt++; |
| 9376 | if (c < '0' || c > '9') |
| 9377 | break; |
| 9378 | if ((width*10) / 10 != width) { |
| 9379 | PyErr_SetString(PyExc_ValueError, |
| 9380 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9381 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9382 | } |
| 9383 | width = width*10 + (c - '0'); |
| 9384 | } |
| 9385 | } |
| 9386 | if (c == '.') { |
| 9387 | prec = 0; |
| 9388 | if (--fmtcnt >= 0) |
| 9389 | c = *fmt++; |
| 9390 | if (c == '*') { |
| 9391 | v = getnextarg(args, arglen, &argidx); |
| 9392 | if (v == NULL) |
| 9393 | goto onError; |
| 9394 | if (!PyLong_Check(v)) { |
| 9395 | PyErr_SetString(PyExc_TypeError, |
| 9396 | "* wants int"); |
| 9397 | goto onError; |
| 9398 | } |
| 9399 | prec = PyLong_AsLong(v); |
| 9400 | if (prec == -1 && PyErr_Occurred()) |
| 9401 | goto onError; |
| 9402 | if (prec < 0) |
| 9403 | prec = 0; |
| 9404 | if (--fmtcnt >= 0) |
| 9405 | c = *fmt++; |
| 9406 | } |
| 9407 | else if (c >= '0' && c <= '9') { |
| 9408 | prec = c - '0'; |
| 9409 | while (--fmtcnt >= 0) { |
Stefan Krah | 99212f6 | 2010-07-19 17:58:26 +0000 | [diff] [blame] | 9410 | c = *fmt++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9411 | if (c < '0' || c > '9') |
| 9412 | break; |
| 9413 | if ((prec*10) / 10 != prec) { |
| 9414 | PyErr_SetString(PyExc_ValueError, |
| 9415 | "prec too big"); |
| 9416 | goto onError; |
| 9417 | } |
| 9418 | prec = prec*10 + (c - '0'); |
| 9419 | } |
| 9420 | } |
| 9421 | } /* prec */ |
| 9422 | if (fmtcnt >= 0) { |
| 9423 | if (c == 'h' || c == 'l' || c == 'L') { |
| 9424 | if (--fmtcnt >= 0) |
| 9425 | c = *fmt++; |
| 9426 | } |
| 9427 | } |
| 9428 | if (fmtcnt < 0) { |
| 9429 | PyErr_SetString(PyExc_ValueError, |
| 9430 | "incomplete format"); |
| 9431 | goto onError; |
| 9432 | } |
| 9433 | if (c != '%') { |
| 9434 | v = getnextarg(args, arglen, &argidx); |
| 9435 | if (v == NULL) |
| 9436 | goto onError; |
| 9437 | } |
| 9438 | sign = 0; |
| 9439 | fill = ' '; |
| 9440 | switch (c) { |
| 9441 | |
| 9442 | case '%': |
| 9443 | pbuf = formatbuf; |
| 9444 | /* presume that buffer length is at least 1 */ |
| 9445 | pbuf[0] = '%'; |
| 9446 | len = 1; |
| 9447 | break; |
| 9448 | |
| 9449 | case 's': |
| 9450 | case 'r': |
| 9451 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 9452 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9453 | temp = v; |
| 9454 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9455 | } |
| 9456 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9457 | if (c == 's') |
| 9458 | temp = PyObject_Str(v); |
| 9459 | else if (c == 'r') |
| 9460 | temp = PyObject_Repr(v); |
| 9461 | else |
| 9462 | temp = PyObject_ASCII(v); |
| 9463 | if (temp == NULL) |
| 9464 | goto onError; |
| 9465 | if (PyUnicode_Check(temp)) |
| 9466 | /* nothing to do */; |
| 9467 | else { |
| 9468 | Py_DECREF(temp); |
| 9469 | PyErr_SetString(PyExc_TypeError, |
| 9470 | "%s argument has non-string str()"); |
| 9471 | goto onError; |
| 9472 | } |
| 9473 | } |
| 9474 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9475 | len = PyUnicode_GET_SIZE(temp); |
| 9476 | if (prec >= 0 && len > prec) |
| 9477 | len = prec; |
| 9478 | break; |
| 9479 | |
| 9480 | case 'i': |
| 9481 | case 'd': |
| 9482 | case 'u': |
| 9483 | case 'o': |
| 9484 | case 'x': |
| 9485 | case 'X': |
| 9486 | if (c == 'i') |
| 9487 | c = 'd'; |
| 9488 | isnumok = 0; |
| 9489 | if (PyNumber_Check(v)) { |
| 9490 | PyObject *iobj=NULL; |
| 9491 | |
| 9492 | if (PyLong_Check(v)) { |
| 9493 | iobj = v; |
| 9494 | Py_INCREF(iobj); |
| 9495 | } |
| 9496 | else { |
| 9497 | iobj = PyNumber_Long(v); |
| 9498 | } |
| 9499 | if (iobj!=NULL) { |
| 9500 | if (PyLong_Check(iobj)) { |
| 9501 | isnumok = 1; |
| 9502 | temp = formatlong(iobj, flags, prec, c); |
| 9503 | Py_DECREF(iobj); |
| 9504 | if (!temp) |
| 9505 | goto onError; |
| 9506 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9507 | len = PyUnicode_GET_SIZE(temp); |
| 9508 | sign = 1; |
| 9509 | } |
| 9510 | else { |
| 9511 | Py_DECREF(iobj); |
| 9512 | } |
| 9513 | } |
| 9514 | } |
| 9515 | if (!isnumok) { |
| 9516 | PyErr_Format(PyExc_TypeError, |
| 9517 | "%%%c format: a number is required, " |
| 9518 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 9519 | goto onError; |
| 9520 | } |
| 9521 | if (flags & F_ZERO) |
| 9522 | fill = '0'; |
| 9523 | break; |
| 9524 | |
| 9525 | case 'e': |
| 9526 | case 'E': |
| 9527 | case 'f': |
| 9528 | case 'F': |
| 9529 | case 'g': |
| 9530 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9531 | temp = formatfloat(v, flags, prec, c); |
| 9532 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9533 | goto onError; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9534 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9535 | len = PyUnicode_GET_SIZE(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9536 | sign = 1; |
| 9537 | if (flags & F_ZERO) |
| 9538 | fill = '0'; |
| 9539 | break; |
| 9540 | |
| 9541 | case 'c': |
| 9542 | pbuf = formatbuf; |
| 9543 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
| 9544 | if (len < 0) |
| 9545 | goto onError; |
| 9546 | break; |
| 9547 | |
| 9548 | default: |
| 9549 | PyErr_Format(PyExc_ValueError, |
| 9550 | "unsupported format character '%c' (0x%x) " |
| 9551 | "at index %zd", |
| 9552 | (31<=c && c<=126) ? (char)c : '?', |
| 9553 | (int)c, |
| 9554 | (Py_ssize_t)(fmt - 1 - |
| 9555 | PyUnicode_AS_UNICODE(uformat))); |
| 9556 | goto onError; |
| 9557 | } |
| 9558 | if (sign) { |
| 9559 | if (*pbuf == '-' || *pbuf == '+') { |
| 9560 | sign = *pbuf++; |
| 9561 | len--; |
| 9562 | } |
| 9563 | else if (flags & F_SIGN) |
| 9564 | sign = '+'; |
| 9565 | else if (flags & F_BLANK) |
| 9566 | sign = ' '; |
| 9567 | else |
| 9568 | sign = 0; |
| 9569 | } |
| 9570 | if (width < len) |
| 9571 | width = len; |
| 9572 | if (rescnt - (sign != 0) < width) { |
| 9573 | reslen -= rescnt; |
| 9574 | rescnt = width + fmtcnt + 100; |
| 9575 | reslen += rescnt; |
| 9576 | if (reslen < 0) { |
| 9577 | Py_XDECREF(temp); |
| 9578 | PyErr_NoMemory(); |
| 9579 | goto onError; |
| 9580 | } |
| 9581 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 9582 | Py_XDECREF(temp); |
| 9583 | goto onError; |
| 9584 | } |
| 9585 | res = PyUnicode_AS_UNICODE(result) |
| 9586 | + reslen - rescnt; |
| 9587 | } |
| 9588 | if (sign) { |
| 9589 | if (fill != ' ') |
| 9590 | *res++ = sign; |
| 9591 | rescnt--; |
| 9592 | if (width > len) |
| 9593 | width--; |
| 9594 | } |
| 9595 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
| 9596 | assert(pbuf[0] == '0'); |
| 9597 | assert(pbuf[1] == c); |
| 9598 | if (fill != ' ') { |
| 9599 | *res++ = *pbuf++; |
| 9600 | *res++ = *pbuf++; |
| 9601 | } |
| 9602 | rescnt -= 2; |
| 9603 | width -= 2; |
| 9604 | if (width < 0) |
| 9605 | width = 0; |
| 9606 | len -= 2; |
| 9607 | } |
| 9608 | if (width > len && !(flags & F_LJUST)) { |
| 9609 | do { |
| 9610 | --rescnt; |
| 9611 | *res++ = fill; |
| 9612 | } while (--width > len); |
| 9613 | } |
| 9614 | if (fill == ' ') { |
| 9615 | if (sign) |
| 9616 | *res++ = sign; |
| 9617 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
| 9618 | assert(pbuf[0] == '0'); |
| 9619 | assert(pbuf[1] == c); |
| 9620 | *res++ = *pbuf++; |
| 9621 | *res++ = *pbuf++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9622 | } |
| 9623 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9624 | Py_UNICODE_COPY(res, pbuf, len); |
| 9625 | res += len; |
| 9626 | rescnt -= len; |
| 9627 | while (--width >= len) { |
| 9628 | --rescnt; |
| 9629 | *res++ = ' '; |
| 9630 | } |
| 9631 | if (dict && (argidx < arglen) && c != '%') { |
| 9632 | PyErr_SetString(PyExc_TypeError, |
| 9633 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9634 | Py_XDECREF(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9635 | goto onError; |
| 9636 | } |
| 9637 | Py_XDECREF(temp); |
| 9638 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9639 | } /* until end */ |
| 9640 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9641 | PyErr_SetString(PyExc_TypeError, |
| 9642 | "not all arguments converted during string formatting"); |
| 9643 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9644 | } |
| 9645 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9646 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9647 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9648 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9649 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9650 | } |
| 9651 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9652 | return (PyObject *)result; |
| 9653 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9654 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9655 | Py_XDECREF(result); |
| 9656 | Py_DECREF(uformat); |
| 9657 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9658 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9659 | } |
| 9660 | return NULL; |
| 9661 | } |
| 9662 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 9663 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9664 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 9665 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9666 | static PyObject * |
| 9667 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9668 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9669 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9670 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 9671 | char *encoding = NULL; |
| 9672 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9673 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9674 | if (type != &PyUnicode_Type) |
| 9675 | return unicode_subtype_new(type, args, kwds); |
| 9676 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9677 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9678 | return NULL; |
| 9679 | if (x == NULL) |
| 9680 | return (PyObject *)_PyUnicode_New(0); |
| 9681 | if (encoding == NULL && errors == NULL) |
| 9682 | return PyObject_Str(x); |
| 9683 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9684 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9685 | } |
| 9686 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9687 | static PyObject * |
| 9688 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9689 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9690 | PyUnicodeObject *tmp, *pnew; |
| 9691 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9692 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9693 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 9694 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 9695 | if (tmp == NULL) |
| 9696 | return NULL; |
| 9697 | assert(PyUnicode_Check(tmp)); |
| 9698 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
| 9699 | if (pnew == NULL) { |
| 9700 | Py_DECREF(tmp); |
| 9701 | return NULL; |
| 9702 | } |
| 9703 | pnew->str = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1)); |
| 9704 | if (pnew->str == NULL) { |
| 9705 | _Py_ForgetReference((PyObject *)pnew); |
| 9706 | PyObject_Del(pnew); |
| 9707 | Py_DECREF(tmp); |
| 9708 | return PyErr_NoMemory(); |
| 9709 | } |
| 9710 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 9711 | pnew->length = n; |
| 9712 | pnew->hash = tmp->hash; |
| 9713 | Py_DECREF(tmp); |
| 9714 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9715 | } |
| 9716 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9717 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9718 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9719 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 9720 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 9721 | encoding defaults to the current default string encoding.\n\ |
| 9722 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9723 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9724 | static PyObject *unicode_iter(PyObject *seq); |
| 9725 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9726 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 9727 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9728 | "str", /* tp_name */ |
| 9729 | sizeof(PyUnicodeObject), /* tp_size */ |
| 9730 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9731 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9732 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 9733 | 0, /* tp_print */ |
| 9734 | 0, /* tp_getattr */ |
| 9735 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 9736 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9737 | unicode_repr, /* tp_repr */ |
| 9738 | &unicode_as_number, /* tp_as_number */ |
| 9739 | &unicode_as_sequence, /* tp_as_sequence */ |
| 9740 | &unicode_as_mapping, /* tp_as_mapping */ |
| 9741 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 9742 | 0, /* tp_call*/ |
| 9743 | (reprfunc) unicode_str, /* tp_str */ |
| 9744 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9745 | 0, /* tp_setattro */ |
| 9746 | 0, /* tp_as_buffer */ |
| 9747 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9748 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9749 | unicode_doc, /* tp_doc */ |
| 9750 | 0, /* tp_traverse */ |
| 9751 | 0, /* tp_clear */ |
| 9752 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 9753 | 0, /* tp_weaklistoffset */ |
| 9754 | unicode_iter, /* tp_iter */ |
| 9755 | 0, /* tp_iternext */ |
| 9756 | unicode_methods, /* tp_methods */ |
| 9757 | 0, /* tp_members */ |
| 9758 | 0, /* tp_getset */ |
| 9759 | &PyBaseObject_Type, /* tp_base */ |
| 9760 | 0, /* tp_dict */ |
| 9761 | 0, /* tp_descr_get */ |
| 9762 | 0, /* tp_descr_set */ |
| 9763 | 0, /* tp_dictoffset */ |
| 9764 | 0, /* tp_init */ |
| 9765 | 0, /* tp_alloc */ |
| 9766 | unicode_new, /* tp_new */ |
| 9767 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9768 | }; |
| 9769 | |
| 9770 | /* Initialize the Unicode implementation */ |
| 9771 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9772 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9773 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9774 | int i; |
| 9775 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9776 | /* XXX - move this array to unicodectype.c ? */ |
| 9777 | Py_UNICODE linebreak[] = { |
| 9778 | 0x000A, /* LINE FEED */ |
| 9779 | 0x000D, /* CARRIAGE RETURN */ |
| 9780 | 0x001C, /* FILE SEPARATOR */ |
| 9781 | 0x001D, /* GROUP SEPARATOR */ |
| 9782 | 0x001E, /* RECORD SEPARATOR */ |
| 9783 | 0x0085, /* NEXT LINE */ |
| 9784 | 0x2028, /* LINE SEPARATOR */ |
| 9785 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 9786 | }; |
| 9787 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 9788 | /* Init the implementation */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 9789 | free_list = NULL; |
| 9790 | numfree = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9791 | unicode_empty = _PyUnicode_New(0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9792 | if (!unicode_empty) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9793 | return; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9794 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9795 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9796 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 9797 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9798 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9799 | |
| 9800 | /* initialize the linebreak bloom filter */ |
| 9801 | bloom_linebreak = make_bloom_mask( |
| 9802 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 9803 | ); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9804 | |
| 9805 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9806 | } |
| 9807 | |
| 9808 | /* Finalize the Unicode implementation */ |
| 9809 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9810 | int |
| 9811 | PyUnicode_ClearFreeList(void) |
| 9812 | { |
| 9813 | int freelist_size = numfree; |
| 9814 | PyUnicodeObject *u; |
| 9815 | |
| 9816 | for (u = free_list; u != NULL;) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9817 | PyUnicodeObject *v = u; |
| 9818 | u = *(PyUnicodeObject **)u; |
| 9819 | if (v->str) |
| 9820 | PyObject_DEL(v->str); |
| 9821 | Py_XDECREF(v->defenc); |
| 9822 | PyObject_Del(v); |
| 9823 | numfree--; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9824 | } |
| 9825 | free_list = NULL; |
| 9826 | assert(numfree == 0); |
| 9827 | return freelist_size; |
| 9828 | } |
| 9829 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9830 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9831 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9832 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9833 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9834 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 9835 | Py_XDECREF(unicode_empty); |
| 9836 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 9837 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9838 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9839 | if (unicode_latin1[i]) { |
| 9840 | Py_DECREF(unicode_latin1[i]); |
| 9841 | unicode_latin1[i] = NULL; |
| 9842 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9843 | } |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9844 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9845 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9846 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9847 | void |
| 9848 | PyUnicode_InternInPlace(PyObject **p) |
| 9849 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9850 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 9851 | PyObject *t; |
| 9852 | if (s == NULL || !PyUnicode_Check(s)) |
| 9853 | Py_FatalError( |
| 9854 | "PyUnicode_InternInPlace: unicode strings only please!"); |
| 9855 | /* If it's a subclass, we don't really know what putting |
| 9856 | it in the interned dict might do. */ |
| 9857 | if (!PyUnicode_CheckExact(s)) |
| 9858 | return; |
| 9859 | if (PyUnicode_CHECK_INTERNED(s)) |
| 9860 | return; |
| 9861 | if (interned == NULL) { |
| 9862 | interned = PyDict_New(); |
| 9863 | if (interned == NULL) { |
| 9864 | PyErr_Clear(); /* Don't leave an exception */ |
| 9865 | return; |
| 9866 | } |
| 9867 | } |
| 9868 | /* It might be that the GetItem call fails even |
| 9869 | though the key is present in the dictionary, |
| 9870 | namely when this happens during a stack overflow. */ |
| 9871 | Py_ALLOW_RECURSION |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9872 | t = PyDict_GetItem(interned, (PyObject *)s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9873 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9874 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9875 | if (t) { |
| 9876 | Py_INCREF(t); |
| 9877 | Py_DECREF(*p); |
| 9878 | *p = t; |
| 9879 | return; |
| 9880 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9881 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9882 | PyThreadState_GET()->recursion_critical = 1; |
| 9883 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 9884 | PyErr_Clear(); |
| 9885 | PyThreadState_GET()->recursion_critical = 0; |
| 9886 | return; |
| 9887 | } |
| 9888 | PyThreadState_GET()->recursion_critical = 0; |
| 9889 | /* The two references in interned are not counted by refcnt. |
| 9890 | The deallocator will take care of this */ |
| 9891 | Py_REFCNT(s) -= 2; |
| 9892 | PyUnicode_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9893 | } |
| 9894 | |
| 9895 | void |
| 9896 | PyUnicode_InternImmortal(PyObject **p) |
| 9897 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9898 | PyUnicode_InternInPlace(p); |
| 9899 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
| 9900 | PyUnicode_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL; |
| 9901 | Py_INCREF(*p); |
| 9902 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9903 | } |
| 9904 | |
| 9905 | PyObject * |
| 9906 | PyUnicode_InternFromString(const char *cp) |
| 9907 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9908 | PyObject *s = PyUnicode_FromString(cp); |
| 9909 | if (s == NULL) |
| 9910 | return NULL; |
| 9911 | PyUnicode_InternInPlace(&s); |
| 9912 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9913 | } |
| 9914 | |
| 9915 | void _Py_ReleaseInternedUnicodeStrings(void) |
| 9916 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9917 | PyObject *keys; |
| 9918 | PyUnicodeObject *s; |
| 9919 | Py_ssize_t i, n; |
| 9920 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9921 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9922 | if (interned == NULL || !PyDict_Check(interned)) |
| 9923 | return; |
| 9924 | keys = PyDict_Keys(interned); |
| 9925 | if (keys == NULL || !PyList_Check(keys)) { |
| 9926 | PyErr_Clear(); |
| 9927 | return; |
| 9928 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9929 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9930 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 9931 | detector, interned unicode strings are not forcibly deallocated; |
| 9932 | rather, we give them their stolen references back, and then clear |
| 9933 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9934 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9935 | n = PyList_GET_SIZE(keys); |
| 9936 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9937 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9938 | for (i = 0; i < n; i++) { |
| 9939 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
| 9940 | switch (s->state) { |
| 9941 | case SSTATE_NOT_INTERNED: |
| 9942 | /* XXX Shouldn't happen */ |
| 9943 | break; |
| 9944 | case SSTATE_INTERNED_IMMORTAL: |
| 9945 | Py_REFCNT(s) += 1; |
| 9946 | immortal_size += s->length; |
| 9947 | break; |
| 9948 | case SSTATE_INTERNED_MORTAL: |
| 9949 | Py_REFCNT(s) += 2; |
| 9950 | mortal_size += s->length; |
| 9951 | break; |
| 9952 | default: |
| 9953 | Py_FatalError("Inconsistent interned string state."); |
| 9954 | } |
| 9955 | s->state = SSTATE_NOT_INTERNED; |
| 9956 | } |
| 9957 | fprintf(stderr, "total size of all interned strings: " |
| 9958 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 9959 | "mortal/immortal\n", mortal_size, immortal_size); |
| 9960 | Py_DECREF(keys); |
| 9961 | PyDict_Clear(interned); |
| 9962 | Py_DECREF(interned); |
| 9963 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9964 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9965 | |
| 9966 | |
| 9967 | /********************* Unicode Iterator **************************/ |
| 9968 | |
| 9969 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9970 | PyObject_HEAD |
| 9971 | Py_ssize_t it_index; |
| 9972 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9973 | } unicodeiterobject; |
| 9974 | |
| 9975 | static void |
| 9976 | unicodeiter_dealloc(unicodeiterobject *it) |
| 9977 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9978 | _PyObject_GC_UNTRACK(it); |
| 9979 | Py_XDECREF(it->it_seq); |
| 9980 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9981 | } |
| 9982 | |
| 9983 | static int |
| 9984 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 9985 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9986 | Py_VISIT(it->it_seq); |
| 9987 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9988 | } |
| 9989 | |
| 9990 | static PyObject * |
| 9991 | unicodeiter_next(unicodeiterobject *it) |
| 9992 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9993 | PyUnicodeObject *seq; |
| 9994 | PyObject *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9995 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9996 | assert(it != NULL); |
| 9997 | seq = it->it_seq; |
| 9998 | if (seq == NULL) |
| 9999 | return NULL; |
| 10000 | assert(PyUnicode_Check(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10001 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10002 | if (it->it_index < PyUnicode_GET_SIZE(seq)) { |
| 10003 | item = PyUnicode_FromUnicode( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10004 | PyUnicode_AS_UNICODE(seq)+it->it_index, 1); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10005 | if (item != NULL) |
| 10006 | ++it->it_index; |
| 10007 | return item; |
| 10008 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10009 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10010 | Py_DECREF(seq); |
| 10011 | it->it_seq = NULL; |
| 10012 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10013 | } |
| 10014 | |
| 10015 | static PyObject * |
| 10016 | unicodeiter_len(unicodeiterobject *it) |
| 10017 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10018 | Py_ssize_t len = 0; |
| 10019 | if (it->it_seq) |
| 10020 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
| 10021 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10022 | } |
| 10023 | |
| 10024 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 10025 | |
| 10026 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10027 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10028 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10029 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10030 | }; |
| 10031 | |
| 10032 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10033 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 10034 | "str_iterator", /* tp_name */ |
| 10035 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 10036 | 0, /* tp_itemsize */ |
| 10037 | /* methods */ |
| 10038 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 10039 | 0, /* tp_print */ |
| 10040 | 0, /* tp_getattr */ |
| 10041 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 10042 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10043 | 0, /* tp_repr */ |
| 10044 | 0, /* tp_as_number */ |
| 10045 | 0, /* tp_as_sequence */ |
| 10046 | 0, /* tp_as_mapping */ |
| 10047 | 0, /* tp_hash */ |
| 10048 | 0, /* tp_call */ |
| 10049 | 0, /* tp_str */ |
| 10050 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 10051 | 0, /* tp_setattro */ |
| 10052 | 0, /* tp_as_buffer */ |
| 10053 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 10054 | 0, /* tp_doc */ |
| 10055 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 10056 | 0, /* tp_clear */ |
| 10057 | 0, /* tp_richcompare */ |
| 10058 | 0, /* tp_weaklistoffset */ |
| 10059 | PyObject_SelfIter, /* tp_iter */ |
| 10060 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 10061 | unicodeiter_methods, /* tp_methods */ |
| 10062 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10063 | }; |
| 10064 | |
| 10065 | static PyObject * |
| 10066 | unicode_iter(PyObject *seq) |
| 10067 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10068 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10069 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10070 | if (!PyUnicode_Check(seq)) { |
| 10071 | PyErr_BadInternalCall(); |
| 10072 | return NULL; |
| 10073 | } |
| 10074 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 10075 | if (it == NULL) |
| 10076 | return NULL; |
| 10077 | it->it_index = 0; |
| 10078 | Py_INCREF(seq); |
| 10079 | it->it_seq = (PyUnicodeObject *)seq; |
| 10080 | _PyObject_GC_TRACK(it); |
| 10081 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10082 | } |
| 10083 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10084 | size_t |
| 10085 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 10086 | { |
| 10087 | int res = 0; |
| 10088 | while(*u++) |
| 10089 | res++; |
| 10090 | return res; |
| 10091 | } |
| 10092 | |
| 10093 | Py_UNICODE* |
| 10094 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 10095 | { |
| 10096 | Py_UNICODE *u = s1; |
| 10097 | while ((*u++ = *s2++)); |
| 10098 | return s1; |
| 10099 | } |
| 10100 | |
| 10101 | Py_UNICODE* |
| 10102 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 10103 | { |
| 10104 | Py_UNICODE *u = s1; |
| 10105 | while ((*u++ = *s2++)) |
| 10106 | if (n-- == 0) |
| 10107 | break; |
| 10108 | return s1; |
| 10109 | } |
| 10110 | |
Victor Stinner | c4eb765 | 2010-09-01 23:43:50 +0000 | [diff] [blame] | 10111 | Py_UNICODE* |
| 10112 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 10113 | { |
| 10114 | Py_UNICODE *u1 = s1; |
| 10115 | u1 += Py_UNICODE_strlen(u1); |
| 10116 | Py_UNICODE_strcpy(u1, s2); |
| 10117 | return s1; |
| 10118 | } |
| 10119 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10120 | int |
| 10121 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 10122 | { |
| 10123 | while (*s1 && *s2 && *s1 == *s2) |
| 10124 | s1++, s2++; |
| 10125 | if (*s1 && *s2) |
| 10126 | return (*s1 < *s2) ? -1 : +1; |
| 10127 | if (*s1) |
| 10128 | return 1; |
| 10129 | if (*s2) |
| 10130 | return -1; |
| 10131 | return 0; |
| 10132 | } |
| 10133 | |
Victor Stinner | ef8d95c | 2010-08-16 22:03:11 +0000 | [diff] [blame] | 10134 | int |
| 10135 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 10136 | { |
| 10137 | register Py_UNICODE u1, u2; |
| 10138 | for (; n != 0; n--) { |
| 10139 | u1 = *s1; |
| 10140 | u2 = *s2; |
| 10141 | if (u1 != u2) |
| 10142 | return (u1 < u2) ? -1 : +1; |
| 10143 | if (u1 == '\0') |
| 10144 | return 0; |
| 10145 | s1++; |
| 10146 | s2++; |
| 10147 | } |
| 10148 | return 0; |
| 10149 | } |
| 10150 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10151 | Py_UNICODE* |
| 10152 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 10153 | { |
| 10154 | const Py_UNICODE *p; |
| 10155 | for (p = s; *p; p++) |
| 10156 | if (*p == c) |
| 10157 | return (Py_UNICODE*)p; |
| 10158 | return NULL; |
| 10159 | } |
| 10160 | |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 10161 | Py_UNICODE* |
| 10162 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 10163 | { |
| 10164 | const Py_UNICODE *p; |
| 10165 | p = s + Py_UNICODE_strlen(s); |
| 10166 | while (p != s) { |
| 10167 | p--; |
| 10168 | if (*p == c) |
| 10169 | return (Py_UNICODE*)p; |
| 10170 | } |
| 10171 | return NULL; |
| 10172 | } |
| 10173 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 10174 | Py_UNICODE* |
Victor Stinner | 4640860 | 2010-09-03 16:18:00 +0000 | [diff] [blame] | 10175 | PyUnicode_AsUnicodeCopy(PyObject *object) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 10176 | { |
| 10177 | PyUnicodeObject *unicode = (PyUnicodeObject *)object; |
| 10178 | Py_UNICODE *copy; |
| 10179 | Py_ssize_t size; |
| 10180 | |
| 10181 | /* Ensure we won't overflow the size. */ |
| 10182 | if (PyUnicode_GET_SIZE(unicode) > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 10183 | PyErr_NoMemory(); |
| 10184 | return NULL; |
| 10185 | } |
| 10186 | size = PyUnicode_GET_SIZE(unicode) + 1; /* copy the nul character */ |
| 10187 | size *= sizeof(Py_UNICODE); |
| 10188 | copy = PyMem_Malloc(size); |
| 10189 | if (copy == NULL) { |
| 10190 | PyErr_NoMemory(); |
| 10191 | return NULL; |
| 10192 | } |
| 10193 | memcpy(copy, PyUnicode_AS_UNICODE(unicode), size); |
| 10194 | return copy; |
| 10195 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10196 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 10197 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 10198 | to the string.Formatter class implemented in Python. */ |
| 10199 | |
| 10200 | static PyMethodDef _string_methods[] = { |
| 10201 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 10202 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 10203 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 10204 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 10205 | {NULL, NULL} |
| 10206 | }; |
| 10207 | |
| 10208 | static struct PyModuleDef _string_module = { |
| 10209 | PyModuleDef_HEAD_INIT, |
| 10210 | "_string", |
| 10211 | PyDoc_STR("string helper module"), |
| 10212 | 0, |
| 10213 | _string_methods, |
| 10214 | NULL, |
| 10215 | NULL, |
| 10216 | NULL, |
| 10217 | NULL |
| 10218 | }; |
| 10219 | |
| 10220 | PyMODINIT_FUNC |
| 10221 | PyInit__string(void) |
| 10222 | { |
| 10223 | return PyModule_Create(&_string_module); |
| 10224 | } |
| 10225 | |
| 10226 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10227 | #ifdef __cplusplus |
| 10228 | } |
| 10229 | #endif |